halfway to moolah2

main
Bel LaPointe 2023-10-27 21:28:31 -06:00
parent e36f08deba
commit dafac49126
4 changed files with 82 additions and 0 deletions

View File

@ -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 {

1
cmd/clitest/moolah.dat Symbolic link
View File

@ -0,0 +1 @@
../../../../../../Sync/Core/tmp/moolah.dat

View File

@ -0,0 +1,49 @@
<html>
<header>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/dark.css">
<script>
function http(method, remote, callback, body) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
callback(xmlhttp.responseText, xmlhttp.status)
}
};
xmlhttp.open(method, remote, true);
if (typeof body == "undefined") {
body = null
}
xmlhttp.send(body);
}
function callback(responseBody, responseStatus) {
}
var f = String(window.location).split("/transactions")[1]
if (!f) {
f = "./moolah.dat"
}
function init() {
load(f)
}
function load(f) {
http("GET", "/api/transactions?f="+f, (body, status) => {
var d = JSON.parse(body)
console.log(status, d)
})
}
</script>
</header>
<body onload="init();">
<h2>Moolah2</h2>
<details>
<summary>Balance</summary>
<div id="bal">
</div>
</details>
<details open>
<div id="reg">
</div>
</details>
</body>
<footer>
</footer>
</html>

View File

@ -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")
}