diff --git a/cmd/http/router.go b/cmd/http/router.go index cfbd1d6..1458760 100644 --- a/cmd/http/router.go +++ b/cmd/http/router.go @@ -124,50 +124,57 @@ func (router Router) APITrends(w http.ResponseWriter, r *http.Request) { } 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{} - for _, delta := range recentHouseRelatedDeltas { - monthsToDeltas[delta.Date] = append(monthsToDeltas[delta.Date], delta) - } - months := []string{} - for k := range monthsToDeltas { - months = append(months, k) - } - slices.Sort(months) + pie := func(title, groupName string, min int) { + recentHouseRelatedDeltas := deltas. + Like(ledger.LikeTransactions( + deltas.Like(ledger.LikeName(`^House`))..., + )). + Like(ledger.LikeAfter(time.Now().Add(-1 * recent).Format("2006-01"))). + Group(ledger.GroupName(groupName)). + Group(ledger.GroupDate(`^[0-9]*-[0-9]*`)). + Like(ledger.LikeNotName(`^$`)) - 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])) + monthsToDeltas := map[string]ledger.Deltas{} + for _, delta := range recentHouseRelatedDeltas { + monthsToDeltas[delta.Date] = append(monthsToDeltas[delta.Date], delta) + } + 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, "

", title, "($", ttl, ")

") + if err := chart.Render(w); err != nil { + panic(err) } } - 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 > 50 { - chart.AddY(cat, []int{median}) - } - } - fmt.Fprintln(w, "

Median Spending ($", ttl, ")

") - if err := chart.Render(w); err != nil { - panic(err) - } + pie("Median Spending", `Withdrawal:[0-9]*`, 50) + pie("Median Spending (detailed)", `Withdrawal:[0-9]*:[^:]*`, 25) + pie("Median Spending (MORE detailed)", `Withdrawal:[0-9]*:[^:]*:[^:]*`, 10) } func (router Router) APIAmend(w http.ResponseWriter, r *http.Request) {