Skip to content

Commit

Permalink
Feature: type filter for leaderboard #307
Browse files Browse the repository at this point in the history
  • Loading branch information
mismirnov authored Nov 15, 2024
2 parents a5d67a5 + 3289387 commit cdb8da1
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/api/handler/responses/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type Enums struct {
MessageType []string `json:"message_type"`
EventType []string `json:"event_type"`
Categories []string `json:"categories"`
RollupTypes []string `json:"rollup_type"`
}

func NewEnums() Enums {
Expand All @@ -78,5 +79,6 @@ func NewEnums() Enums {
MessageType: types.MsgTypeNames(),
EventType: types.EventTypeNames(),
Categories: types.RollupCategoryNames(),
RollupTypes: types.RollupTypeNames(),
}
}
14 changes: 14 additions & 0 deletions cmd/api/handler/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type rollupList struct {
Sort string `query:"sort" validate:"omitempty,oneof=asc desc"`
SortBy string `query:"sort_by" validate:"omitempty,oneof=time blobs_count size fee"`
Category StringArray `query:"category" validate:"omitempty,dive,category"`
Type StringArray `query:"type" validate:"omitempty,dive,type"`
}

func (p *rollupList) SetDefault() {
Expand Down Expand Up @@ -80,12 +81,18 @@ func (handler RollupHandler) Leaderboard(c echo.Context) error {
categories[i] = types.RollupCategory(req.Category[i])
}

rollupTypes := make([]types.RollupType, len(req.Type))
for i := range rollupTypes {
rollupTypes[i] = types.RollupType(req.Type[i])
}

rollups, err := handler.rollups.Leaderboard(c.Request().Context(), storage.LeaderboardFilters{
SortField: req.SortBy,
Sort: pgSort(req.Sort),
Limit: req.Limit,
Offset: req.Offset,
Category: categories,
Type: rollupTypes,
})
if err != nil {
return handleError(c, err, handler.rollups)
Expand All @@ -103,6 +110,7 @@ type rollupDayList struct {
Sort string `query:"sort" validate:"omitempty,oneof=asc desc"`
SortBy string `query:"sort_by" validate:"omitempty,oneof=avg_size blobs_count total_size total_fee throughput namespace_count pfb_count mb_price"`
Category StringArray `query:"category" validate:"omitempty,dive,category"`
Type StringArray `query:"type" validate:"omitempty,dive,type"`
}

func (p *rollupDayList) SetDefault() {
Expand Down Expand Up @@ -145,12 +153,18 @@ func (handler RollupHandler) LeaderboardDay(c echo.Context) error {
categories[i] = types.RollupCategory(req.Category[i])
}

rollupTypes := make([]types.RollupType, len(req.Type))
for i := range rollupTypes {
rollupTypes[i] = types.RollupType(req.Type[i])
}

rollups, err := handler.rollups.LeaderboardDay(c.Request().Context(), storage.LeaderboardFilters{
SortField: req.SortBy,
Sort: pgSort(req.Sort),
Limit: req.Limit,
Offset: req.Offset,
Category: categories,
Type: rollupTypes,
})
if err != nil {
return handleError(c, err, handler.rollups)
Expand Down
9 changes: 9 additions & 0 deletions cmd/api/handler/rollup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (
Logo: "image.png",
Slug: "test-rollup",
Category: types.RollupCategoryNft,
Type: types.RollupTypeSettled,
}
testRollupWithStats = storage.RollupWithStats{
Rollup: testRollup,
Expand Down Expand Up @@ -90,6 +91,7 @@ func (s *RollupTestSuite) TestLeaderboard() {
q := make(url.Values)
q.Add("sort_by", sort)
q.Add("category", "nft,gaming")
q.Add("type", "sovereign")

req := httptest.NewRequest(http.MethodGet, "/?"+q.Encode(), nil)
rec := httptest.NewRecorder()
Expand All @@ -106,6 +108,9 @@ func (s *RollupTestSuite) TestLeaderboard() {
types.RollupCategoryNft,
types.RollupCategoryGaming,
},
Type: []types.RollupType{
types.RollupTypeSovereign,
},
}).
Return([]storage.RollupWithStats{testRollupWithStats}, nil).
Times(1)
Expand Down Expand Up @@ -140,6 +145,7 @@ func (s *RollupTestSuite) TestLeaderboardDay() {
q := make(url.Values)
q.Add("sort_by", sort)
q.Add("category", "nft,gaming")
q.Add("type", "sovereign")

req := httptest.NewRequest(http.MethodGet, "/?"+q.Encode(), nil)
rec := httptest.NewRecorder()
Expand All @@ -156,6 +162,9 @@ func (s *RollupTestSuite) TestLeaderboardDay() {
types.RollupCategoryNft,
types.RollupCategoryGaming,
},
Type: []types.RollupType{
types.RollupTypeSovereign,
},
}).
Return([]storage.RollupWithDayStats{
{
Expand Down
10 changes: 10 additions & 0 deletions cmd/api/handler/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func NewCelestiaApiValidator() *CelestiaApiValidator {
if err := v.RegisterValidation("category", categoryValidator()); err != nil {
panic(err)
}
if err := v.RegisterValidation("type", typeValidator()); err != nil {
panic(err)
}
return &CelestiaApiValidator{validator: v}
}

Expand Down Expand Up @@ -109,3 +112,10 @@ func categoryValidator() validator.Func {
return err == nil
}
}

func typeValidator() validator.Func {
return func(fl validator.FieldLevel) bool {
_, err := types.ParseRollupType(fl.Field().String())
return err == nil
}
}
8 changes: 8 additions & 0 deletions internal/storage/postgres/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func (r *Rollup) Leaderboard(ctx context.Context, fltrs storage.LeaderboardFilte
query = query.Where("category IN (?)", bun.In(fltrs.Category))
}

if len(fltrs.Type) > 0 {
query = query.Where("type IN (?)", bun.In(fltrs.Type))
}

query = sortScope(query, fltrs.SortField, fltrs.Sort)
query = limitScope(query, fltrs.Limit)
err = query.Scan(ctx, &rollups)
Expand All @@ -72,6 +76,10 @@ func (r *Rollup) LeaderboardDay(ctx context.Context, fltrs storage.LeaderboardFi
query = query.Where("category IN (?)", bun.In(fltrs.Category))
}

if len(fltrs.Type) > 0 {
query = query.Where("type IN (?)", bun.In(fltrs.Type))
}

query = sortScope(query, fltrs.SortField, fltrs.Sort)
query = limitScope(query, fltrs.Limit)
err = query.Scan(ctx, &rollups)
Expand Down
36 changes: 36 additions & 0 deletions internal/storage/postgres/rollup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,42 @@ func (s *StorageTestSuite) TestRollupLeaderboardWithCategory() {
}
}

func (s *StorageTestSuite) TestRollupLeaderboardWithType() {
ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer ctxCancel()

_, err := s.storage.Connection().Exec(ctx, "REFRESH MATERIALIZED VIEW leaderboard;")
s.Require().NoError(err)

for _, column := range []string{
sizeColumn, blobsCountColumn, timeColumn, feeColumn, "",
} {

rollups, err := s.storage.Rollup.Leaderboard(ctx, storage.LeaderboardFilters{
SortField: column,
Sort: sdk.SortOrderDesc,
Limit: 10,
Offset: 0,
Type: []types.RollupType{types.RollupTypeSovereign},
})
s.Require().NoError(err, column)
s.Require().Len(rollups, 1, column)

rollup := rollups[0]
s.Require().EqualValues("Rollup 3", rollup.Name, column)
s.Require().EqualValues("The third", rollup.Description, column)
s.Require().EqualValues(34, rollup.Size, column)
s.Require().EqualValues(3, rollup.BlobsCount, column)
s.Require().False(rollup.LastActionTime.IsZero())
s.Require().False(rollup.FirstActionTime.IsZero())
s.Require().Equal("7000", rollup.Fee.String())
s.Require().EqualValues(0.6363636363636364, rollup.FeePct)
s.Require().EqualValues(0.42857142857142855, rollup.BlobsCountPct)
s.Require().EqualValues(0.3953488372093023, rollup.SizePct)
s.Require().EqualValues("sovereign", rollup.Type)
}
}

func (s *StorageTestSuite) TestRollupLeaderboardDay() {
ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer ctxCancel()
Expand Down
1 change: 1 addition & 0 deletions internal/storage/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type LeaderboardFilters struct {
Limit int
Offset int
Category []types.RollupCategory
Type []types.RollupType
}

//go:generate mockgen -source=$GOFILE -destination=mock/$GOFILE -package=mock -typed
Expand Down
3 changes: 3 additions & 0 deletions test/data/rollup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
logo: https://rollup1.com/image.png
slug: rollup_1
category: finance
type: settled
- id: 2
name: Rollup 2
description: The second
Expand All @@ -16,6 +17,7 @@
logo: https://rollup2.com/image.png
slug: rollup_2
category: gaming
type: settled
- id: 3
name: Rollup 3
description: The third
Expand All @@ -25,3 +27,4 @@
logo: https://rollup3.com/image.png
slug: rollup_3
category: nft
type: sovereign

0 comments on commit cdb8da1

Please sign in to comment.