Skip to content

Commit

Permalink
Feature: add active addresses count (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Oct 4, 2024
1 parent 6dd7976 commit b09c91d
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 0 deletions.
27 changes: 27 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.

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

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

18 changes: 18 additions & 0 deletions cmd/api/handler/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,21 @@ func (sh StatsHandler) TokenTransferDistribution(c echo.Context) error {
}
return c.JSON(http.StatusOK, response)
}

// ActiveAddressesCount godoc
//
// @Summary Active adddresses count
// @Description Active adddresses count
// @Tags stats
// @ID stats-active-addresses-count
// @Produce json
// @Success 200 {integer} int64
// @Failure 500 {object} Error
// @Router /v1/stats/summary/active_addresses_count [get]
func (sh StatsHandler) ActiveAddressesCount(c echo.Context) error {
count, err := sh.repo.ActiveAddressesCount(c.Request().Context())
if err != nil {
return handleError(c, err, sh.rollups)
}
return c.JSON(http.StatusOK, count)
}
19 changes: 19 additions & 0 deletions cmd/api/handler/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,22 @@ func (s *StatsTestSuite) TestTokenTransferDistribution() {
s.Require().EqualValues(100, summary.TransfersCount)
s.Require().EqualValues(currency.DefaultCurrency, summary.Asset)
}

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

s.stats.EXPECT().
ActiveAddressesCount(gomock.Any()).
Return(100, nil)

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

var result int64
err := json.NewDecoder(rec.Body).Decode(&result)
s.Require().NoError(err)
s.Require().EqualValues(100, result)
}
1 change: 1 addition & 0 deletions cmd/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ func initHandlers(ctx context.Context, e *echo.Echo, cfg Config, db postgres.Sto
{
stats.GET("/summary", statsHandler.Summary)
stats.GET("/summary/:timeframe", statsHandler.SummaryTimeframe)
stats.GET("/summary/active_addresses_count", statsHandler.ActiveAddressesCount)
stats.GET("/series/:name/:timeframe", statsHandler.Series)

rollup := stats.Group("/rollup")
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.

9 changes: 9 additions & 0 deletions internal/storage/postgres/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,12 @@ func (s Stats) TokenTransferDistribution(ctx context.Context, limit int) (items
err = query.Scan(ctx, &items)
return
}

func (s Stats) ActiveAddressesCount(ctx context.Context) (val int64, err error) {
err = s.db.DB().NewSelect().
Model((*storage.Tx)(nil)).
ColumnExpr("count(distinct signer_id)").
Where("time > now() - '1 month'::interval").
Scan(ctx, &val)
return
}
23 changes: 23 additions & 0 deletions internal/storage/postgres/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/celenium-io/astria-indexer/internal/storage"
"github.com/celenium-io/astria-indexer/internal/storage/types"
"github.com/dipdup-net/go-lib/config"
"github.com/dipdup-net/go-lib/database"
"github.com/go-testfixtures/testfixtures/v3"
Expand Down Expand Up @@ -157,6 +158,28 @@ func (s *StatsTestSuite) TestTokenTransferDistribution() {
s.Require().Len(summary, 1)
}

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

tx, err := BeginTransaction(ctx, s.storage.Transactable)
s.Require().NoError(err)

err = tx.SaveTransactions(ctx, &storage.Tx{
Time: time.Now().UTC(),
SignerId: 1,
Status: types.StatusSuccess,
})
s.Require().NoError(err)

s.Require().NoError(tx.Flush(ctx))
s.Require().NoError(tx.Close(ctx))

value, err := s.storage.Stats.ActiveAddressesCount(ctx)
s.Require().NoError(err)
s.Require().EqualValues(1, value)
}

func TestSuiteStats_Run(t *testing.T) {
suite.Run(t, new(StatsTestSuite))
}
1 change: 1 addition & 0 deletions internal/storage/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,5 @@ type IStats interface {
RollupSeries(ctx context.Context, rollupId uint64, timeframe Timeframe, name string, req SeriesRequest) ([]SeriesItem, error)
FeeSummary(ctx context.Context) ([]FeeSummary, error)
TokenTransferDistribution(ctx context.Context, limit int) ([]TokenTransferDistributionItem, error)
ActiveAddressesCount(ctx context.Context) (int64, error)
}

0 comments on commit b09c91d

Please sign in to comment.