Skip to content

Commit

Permalink
Don't query previous metrics
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
tedpearson committed Mar 3, 2024
1 parent 8f5d856 commit c2c9d5c
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 59 deletions.
8 changes: 1 addition & 7 deletions internal/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
53 changes: 1 addition & 52 deletions internal/app/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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).
Expand All @@ -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
}

0 comments on commit c2c9d5c

Please sign in to comment.