Skip to content

Commit

Permalink
fix: post commit effective volumes rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Dec 20, 2024
1 parent 2f85f35 commit 348a006
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 18 deletions.
2 changes: 1 addition & 1 deletion internal/storage/ledger/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func (r *paginatedResourceRepository[ResourceType, OptionsType, PaginationQueryT
finalQuery = finalQuery.Order("row_number")

ret := make([]ResourceType, 0)
//fmt.Println(finalQuery.Model(&ret).String())
fmt.Println(finalQuery.Model(&ret).String())
err = finalQuery.Model(&ret).Scan(ctx)
if err != nil {
return nil, err
Expand Down
53 changes: 38 additions & 15 deletions internal/storage/ledger/resource_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (h transactionsResourceHandler) buildDataset(store *Store, opts repositoryH
ret = ret.ColumnExpr("metadata")
}

if opts.PIT != nil && !opts.PIT.IsZero() {
if opts.UsePIT() {
ret = ret.ColumnExpr("(case when transactions.reverted_at <= ? then transactions.reverted_at else null end) as reverted_at", opts.PIT)
} else {
ret = ret.Column("reverted_at")
Expand Down Expand Up @@ -162,6 +162,39 @@ func (h transactionsResourceHandler) expand(store *Store, opts ledgercontroller.
return nil, nil, nil
}

/**
SELECT "transactions_id", public.aggregate_objects(post_commit_effective_volumes::JSONB) AS post_commit_effective_volumes
FROM (
SELECT "transactions_id", json_build_object(moves.accounts_address, json_build_object(moves.asset, json_build_object('input', (moves.post_commit_effective_volumes).inputs, 'output', (moves.post_commit_effective_volumes).outputs))) AS post_commit_effective_volumes
FROM (
SELECT DISTINCT ON (transactions_id, accounts_address, asset)
"transactions_id",
"accounts_address",
"asset",
first_value(moves.post_commit_effective_volumes)
OVER (PARTITION BY (transactions_id, accounts_address, asset) ORDER BY seq DESC) AS post_commit_effective_volumes
FROM "_default".moves
) moves
) DATA
GROUP BY "transactions_id";
SELECT "transactions_id", public.aggregate_objects(json_build_object(accounts_address, post_commit_effective_volumes)::jsonb) AS post_commit_effective_volumes
FROM (
SELECT "transactions_id", "accounts_address", public.aggregate_objects(json_build_object(moves.asset, json_build_object('input', (moves.post_commit_effective_volumes).inputs, 'output', (moves.post_commit_effective_volumes).outputs))::jsonb) AS post_commit_effective_volumes
FROM (
SELECT DISTINCT ON (transactions_id, accounts_address, asset)
"transactions_id",
"accounts_address",
"asset",
first_value(moves.post_commit_effective_volumes)
OVER (PARTITION BY (transactions_id, accounts_address, asset) ORDER BY seq DESC) AS post_commit_effective_volumes
FROM "_default".moves
) moves
GROUP BY "transactions_id", "accounts_address"
) data
GROUP BY "transactions_id";
*/

ret := store.db.NewSelect().
TableExpr(
"(?) data",
Expand All @@ -176,22 +209,12 @@ func (h transactionsResourceHandler) expand(store *Store, opts ledgercontroller.
Where("ledger = ?", store.ledger.Name).
Where("transactions_id in (select id from dataset)"),
).
Column("transactions_id").
ColumnExpr(`
json_build_object(
moves.accounts_address,
json_build_object(
moves.asset,
json_build_object(
'input', (moves.post_commit_effective_volumes).inputs,
'output', (moves.post_commit_effective_volumes).outputs
)
)
) as post_commit_effective_volumes
`),
Column("transactions_id", "accounts_address").
ColumnExpr(`public.aggregate_objects(json_build_object(moves.asset, json_build_object('input', (moves.post_commit_effective_volumes).inputs, 'output', (moves.post_commit_effective_volumes).outputs))::jsonb) AS post_commit_effective_volumes`).
Group("transactions_id", "accounts_address"),
).
Column("transactions_id").
ColumnExpr("public.aggregate_objects(post_commit_effective_volumes::jsonb) as post_commit_effective_volumes").
ColumnExpr("public.aggregate_objects(json_build_object(accounts_address, post_commit_effective_volumes)::jsonb) AS post_commit_effective_volumes").
Group("transactions_id")

return ret, &joinCondition{
Expand Down
3 changes: 2 additions & 1 deletion internal/storage/ledger/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func (store *Store) CommitTransaction(ctx context.Context, tx *ledger.Transactio

if store.ledger.HasFeature(features.FeatureMovesHistory, "ON") {
moves := ledger.Moves{}
postings := tx.Postings
postings := make([]ledger.Posting, len(tx.Postings))
copy(postings, tx.Postings)
slices.Reverse(postings)

for _, posting := range postings {
Expand Down
5 changes: 4 additions & 1 deletion internal/storage/ledger/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ func TestTransactionsList(t *testing.T) {
tx1 := ledger.NewTransaction().
WithPostings(
ledger.NewPosting("world", "alice", "USD", big.NewInt(100)),
ledger.NewPosting("world", "alice", "EUR", big.NewInt(100)),
).
WithMetadata(metadata.Metadata{"category": "1"}).
WithTimestamp(now.Add(-3 * time.Hour))
Expand Down Expand Up @@ -851,6 +852,8 @@ func TestTransactionsList(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

store.DumpTables(ctx, "transactions")

tc.query.Options.Expand = []string{"volumes", "effectiveVolumes"}

cursor, err := store.Transactions().Paginate(ctx, tc.query)
Expand All @@ -868,4 +871,4 @@ func TestTransactionsList(t *testing.T) {
}
})
}
}
}
6 changes: 6 additions & 0 deletions test/e2e/api_transactions_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ var _ = Context("Ledger transactions list API tests", func() {
Source: "world",
Destination: fmt.Sprintf("account:%d", i),
},
{
Amount: big.NewInt(100),
Asset: "EUR",
Source: "world",
Destination: fmt.Sprintf("account:%d", i),
},
},
Timestamp: pointer.For(txTimestamp),
},
Expand Down

0 comments on commit 348a006

Please sign in to comment.