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"
|
|
|
|
|
2024-11-11 13:24:37 +00:00
|
|
|
"code.stevenpolley.net/steven/ynab-portfolio-monitor/ynab"
|
2023-11-12 19:04:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2024-09-01 13:30:42 +00:00
|
|
|
wg := sync.WaitGroup{}
|
2024-09-01 13:27:49 +00:00
|
|
|
|
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 {
|
2024-09-01 13:27:49 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
balances, accountIDs, err := p.GetBalances()
|
2023-11-13 04:40:00 +00:00
|
|
|
if err != nil {
|
2024-09-01 13:27:49 +00:00
|
|
|
log.Printf("failed to get balances with provider '%s': %v", p.Name(), err)
|
|
|
|
return
|
2023-11-13 04:40:00 +00:00
|
|
|
}
|
2024-09-01 13:27:49 +00:00
|
|
|
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))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := range balances {
|
|
|
|
err = ynabClient.SetAccountBalance(accountIDs[i], balances[i])
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to update ynab account '%s' balance: %v", accountIDs[i], err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2023-11-12 23:50:46 +00:00
|
|
|
}
|
2024-09-01 13:27:49 +00:00
|
|
|
wg.Wait()
|
2023-11-12 19:04:22 +00:00
|
|
|
}
|