wow taht passes

main
bel 2023-10-26 22:10:27 -06:00
parent c1237b4f5e
commit c044a48455
2 changed files with 8 additions and 7 deletions

View File

@ -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 {

View File

@ -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)
}