/api/trends has PIES
cicd / ci (push) Successful in 1m36s
Details
cicd / ci (push) Successful in 1m36s
Details
parent
6be00d6423
commit
6df7171057
|
|
@ -124,50 +124,57 @@ func (router Router) APITrends(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
recent := time.Hour * 24 * 365 / 2
|
recent := time.Hour * 24 * 365 / 2
|
||||||
recentHouseRelatedDeltas := deltas.
|
|
||||||
Like(ledger.LikeTransactions(
|
|
||||||
deltas.Like(ledger.LikeName(`^House`))...,
|
|
||||||
)).
|
|
||||||
Like(ledger.LikeAfter(time.Now().Add(-1 * recent).Format("2006-01"))).
|
|
||||||
Group(ledger.GroupName(`Withdrawal:[0-9]*:[^:]*`)).
|
|
||||||
Group(ledger.GroupDate(`^[0-9]*-[0-9]*`)).
|
|
||||||
Like(ledger.LikeNotName(`^$`))
|
|
||||||
|
|
||||||
monthsToDeltas := map[string]ledger.Deltas{}
|
pie := func(title, groupName string, min int) {
|
||||||
for _, delta := range recentHouseRelatedDeltas {
|
recentHouseRelatedDeltas := deltas.
|
||||||
monthsToDeltas[delta.Date] = append(monthsToDeltas[delta.Date], delta)
|
Like(ledger.LikeTransactions(
|
||||||
}
|
deltas.Like(ledger.LikeName(`^House`))...,
|
||||||
months := []string{}
|
)).
|
||||||
for k := range monthsToDeltas {
|
Like(ledger.LikeAfter(time.Now().Add(-1 * recent).Format("2006-01"))).
|
||||||
months = append(months, k)
|
Group(ledger.GroupName(groupName)).
|
||||||
}
|
Group(ledger.GroupDate(`^[0-9]*-[0-9]*`)).
|
||||||
slices.Sort(months)
|
Like(ledger.LikeNotName(`^$`))
|
||||||
|
|
||||||
catToMonth := map[string][]int{}
|
monthsToDeltas := map[string]ledger.Deltas{}
|
||||||
for _, month := range months {
|
for _, delta := range recentHouseRelatedDeltas {
|
||||||
balances := monthsToDeltas[month].Balances().WithBPIs(bpis)
|
monthsToDeltas[delta.Date] = append(monthsToDeltas[delta.Date], delta)
|
||||||
for category, balance := range balances {
|
}
|
||||||
catToMonth[category] = append(catToMonth[category], int(balance[ledger.USD]))
|
months := []string{}
|
||||||
|
for k := range monthsToDeltas {
|
||||||
|
months = append(months, k)
|
||||||
|
}
|
||||||
|
slices.Sort(months)
|
||||||
|
|
||||||
|
catToMonth := map[string][]int{}
|
||||||
|
for _, month := range months {
|
||||||
|
balances := monthsToDeltas[month].Balances().WithBPIs(bpis)
|
||||||
|
for category, balance := range balances {
|
||||||
|
catToMonth[category] = append(catToMonth[category], int(balance[ledger.USD]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
chart := view.NewChart("pie")
|
||||||
|
ttl := 0
|
||||||
|
for cat, month := range catToMonth {
|
||||||
|
for i := 0; i < len(months)-len(month); i++ {
|
||||||
|
month = append(month, 0)
|
||||||
|
}
|
||||||
|
slices.Sort(month)
|
||||||
|
median := month[len(month)/2]
|
||||||
|
ttl += median
|
||||||
|
if median > min {
|
||||||
|
chart.AddY(cat, []int{median})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Fprintln(w, "<!DOCTYPE html><h2>", title, "($", ttl, ")</h2>")
|
||||||
|
if err := chart.Render(w); err != nil {
|
||||||
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
chart := view.NewChart("pie")
|
pie("Median Spending", `Withdrawal:[0-9]*`, 50)
|
||||||
ttl := 0
|
pie("Median Spending (detailed)", `Withdrawal:[0-9]*:[^:]*`, 25)
|
||||||
for cat, month := range catToMonth {
|
pie("Median Spending (MORE detailed)", `Withdrawal:[0-9]*:[^:]*:[^:]*`, 10)
|
||||||
for i := 0; i < len(months)-len(month); i++ {
|
|
||||||
month = append(month, 0)
|
|
||||||
}
|
|
||||||
slices.Sort(month)
|
|
||||||
median := month[len(month)/2]
|
|
||||||
ttl += median
|
|
||||||
if median > 50 {
|
|
||||||
chart.AddY(cat, []int{median})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fmt.Fprintln(w, "<!DOCTYPE html><h2>Median Spending ($", ttl, ")</h2>")
|
|
||||||
if err := chart.Render(w); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (router Router) APIAmend(w http.ResponseWriter, r *http.Request) {
|
func (router Router) APIAmend(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue