From dafac4912646062d3102d97fbe2184269617eb77 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Fri, 27 Oct 2023 21:28:31 -0600 Subject: [PATCH] halfway to moolah2 --- cmd/clitest/main.go | 19 +++++++++++ cmd/clitest/moolah.dat | 1 + cmd/clitest/public/transactions.html | 49 ++++++++++++++++++++++++++++ ledger/balances.go | 13 ++++++++ 4 files changed, 82 insertions(+) create mode 120000 cmd/clitest/moolah.dat create mode 100644 cmd/clitest/public/transactions.html diff --git a/cmd/clitest/main.go b/cmd/clitest/main.go index 545e717..ed5175a 100644 --- a/cmd/clitest/main.go +++ b/cmd/clitest/main.go @@ -54,6 +54,25 @@ func main() { if *httpOutput != "" { foo := func(w http.ResponseWriter, r *http.Request) { if strings.HasPrefix(r.URL.Path, "/api") { + switch r.URL.Path { + case "/api/transactions": + reqF := f + if queryF := r.URL.Query().Get("f"); queryF != "" { + reqF, err = ledger.NewFiles(queryF) + if err != nil { + panic(err) + } + } + deltas, err := reqF.Deltas() + if err != nil { + panic(err) + } + json.NewEncoder(w).Encode(map[string]any{ + "deltas": deltas.Like(ledger.LikeAfter(time.Now().Add(-1 * time.Hour * 24 * 365 / 2).Format("2006-01"))), + "balances": deltas.Balances().Like("^AssetAccount:").WithBPIs(bpis), + }) + return + } } else if strings.HasPrefix(r.URL.Path, "/transactions") { f, err := os.Open("./public/transactions.html") if err != nil { diff --git a/cmd/clitest/moolah.dat b/cmd/clitest/moolah.dat new file mode 120000 index 0000000..f1b571b --- /dev/null +++ b/cmd/clitest/moolah.dat @@ -0,0 +1 @@ +../../../../../../Sync/Core/tmp/moolah.dat \ No newline at end of file diff --git a/cmd/clitest/public/transactions.html b/cmd/clitest/public/transactions.html new file mode 100644 index 0000000..e715e50 --- /dev/null +++ b/cmd/clitest/public/transactions.html @@ -0,0 +1,49 @@ + +
+ + +
+ +

Moolah2

+
+ Balance +
+
+
+
+
+
+
+ + + diff --git a/ledger/balances.go b/ledger/balances.go index 06670dc..94c6233 100644 --- a/ledger/balances.go +++ b/ledger/balances.go @@ -2,6 +2,8 @@ package ledger import ( "fmt" + "maps" + "regexp" "strings" ) @@ -9,6 +11,17 @@ type Balances map[string]Balance type Balance map[Currency]float64 +func (balances Balances) Like(pattern string) Balances { + result := make(Balances) + p := regexp.MustCompile(pattern) + for k, v := range balances { + if p.MatchString(k) { + result[k] = maps.Clone(v) + } + } + return result +} + func (balances Balances) WithBPIs(bpis BPIs) Balances { return balances.WithBPIsAt(bpis, "9") }