impl normalize in HTTP
All checks were successful
cicd / ci (push) Successful in 2m18s

This commit is contained in:
Bel LaPointe
2025-02-21 15:31:09 -07:00
parent 5cc7b7ec55
commit 4d01a5e481
3 changed files with 87 additions and 22 deletions

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"io/fs"
"math"
"net/http"
"os"
"slices"
@@ -99,17 +100,59 @@ func (router Router) APITransactions(w http.ResponseWriter, r *http.Request) {
))
recent := time.Hour * 24 * 365 / 6
json.NewEncoder(w).Encode(map[string]any{
"deltas": houseRelatedDeltas.
Like(ledger.LikeAfter(time.Now().Add(-1 * recent).Format("2006-01"))),
"balances": houseRelatedDeltas.Balances().
normalizer := ana.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
{
deltas := houseRelatedDeltas.
Like(ledger.LikeAfter(time.Now().Add(-1 * recent).Format("2006-01")))
balances := houseRelatedDeltas.Balances().
Like(`^(Zach|Bel|House[^:]*:Debts:)`).
Group(`^[^:]*`).
WithBPIs(bpis),
"transactions": houseRelatedDeltas.
WithBPIs(bpis)
transactions := houseRelatedDeltas.
Like(ledger.LikeAfter(time.Now().Add(-1 * recent).Format("2006-01"))).
Transactions(),
})
Transactions()
normalized := normalizer.Normalize(houseRelatedDeltas).Balances().
Like(`^(Zach|Bel):`).
Group(`^[^:]*:`).
WithBPIs(bpis)
var biggest float64
for _, v := range normalized {
if v := math.Abs(v["$"]); v > biggest {
biggest = v
}
}
for k, v := range normalized {
if v := math.Abs(v["$"]); v < biggest {
normalizedDelta := biggest - v
normalizedFactor := normalizer.NormalizeFactor(ledger.Delta{Name: k, Date: time.Now().Format("2006-01-02")})
normalized[fmt.Sprintf(`(%s trailing $)`, k)] = ledger.Balance{"$": normalizedDelta * normalizedFactor}
}
}
json.NewEncoder(w).Encode(map[string]any{
"deltas": deltas,
"balances": balances,
"normalized": normalized,
"transactions": transactions,
})
}
}
func (router Router) APITrends(w http.ResponseWriter, r *http.Request) {