diff --git a/internal/app/browser.go b/internal/app/browser.go index 9c52e42..da4d494 100644 --- a/internal/app/browser.go +++ b/internal/app/browser.go @@ -46,7 +46,19 @@ func DownloadCsv(config *Config, startDate string, endDate string) (string, erro browser.SetDownloadBehavior(browser.SetDownloadBehaviorBehaviorAllowAndName). WithDownloadPath(config.DownloadDir). WithEventsEnabled(true), - chromedp.Click("#MyUsageDropDown > a", chromedp.NodeVisible), + ) + if err != nil { + return "", err + } + // possible modal dialog that needs to be dismissed + // if it doesn't show up, ignore error finding it + modalCtx, cancel := context.WithTimeout(ctx, 10*time.Second) + defer cancel() + _ = chromedp.Run(modalCtx, + chromedp.Click(`//div[contains(@class, "modal-body")]//button[.="No"]`, chromedp.NodeVisible), + ) + err = chromedp.Run(ctx, + chromedp.Click("#ViewUsageLink", chromedp.NodeVisible), chromedp.Click(`//div[.="Usage Explorer"]`, chromedp.NodeVisible), chromedp.Click(`//img[@alt='Usage Management']`, chromedp.NodeVisible), chromedp.Sleep(time.Second), diff --git a/internal/app/main.go b/internal/app/main.go index c9c6a1e..cc35004 100644 --- a/internal/app/main.go +++ b/internal/app/main.go @@ -1,6 +1,7 @@ package app import ( + "errors" "flag" "log" "os" @@ -9,11 +10,6 @@ import ( "gopkg.in/yaml.v3" ) -//var ( -// startDate = "09/29/2022" -// endDate = "09/30/2022" -//) - type InfluxDB struct { Host string User string @@ -32,7 +28,7 @@ type Config struct { InfluxDB InfluxDB } -func Main() { +func Main() error { configFlag := flag.String("config", "config.yaml", "Config file") startFlag := flag.String("start", "", "Start date of period to extract from electric co.") endFlag := flag.String("end", "", "End date of period to extract from electric co.") @@ -41,32 +37,32 @@ func Main() { // read config file, err := os.ReadFile(*configFlag) if err != nil { - log.Fatal(err) + return err } config := &Config{} err = yaml.Unmarshal(file, config) if err != nil { - log.Fatal(err) + return err } if config.ExtractDays > 45 || config.ExtractDays < 2 { - log.Fatal("ExtractDays must be between 2 and 45 per smarthub") + return errors.New("ExtractDays must be between 2 and 45 per smarthub") } var startDate, endDate time.Time if *startFlag != "" { startDate, err = time.ParseInLocation("2006-01-02", *startFlag, time.Local) if err != nil { - log.Fatal(err) + return err } if *endFlag == "" { - log.Fatal("start and end parameters must both be provided") + return errors.New("start and end parameters must both be provided") } endDate, err = time.ParseInLocation("2006-01-02", *endFlag, time.Local) if err != nil { - log.Fatal(err) + return err } if endDate.Sub(startDate).Hours() > 24*45 { - log.Fatal("start and end parameters must define a period of no more than 45 days") + return errors.New("start and end parameters must define a period of no more than 45 days") } // endDate should be the last minute of the day for the VictoriaMetrics query. endDate = endDate.Add((24 * time.Hour) - time.Minute) @@ -83,23 +79,32 @@ func Main() { log.Println("Downloading CSV...") path, err := DownloadCsv(config, startDate.Format("01/02/2006"), endDate.Format("01/02/2006")) if err != nil { - log.Fatal(err) + return err } log.Printf("CSV downloaded: %s\n", path) + defer cleanup(path) records, err := ParseCsv(path) if err != nil { - log.Fatal(err) + return err } log.Println("Querying previous metrics...") existingPoints, err := QueryPreviousMetrics(startDate, endDate, config.InfluxDB) if err != nil { - log.Fatal(err) + return err } log.Println("Inserting data...") err = WriteMetrics(records, config.InfluxDB, existingPoints) if err != nil { - log.Fatal(err) + return err } log.Println("Done") + return nil +} + +func cleanup(path string) { + log.Printf("Removing CSV: %s", path) + if err := os.Remove(path); err != nil { + log.Printf("Failed to remove CSV: %s", path) + } } diff --git a/internal/app/parser.go b/internal/app/parser.go index ba34971..53ce901 100644 --- a/internal/app/parser.go +++ b/internal/app/parser.go @@ -21,7 +21,7 @@ type ElectricUsage struct { func ParseCsv(file string) ([]*ElectricUsage, error) { dateRegex, err := regexp.Compile(`(\d{4}-\d\d-\d\d \d\d:\d\d) to (\d{4}-\d\d-\d\d \d\d:\d\d)`) if err != nil { - log.Fatal(err) + panic(err) } // drop all lines until it starts with dddd-dd-dd data, err := os.Open(file) diff --git a/main.go b/main.go index 7f81c37..f6d2781 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,11 @@ package main import ( "electric-usage-downloader/internal/app" + "log" ) func main() { - app.Main() + if err := app.Main(); err != nil { + log.Fatal(err) + } }