diff --git a/ana/predictor.go b/ana/predictor.go index 5bb4aa0..bec9e5c 100644 --- a/ana/predictor.go +++ b/ana/predictor.go @@ -39,9 +39,26 @@ func NewInterestPredictor(namePattern string, currencyPattern string, apy float6 func NewContributionPredictor(reg ledger.Register) Predictor { monthlyRate := getMonthlyContributionRates(reg) - _ = monthlyRate + return newContributionPredictor(monthlyRate) +} + +func newContributionPredictor(monthlyRate map[string]ledger.Balance) Predictor { return func(given ledger.Balances, delta time.Duration) ledger.Balances { - panic(nil) + month := time.Hour * 24 * 365 / 12 + months := float64(delta) / float64(month) + result := make(ledger.Balances) + for k, v := range given { + result[k] = maps.Clone(v) + } + for name := range monthlyRate { + if _, ok := result[name]; !ok { + result[name] = make(ledger.Balance) + } + for currency := range monthlyRate[name] { + result[name][currency] += monthlyRate[name][currency] * months + } + } + return result } } diff --git a/ana/predictor_test.go b/ana/predictor_test.go index 4bf4dc2..bc5d66a 100644 --- a/ana/predictor_test.go +++ b/ana/predictor_test.go @@ -105,3 +105,33 @@ func TestGetMonthlyContributionRate(t *testing.T) { t.Error(got["y"]) } } + +func TestNewContributionPredictor(t *testing.T) { + name := "x" + currency := ledger.USD + predictor := newContributionPredictor(map[string]ledger.Balance{ + name: {currency: 10}, + "y": {"XYZ": 3}, + }) + month := time.Hour * 24 * 365 / 12 + + if got := predictor(ledger.Balances{}, 2*month); got[name][currency] != 20 { + t.Error(got[name]) + } else if got["y"]["XYZ"] != 6 { + t.Error(got["y"]) + } + + if got := predictor(ledger.Balances{name: {currency: 30}}, 2*month); got[name][currency] != 30+20 { + t.Error(got) + } else if got["y"]["XYZ"] != 6 { + t.Error(got["y"]) + } + + if got := predictor(ledger.Balances{"z": {"ABC": 100}}, 2*month); got[name][currency] != 20 { + t.Error(got) + } else if got["y"]["XYZ"] != 6 { + t.Error(got["y"]) + } else if got["z"]["ABC"] != 100 { + t.Error(got["z"]) + } +}