Skip to content

Commit

Permalink
feat: refine query in storage
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 16, 2024
1 parent 0f16fc4 commit ccba7b8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
24 changes: 17 additions & 7 deletions internal/storage/ledger/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,20 @@ func convertOperatorToSQL(operator string) string {
}

func (s *Store) selectBalance(date *time.Time) *bun.SelectQuery {
return s.SortMovesBySeq(date).
ColumnExpr("(post_commit_volumes->>'input')::numeric - (post_commit_volumes->>'output')::numeric as balance").
Limit(1)

if date != nil && !date.IsZero() {
sortedMoves := s.SortMovesBySeq(date).
ColumnExpr("(post_commit_volumes->>'input')::numeric - (post_commit_volumes->>'output')::numeric as balance").
Limit(1)

return s.db.NewSelect().
ModelTableExpr("(?) moves", sortedMoves).
ColumnExpr("accounts_address, asset")
} else {
return s.db.NewSelect().
ModelTableExpr(s.GetPrefixedRelationName("accounts_volumes")).
ColumnExpr("input - output as balance")
}
}

func (s *Store) selectDistinctAccountMetadataHistories(date *time.Time) *bun.SelectQuery {
Expand Down Expand Up @@ -144,13 +155,11 @@ func (s *Store) selectAccounts(date *time.Time, expandVolumes, expandEffectiveVo
match := balanceRegex.FindAllStringSubmatch(key, 2)
asset := match[0][1]

// todo: use moves only if feature is enabled
return s.db.NewSelect().
// todo: use already loaded pcv
TableExpr(
"(?) balance",
s.selectBalance(date).
Where("asset = ? and moves.accounts_address = accounts.address", asset),
Where("asset = ? and accounts_address = accounts.address", asset),
).
ColumnExpr(fmt.Sprintf("balance %s ?", convertOperatorToSQL(operator)), value).
String(), nil, nil
Expand All @@ -160,7 +169,7 @@ func (s *Store) selectAccounts(date *time.Time, expandVolumes, expandEffectiveVo
TableExpr(
"(?) balance",
s.selectBalance(date).
Where("moves.accounts_address = accounts.address"),
Where("accounts_address = accounts.address"),
).
ColumnExpr(fmt.Sprintf("balance %s ?", convertOperatorToSQL(operator)), value).
String(), nil, nil
Expand Down Expand Up @@ -196,6 +205,7 @@ func (s *Store) selectAccounts(date *time.Time, expandVolumes, expandEffectiveVo
ret = ret.Where(where)
}
}
fmt.Println(ret.String())

return ret
}
Expand Down
9 changes: 8 additions & 1 deletion internal/storage/ledger/moves.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ledger

import (
"context"
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"

"github.com/formancehq/go-libs/platform/postgres"
"github.com/formancehq/go-libs/time"
Expand All @@ -11,7 +12,13 @@ import (
)

func (s *Store) SortMovesBySeq(date *time.Time) *bun.SelectQuery {
ret := s.db.NewSelect().

ret := s.db.NewSelect()
if !s.ledger.HasFeature(ledger.FeatureMovesHistory, "ON") {
return ret.Err(ledgercontroller.NewErrMissingFeature(ledger.FeatureMovesHistory))
}

ret = ret.
ModelTableExpr(s.GetPrefixedRelationName("moves")).
Where("ledger = ?", s.ledger.Name).
Order("seq desc")
Expand Down

0 comments on commit ccba7b8

Please sign in to comment.