diff --git a/ana/predictor.go b/ana/predictor.go index 7f2aed7..e5939de 100644 --- a/ana/predictor.go +++ b/ana/predictor.go @@ -46,16 +46,17 @@ func NewContributionPredictor(reg ledger.Register) Predictor { } func getMonthlyContributionRates(reg ledger.Register) map[string]ledger.Balance { - contributions := getRecentContributions(reg) + window := time.Hour * 24 * 365 / 2 + contributions := getRecentContributions(reg, window) result := make(map[string]ledger.Balance) for name := range contributions { - result[name] = getMonthlyContributionRate(contributions[name]) + result[name] = getMonthlyContributionRate(contributions[name], window) } return result } // TODO better than average -func getMonthlyContributionRate(contributions []ledger.Balance) ledger.Balance { +func getMonthlyContributionRate(contributions []ledger.Balance, window time.Duration) ledger.Balance { sumPerCurrency := map[ledger.Currency]float64{} for _, balance := range contributions { for currency, v := range balance { @@ -64,13 +65,13 @@ func getMonthlyContributionRate(contributions []ledger.Balance) ledger.Balance { } result := make(ledger.Balance) for currency, summed := range sumPerCurrency { - result[currency] = summed / float64(len(contributions)) + result[currency] = summed / (float64(window) / float64(time.Hour*24*365/12)) } return result } -func getRecentContributions(reg ledger.Register) map[string][]ledger.Balance { - return getContributions(reg.Between(time.Now().Add(-1*time.Hour*365/2), time.Now())) +func getRecentContributions(reg ledger.Register, window time.Duration) map[string][]ledger.Balance { + return getContributions(reg.Between(time.Now().Add(-1*window), time.Now())) } func getContributions(reg ledger.Register) map[string][]ledger.Balance { diff --git a/ana/predictor_test.go b/ana/predictor_test.go index 77e7429..4bf4dc2 100644 --- a/ana/predictor_test.go +++ b/ana/predictor_test.go @@ -94,7 +94,7 @@ func TestGetMonthlyContributionRate(t *testing.T) { ledger.Balance{"x": 4}, ledger.Balance{"y": 3}, } - got := getMonthlyContributionRate(input) + got := getMonthlyContributionRate(input, time.Hour*24*365/4) if len(got) != 2 { t.Error(got) }