removed some critical bugs
This commit is contained in:
+26
-81
@@ -2,7 +2,6 @@ package pages
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
exp "github.com/ESilva15/expenses/expenses"
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
@@ -18,60 +17,6 @@ type MonthlyTotal struct {
|
||||
Total dec.Decimal `json:"total"`
|
||||
}
|
||||
|
||||
func getMonthlyBalance(expenses mod.Expenses, uID int32) ([]MonthlyTotal, error) {
|
||||
monthlyBalance := make([]MonthlyTotal, val.MonthsPerYear)
|
||||
for k := range val.MonthsPerYear {
|
||||
monthlyBalance[k].Month = k
|
||||
}
|
||||
|
||||
for _, e := range expenses {
|
||||
month := e.Date.Month() - 1
|
||||
t := e.Type.TypeName
|
||||
|
||||
// Get the current expenses value and make it negative if its an expense
|
||||
// It's not the value, its what we paid on it (our payments)
|
||||
// I don't like this like this - takes too long, we should load it from DB
|
||||
// at the start, and cache it too
|
||||
_ = exp.App.LoadExpensePayments(&e)
|
||||
value := exp.App.PaidByUser(e, uID)
|
||||
// value := e.Value
|
||||
if t == "Despesa" {
|
||||
value = value.Neg()
|
||||
}
|
||||
|
||||
// Store the data on the montly balance list
|
||||
monthlyBalance[month].Total = monthlyBalance[month].Total.Add(value)
|
||||
}
|
||||
|
||||
return monthlyBalance, nil
|
||||
}
|
||||
|
||||
func getMonthlyTotal(expenses mod.Expenses, uID int32) ([]MonthlyTotal, error) {
|
||||
mTotal := make([]MonthlyTotal, val.MonthsPerYear)
|
||||
for k := range mTotal {
|
||||
mTotal[k].Month = k
|
||||
}
|
||||
|
||||
// Step 1: net total for each month
|
||||
for _, e := range expenses {
|
||||
month := int(e.Date.Month()) - 1 // Jan = 0
|
||||
// Same as on the other one here
|
||||
_ = exp.App.LoadExpensePayments(&e)
|
||||
value := exp.App.PaidByUser(e, uID)
|
||||
if e.Type.TypeName == "Despesa" {
|
||||
value = value.Neg()
|
||||
}
|
||||
mTotal[month].Total = mTotal[month].Total.Add(value)
|
||||
}
|
||||
|
||||
// Step 2: cumulative balance
|
||||
for i := 1; i < len(mTotal); i++ {
|
||||
mTotal[i].Total = mTotal[i].Total.Add(mTotal[i-1].Total)
|
||||
}
|
||||
|
||||
return mTotal, nil
|
||||
}
|
||||
|
||||
func getAnalysisDataWG(c *gin.Context) (map[string]any, error) {
|
||||
ctx, err := getLoggedInUserCTX(c)
|
||||
if err != nil {
|
||||
@@ -89,30 +34,31 @@ func getAnalysisDataWG(c *gin.Context) (map[string]any, error) {
|
||||
return map[string]any{}, err
|
||||
}
|
||||
|
||||
var (
|
||||
mBalance []MonthlyTotal
|
||||
mTotal []MonthlyTotal
|
||||
bErr error
|
||||
tErr error
|
||||
wg sync.WaitGroup
|
||||
)
|
||||
|
||||
wg.Add(2)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
mBalance, bErr = getMonthlyBalance(queriedExpenses, user.UserID)
|
||||
}()
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
mTotal, tErr = getMonthlyTotal(queriedExpenses, user.UserID)
|
||||
}()
|
||||
wg.Wait()
|
||||
|
||||
if tErr != nil {
|
||||
return map[string]any{}, tErr
|
||||
for i := range queriedExpenses {
|
||||
_ = exp.App.LoadExpensePayments(&queriedExpenses[i])
|
||||
}
|
||||
if bErr != nil {
|
||||
return map[string]any{}, bErr
|
||||
|
||||
mBalance := make([]MonthlyTotal, val.MonthsPerYear)
|
||||
for k := range val.MonthsPerYear {
|
||||
mBalance[k].Month = k
|
||||
}
|
||||
|
||||
for _, e := range queriedExpenses {
|
||||
month := int(e.Date.Month()) - 1 // Jan = 0
|
||||
t := e.Type.TypeName
|
||||
|
||||
value := exp.App.PaidByUser(e, user.UserID)
|
||||
if t == "Despesa" {
|
||||
value = value.Neg()
|
||||
}
|
||||
|
||||
mBalance[month].Total = mBalance[month].Total.Add(value)
|
||||
}
|
||||
|
||||
mTotal := make([]MonthlyTotal, len(mBalance))
|
||||
copy(mTotal, mBalance)
|
||||
for i := 1; i < len(mTotal); i++ {
|
||||
mTotal[i].Total = mTotal[i].Total.Add(mTotal[i-1].Total)
|
||||
}
|
||||
|
||||
plotData := map[string]any{
|
||||
@@ -124,10 +70,9 @@ func getAnalysisDataWG(c *gin.Context) (map[string]any, error) {
|
||||
}
|
||||
|
||||
func analysisPartialPage(c *gin.Context) {
|
||||
// TODO - this is repeated code with the analysisPage one, figure something
|
||||
data, err := getAnalysisDataWG(c)
|
||||
if err != nil {
|
||||
ServerErrorView(c, "failed to compiled analysis data")
|
||||
ServerErrorView(c, "failed to compile analysis data")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -139,7 +84,7 @@ func analysisPartialPage(c *gin.Context) {
|
||||
func analysisPage(c *gin.Context) {
|
||||
data, err := getAnalysisDataWG(c)
|
||||
if err != nil {
|
||||
ServerErrorView(c, "failed to compiled analysis data")
|
||||
ServerErrorView(c, "failed to compile analysis data")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user