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"`
|
NextMonth int `json:"nextMonth"`
|
||||||
Currency string `json:"currency"`
|
Currency string `json:"currency"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type WalletTransactions struct {
|
||||||
|
WalletId string
|
||||||
|
Transactions []Transaction
|
||||||
|
}
|
||||||
|
|||||||
@@ -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")
|
||||||
@@ -65,31 +67,41 @@ func (as *WalletService) GetHeader(am *models.Auth, embed string, walletId strin
|
|||||||
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 {
|
for _, trans := range *transactions {
|
||||||
if trans.TransactionDate.Before(firstOfNextMonth) && trans.TransactionDate.After(firstOfMonth) {
|
addWhere(&wallets, trans.WalletID, trans)
|
||||||
if trans.TransactionType.Type == "expense" {
|
|
||||||
currentBalance -= trans.Amount
|
|
||||||
} else {
|
|
||||||
currentBalance += trans.Amount
|
|
||||||
}
|
|
||||||
} else if trans.TransactionDate.Before(firstOfMonthAfterNext) && trans.TransactionDate.After(firstOfNextMonth) {
|
|
||||||
if trans.TransactionType.Type == "expense" {
|
|
||||||
nextMonth -= trans.Amount
|
|
||||||
} else {
|
|
||||||
nextMonth += trans.Amount
|
|
||||||
}
|
|
||||||
} else if trans.TransactionDate.Before(firstOfMonth) {
|
|
||||||
if trans.TransactionType.Type == "expense" {
|
|
||||||
lastMonthBalance -= trans.Amount
|
|
||||||
currentBalance -= trans.Amount
|
|
||||||
} else {
|
|
||||||
lastMonthBalance += trans.Amount
|
|
||||||
currentBalance += trans.Amount
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wm.CurrentBalance = currentBalance
|
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" {
|
||||||
|
currentBalance -= trans.Amount
|
||||||
|
} else {
|
||||||
|
currentBalance += trans.Amount
|
||||||
|
}
|
||||||
|
} else if trans.TransactionDate.Before(firstOfMonthAfterNext) && trans.TransactionDate.After(firstOfNextMonth) {
|
||||||
|
if trans.TransactionType.Type == "expense" {
|
||||||
|
nextMonth -= trans.Amount
|
||||||
|
} else {
|
||||||
|
nextMonth += trans.Amount
|
||||||
|
}
|
||||||
|
} else if trans.TransactionDate.Before(firstOfMonth) {
|
||||||
|
if trans.TransactionType.Type == "expense" {
|
||||||
|
lastMonthBalance -= trans.Amount
|
||||||
|
} else {
|
||||||
|
lastMonthBalance += trans.Amount
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user