From 5cc9b141b98dcbcdd839ae9da42cc84be9e77d31 Mon Sep 17 00:00:00 2001 From: bel Date: Thu, 3 Apr 2025 23:04:08 -0600 Subject: [PATCH] normalize --- cmd/cli/main.go | 19 +++++++++++++++---- src/ana/normalize.go | 26 +++++++++++++------------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 00afd6f..065243b 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -7,6 +7,7 @@ import ( "os" "slices" "strings" + "time" "gogs.inhome.blapointe.com/ana-ledger/src/ana" "gogs.inhome.blapointe.com/ana-ledger/src/ledger" @@ -98,7 +99,7 @@ func Main() { KindaLike(q). KindaGroup(group). Nonzero() - FPrintBalances(os.Stdout, "", balances, nil, config.Query.USDOnly) + FPrintBalances(os.Stdout, "", balances, nil, config.Query.USDOnly, config.Query.Normalize, time.Now().Format("2006-01-02")) case "reg": transactions := deltas.Transactions() @@ -116,7 +117,7 @@ func Main() { } if shouldPrint { fmt.Printf("%s\t%s\n", transaction[0].Date, transaction[0].Description) - FPrintBalances(os.Stdout, "\t\t", balances, transactions[:i+1].Deltas().Like(q).Group(group).Balances().WithBPIsAt(bpis, transaction[0].Date).Nonzero(), config.Query.USDOnly) + FPrintBalances(os.Stdout, "\t\t", balances, transactions[:i+1].Deltas().Like(q).Group(group).Balances().WithBPIsAt(bpis, transaction[0].Date).Nonzero(), config.Query.USDOnly, config.Query.Normalize, transaction[0].Date) } } default: @@ -124,7 +125,7 @@ func Main() { } } -func FPrintBalances(w io.Writer, linePrefix string, balances, cumulatives ledger.Balances, usdOnly bool) { +func FPrintBalances(w io.Writer, linePrefix string, balances, cumulatives ledger.Balances, usdOnly, normalized bool, date string) { keys := []string{} for k := range balances { keys = append(keys, k) @@ -138,7 +139,12 @@ func FPrintBalances(w io.Writer, linePrefix string, balances, cumulatives ledger } } + normalizer := ana.NewDefaultNormalizer() + format := fmt.Sprintf("%s%%-%ds\t%%s%%.2f (%%s%%.2f)\n", linePrefix, max) + if normalized { + format = fmt.Sprintf("%s%%-%ds\t%%s%%.2f (%%s%%.2f (%%.2f @%%.2f))\n", linePrefix, max) + } for _, key := range keys { currencies := []ledger.Currency{} for currency := range balances[key] { @@ -162,7 +168,12 @@ func FPrintBalances(w io.Writer, linePrefix string, balances, cumulatives ledger cumulative = value } - fmt.Fprintf(w, format, key, printableCurrency, balances[key][currency], printableCurrency, cumulative) + if !normalized { + fmt.Fprintf(w, format, key, printableCurrency, balances[key][currency], printableCurrency, cumulative) + } else { + factor := normalizer.NormalizeFactor(ledger.Delta{Name: key, Date: date}) + fmt.Fprintf(w, format, key, printableCurrency, balances[key][currency], printableCurrency, cumulative, cumulative*factor, factor) + } } } } diff --git a/src/ana/normalize.go b/src/ana/normalize.go index 3982561..ebbe6c8 100644 --- a/src/ana/normalize.go +++ b/src/ana/normalize.go @@ -19,19 +19,19 @@ type normalize struct { func NewDefaultNormalizer() Normalizer { return NewNormalizer(). - With("^Zach:", "2023-10-05", 139). // to turtle - With("^Zach:", "2021-12-30", 135). // at pluralsight - With("^Zach:", "2020-07-30", 120). // to pluralsight - With("^Zach:", "2019-07-16", 77). // at fedex - With("^Zach:", "2017-02-16", 49). // to fedex - With("^Bel:", "2023-12-05", 190). // to render - With("^Bel:", "2022-12-31", 154). // at q - With("^Bel:", "2022-06-30", 148). // at q - With("^Bel:", "2021-12-31", 122). // at q - With("^Bel:", "2020-12-31", 118). // at q - With("^Bel:", "2019-12-31", 111). // at q - With("^Bel:", "2018-12-31", 92). // at q - With("^Bel:", "2018-02-16", 86) // to q + With("^Zach", "2023-10-05", 139). // to turtle + With("^Zach", "2021-12-30", 135). // at pluralsight + With("^Zach", "2020-07-30", 120). // to pluralsight + With("^Zach", "2019-07-16", 77). // at fedex + With("^Zach", "2017-02-16", 49). // to fedex + With("^Bel", "2023-12-05", 190). // to render + With("^Bel", "2022-12-31", 154). // at q + With("^Bel", "2022-06-30", 148). // at q + With("^Bel", "2021-12-31", 122). // at q + With("^Bel", "2020-12-31", 118). // at q + With("^Bel", "2019-12-31", 111). // at q + With("^Bel", "2018-12-31", 92). // at q + With("^Bel", "2018-02-16", 86) // to q } func NewNormalizer() Normalizer {