mirror of
https://github.com/aclindsa/moneygo.git
synced 2024-12-27 07:52:28 -05:00
Optimize fetching account balances
There is no need to make a separate SQL query for every split after finding every transaction - we can query for all the splits initially.
This commit is contained in:
parent
c0fb04b722
commit
5157d13737
@ -642,24 +642,29 @@ func TransactionsBalanceDifference(transaction *gorp.Transaction, accountid int6
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetAccountBalance(user *User, accountid int64) (*big.Rat, error) {
|
func GetAccountBalance(user *User, accountid int64) (*big.Rat, error) {
|
||||||
var transactions []Transaction
|
var splits []Split
|
||||||
transaction, err := DB.Begin()
|
transaction, err := DB.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
sql := "SELECT DISTINCT transactions.* FROM transactions INNER JOIN splits ON transactions.TransactionId = splits.TransactionId WHERE transactions.UserId=? AND splits.AccountId=?"
|
sql := "SELECT DISTINCT splits.* FROM splits INNER JOIN transactions ON transactions.TransactionId = splits.TransactionId WHERE splits.AccountId=? AND transactions.UserId=?"
|
||||||
_, err = transaction.Select(&transactions, sql, user.UserId, accountid)
|
_, err = transaction.Select(&splits, sql, accountid, user.UserId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
transaction.Rollback()
|
transaction.Rollback()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
pageDifference, err := TransactionsBalanceDifference(transaction, accountid, transactions)
|
var balance, tmp big.Rat
|
||||||
|
for _, s := range splits {
|
||||||
|
rat_amount, err := GetBigAmount(s.Amount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
transaction.Rollback()
|
transaction.Rollback()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
tmp.Add(&balance, rat_amount)
|
||||||
|
balance.Set(&tmp)
|
||||||
|
}
|
||||||
|
|
||||||
err = transaction.Commit()
|
err = transaction.Commit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -667,28 +672,34 @@ func GetAccountBalance(user *User, accountid int64) (*big.Rat, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return pageDifference, nil
|
return &balance, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Assumes accountid is valid and is owned by the current user
|
||||||
func GetAccountBalanceDate(user *User, accountid int64, date *time.Time) (*big.Rat, error) {
|
func GetAccountBalanceDate(user *User, accountid int64, date *time.Time) (*big.Rat, error) {
|
||||||
var transactions []Transaction
|
var splits []Split
|
||||||
transaction, err := DB.Begin()
|
transaction, err := DB.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
sql := "SELECT DISTINCT transactions.* FROM transactions INNER JOIN splits ON transactions.TransactionId = splits.TransactionId WHERE transactions.UserId=? AND splits.AccountId=? AND transactions.Date < ?"
|
sql := "SELECT DISTINCT splits.* FROM splits INNER JOIN transactions ON transactions.TransactionId = splits.TransactionId WHERE splits.AccountId=? AND transactions.UserId=? AND transactions.Date < ?"
|
||||||
_, err = transaction.Select(&transactions, sql, user.UserId, accountid, date)
|
_, err = transaction.Select(&splits, sql, accountid, user.UserId, date)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
transaction.Rollback()
|
transaction.Rollback()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
pageDifference, err := TransactionsBalanceDifference(transaction, accountid, transactions)
|
var balance, tmp big.Rat
|
||||||
|
for _, s := range splits {
|
||||||
|
rat_amount, err := GetBigAmount(s.Amount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
transaction.Rollback()
|
transaction.Rollback()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
tmp.Add(&balance, rat_amount)
|
||||||
|
balance.Set(&tmp)
|
||||||
|
}
|
||||||
|
|
||||||
err = transaction.Commit()
|
err = transaction.Commit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -696,28 +707,33 @@ func GetAccountBalanceDate(user *User, accountid int64, date *time.Time) (*big.R
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return pageDifference, nil
|
return &balance, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAccountBalanceDateRange(user *User, accountid int64, begin, end *time.Time) (*big.Rat, error) {
|
func GetAccountBalanceDateRange(user *User, accountid int64, begin, end *time.Time) (*big.Rat, error) {
|
||||||
var transactions []Transaction
|
var splits []Split
|
||||||
transaction, err := DB.Begin()
|
transaction, err := DB.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
sql := "SELECT DISTINCT transactions.* FROM transactions INNER JOIN splits ON transactions.TransactionId = splits.TransactionId WHERE transactions.UserId=? AND splits.AccountId=? AND transactions.Date >= ? AND transactions.Date < ?"
|
sql := "SELECT DISTINCT splits.* FROM splits INNER JOIN transactions ON transactions.TransactionId = splits.TransactionId WHERE splits.AccountId=? AND transactions.UserId=? AND transactions.Date >= ? AND transactions.Date < ?"
|
||||||
_, err = transaction.Select(&transactions, sql, user.UserId, accountid, begin, end)
|
_, err = transaction.Select(&splits, sql, accountid, user.UserId, begin, end)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
transaction.Rollback()
|
transaction.Rollback()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
pageDifference, err := TransactionsBalanceDifference(transaction, accountid, transactions)
|
var balance, tmp big.Rat
|
||||||
|
for _, s := range splits {
|
||||||
|
rat_amount, err := GetBigAmount(s.Amount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
transaction.Rollback()
|
transaction.Rollback()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
tmp.Add(&balance, rat_amount)
|
||||||
|
balance.Set(&tmp)
|
||||||
|
}
|
||||||
|
|
||||||
err = transaction.Commit()
|
err = transaction.Commit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -725,7 +741,7 @@ func GetAccountBalanceDateRange(user *User, accountid int64, begin, end *time.Ti
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return pageDifference, nil
|
return &balance, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAccountTransactions(user *User, accountid int64, sort string, page uint64, limit uint64) (*AccountTransactionsList, error) {
|
func GetAccountTransactions(user *User, accountid int64, sort string, page uint64, limit uint64) (*AccountTransactionsList, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user