Skip to content

Commit

Permalink
Merge pull request #59 from celenium-io/feature/ns-usage-top-count-pa…
Browse files Browse the repository at this point in the history
…rameter

Feature: add top parameter to namespace usage
  • Loading branch information
aopoltorzhicky authored Nov 30, 2023
2 parents 2307016 + 5ef8bcf commit aeee6f2
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 9 deletions.
18 changes: 18 additions & 0 deletions cmd/api/docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions cmd/api/docs/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions cmd/api/docs/swagger.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions cmd/api/handler/responses/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package responses

import (
"encoding/hex"
"time"

"github.com/celenium-io/celestia-indexer/internal/storage"
Expand Down Expand Up @@ -40,14 +41,18 @@ func NewTxCountHistogramItem(item storage.TxCountForLast24hItem) TxCountHistogra
}

type NamespaceUsage struct {
Name string `example:"00112233" format:"string" json:"name" swaggertype:"string"`
Size int64 `example:"1283518" format:"integer" json:"size" swaggertype:"number"`
Name string `example:"00112233" format:"string" json:"name" swaggertype:"string"`
Version *byte `examle:"1" format:"byte" json:"version,omitempty" swaggertype:"integer"`
NamespaceID string `example:"4723ce10b187716adfc55ff7e6d9179c226e6b5440b02577cca49d02" format:"binary" json:"namespace_id,omitempty" swaggertype:"string"`
Size int64 `example:"1283518" format:"integer" json:"size" swaggertype:"number"`
}

func NewNamespaceUsage(ns storage.Namespace) NamespaceUsage {
return NamespaceUsage{
Name: decodeName(ns.NamespaceID),
Size: ns.Size,
Name: decodeName(ns.NamespaceID),
Size: ns.Size,
Version: &ns.Version,
NamespaceID: hex.EncodeToString(ns.NamespaceID),
}
}

Expand Down
21 changes: 18 additions & 3 deletions cmd/api/handler/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,32 @@ func (sh StatsHandler) TxCountHourly24h(c echo.Context) error {
return returnArray(c, response)
}

type namespaceUsageRequest struct {
Top *int `example:"100" query:"top" validate:"omitempty,min=1,max=100"`
}

// NamespaceUsage godoc
//
// @Summary Get namespaces with sorting by size.
// @Description Get namespaces with sorting by size. Returns top 100 namespaces. Namespaces which is not included to top 100 grouped into 'others' item
// @Tags stats
// @ID stats-namespace-usage
// @Param top query integer false "Count of entities" mininum(1) maximum(100)
// @Produce json
// @Success 200 {array} responses.NamespaceUsage
// @Failure 500 {object} Error
// @Router /v1/stats/namespace/usage [get]
func (sh StatsHandler) NamespaceUsage(c echo.Context) error {
namespaces, err := sh.nsRepo.Active(c.Request().Context(), "size", 100)
req, err := bindAndValidate[namespaceUsageRequest](c)
if err != nil {
return badRequestError(c, err)
}
if req.Top == nil {
top := 10
req.Top = &top
}

namespaces, err := sh.nsRepo.Active(c.Request().Context(), "size", *req.Top)
if err != nil {
return internalServerError(c, err)
}
Expand All @@ -224,8 +238,9 @@ func (sh StatsHandler) NamespaceUsage(c echo.Context) error {
}

response = append(response, responses.NamespaceUsage{
Name: "others",
Size: state[0].TotalBlobsSize - top100Size,
Name: "others",
Size: state[0].TotalBlobsSize - top100Size,
Version: nil,
})

return returnArray(c, response)
Expand Down
11 changes: 9 additions & 2 deletions cmd/api/handler/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,16 @@ func (s *StatsTestSuite) TestTxCount24h() {
}

func (s *StatsTestSuite) TestNamespaceUsage() {
req := httptest.NewRequest(http.MethodGet, "/", nil)
q := make(url.Values)
q.Set("top", "1")

req := httptest.NewRequest(http.MethodGet, "/?"+q.Encode(), nil)
rec := httptest.NewRecorder()
c := s.echo.NewContext(req, rec)
c.SetPath("/v1/stats/namespace/usage")

s.ns.EXPECT().
Active(gomock.Any(), "size", 100).
Active(gomock.Any(), "size", 1).
Return([]storage.Namespace{
testNamespace,
}, nil)
Expand All @@ -293,10 +296,14 @@ func (s *StatsTestSuite) TestNamespaceUsage() {
item0 := response[0]
s.Require().Equal("010203040506070809000102030405060708090001020304050607", item0.Name)
s.Require().Equal(testNamespace.Size, item0.Size)
s.Require().Equal("00010203040506070809000102030405060708090001020304050607", item0.NamespaceID)
s.Require().NotNil(item0.Version)
s.Require().EqualValues(1, *item0.Version)

item1 := response[1]
s.Require().Equal("others", item1.Name)
s.Require().EqualValues(900, item1.Size)
s.Require().Nil(item1.Version)
}

func (s *StatsTestSuite) TestBlockStatsHistogram() {
Expand Down

0 comments on commit aeee6f2

Please sign in to comment.