From d7689c202bf602d74751d7323aa13098bf963b9e Mon Sep 17 00:00:00 2001 From: Ragot Geoffrey Date: Thu, 10 Oct 2024 12:29:19 +0200 Subject: [PATCH] fix(ledger): /volumes with metadata and group (#1730) --- .../internal/storage/ledgerstore/volumes.go | 51 ++++++++++++------- .../storage/ledgerstore/volumes_test.go | 26 ++++++++-- 2 files changed, 53 insertions(+), 24 deletions(-) diff --git a/components/ledger/internal/storage/ledgerstore/volumes.go b/components/ledger/internal/storage/ledgerstore/volumes.go index a3c4ee3bca..3eebfa4a9e 100644 --- a/components/ledger/internal/storage/ledgerstore/volumes.go +++ b/components/ledger/internal/storage/ledgerstore/volumes.go @@ -99,35 +99,56 @@ func (store *Store) buildVolumesWithBalancesQuery(query *bun.SelectQuery, q GetV dateFilterColumn = "insertion_date" } - query = query. + selectAccounts := store.GetDB().NewSelect(). Column("account_address_array"). Column("account_address"). + Column("accounts_seq"). Column("asset"). + Column("ledger"). 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"). - Table("moves") + Table("moves"). + Group("ledger", "accounts_seq", "account_address", "account_address_array", "asset"). + Apply(filterPIT(filtersForVolumes.PIT, dateFilterColumn)). + Apply(filterOOT(filtersForVolumes.OOT, dateFilterColumn)) + + query = query. + TableExpr("(?) accountsWithVolumes", selectAccounts). + Column( + "account_address", + "account_address_array", + "accounts_seq", + "ledger", + "asset", + "input", + "output", + "balance", + ) if useMetadata { - query = query.ColumnExpr("accounts.metadata as metadata"). + query = query. + ColumnExpr("accounts_metadata.metadata as metadata"). Join(`join lateral ( - select metadata - from accounts a - where a.seq = moves.accounts_seq - ) accounts on true`).Group("metadata") + select metadata + from accounts a + where a.seq = accountsWithVolumes.accounts_seq + ) accounts_metadata on true`, + ) } query = query. - Where("ledger = ?", store.name). - Apply(filterPIT(filtersForVolumes.PIT, dateFilterColumn)). - Apply(filterOOT(filtersForVolumes.OOT, dateFilterColumn)). - GroupExpr("account_address, account_address_array, asset") + Where("ledger = ?", store.name) globalQuery := query.NewSelect() globalQuery = globalQuery. With("query", query). TableExpr("query") + if where != "" { + globalQuery.Where(where, args...) + } + if filtersForVolumes.GroupLvl > 0 { globalQuery = globalQuery. ColumnExpr(fmt.Sprintf(`(array_to_string((string_to_array(account_address, ':'))[1:LEAST(array_length(string_to_array(account_address, ':'),1),%d)],':')) as account`, filtersForVolumes.GroupLvl)). @@ -140,14 +161,6 @@ func (store *Store) buildVolumesWithBalancesQuery(query *bun.SelectQuery, q GetV globalQuery = globalQuery.ColumnExpr("account_address as account, asset, input, output, balance") } - if useMetadata { - globalQuery = globalQuery.Column("metadata") - } - - if where != "" { - globalQuery.Where(where, args...) - } - return globalQuery } diff --git a/components/ledger/internal/storage/ledgerstore/volumes_test.go b/components/ledger/internal/storage/ledgerstore/volumes_test.go index 4c8750d368..578d1f4452 100644 --- a/components/ledger/internal/storage/ledgerstore/volumes_test.go +++ b/components/ledger/internal/storage/ledgerstore/volumes_test.go @@ -395,9 +395,7 @@ func TestGetVolumesWithBalances(t *testing.T) { require.NoError(t, err) require.Len(t, volumes.Data, 1) - }) - } func TestAggGetVolumesWithBalances(t *testing.T) { @@ -406,11 +404,8 @@ func TestAggGetVolumesWithBalances(t *testing.T) { now := time.Now() ctx := logging.TestingContext() - // previousPIT := now.Add(-2 * time.Minute) futurPIT := now.Add(2 * time.Minute) - previousOOT := now.Add(-2 * time.Minute) - // futurOOT := now.Add(2 * time.Minute) require.NoError(t, store.InsertLogs(ctx, ledger.ChainLogs( @@ -468,6 +463,10 @@ func TestAggGetVolumesWithBalances(t *testing.T) { WithIDUint64(7), map[string]metadata.Metadata{}, ).WithDate(now), + + ledger.NewSetMetadataOnAccountLog(time.Now(), "account:1:1", metadata.Metadata{ + "foo": "bar", + }), )..., )) @@ -629,4 +628,21 @@ func TestAggGetVolumesWithBalances(t *testing.T) { }) }) + t.Run("filter using account matching, metadata, and group", func(t *testing.T) { + t.Parallel() + + volumes, err := store.GetVolumesWithBalances(ctx, + NewGetVolumesWithBalancesQuery( + NewPaginatedQueryOptions( + FiltersForVolumes{ + GroupLvl: 1, + }).WithQueryBuilder(query.And( + query.Match("account", "account::"), + query.Match("metadata[foo]", "bar"), + ))), + ) + + require.NoError(t, err) + require.Len(t, volumes.Data, 1) + }) }