From 471df930a94f036e8036702d1c8cde9f2499d5ae Mon Sep 17 00:00:00 2001 From: Ragot Geoffrey Date: Mon, 9 Dec 2024 20:39:24 +0100 Subject: [PATCH] feat: optimize volumes endpoint when not using pit (#608) --- internal/storage/ledger/volumes.go | 65 ++++++++++++++++++------------ 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/internal/storage/ledger/volumes.go b/internal/storage/ledger/volumes.go index c0a8644cf..8b7c26722 100644 --- a/internal/storage/ledger/volumes.go +++ b/internal/storage/ledger/volumes.go @@ -5,9 +5,8 @@ import ( "fmt" "github.com/formancehq/go-libs/v2/collectionutils" "github.com/formancehq/go-libs/v2/platform/postgres" - "github.com/formancehq/ledger/pkg/features" - "github.com/formancehq/ledger/internal/tracing" + "github.com/formancehq/ledger/pkg/features" "github.com/formancehq/go-libs/v2/bun/bunpaginate" lquery "github.com/formancehq/go-libs/v2/query" @@ -68,10 +67,6 @@ func (s *Store) UpdateVolumes(ctx context.Context, accountVolumes ...ledger.Acco func (s *Store) selectVolumes(oot, pit *time.Time, useInsertionDate bool, groupLevel int, q lquery.Builder) *bun.SelectQuery { ret := s.db.NewSelect() - if !s.ledger.HasFeature(features.FeatureMovesHistory, "ON") { - return ret.Err(ledgercontroller.NewErrMissingFeature(features.FeatureMovesHistory)) - } - var ( useMetadata bool needSegmentAddress bool @@ -107,27 +102,45 @@ func (s *Store) selectVolumes(oot, pit *time.Time, useInsertionDate bool, groupL } } - selectVolumes := s.db.NewSelect(). - ColumnExpr("accounts_address as address"). - Column("asset"). - ColumnExpr("sum(case when not is_source then amount else 0 end) as input"). - ColumnExpr("sum(case when is_source then amount else 0 end) as output"). - ColumnExpr("sum(case when not is_source then amount else -amount end) as balance"). - ModelTableExpr(s.GetPrefixedRelationName("moves")). - Where("ledger = ?", s.ledger.Name). - GroupExpr("accounts_address, asset"). - Order("accounts_address", "asset") - - dateFilterColumn := "effective_date" - if useInsertionDate { - dateFilterColumn = "insertion_date" - } + var selectVolumes *bun.SelectQuery - if pit != nil && !pit.IsZero() { - selectVolumes = selectVolumes.Where(dateFilterColumn+" <= ?", pit) - } - if oot != nil && !oot.IsZero() { - selectVolumes = selectVolumes.Where(dateFilterColumn+" >= ?", oot) + if (pit == nil || pit.IsZero()) && (oot == nil || oot.IsZero()) { + selectVolumes = s.db.NewSelect(). + DistinctOn("accounts_address, asset"). + ColumnExpr("accounts_address as address"). + Column("asset", "input", "output"). + ColumnExpr("input - output as balance"). + ModelTableExpr(s.GetPrefixedRelationName("accounts_volumes")). + Where("ledger = ?", s.ledger.Name). + Order("accounts_address", "asset") + } else { + if !s.ledger.HasFeature(features.FeatureMovesHistory, "ON") { + return ret.Err(ledgercontroller.NewErrMissingFeature(features.FeatureMovesHistory)) + } + + dateFilterColumn := "effective_date" + if useInsertionDate { + dateFilterColumn = "insertion_date" + } + + selectVolumes = s.db.NewSelect(). + ColumnExpr("accounts_address as address"). + Column("asset"). + ColumnExpr("sum(case when not is_source then amount else 0 end) as input"). + ColumnExpr("sum(case when is_source then amount else 0 end) as output"). + ColumnExpr("sum(case when not is_source then amount else -amount end) as balance"). + ModelTableExpr(s.GetPrefixedRelationName("moves")). + Where("ledger = ?", s.ledger.Name). + GroupExpr("accounts_address, asset"). + Order("accounts_address", "asset") + + if pit != nil && !pit.IsZero() { + selectVolumes = selectVolumes.Where(dateFilterColumn+" <= ?", pit) + } + + if oot != nil && !oot.IsZero() { + selectVolumes = selectVolumes.Where(dateFilterColumn+" >= ?", oot) + } } ret = ret.