From d0935fe4ea88a2129afe25ebca1947f386855bb9 Mon Sep 17 00:00:00 2001 From: Geoffrey Ragot Date: Fri, 27 Sep 2024 11:54:25 +0200 Subject: [PATCH] chore: simplify storage --- internal/storage/ledger/accounts.go | 16 ++-------------- internal/storage/ledger/balances.go | 9 +++++++++ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/internal/storage/ledger/accounts.go b/internal/storage/ledger/accounts.go index abed4d958..bad568f03 100644 --- a/internal/storage/ledger/accounts.go +++ b/internal/storage/ledger/accounts.go @@ -147,28 +147,16 @@ func (s *Store) selectAccounts(date *time.Time, expandVolumes, expandEffectiveVo } if s.ledger.HasFeature(ledger.FeatureMovesHistory, "ON") && needPCV { - selectAccountWithAssetAndVolumes := s.selectAccountWithAssetAndVolumes(date, true, nil) - selectAccountWithVolumes := s.db.NewSelect(). - TableExpr("(?) values", selectAccountWithAssetAndVolumes). - Group("accounts_address"). - Column("accounts_address"). - ColumnExpr("aggregate_objects(json_build_object(asset, volumes)::jsonb) as pcv") ret = ret.Join( `left join (?) pcv on pcv.accounts_address = accounts.address`, - selectAccountWithVolumes, + s.selectAccountWithAggregatedVolumes(date, true, "pcv"), ).Column("pcv.*") } if s.ledger.HasFeature(ledger.FeatureMovesHistoryPostCommitEffectiveVolumes, "SYNC") && expandEffectiveVolumes { - selectAccountWithAssetAndVolumes := s.selectAccountWithAssetAndVolumes(date, false, nil) - selectAccountWithVolumes := s.db.NewSelect(). - TableExpr("(?) values", selectAccountWithAssetAndVolumes). - Group("accounts_address"). - Column("accounts_address"). - ColumnExpr("aggregate_objects(json_build_object(asset, volumes)::jsonb) as pcev") ret = ret.Join( `left join (?) pcev on pcev.accounts_address = accounts.address`, - selectAccountWithVolumes, + s.selectAccountWithAggregatedVolumes(date, false, "pcev"), ).Column("pcev.*") } diff --git a/internal/storage/ledger/balances.go b/internal/storage/ledger/balances.go index 1d785a4f4..026c21a2b 100644 --- a/internal/storage/ledger/balances.go +++ b/internal/storage/ledger/balances.go @@ -154,6 +154,15 @@ func (s *Store) selectAccountWithAssetAndVolumes(date *time.Time, useInsertionDa return selectAccountsWithVolumes } +func (s *Store) selectAccountWithAggregatedVolumes(date *time.Time, useInsertionDate bool, alias string) *bun.SelectQuery { + selectAccountWithAssetAndVolumes := s.selectAccountWithAssetAndVolumes(date, useInsertionDate, nil) + return s.db.NewSelect(). + TableExpr("(?) values", selectAccountWithAssetAndVolumes). + Group("accounts_address"). + Column("accounts_address"). + ColumnExpr("aggregate_objects(json_build_object(asset, volumes)::jsonb) as " + alias) +} + func (s *Store) SelectAggregatedBalances(date *time.Time, useInsertionDate bool, builder query.Builder) *bun.SelectQuery { selectAccountsWithVolumes := s.selectAccountWithAssetAndVolumes(date, useInsertionDate, builder)