Skip to content

Commit

Permalink
Rollup stats tvl;
Browse files Browse the repository at this point in the history
  • Loading branch information
mismirnov committed Jan 14, 2025
1 parent ea438f1 commit 5b53c7f
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 12 deletions.
29 changes: 20 additions & 9 deletions cmd/api/handler/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func (handler RollupHandler) GetBlobs(c echo.Context) error {
type rollupStatsRequest struct {
Id uint64 `example:"1" param:"id" swaggertype:"integer" validate:"required,min=1"`
Timeframe string `example:"hour" param:"timeframe" swaggertype:"string" validate:"required,oneof=hour day month"`
SeriesName string `example:"tps" param:"name" swaggertype:"string" validate:"required,oneof=blobs_count size size_per_blob fee"`
SeriesName string `example:"tps" param:"name" swaggertype:"string" validate:"required,oneof=blobs_count size size_per_blob fee tvl"`
From int64 `example:"1692892095" query:"from" swaggertype:"integer" validate:"omitempty,min=1"`
To int64 `example:"1692892095" query:"to" swaggertype:"integer" validate:"omitempty,min=1"`
}
Expand All @@ -341,7 +341,7 @@ type rollupStatsRequest struct {
// @Tags rollup
// @ID get-rollup-stats
// @Param id path integer true "Internal identity" mininum(1)
// @Param name path string true "Series name" Enums(blobs_count, size, size_per_blob, fee)
// @Param name path string true "Series name" Enums(blobs_count, size, size_per_blob, fee, tvl)
// @Param timeframe path string true "Timeframe" Enums(hour, 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)
Expand All @@ -356,13 +356,24 @@ func (handler RollupHandler) Stats(c echo.Context) error {
return badRequestError(c, err)
}

histogram, err := handler.rollups.Series(
c.Request().Context(),
req.Id,
req.Timeframe,
req.SeriesName,
storage.NewSeriesRequest(req.From, req.To),
)
var histogram []storage.HistogramItem
if req.SeriesName == "tvl" {
histogram, err = handler.rollups.Tvl(
c.Request().Context(),
req.Id,
req.Timeframe,
storage.NewSeriesRequest(req.From, req.To),
)
} else {
histogram, err = handler.rollups.Series(
c.Request().Context(),
req.Id,
req.Timeframe,
req.SeriesName,
storage.NewSeriesRequest(req.From, req.To),
)
}

if err != nil {
return handleError(c, err, handler.rollups)
}
Expand Down
10 changes: 10 additions & 0 deletions database/views/25_rollup_tvl_by_month.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE MATERIALIZED VIEW IF NOT EXISTS rollup_tvl_by_month
WITH (timescaledb.continuous, timescaledb.materialized_only=false) AS
select
time_bucket('1 month'::interval, logs.time) AS time,
logs.rollup_id as rollup_id,
sum(logs.value) as value
from tvl as logs
group by 1, 2
with no data;
CALL add_view_refresh_job('rollup_tvl_by_month', NULL, INTERVAL '1 hour');
42 changes: 39 additions & 3 deletions internal/storage/mock/rollup.go

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

30 changes: 30 additions & 0 deletions internal/storage/postgres/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,36 @@ func (r *Rollup) ById(ctx context.Context, rollupId uint64) (rollup storage.Roll
return
}

func (r *Rollup) Tvl(ctx context.Context, rollupId uint64, timeframe string, req storage.SeriesRequest) (items []storage.HistogramItem, err error) {
query := r.DB().NewSelect().
ColumnExpr("value, time as bucket").
Where("rollup_id = ?", rollupId).
Limit(100).
Order("time desc")

switch timeframe {
case "hour":
return nil, errors.Errorf("unavailable data for this timeframe: %s", timeframe)
case "day":
query = query.Table("tvl")
case "month":
query = query.Table(storage.ViewRollupTvlByMonth)
default:
return nil, errors.Errorf("invalid 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.Scan(ctx, &items)

return
}

func (r *Rollup) Distribution(ctx context.Context, rollupId uint64, series, groupBy string) (items []storage.DistributionItem, err error) {
providers, err := r.Providers(ctx, rollupId)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/storage/postgres/tvl_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// SPDX-FileCopyrightText: 2024 PK Lab AG <[email protected]>
// SPDX-License-Identifier: MIT

package postgres

import (
Expand Down
1 change: 1 addition & 0 deletions internal/storage/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type IRollup interface {
ById(ctx context.Context, rollupId uint64) (RollupWithStats, error)
Series(ctx context.Context, rollupId uint64, timeframe, column string, req SeriesRequest) (items []HistogramItem, err error)
AllSeries(ctx context.Context) ([]RollupHistogramItem, error)
Tvl(ctx context.Context, rollupId uint64, timeframe string, req SeriesRequest) (items []HistogramItem, err error)
Count(ctx context.Context) (int64, error)
Distribution(ctx context.Context, rollupId uint64, series, groupBy string) (items []DistributionItem, err error)
BySlug(ctx context.Context, slug string) (RollupWithStats, error)
Expand Down
1 change: 1 addition & 0 deletions internal/storage/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ const (
ViewRollupStatsByHour = "rollup_stats_by_hour"
ViewRollupStatsByDay = "rollup_stats_by_day"
ViewRollupStatsByMonth = "rollup_stats_by_month"
ViewRollupTvlByMonth = "rollup_tvl_by_month"
)

0 comments on commit 5b53c7f

Please sign in to comment.