This commit is contained in:
Bel LaPointe
2023-10-25 09:38:29 -06:00
parent 76f6cf1016
commit 49a77c5424
4 changed files with 36 additions and 43 deletions

View File

@@ -7,11 +7,12 @@ import (
"log"
"net/http"
"os"
"slices"
"sort"
"strings"
"github.com/go-echarts/go-echarts/charts"
"gogs.inhome.blapointe.com/ana-ledger/ledger"
"gogs.inhome.blapointe.com/local/gziphttp"
)
func main() {
@@ -53,13 +54,15 @@ func main() {
}
if *httpOutput != "" {
deltas = deltas.Like(like...)
switch *foo {
case "reg":
foo := func(w http.ResponseWriter, r *http.Request) {
register := deltas.Like(like...).Register()
nameCurrencyDateValue := map[string]map[ledger.Currency]map[string]float64{}
dates := []string{}
for date, balances := range register {
dates = append(dates, date)
for name, balance := range balances {
for currency, value := range balance {
if _, ok := nameCurrencyDateValue[name]; !ok {
@@ -72,10 +75,23 @@ func main() {
}
}
}
if gziphttp.Can(r) {
w = gziphttp.New(w)
slices.Sort(dates)
line := charts.NewLine()
line.AddXAxis(dates)
for name, currencyDateValue := range nameCurrencyDateValue {
for currency, dateValue := range currencyDateValue {
series := make([]int, len(dates))
for i, date := range dates {
series[i] = int(dateValue[date])
}
key := fmt.Sprintf("%s (%s)", name, currency)
line.AddYAxis(key, series)
}
}
if err := line.Render(w); err != nil {
panic(err)
}
json.NewEncoder(w).Encode(nameCurrencyDateValue)
}
log.Println("listening on", *httpOutput)
if err := http.ListenAndServe(*httpOutput, http.HandlerFunc(foo)); err != nil {