Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 23, 2024
1 parent 7ffc3e9 commit 991f710
Show file tree
Hide file tree
Showing 5 changed files with 327 additions and 25 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/alitto/pond v1.9.2
github.com/antlr/antlr4/runtime/Go/antlr v1.4.10
github.com/bluele/gcache v0.0.2
github.com/formancehq/go-libs v1.7.2-0.20240927143916-90a2f07df259
github.com/formancehq/go-libs v1.7.2-0.20240930151849-9e7ee54046e9
github.com/formancehq/stack/ledger/client v0.0.0-00010101000000-000000000000
github.com/go-chi/chi/v5 v5.1.0
github.com/go-chi/cors v1.2.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/formancehq/go-libs v1.7.2-0.20240927143916-90a2f07df259 h1:UNR167W1JQgqqcHyvE6qRc0oYYN68s6zdee2bjlxfDA=
github.com/formancehq/go-libs v1.7.2-0.20240927143916-90a2f07df259/go.mod h1:oLMVltNsXmvqLA0Ox5NSwVSeH8gbnG2ZUo6Lchp23XU=
github.com/formancehq/go-libs v1.7.2-0.20240930151849-9e7ee54046e9 h1:l9ZGJmrgHPww1eyPhpo4jnv4Bjwz2o/Wvt0Fjh//gYg=
github.com/formancehq/go-libs v1.7.2-0.20240930151849-9e7ee54046e9/go.mod h1:oLMVltNsXmvqLA0Ox5NSwVSeH8gbnG2ZUo6Lchp23XU=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/go-chi/chi v4.1.2+incompatible h1:fGFk2Gmi/YKXk0OmGfBh0WgmN3XB8lVnEyNz34tQRec=
Expand Down
80 changes: 56 additions & 24 deletions internal/storage/ledger/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,37 +97,30 @@ func (s *Store) selectAccountWithAssetAndVolumes(date *time.Time, useInsertionDa
Join(
`left join (?) accounts_metadata on accounts_metadata.accounts_address = accounts_volumes.accounts_address`,
s.selectDistinctAccountMetadataHistories(date),
).
ColumnExpr("coalesce(accounts_metadata.metadata, '{}'::jsonb) as metadata")

if needAddressSegment {
selectAccountsWithVolumes = selectAccountsWithVolumes.
Join("join " + s.GetPrefixedRelationName("accounts") + " on accounts.address = accounts_volumes.accounts_address").
Column("accounts.address_array")
}
)
} else {
selectAccountsWithVolumes = selectAccountsWithVolumes.
Join(
`join (?) accounts on accounts.address = accounts_volumes.accounts_address`,
s.db.NewSelect().ModelTableExpr(s.GetPrefixedRelationName("accounts")),
)

if needAddressSegment {
selectAccountsWithVolumes = selectAccountsWithVolumes.Column("accounts.address_array")
}
}
} else {
if needAddressSegment {
selectAccountsWithVolumes = s.db.NewSelect().
TableExpr(
"(?) accounts",
selectAccountsWithVolumes.
Join("join "+s.GetPrefixedRelationName("accounts")+" accounts on accounts.address = accounts_volumes.accounts_address"),
).
ColumnExpr("*")
}
}

if needAddressSegment {
selectAccountsWithVolumes = s.db.NewSelect().
TableExpr(
"(?) accounts",
selectAccountsWithVolumes.
Join("join "+s.GetPrefixedRelationName("accounts")+" accounts on accounts.address = accounts_volumes.accounts_address"),
).
ColumnExpr("address, asset, volumes, metadata").
ColumnExpr("accounts.address_array as accounts_address_array")
}

finalQuery := s.db.NewSelect().
TableExpr("(?) accounts", selectAccountsWithVolumes)

if builder != nil {
where, args, err := builder.Build(query.ContextFn(func(key, operator string, value any) (string, []any, error) {
switch {
Expand All @@ -149,10 +142,10 @@ func (s *Store) selectAccountWithAssetAndVolumes(date *time.Time, useInsertionDa
if err != nil {
return ret.Err(errors.Wrap(err, "building where clause"))
}
selectAccountsWithVolumes = selectAccountsWithVolumes.Where(where, args...)
finalQuery = finalQuery.Where(where, args...)
}

return selectAccountsWithVolumes
return finalQuery
}

func (s *Store) selectAccountWithAggregatedVolumes(date *time.Time, useInsertionDate bool, alias string) *bun.SelectQuery {
Expand Down Expand Up @@ -272,3 +265,42 @@ func (s *Store) GetBalances(ctx context.Context, query ledgercontroller.BalanceQ
return ret, nil
})
}

/**
SELECT "aggregated_volumes"."aggregated"
FROM
(SELECT aggregate_objects(json_build_object(asset, volumes)::JSONB) AS aggregated
FROM
(SELECT "asset",
json_build_object('input', sum((volumes->>'input')::numeric), 'output', sum((volumes->>'output')::numeric)) AS volumes
FROM
(SELECT *
FROM
(SELECT *,
coalesce(accounts_metadata.metadata, '{}'::JSONB) AS metadata
FROM
(SELECT "asset",
"accounts_address",
moves.post_commit_effective_volumes AS volumes
FROM
(SELECT DISTINCT ON (accounts_address,
asset) "accounts_address",
"asset",
first_value(post_commit_effective_volumes) OVER (PARTITION BY (accounts_address, asset)
ORDER BY effective_date DESC, seq DESC) AS post_commit_effective_volumes
FROM "3c29654b".moves
WHERE (ledger = '3c29654b')
AND (effective_date <= '2024-09-30T15:06:25.591246Z')) moves) accounts_volumes
LEFT JOIN
(SELECT DISTINCT ON (accounts_address) "accounts_address",
"metadata"
FROM "3c29654b".accounts_metadata
WHERE (ledger = '3c29654b')
AND (date <= '2024-09-30T15:06:25.591246Z')
ORDER BY "accounts_address",
"revision" DESC) accounts_metadata ON accounts_metadata.accounts_address = accounts_volumes.accounts_address) accounts
WHERE (metadata @> '{"category":"premium"}'))
VALUES
GROUP BY "asset")
VALUES) aggregated_volumes
*/
8 changes: 8 additions & 0 deletions pkg/testserver/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ func GetBalancesAggregated(ctx context.Context, srv *Server, request operations.
return response.V2AggregateBalancesResponse.Data, nil
}

func GetVolumesWithBalances(ctx context.Context, srv *Server, request operations.V2GetVolumesWithBalancesRequest) (*components.V2VolumesWithBalanceCursorResponseCursor, error) {
response, err := srv.Client().Ledger.V2.GetVolumesWithBalances(ctx, request)
if err != nil {
return nil, mapSDKError(err)
}
return &response.V2VolumesWithBalanceCursorResponse.Cursor, nil
}

func UpdateLedgerMetadata(ctx context.Context, srv *Server, request operations.V2UpdateLedgerMetadataRequest) error {
_, err := srv.Client().Ledger.V2.UpdateLedgerMetadata(ctx, request)
return mapSDKError(err)
Expand Down
Loading

0 comments on commit 991f710

Please sign in to comment.