diff --git a/ana/predictor.go b/ana/predictor.go index 5e6efb1..f2d4812 100644 --- a/ana/predictor.go +++ b/ana/predictor.go @@ -39,10 +39,31 @@ func NewInterestPredictor(namePattern string, currencyPattern string, apy float6 func NewContributionPredictor(reg ledger.Register) Predictor { monthlyRate := getMonthlyContributionRate(reg) + _ = monthlyRate return func(given ledger.Balances, delta time.Duration) ledger.Balances { panic(nil) } } func getMonthlyContributionRate(reg ledger.Register) map[string]ledger.Balance { + panic(nil) +} + +func getRecentContributions(reg ledger.Register) map[string][]ledger.Balance { + return getContributions(reg.Between(time.Now().Add(-1*time.Hour*365/2), time.Now())) +} + +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] + } + } + } + } + return contributions } diff --git a/ana/predictor_test.go b/ana/predictor_test.go index 37d51a0..bf8d486 100644 --- a/ana/predictor_test.go +++ b/ana/predictor_test.go @@ -41,3 +41,48 @@ func TestNewInterestPredictor(t *testing.T) { }) } } + +func TestGetContributions(t *testing.T) { + input := ledger.Register{ + "2001-01": ledger.Balances{ + "a": ledger.Balance{"x": 1}, + "b": ledger.Balance{"z": 1}, + }, + "2001-02": ledger.Balances{ + "a": ledger.Balance{"y": 1}, + }, + "2001-03": ledger.Balances{ + "b": ledger.Balance{"z": 2}, + }, + } + + got := getContributions(input) + if len(got) != 2 { + t.Error(len(got)) + } + if len(got["a"]) != 2 { + t.Error(len(got["a"])) + } else if len(got["a"][0]) != 1 { + t.Error(got["a"]) + } else if got["a"][0]["x"] != 1 { + t.Error(got["a"][0]) + } else if len(got["a"][1]) != 2 { + t.Error(got["a"][1]) + } else if got["a"][1]["x"] != -1 { + t.Error(got["a"][1]["x"]) + } else if got["a"][1]["y"] != 1 { + t.Error(got["a"][1]["y"]) + } + + if len(got["b"]) != 2 { + t.Error(len(got["b"])) + } else if len(got["b"][0]) != 1 { + t.Error(got["b"]) + } else if got["b"][0]["z"] != 1 { + t.Error(got["b"][0]) + } else if len(got["b"][1]) != 1 { + t.Error(got["b"][1]) + } else if got["b"][1]["z"] != 1 { + t.Error(got["b"][1]["z"]) + } +}