fixed get contributions
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package ana
|
||||
|
||||
import (
|
||||
"log"
|
||||
"maps"
|
||||
"math"
|
||||
"regexp"
|
||||
@@ -44,11 +43,6 @@ func NewContributionPredictor(reg ledger.Register) Predictor {
|
||||
}
|
||||
|
||||
func newContributionPredictor(monthlyRate map[string]ledger.Balance) Predictor {
|
||||
for name, balance := range monthlyRate {
|
||||
for curr, v := range balance {
|
||||
log.Printf("%s/%s gains %.2f per month", name, curr, v)
|
||||
}
|
||||
}
|
||||
return func(given ledger.Balances, delta time.Duration) ledger.Balances {
|
||||
month := time.Hour * 24 * 365 / 12
|
||||
months := float64(delta) / float64(month)
|
||||
@@ -73,13 +67,13 @@ 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)
|
||||
result[name] = getMonthlyContributionRate(contributions[name], window, name)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// TODO better than average
|
||||
func getMonthlyContributionRate(contributions []ledger.Balance, window time.Duration) ledger.Balance {
|
||||
func getMonthlyContributionRate(contributions []ledger.Balance, window time.Duration, name string) ledger.Balance {
|
||||
sumPerCurrency := map[ledger.Currency]float64{}
|
||||
for _, balance := range contributions {
|
||||
for currency, v := range balance {
|
||||
@@ -99,25 +93,36 @@ func getRecentContributions(reg ledger.Register, window time.Duration) map[strin
|
||||
|
||||
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], 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]
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, name := range reg.Names() {
|
||||
contributions[name] = getContributionsFor(reg, name)
|
||||
}
|
||||
return contributions
|
||||
}
|
||||
|
||||
func getContributionsFor(reg ledger.Register, name string) []ledger.Balance {
|
||||
result := make([]ledger.Balance, 0)
|
||||
var last ledger.Balance
|
||||
for _, date := range reg.Dates() {
|
||||
if _, ok := reg[date][name]; !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if last == nil {
|
||||
last = reg[date][name]
|
||||
continue
|
||||
}
|
||||
|
||||
result = append(result, make(ledger.Balance))
|
||||
this := result[len(result)-1]
|
||||
|
||||
for k, v := range last {
|
||||
this[k] = -1 * v
|
||||
}
|
||||
for k, v := range reg[date][name] {
|
||||
this[k] += v
|
||||
}
|
||||
|
||||
last = reg[date][name]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user