2023-11-12 19:04:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
2023-11-14 01:05:20 +00:00
|
|
|
"sync"
|
|
|
|
"text/template"
|
2023-11-12 19:04:22 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"deadbeef.codes/steven/ynab-portfolio-monitor/ynab"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-11-14 01:05:20 +00:00
|
|
|
configuredProviders []AccountProvider // Any account providers that are successfully configured get added to this slice
|
|
|
|
ynabClient *ynab.Client // YNAB HTTP client
|
|
|
|
lastRefresh time.Time // Timestamp that last data refresh ran - used for rate limiting
|
|
|
|
refreshRunning *sync.Mutex // True if a refresh is currently running
|
|
|
|
t *template.Template // HTML templates in the templates directory
|
2023-11-12 19:04:22 +00:00
|
|
|
)
|
|
|
|
|
2023-11-13 04:40:00 +00:00
|
|
|
// Called at program startup or if SIGHUP is received
|
2023-11-12 19:04:22 +00:00
|
|
|
func init() {
|
|
|
|
log.Printf("ynab-portfolio-monitor init")
|
|
|
|
|
2023-11-13 04:40:00 +00:00
|
|
|
// Load mandatory application configuration from environment variables
|
2023-11-12 19:04:22 +00:00
|
|
|
envVars := make(map[string]string)
|
|
|
|
envVars["ynab_secret"] = os.Getenv("ynab_secret")
|
|
|
|
envVars["ynab_budget_id"] = os.Getenv("ynab_budget_id")
|
|
|
|
|
|
|
|
// Validate that all required environment variables are set
|
|
|
|
for key, value := range envVars {
|
|
|
|
if value == "" {
|
|
|
|
log.Fatalf("shell environment variable %s is not set", key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-13 19:14:19 +00:00
|
|
|
// Loop through all account providers and attempt to configure them
|
|
|
|
// if configuration fails, the provider will not be used
|
2023-11-13 04:40:00 +00:00
|
|
|
configuredProviders = make([]AccountProvider, 0)
|
|
|
|
for _, p := range allProviders {
|
|
|
|
err := p.Configure()
|
2023-11-12 19:04:22 +00:00
|
|
|
if err != nil {
|
2023-11-13 04:40:00 +00:00
|
|
|
log.Printf("skipping provider '%s': %v", p.Name(), err)
|
|
|
|
continue
|
2023-11-12 23:50:46 +00:00
|
|
|
}
|
2023-11-13 04:40:00 +00:00
|
|
|
configuredProviders = append(configuredProviders, p)
|
|
|
|
log.Printf("enabled provider '%s'", p.Name())
|
2023-11-12 19:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ynab client is static and has no persistent data so is initialized here and not in main program loop
|
2023-11-13 04:40:00 +00:00
|
|
|
var err error
|
2023-11-12 19:04:22 +00:00
|
|
|
ynabClient, err = ynab.NewClient(envVars["ynab_budget_id"], envVars["ynab_secret"])
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to create ynab client: %v", err)
|
|
|
|
}
|
2023-11-14 01:05:20 +00:00
|
|
|
|
|
|
|
// Web Templates
|
|
|
|
// Parse all template files at startup
|
|
|
|
t, err = template.ParseGlob("./templates/*")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("couldn't parse HTML templates: %v", err)
|
|
|
|
}
|
2023-11-14 01:12:13 +00:00
|
|
|
|
|
|
|
refreshRunning = &sync.Mutex{}
|
2023-11-12 19:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2023-11-13 21:07:57 +00:00
|
|
|
go webServer()
|
|
|
|
|
|
|
|
for { // Main program loop
|
|
|
|
refreshData()
|
|
|
|
log.Print("Sleeping for 6 hours...")
|
|
|
|
time.Sleep(time.Hour * 6)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func refreshData() {
|
2023-11-14 01:05:20 +00:00
|
|
|
refreshRunning.Lock()
|
|
|
|
defer refreshRunning.Unlock()
|
2023-11-13 21:55:48 +00:00
|
|
|
|
|
|
|
// Only allow a refresh at most once every 5 minutes
|
|
|
|
if time.Now().Before(lastRefresh.Add(time.Minute * 5)) {
|
|
|
|
log.Printf("refresh rate limited")
|
2023-11-14 01:05:20 +00:00
|
|
|
|
2023-11-13 21:55:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
lastRefresh = time.Now()
|
|
|
|
|
2023-11-13 21:07:57 +00:00
|
|
|
// Loop through each configured account provider and attempt to get the account balances, and update YNAB
|
|
|
|
for _, p := range configuredProviders {
|
|
|
|
balances, accountIDs, err := p.GetBalances()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to get balances with provider '%s': %v", p.Name(), err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(balances) != len(accountIDs) {
|
|
|
|
log.Printf("'%s' provider data validation error: mismatched balance and accountID slice lengths - expected the same: balances length = %d, accountIDs length = %d", p.Name(), len(balances), len(accountIDs))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for i := range balances {
|
|
|
|
err = ynabClient.SetAccountBalance(accountIDs[i], balances[i])
|
2023-11-13 04:40:00 +00:00
|
|
|
if err != nil {
|
2023-11-13 21:07:57 +00:00
|
|
|
log.Printf("failed to update ynab account '%s' balance: %v", accountIDs[i], err)
|
2023-11-13 04:40:00 +00:00
|
|
|
}
|
2023-11-12 23:50:46 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-12 19:04:22 +00:00
|
|
|
}
|