mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
optimized wallet header calculation for better performance
160 ms for three wallets over 9500 transactions
This commit is contained in:
@@ -20,3 +20,8 @@ type WalletHeader struct {
|
||||
NextMonth int `json:"nextMonth"`
|
||||
Currency string `json:"currency"`
|
||||
}
|
||||
|
||||
type WalletTransactions struct {
|
||||
WalletId string
|
||||
Transactions []Transaction
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
"wallet-api/pkg/models"
|
||||
"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 {
|
||||
wm := new(models.WalletHeader)
|
||||
//wallets := new([]models.Wallet)
|
||||
var wallets []models.WalletTransactions
|
||||
var wg sync.WaitGroup
|
||||
transactions := new([]models.Transaction)
|
||||
|
||||
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)
|
||||
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 {
|
||||
if trans.TransactionDate.Before(firstOfNextMonth) && trans.TransactionDate.After(firstOfMonth) {
|
||||
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) {
|
||||
if trans.TransactionType.Type == "expense" {
|
||||
lastMonthBalance -= trans.Amount
|
||||
currentBalance -= trans.Amount
|
||||
} else {
|
||||
lastMonthBalance += trans.Amount
|
||||
currentBalance += trans.Amount
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
wm.CurrentBalance = currentBalance
|
||||
wm.LastMonth = lastMonthBalance
|
||||
wm.CurrentBalance = currentBalance + lastMonthBalance
|
||||
wm.NextMonth = currentBalance + nextMonth
|
||||
wm.Currency = "USD"
|
||||
wm.WalletId = walletId
|
||||
@@ -98,3 +110,19 @@ func (as *WalletService) GetHeader(am *models.Auth, embed string, walletId strin
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user