From c2c9d5c264203520e2ff95953116d945c3e3b960 Mon Sep 17 00:00:00 2001 From: Ted Pearson <1477390+tedpearson@users.noreply.github.com> Date: Sat, 2 Mar 2024 19:12:42 -0500 Subject: [PATCH] Don't query previous metrics - With deduplication turned on via -dedup.minScrapeInterval, duplicate points should be consolidated to a single point. - Since previous data should not change, it should be safe to write duplicate points. --- internal/app/main.go | 8 +------ internal/app/metrics.go | 53 +---------------------------------------- 2 files changed, 2 insertions(+), 59 deletions(-) diff --git a/internal/app/main.go b/internal/app/main.go index 6731d14..b3e76cc 100644 --- a/internal/app/main.go +++ b/internal/app/main.go @@ -109,14 +109,8 @@ func Main() error { if err != nil { return err } - - log.Println("Querying previous metrics...") - existingPoints, err := QueryPreviousMetrics(startDate, endDate, config.InfluxDB) - if err != nil { - return err - } fmt.Println("Writing data to database...") - err = WriteMetrics(usage, config.InfluxDB, existingPoints) + err = WriteMetrics(usage, config.InfluxDB) if err != nil { return err } diff --git a/internal/app/metrics.go b/internal/app/metrics.go index d618a1b..3ce6a6c 100644 --- a/internal/app/metrics.go +++ b/internal/app/metrics.go @@ -3,23 +3,13 @@ package app import ( "context" "crypto/tls" - "encoding/json" - "fmt" - "log" - "net/http" - "net/url" - "strings" "time" "github.com/influxdata/influxdb-client-go/v2" "github.com/influxdata/influxdb-client-go/v2/api/write" ) -type MetricLine struct { - Timestamps []int64 -} - -func WriteMetrics(records []ElectricUsage, config InfluxConfig, existingPoints map[int64]struct{}) error { +func WriteMetrics(records []ElectricUsage, config InfluxConfig) error { opts := influxdb2.DefaultOptions() if config.Insecure { opts.SetTLSConfig(&tls.Config{InsecureSkipVerify: true}) @@ -31,9 +21,6 @@ func WriteMetrics(records []ElectricUsage, config InfluxConfig, existingPoints m 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 - } point := influxdb2.NewPointWithMeasurement("electric"). SetTime(t). AddField("watts", float64(record.WattHours)*multiplier). @@ -47,41 +34,3 @@ func WriteMetrics(records []ElectricUsage, config InfluxConfig, existingPoints m } return nil } - -func QueryPreviousMetrics(startTime time.Time, endTime time.Time, config InfluxConfig) (map[int64]struct{}, error) { - client := &http.Client{} - http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} - v := url.Values{ - "match[]": {"electric_usage"}, - "start": {startTime.Format(`2006-01-02T15:04:05Z07:00`)}, - "end": {endTime.Format("2006-01-02T15:04:05Z07:00")}, - } - req, err := http.NewRequest("POST", config.Host+"/api/v1/export", strings.NewReader(v.Encode())) - if err != nil { - return nil, err - } - req.Header.Add("Content-Type", "application/x-www-form-urlencoded") - req.SetBasicAuth(config.User, config.Password) - resp, err := client.Do(req) - if err != nil { - return nil, err - } - defer func() { - err := resp.Body.Close() - if err != nil { - log.Fatal(err) - } - }() - existing := make(map[int64]struct{}) - decoder := json.NewDecoder(resp.Body) - for decoder.More() { - var line MetricLine - if err := decoder.Decode(&line); err != nil { - log.Println(fmt.Errorf("Bad line: %w\n", err)) - } - for _, ts := range line.Timestamps { - existing[ts] = struct{}{} - } - } - return existing, nil -}