Skip to content

Commit

Permalink
Feature: add sorting field in list rollup
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Mar 12, 2024
1 parent 0b6209c commit 2498465
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 6 deletions.
10 changes: 10 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.

10 changes: 10 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.

7 changes: 7 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.

35 changes: 29 additions & 6 deletions cmd/api/handler/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,35 +69,58 @@ func (handler *RollupHandler) Get(c echo.Context) error {
return c.JSON(http.StatusOK, responses.NewRollup(&rollup))
}

type listRollupsRequest struct {
Limit int `query:"limit" validate:"omitempty,min=1,max=100"`
Offset int `query:"offset" validate:"omitempty,min=0"`
Sort string `query:"sort" validate:"omitempty,oneof=asc desc"`
SortField string `query:"sort_by" validate:"omitempty,oneof=size id"`
}

func (p *listRollupsRequest) SetDefault() {
if p.Limit == 0 {
p.Limit = 10
}
if p.Sort == "" {
p.Sort = desc
}
}

// List godoc
//
// @Summary List rollups info
// @Description List rollups info
// @Tags rollup
// @ID list-rollups
// @Param limit query integer false "Count of requested entities" mininum(1) maximum(100)
// @Param offset query integer false "Offset" mininum(1)
// @Param sort query string false "Sort order" Enums(asc, desc)
// @Param limit query integer false "Count of requested entities" mininum(1) maximum(100)
// @Param offset query integer false "Offset" mininum(1)
// @Param sort query string false "Sort order" Enums(asc, desc)
// @Param sort_by query string false "Field using for sorting. Default: id" Enums(id, size)
// @Produce json
// @Success 200 {array} responses.Rollup
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /v1/rollup [get]
func (handler *RollupHandler) List(c echo.Context) error {
req, err := bindAndValidate[listRequest](c)
req, err := bindAndValidate[listRollupsRequest](c)
if err != nil {
return badRequestError(c, err)
}
req.SetDefault()

rollups, err := handler.rollups.List(c.Request().Context(), req.Limit, req.Offset, pgSort(req.Sort))
fltrs := storage.RollupListFilter{
Limit: req.Limit,
Offset: req.Offset,
SortOrder: pgSort(req.Sort),
SortField: req.Sort,
}
rollups, err := handler.rollups.ListExt(c.Request().Context(), fltrs)
if err != nil {
return handleError(c, err, handler.rollups)
}

response := make([]responses.Rollup, len(rollups))
for i := range rollups {
response[i] = responses.NewRollup(rollups[i])
response[i] = responses.NewRollup(&rollups[i])
}

return returnArray(c, response)
Expand Down
39 changes: 39 additions & 0 deletions internal/storage/mock/rollup.go

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

8 changes: 8 additions & 0 deletions internal/storage/postgres/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ func createIndices(ctx context.Context, conn *database.Bun) error {
Exec(ctx); err != nil {
return err
}
if _, err := tx.NewCreateIndex().
IfNotExists().
Model((*storage.Rollup)(nil)).
Index("rollup_size_idx").
Column("size").
Exec(ctx); err != nil {
return err
}

// Rollup actions
if _, err := tx.NewCreateIndex().
Expand Down
16 changes: 16 additions & 0 deletions internal/storage/postgres/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,19 @@ func (r *Rollup) ListRollupsByAddress(ctx context.Context, addressId uint64, lim
err = query.Scan(ctx)
return
}

func (r *Rollup) ListExt(ctx context.Context, fltrs storage.RollupListFilter) (rollups []storage.Rollup, err error) {
query := r.DB().NewSelect().Model(&rollups)

query = limitScope(query, fltrs.Limit)
switch fltrs.SortField {
case "size":
query = sortScope(query, "size", fltrs.SortOrder)
default:
query = sortScope(query, "id", fltrs.SortOrder)
}
query = offsetScope(query, fltrs.Offset)

err = query.Scan(ctx)
return
}
38 changes: 38 additions & 0 deletions internal/storage/postgres/rollup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/hex"
"time"

models "github.com/celenium-io/astria-indexer/internal/storage"
"github.com/dipdup-net/indexer-sdk/pkg/storage"
)

Expand Down Expand Up @@ -106,3 +107,40 @@ func (s *StorageTestSuite) TestListRollupsByAddress() {
s.Require().NotNil(rollup.Rollup)
s.Require().EqualValues(1, rollup.Rollup.Id)
}

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

for _, order := range []storage.SortOrder{
storage.SortOrderAsc,
storage.SortOrderDesc,
} {
for _, field := range []string{
"size",
"id",
"",
} {
rollups, err := s.storage.Rollup.ListExt(ctx, models.RollupListFilter{
Limit: 1,
Offset: 0,
SortField: field,
SortOrder: order,
})
s.Require().NoError(err)
s.Require().Len(rollups, 1)

rollup := rollups[0]

hash, err := hex.DecodeString("19ba8abb3e4b56a309df6756c47b97e298e3a72d88449d36a0fadb1ca7366539")
s.Require().NoError(err)

s.Require().EqualValues(1, rollup.Id)
s.Require().EqualValues(hash, rollup.AstriaId)
s.Require().EqualValues(112, rollup.Size)
s.Require().EqualValues(1, rollup.ActionsCount)
s.Require().EqualValues(7316, rollup.FirstHeight)
}
}

}
8 changes: 8 additions & 0 deletions internal/storage/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type IRollup interface {
ByHash(ctx context.Context, hash []byte) (Rollup, error)
Addresses(ctx context.Context, rollupId uint64, limit, offset int, sort sdk.SortOrder) ([]RollupAddress, error)
ListRollupsByAddress(ctx context.Context, addressId uint64, limit, offset int, sort sdk.SortOrder) ([]RollupAddress, error)
ListExt(ctx context.Context, fltrs RollupListFilter) ([]Rollup, error)
}

type Rollup struct {
Expand All @@ -44,3 +45,10 @@ func (Rollup) TableName() string {
func (r Rollup) String() string {
return hex.EncodeToString(r.AstriaId)
}

type RollupListFilter struct {
Limit int
Offset int
SortField string
SortOrder sdk.SortOrder
}

0 comments on commit 2498465

Please sign in to comment.