tests a go
This commit is contained in:
@@ -38,18 +38,33 @@ func NewInterestPredictor(namePattern string, currencyPattern string, apy float6
|
||||
}
|
||||
|
||||
func NewContributionPredictor(reg ledger.Register) Predictor {
|
||||
monthlyRate := getMonthlyContributionRate(reg)
|
||||
monthlyRate := getMonthlyContributionRates(reg)
|
||||
_ = monthlyRate
|
||||
return func(given ledger.Balances, delta time.Duration) ledger.Balances {
|
||||
panic(nil)
|
||||
}
|
||||
}
|
||||
|
||||
func getMonthlyContributionRate(reg ledger.Register) map[string]ledger.Balance {
|
||||
func getMonthlyContributionRates(reg ledger.Register) map[string]ledger.Balance {
|
||||
contributions := getRecentContributions(reg)
|
||||
result := make(map[string]ledger.Balance)
|
||||
for name := range contributions {
|
||||
panic(nil)
|
||||
result[name] = getMonthlyContributionRate(contributions[name])
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// TODO better than average
|
||||
func getMonthlyContributionRate(contributions []ledger.Balance) ledger.Balance {
|
||||
sumPerCurrency := map[ledger.Currency]float64{}
|
||||
for _, balance := range contributions {
|
||||
for currency, v := range balance {
|
||||
sumPerCurrency[currency] += v
|
||||
}
|
||||
}
|
||||
result := make(ledger.Balance)
|
||||
for currency, summed := range sumPerCurrency {
|
||||
result[currency] = summed / float64(len(contributions))
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -62,10 +77,20 @@ func getContributions(reg ledger.Register) map[string][]ledger.Balance {
|
||||
contributions := make(map[string][]ledger.Balance)
|
||||
for _, date := range reg.Dates() {
|
||||
for name := range reg[date] {
|
||||
contributions[name] = append(contributions[name], maps.Clone(reg[date][name]))
|
||||
if a := contributions[name]; len(a) > 1 {
|
||||
for k := range a[len(a)-2] {
|
||||
a[len(a)-1][k] -= a[len(a)-2][k]
|
||||
contributions[name] = append(contributions[name], make(ledger.Balance))
|
||||
if len(contributions[name]) > 1 {
|
||||
for k := range contributions[name][len(contributions[name])-2] {
|
||||
contributions[name][len(contributions[name])-1][k] = 0
|
||||
}
|
||||
}
|
||||
balance := contributions[name][len(contributions[name])-1]
|
||||
for currency, value := range reg[date][name] {
|
||||
balance[currency] = value
|
||||
}
|
||||
if forName := contributions[name]; len(forName) > 1 {
|
||||
lastBalance := forName[len(forName)-2]
|
||||
for currency := range lastBalance {
|
||||
balance[currency] -= lastBalance[currency]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user