refactor
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user