Skip to content

Commit

Permalink
feat: optimize volumes endpoint when not using pit (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag authored Dec 9, 2024
1 parent 5814540 commit 471df93
Showing 1 changed file with 39 additions and 26 deletions.
65 changes: 39 additions & 26 deletions internal/storage/ledger/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 471df93

Please sign in to comment.