Skip to content

Commit

Permalink
Feature: add messages count distribution for the last 24 hours (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Jul 14, 2024
1 parent 6eb277c commit 59ab202
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 0 deletions.
45 changes: 45 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.

12 changes: 12 additions & 0 deletions cmd/api/handler/responses/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,15 @@ func NewRollupStats24h(stats storage.RollupStats24h) RollupStats24h {
BlobsCount: stats.BlobsCount,
}
}

type CountItem struct {
Name string `example:"test" format:"string" json:"name" swaggertype:"string"`
Value int64 `example:"17632" format:"string" json:"value" swaggertype:"string"`
}

func NewCountItem(item storage.CountItem) CountItem {
return CountItem{
Name: item.Name,
Value: item.Value,
}
}
25 changes: 25 additions & 0 deletions cmd/api/handler/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,3 +519,28 @@ func (sh StatsHandler) RollupStats24h(c echo.Context) error {
}
return returnArray(c, response)
}

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

response := make([]responses.CountItem, len(items))
for i := range items {
response[i] = responses.NewCountItem(items[i])
}
return returnArray(c, response)
}
27 changes: 27 additions & 0 deletions cmd/api/handler/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,30 @@ func (s *StatsTestSuite) TestRollupStats24h() {
s.Require().EqualValues("name", item.Name)
s.Require().EqualValues("logo", item.Logo)
}

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

s.stats.EXPECT().
MessagesCount24h(gomock.Any()).
Return([]storage.CountItem{
{
Name: "test",
Value: 100,
},
}, nil)

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

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

s.Require().EqualValues("test", response[0].Name)
s.Require().EqualValues(100, response[0].Value)
}
1 change: 1 addition & 0 deletions cmd/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ func initHandlers(ctx context.Context, e *echo.Echo, cfg Config, db postgres.Sto
stats.GET("/changes_24h", statsHandler.Change24hBlockStats)
stats.GET("/rollup_stats_24h", statsHandler.RollupStats24h)
stats.GET("/square_size", statsHandler.SquareSize)
stats.GET("/messages_count_24h", statsHandler.MessagesCount24h)

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 @@ -99,6 +99,7 @@ func TestRoutes(t *testing.T) {
"/v1/docs GET": {},
"/v1/stats/square_size GET": {},
"/v1/stats/rollup_stats_24h GET": {},
"/v1/stats/messages_count_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.

10 changes: 10 additions & 0 deletions internal/storage/postgres/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,16 @@ func (s Stats) RollupStats24h(ctx context.Context) (response []storage.RollupSta
Join("left join rollup as r on r.id = grouped.rollup_id").
Order("blobs_count desc").
Scan(ctx, &response)
return
}

func (s Stats) MessagesCount24h(ctx context.Context) (response []storage.CountItem, err error) {
err = s.db.DB().NewSelect().
Model((*storage.Message)(nil)).
ColumnExpr("count(*) as value, type as name").
Where("time > now() - '1 day'::interval").
Group("type").
Order("value 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 @@ -296,6 +296,15 @@ func (s *StatsTestSuite) TestRollupStats24h() {
s.Require().Len(stats, 0)
}

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

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

func TestSuiteStats_Run(t *testing.T) {
suite.Run(t, new(StatsTestSuite))
}
6 changes: 6 additions & 0 deletions internal/storage/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ type DistributionItem struct {
Value string `bun:"value"`
}

type CountItem struct {
Name string `bun:"name"`
Value int64 `bun:"value"`
}

func NewSeriesRequest(from, to int64) SeriesRequest {
var seriesRequest SeriesRequest
if from > 0 {
Expand Down Expand Up @@ -174,4 +179,5 @@ type IStats interface {
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)
MessagesCount24h(ctx context.Context) ([]CountItem, error)
}

0 comments on commit 59ab202

Please sign in to comment.