Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Filters for TVS #333

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions cmd/api/handler/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,8 @@ func (sh StatsHandler) Tvs(c echo.Context) error {
// @Tags stats
// @ID stats-tvs-series
// @Param timeframe path string true "Timeframe" Enums(day, month)
// @Param from query integer false "Time from in unix timestamp" mininum(1)
// @Param to query integer false "Time to in unix timestamp" mininum(1)
// @Produce json
// @Success 200 {array} responses.SeriesItem
// @Failure 400 {object} Error
Expand All @@ -581,7 +583,11 @@ func (sh StatsHandler) TvsSeries(c echo.Context) error {
return badRequestError(c, err)
}

tvs, err := sh.repo.TvsSeries(c.Request().Context(), storage.Timeframe(req.Timeframe))
tvs, err := sh.repo.TvsSeries(
c.Request().Context(),
storage.Timeframe(req.Timeframe),
storage.NewSeriesRequest(req.From, req.To))

if err != nil {
return handleError(c, err, sh.nsRepo)
}
Expand All @@ -597,5 +603,7 @@ func (sh StatsHandler) TvsSeries(c echo.Context) error {
}

type tvsSeriesRequest struct {
Timeframe string `example:"hour" param:"timeframe" swaggertype:"string" validate:"required,oneof=day month"`
Timeframe string `example:"hour" param:"timeframe" swaggertype:"string" validate:"required,oneof=day month"`
From int64 `example:"1692892095" query:"from" swaggertype:"integer" validate:"omitempty,min=1"`
To int64 `example:"1692892095" query:"to" swaggertype:"integer" validate:"omitempty,min=1"`
}
2 changes: 1 addition & 1 deletion cmd/api/handler/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ func (s *StatsTestSuite) TestTvsSeries() {
c.SetParamValues(string(tf))

s.stats.EXPECT().
TvsSeries(gomock.Any(), tf).
TvsSeries(gomock.Any(), tf, gomock.Any()).
Return([]storage.SeriesItem{
{
Time: testTime,
Expand Down
12 changes: 6 additions & 6 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: 8 additions & 1 deletion internal/storage/postgres/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ func (s Stats) Tvs(ctx context.Context) (tvs decimal.Decimal, err error) {
return
}

func (s Stats) TvsSeries(ctx context.Context, timeframe storage.Timeframe) (response []storage.SeriesItem, err error) {
func (s Stats) TvsSeries(ctx context.Context, timeframe storage.Timeframe, req storage.SeriesRequest) (response []storage.SeriesItem, err error) {
query := s.db.DB().NewSelect()

switch timeframe {
Expand All @@ -452,6 +452,13 @@ func (s Stats) TvsSeries(ctx context.Context, timeframe storage.Timeframe) (resp
return nil, errors.Errorf("unexpected timeframe %s", timeframe)
}

if !req.From.IsZero() {
query = query.Where("time >= ?", req.From)
}
if !req.To.IsZero() {
query = query.Where("time < ?", req.To)
}

err = query.Order("time desc").
Limit(100).
Scan(ctx, &response)
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,5 @@ type IStats interface {
Change24hBlockStats(ctx context.Context) (response Change24hBlockStats, err error)
MessagesCount24h(ctx context.Context) ([]CountItem, error)
Tvs(ctx context.Context) (decimal.Decimal, error)
TvsSeries(ctx context.Context, timeframe Timeframe) ([]SeriesItem, error)
TvsSeries(ctx context.Context, timeframe Timeframe, req SeriesRequest) ([]SeriesItem, error)
}
Loading