main
Bel LaPointe 2023-10-27 17:33:05 -06:00
parent 13ff558de8
commit 859e47f9e1
2 changed files with 20 additions and 12 deletions

View File

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

View File

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