Skip to content

Commit b2cc65b

Browse files
committed
view balances of "quickview" configured accounts on main page
1 parent 981281b commit b2cc65b

3 files changed

Lines changed: 81 additions & 3 deletions

File tree

cmd/lweb/handler_accounts.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,76 @@ import (
77

88
"github.com/howeyc/ledger"
99

10+
"github.com/BurntSushi/toml"
1011
"github.com/go-martini/martini"
1112
)
1213

14+
type quickviewAccountConfig struct {
15+
Name string
16+
ShortName string `toml:"short_name"`
17+
}
18+
19+
type quickviewConfigStruct struct {
20+
Accounts []quickviewAccountConfig `toml:"account"`
21+
}
22+
23+
func quickviewHandler(w http.ResponseWriter, r *http.Request) {
24+
if len(quickviewConfigFileName) == 0 {
25+
http.Redirect(w, r, "/accounts", http.StatusFound)
26+
}
27+
var quickviewConfigData quickviewConfigStruct
28+
if _, lerr := toml.DecodeFile(quickviewConfigFileName, &quickviewConfigData); lerr != nil || len(quickviewConfigData.Accounts) < 1 {
29+
http.Redirect(w, r, "/accounts", http.StatusFound)
30+
}
31+
32+
shorten := func(accname string) string {
33+
for _, qvc := range quickviewConfigData.Accounts {
34+
if qvc.Name == accname {
35+
return qvc.ShortName
36+
}
37+
}
38+
return abbrev(accname)
39+
}
40+
41+
funcMap := template.FuncMap{
42+
"abbrev": shorten,
43+
}
44+
45+
t, err := parseAssetsWithFunc(funcMap, "templates/template.accounts.html", "templates/template.nav.html")
46+
if err != nil {
47+
http.Error(w, err.Error(), 500)
48+
return
49+
}
50+
51+
trans, terr := getTransactions()
52+
if terr != nil {
53+
http.Error(w, terr.Error(), 500)
54+
return
55+
}
56+
57+
var pData pageData
58+
pData.Reports = reportConfigData.Reports
59+
pData.Portfolios = portfolioConfigData.Portfolios
60+
pData.Transactions = trans
61+
62+
includeNames := make(map[string]bool)
63+
for _, qvc := range quickviewConfigData.Accounts {
64+
includeNames[qvc.Name] = true
65+
}
66+
67+
balances := ledger.GetBalances(trans, []string{})
68+
for _, bal := range balances {
69+
if includeNames[bal.Name] {
70+
pData.Accounts = append(pData.Accounts, bal)
71+
}
72+
}
73+
74+
err = t.Execute(w, pData)
75+
if err != nil {
76+
http.Error(w, err.Error(), 500)
77+
}
78+
}
79+
1380
func accountsHandler(w http.ResponseWriter, r *http.Request) {
1481
funcMap := template.FuncMap{
1582
"abbrev": abbrev,

cmd/lweb/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
var ledgerFileName string
2323
var reportConfigFileName string
2424
var stockConfigFileName string
25+
var quickviewConfigFileName string
2526
var ledgerLock sync.Mutex
2627
var currentSum []byte
2728
var currentTrans []*ledger.Transaction
@@ -139,6 +140,7 @@ func main() {
139140
flag.StringVar(&ledgerFileName, "f", "", "Ledger file name (*Required).")
140141
flag.StringVar(&reportConfigFileName, "r", "", "Report config file name (*Required).")
141142
flag.StringVar(&stockConfigFileName, "s", "", "Stock config file name (*Optional).")
143+
flag.StringVar(&quickviewConfigFileName, "q", "", "Quickview config file name (*Optional).")
142144
flag.IntVar(&serverPort, "port", 8056, "Port to listen on.")
143145
flag.BoolVar(&localhost, "localhost", false, "Listen on localhost only.")
144146

@@ -181,9 +183,7 @@ func main() {
181183
m.Get("/portfolio/:portfolioName", portfolioHandler)
182184
m.Get("/account/:accountName", accountHandler)
183185
m.Get("/report/:reportName", reportHandler)
184-
m.Get("/", func(w http.ResponseWriter, r *http.Request) {
185-
http.Redirect(w, r, "/accounts", http.StatusFound)
186-
})
186+
m.Get("/", quickviewHandler)
187187

188188
fmt.Println("Listening on port", serverPort)
189189
var listenAddress string

cmd/lweb/quickview-sample.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[[account]]
2+
name = "Assets:Current Assets:TD Bank:Checking"
3+
short_name = "TD Checking"
4+
5+
[[account]]
6+
name = "Assets:Current Assets:Wallet"
7+
short_name = "Wallet"
8+
9+
[[account]]
10+
name = "Liabilities:Credit Card:TD Visa"
11+
short_name = "TD Visa"

0 commit comments

Comments
 (0)