From 859e47f9e101169dc9a7aa63637cf3b373bc3503 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Fri, 27 Oct 2023 17:33:05 -0600 Subject: [PATCH] refactor --- ana/predictor.go | 30 +++++++++++++++++++----------- ana/predictor_test.go | 2 +- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/ana/predictor.go b/ana/predictor.go index e744222..7605845 100644 --- a/ana/predictor.go +++ b/ana/predictor.go @@ -67,24 +67,32 @@ func getMonthlyContributionRates(reg ledger.Register) map[string]ledger.Balance contributions := getRecentContributions(reg, window) result := make(map[string]ledger.Balance) for name := range contributions { - result[name] = getMonthlyContributionRate(contributions[name], window, name) + result[name] = getMonthlyContributionRate(contributions[name], window) + } + return result +} + +func getMonthlyContributionRate(contributions []ledger.Balance, window time.Duration) ledger.Balance { + currencies := map[ledger.Currency]int{} + for _, balance := range contributions { + for currency := range balance { + currencies[currency] = 1 + } + } + result := make(ledger.Balance) + for currency := range currencies { + result[currency] = getMonthlyContributionRateForCurrency(contributions, window, currency) } return result } // TODO better than average -func getMonthlyContributionRate(contributions []ledger.Balance, window time.Duration, name string) ledger.Balance { - sumPerCurrency := map[ledger.Currency]float64{} +func getMonthlyContributionRateForCurrency(contributions []ledger.Balance, window time.Duration, currency ledger.Currency) float64 { + sum := 0.0 for _, balance := range contributions { - for currency, v := range balance { - sumPerCurrency[currency] += v - } + sum += balance[currency] } - result := make(ledger.Balance) - for currency, summed := range sumPerCurrency { - result[currency] = summed / (float64(window) / float64(time.Hour*24.0*365.0/12.0)) - } - return result + return sum / (float64(window) / float64(time.Hour*24.0*365.0/12.0)) } func getRecentContributions(reg ledger.Register, window time.Duration) map[string][]ledger.Balance { diff --git a/ana/predictor_test.go b/ana/predictor_test.go index edd14a9..c1559dd 100644 --- a/ana/predictor_test.go +++ b/ana/predictor_test.go @@ -86,7 +86,7 @@ func TestGetMonthlyContributionRate(t *testing.T) { ledger.Balance{"x": 4}, ledger.Balance{"y": 3}, } - got := getMonthlyContributionRate(input, time.Hour*24*365/4, "") + got := getMonthlyContributionRate(input, time.Hour*24*365/4) if len(got) != 2 { t.Error(got) }