optimized wallet header calculation for better performance

160 ms for three wallets over 9500 transactions
This commit is contained in:
Fran Jurmanović
2021-06-15 20:13:34 +02:00
parent c30da47083
commit ebcb507629
3 changed files with 57 additions and 24 deletions

View File

@@ -20,3 +20,8 @@ type WalletHeader struct {
NextMonth int `json:"nextMonth"` NextMonth int `json:"nextMonth"`
Currency string `json:"currency"` Currency string `json:"currency"`
} }
type WalletTransactions struct {
WalletId string
Transactions []Transaction
}

View File

@@ -1,6 +1,7 @@
package services package services
import ( import (
"sync"
"time" "time"
"wallet-api/pkg/models" "wallet-api/pkg/models"
"wallet-api/pkg/utl/common" "wallet-api/pkg/utl/common"
@@ -40,7 +41,8 @@ func (as *WalletService) GetAll(am *models.Auth, filtered *models.FilteredRespon
func (as *WalletService) GetHeader(am *models.Auth, embed string, walletId string) *models.WalletHeader { func (as *WalletService) GetHeader(am *models.Auth, embed string, walletId string) *models.WalletHeader {
wm := new(models.WalletHeader) wm := new(models.WalletHeader)
//wallets := new([]models.Wallet) var wallets []models.WalletTransactions
var wg sync.WaitGroup
transactions := new([]models.Transaction) transactions := new([]models.Transaction)
query := as.Db.Model(transactions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType") query := as.Db.Model(transactions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType")
@@ -64,6 +66,14 @@ func (as *WalletService) GetHeader(am *models.Auth, embed string, walletId strin
firstOfNextMonth := time.Date(currentYear, currentMonth+1, 1, 0, 0, 0, 0, currentLocation) firstOfNextMonth := time.Date(currentYear, currentMonth+1, 1, 0, 0, 0, 0, currentLocation)
firstOfMonthAfterNext := time.Date(currentYear, currentMonth+2, 1, 0, 0, 0, 0, currentLocation) firstOfMonthAfterNext := time.Date(currentYear, currentMonth+2, 1, 0, 0, 0, 0, currentLocation)
for _, trans := range *transactions {
addWhere(&wallets, trans.WalletID, trans)
}
for range wallets {
wg.Add(1)
go func() {
defer wg.Done()
for _, trans := range *transactions { for _, trans := range *transactions {
if trans.TransactionDate.Before(firstOfNextMonth) && trans.TransactionDate.After(firstOfMonth) { if trans.TransactionDate.Before(firstOfNextMonth) && trans.TransactionDate.After(firstOfMonth) {
if trans.TransactionType.Type == "expense" { if trans.TransactionType.Type == "expense" {
@@ -80,16 +90,18 @@ func (as *WalletService) GetHeader(am *models.Auth, embed string, walletId strin
} else if trans.TransactionDate.Before(firstOfMonth) { } else if trans.TransactionDate.Before(firstOfMonth) {
if trans.TransactionType.Type == "expense" { if trans.TransactionType.Type == "expense" {
lastMonthBalance -= trans.Amount lastMonthBalance -= trans.Amount
currentBalance -= trans.Amount
} else { } else {
lastMonthBalance += trans.Amount lastMonthBalance += trans.Amount
currentBalance += trans.Amount
} }
} }
} }
}()
}
wg.Wait()
wm.CurrentBalance = currentBalance
wm.LastMonth = lastMonthBalance wm.LastMonth = lastMonthBalance
wm.CurrentBalance = currentBalance + lastMonthBalance
wm.NextMonth = currentBalance + nextMonth wm.NextMonth = currentBalance + nextMonth
wm.Currency = "USD" wm.Currency = "USD"
wm.WalletId = walletId wm.WalletId = walletId
@@ -98,3 +110,19 @@ func (as *WalletService) GetHeader(am *models.Auth, embed string, walletId strin
return wm return wm
} }
func addWhere(s *[]models.WalletTransactions, walletId string, e models.Transaction) {
var exists bool
for _, a := range *s {
if a.WalletId == walletId {
a.Transactions = append(a.Transactions, e)
}
exists = true
}
if !exists {
var walletTransaction models.WalletTransactions
walletTransaction.WalletId = walletId
walletTransaction.Transactions = append(walletTransaction.Transactions, e)
*s = append(*s, walletTransaction)
}
}