1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-12-26 15:42:27 -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:
Aaron Lindsay 2017-02-19 07:54:27 -05:00
parent c0fb04b722
commit 5157d13737

View File

@ -642,24 +642,29 @@ func TransactionsBalanceDifference(transaction *gorp.Transaction, accountid int6
}
func GetAccountBalance(user *User, accountid int64) (*big.Rat, error) {
var transactions []Transaction
var splits []Split
transaction, err := DB.Begin()
if err != nil {
return nil, err
}
sql := "SELECT DISTINCT transactions.* FROM transactions INNER JOIN splits ON transactions.TransactionId = splits.TransactionId WHERE transactions.UserId=? AND splits.AccountId=?"
_, err = transaction.Select(&transactions, sql, user.UserId, accountid)
sql := "SELECT DISTINCT splits.* FROM splits INNER JOIN transactions ON transactions.TransactionId = splits.TransactionId WHERE splits.AccountId=? AND transactions.UserId=?"
_, err = transaction.Select(&splits, sql, accountid, user.UserId)
if err != nil {
transaction.Rollback()
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 {
transaction.Rollback()
return nil, err
}
tmp.Add(&balance, rat_amount)
balance.Set(&tmp)
}
err = transaction.Commit()
if err != nil {
@ -667,28 +672,34 @@ func GetAccountBalance(user *User, accountid int64) (*big.Rat, error) {
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) {
var transactions []Transaction
var splits []Split
transaction, err := DB.Begin()
if err != nil {
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 < ?"
_, err = transaction.Select(&transactions, sql, user.UserId, accountid, 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(&splits, sql, accountid, user.UserId, date)
if err != nil {
transaction.Rollback()
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 {
transaction.Rollback()
return nil, err
}
tmp.Add(&balance, rat_amount)
balance.Set(&tmp)
}
err = transaction.Commit()
if err != nil {
@ -696,28 +707,33 @@ func GetAccountBalanceDate(user *User, accountid int64, date *time.Time) (*big.R
return nil, err
}
return pageDifference, nil
return &balance, nil
}
func GetAccountBalanceDateRange(user *User, accountid int64, begin, end *time.Time) (*big.Rat, error) {
var transactions []Transaction
var splits []Split
transaction, err := DB.Begin()
if err != nil {
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 < ?"
_, err = transaction.Select(&transactions, sql, user.UserId, accountid, begin, end)
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(&splits, sql, accountid, user.UserId, begin, end)
if err != nil {
transaction.Rollback()
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 {
transaction.Rollback()
return nil, err
}
tmp.Add(&balance, rat_amount)
balance.Set(&tmp)
}
err = transaction.Commit()
if err != nil {
@ -725,7 +741,7 @@ func GetAccountBalanceDateRange(user *User, accountid int64, begin, end *time.Ti
return nil, err
}
return pageDifference, nil
return &balance, nil
}
func GetAccountTransactions(user *User, accountid int64, sort string, page uint64, limit uint64) (*AccountTransactionsList, error) {