Skip to content

Commit

Permalink
Feature: add rollup stats for last hours (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Jul 13, 2024
1 parent dc338d1 commit e2bf484
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 0 deletions.
71 changes: 71 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.

20 changes: 20 additions & 0 deletions cmd/api/handler/responses/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,23 @@ func NewSquareSizeResponse(m map[int][]storage.SeriesItem) SquareSizeResponse {
}
return response
}

type RollupStats24h struct {
Id int64 `example:"321" format:"integer" json:"id,omitempty" swaggertype:"integer"`
Name string `example:"Rollup name" format:"string" json:"name,omitempty" swaggertype:"string"`
Logo string `example:"https://some_link.com/image.png" format:"string" json:"logo,omitempty" swaggertype:"string"`
Size int64 `example:"123" format:"integer" json:"size" swaggertype:"integer"`
Fee int64 `example:"123" format:"integer" json:"fee" swaggertype:"integer"`
BlobsCount int64 `example:"123" format:"integer" json:"blobs_count" swaggertype:"integer"`
}

func NewRollupStats24h(stats storage.RollupStats24h) RollupStats24h {
return RollupStats24h{
Id: stats.RollupId,
Name: stats.Name,
Logo: stats.Logo,
Size: stats.Size,
Fee: stats.Fee,
BlobsCount: stats.BlobsCount,
}
}
26 changes: 26 additions & 0 deletions cmd/api/handler/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,29 @@ func (sh StatsHandler) SquareSize(c echo.Context) error {

return c.JSON(http.StatusOK, responses.NewSquareSizeResponse(histogram))
}

// RollupStats24h godoc
//
// @Summary Get rollups stats for last 24 hours
// @Description Get rollups stats for last 24 hours
// @Tags stats
// @ID stats-rollup-24h
// @Produce json
// @Success 200 {array} responses.RollupStats24h
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /stats/rollup_stats_24h [get]
func (sh StatsHandler) RollupStats24h(c echo.Context) error {
items, err := sh.repo.RollupStats24h(
c.Request().Context(),
)
if err != nil {
return handleError(c, err, sh.nsRepo)
}

response := make([]responses.RollupStats24h, len(items))
for i := range items {
response[i] = responses.NewRollupStats24h(items[i])
}
return returnArray(c, response)
}
36 changes: 36 additions & 0 deletions cmd/api/handler/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,3 +519,39 @@ func (s *StatsTestSuite) TestCumulativeSeries() {
}
}
}

