Skip to content

Commit

Permalink
Update chromedp logic for new site redesign
Browse files Browse the repository at this point in the history
- also update write to database logic to write each csv record immediately rather than batching all points.
- only create one point for shared measurement name
  • Loading branch information
tedpearson committed Feb 25, 2024
1 parent f587a4d commit 66a3b1d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
28 changes: 17 additions & 11 deletions internal/app/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ func DownloadCsv(config *Config, startDate string, endDate string) (string, erro

err = chromedp.Run(ctx,
chromedp.Navigate(config.LoginUrl),
chromedp.SetValue("#LoginUsernameTextBox", config.Username, chromedp.NodeVisible),
chromedp.SetValue("#LoginPasswordTextBox", config.Password),
chromedp.Click("#LoginSubmitButton"),
chromedp.SetValue("(//input)[1]", config.Username, chromedp.NodeVisible),
chromedp.SetValue("(//input)[2]", config.Password),
chromedp.Sleep(time.Second),
chromedp.Click(`//button[contains(.,"Sign In")]`),
browser.SetDownloadBehavior(browser.SetDownloadBehaviorBehaviorAllowAndName).
WithDownloadPath(config.DownloadDir).
WithEventsEnabled(true),
Expand All @@ -59,15 +60,20 @@ func DownloadCsv(config *Config, startDate string, endDate string) (string, erro
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.Click(`//button[.="USAGE"]`, chromedp.NodeVisible),
chromedp.Click(`//a[.='Usage Management']`, chromedp.NodeVisible),
chromedp.Click(`//a[contains(text(), "Download Your Data")]`, chromedp.NodeVisible),
chromedp.Click(`//app-usage-management-download-your-data//button[contains(., "Download")]`, chromedp.NodeVisible),
chromedp.Sleep(time.Second),
chromedp.Click(`//div[contains(@class, "interval")]//mat-select`),
chromedp.Sleep(time.Second),
chromedp.Click(`//mat-option[contains(., "HOURLY")]`, chromedp.NodeVisible),
chromedp.SetValue(`//div[contains(@class, "start-picker")]//input`, startDate),
chromedp.SetValue(`//div[contains(@class, "end-picker")]//input`, endDate),
chromedp.Click(`//div[contains(@class, "file-format")]//mat-select`),
chromedp.Click(`//mat-option[contains(., "CSV")]`, chromedp.NodeVisible),
chromedp.Sleep(time.Second),
chromedp.Click(`(//input[@name="timeFrameRadio"])[3]`, chromedp.NodeVisible),
chromedp.SetValue(`(//input[contains(@class, "form-control-readonly")])[1]`, startDate),
chromedp.SetValue(`(//input[contains(@class, "form-control-readonly")])[2]`, endDate),
chromedp.Click(`(//input[@name="fileFormatRadio"])[2]`),
chromedp.Click(`//button[.="Download Usage Data"]`),
chromedp.Click(`//button[.="Download"]`),
)
if err != nil {
return "", err
Expand Down
23 changes: 12 additions & 11 deletions internal/app/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,26 @@ type MetricLine struct {
func WriteMetrics(records []*ElectricUsage, config InfluxDB, existingPoints map[int64]struct{}) error {
client := influxdb2.NewClient(config.Host, config.User+":"+config.Password)
writeApi := client.WriteAPIBlocking("", config.Database)
points := make([]*write.Point, 0, 15*2*len(records))
for _, record := range records {
divisor := record.EndTime.Sub(record.StartTime).Minutes()
multiplier := 60 / divisor
minutes := record.EndTime.Sub(record.StartTime).Minutes()
points := make([]*write.Point, 0, int(minutes))
multiplier := 60 / minutes
for t := record.StartTime; record.EndTime.After(t); t = t.Add(time.Minute) {
if _, ok := existingPoints[t.UnixMilli()]; ok {
continue
}
watts := influxdb2.NewPointWithMeasurement("electric").
point := influxdb2.NewPointWithMeasurement("electric").
SetTime(t).
AddField("watts", float64(record.WattHours)*multiplier)
cost := influxdb2.NewPointWithMeasurement("electric").
SetTime(t).
AddField("cost", float64(record.CostInCents)/divisor)
points = append(points, watts, cost)
AddField("watts", float64(record.WattHours)*multiplier).
AddField("cost", float64(record.CostInCents)/minutes)
points = append(points, point)
}
err := writeApi.WritePoint(context.Background(), points...)
if err != nil {
return err
}
}

return writeApi.WritePoint(context.Background(), points...)
return nil
}

func QueryPreviousMetrics(startTime time.Time, endTime time.Time, config InfluxDB) (map[int64]struct{}, error) {
Expand Down

0 comments on commit 66a3b1d

Please sign in to comment.