Skip to content

Commit

Permalink
clean up CSV temp file, clear possible modal dialog, don't log.Fatal …
Browse files Browse the repository at this point in the history
…because defer won't be called.
  • Loading branch information
tedpearson committed Nov 5, 2022
1 parent 6b74f50 commit 09638ca
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
14 changes: 13 additions & 1 deletion internal/app/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
39 changes: 22 additions & 17 deletions internal/app/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"errors"
"flag"
"log"
"os"
Expand All @@ -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
Expand All @@ -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.")
Expand All @@ -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)
Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion internal/app/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 09638ca

Please sign in to comment.