func (s *StatsTestSuite) TestRollupStats24h() {
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := s.echo.NewContext(req, rec)
c.SetPath("/v1/stats/rollup_stats_24h")

s.stats.EXPECT().
RollupStats24h(gomock.Any()).
Return([]storage.RollupStats24h{
{
Name: "name",
Logo: "logo",
RollupId: 1,
Size: 12,
Fee: 43,
BlobsCount: 123,
},
}, nil)

s.Require().NoError(s.handler.RollupStats24h(c))
s.Require().Equal(http.StatusOK, rec.Code)

var response []responses.RollupStats24h
err := json.NewDecoder(rec.Body).Decode(&response)
s.Require().NoError(err)
s.Require().Len(response, 1)

item := response[0]
s.Require().EqualValues(1, item.Id)
s.Require().EqualValues(12, item.Size)
s.Require().EqualValues(43, item.Fee)
s.Require().EqualValues(123, item.BlobsCount)
s.Require().EqualValues("name", item.Name)
s.Require().EqualValues("logo", item.Logo)
}
1 change: 1 addition & 0 deletions cmd/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ func initHandlers(ctx context.Context, e *echo.Echo, cfg Config, db postgres.Sto
stats.GET("/summary/:table/:function", statsHandler.Summary)
stats.GET("/tps", statsHandler.TPS)
stats.GET("/changes_24h", statsHandler.Change24hBlockStats)
stats.GET("/rollup_stats_24h", statsHandler.RollupStats24h)
stats.GET("/square_size", statsHandler.SquareSize)

price := stats.Group("/price")
Expand Down
1 change: 1 addition & 0 deletions cmd/api/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func TestRoutes(t *testing.T) {
"/v1/rollup/:id/export GET": {},
"/v1/docs GET": {},
"/v1/stats/square_size GET": {},
"/v1/stats/rollup_stats_24h GET": {},
}

ctx, cancel := context.WithCancel(context.Background())
Expand Down
39 changes: 39 additions & 0 deletions internal/storage/mock/stats.go

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

22 changes: 22 additions & 0 deletions internal/storage/postgres/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,25 @@ func (s Stats) SquareSize(ctx context.Context, from, to *time.Time) (result map[

return
}

func (s Stats) RollupStats24h(ctx context.Context) (response []storage.RollupStats24h, err error) {
inner := s.db.DB().NewSelect().
Table(storage.ViewRollupStatsByHour).
Column("namespace_id", "signer_id", "size", "fee", "blobs_count").
Where("time > now() - '1 day'::interval")

joined := s.db.DB().NewSelect().
TableExpr("(?) as data", inner).
ColumnExpr("rollup_id, sum(data.size) as size, sum(data.fee) as fee, sum(data.blobs_count) as blobs_count").
Join("left join rollup_provider as rp on rp.address_id = data.signer_id AND (rp.namespace_id = data.namespace_id OR rp.namespace_id = 0)").
Group("rollup_id")

err = s.db.DB().NewSelect().
TableExpr("(?) as grouped", joined).
ColumnExpr("grouped.*, r.name, r.logo").
Join("left join rollup as r on r.id = grouped.rollup_id").
Order("blobs_count desc").
Scan(ctx, &response)

return
}
9 changes: 9 additions & 0 deletions internal/storage/postgres/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,15 @@ func (s *StatsTestSuite) TestSquareSize() {
s.Require().Len(items, 1)
}

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

stats, err := s.storage.Stats.RollupStats24h(ctx)
s.Require().NoError(err)
s.Require().Len(stats, 0)
}

func TestSuiteStats_Run(t *testing.T) {
suite.Run(t, new(StatsTestSuite))
}
10 changes: 10 additions & 0 deletions internal/storage/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ func NewSeriesRequest(from, to int64) SeriesRequest {
return seriesRequest
}

type RollupStats24h struct {
RollupId int64 `bun:"rollup_id"`
Name string `bun:"name"`
Logo string `bun:"logo"`
Size int64 `bun:"size"`
Fee int64 `bun:"fee"`
BlobsCount int64 `bun:"blobs_count"`
}

type SeriesItem struct {
Time time.Time `bun:"ts"`
Value string `bun:"value"`
Expand Down Expand Up @@ -162,6 +171,7 @@ type IStats interface {
CumulativeSeries(ctx context.Context, timeframe Timeframe, name string, req SeriesRequest) ([]SeriesItem, error)
NamespaceSeries(ctx context.Context, timeframe Timeframe, name string, nsId uint64, req SeriesRequest) (response []SeriesItem, err error)
StakingSeries(ctx context.Context, timeframe Timeframe, name string, validatorId uint64, req SeriesRequest) (response []SeriesItem, err error)
RollupStats24h(ctx context.Context) ([]RollupStats24h, error)
SquareSize(ctx context.Context, from, to *time.Time) (map[int][]SeriesItem, error)
Change24hBlockStats(ctx context.Context) (response Change24hBlockStats, err error)
}
3 changes: 3 additions & 0 deletions internal/storage/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ const (
ViewStakingByMonth = "staking_by_month"
ViewLeaderboard = "leaderboard"
ViewSquareSize = "square_size"
ViewRollupStatsByHour = "rollup_stats_by_hour"
ViewRollupStatsByDay = "rollup_stats_by_day"
ViewRollupStatsByMonth = "rollup_stats_by_month"
)

0 comments on commit e2bf484

Please sign in to comment.