-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmetrics.go
134 lines (121 loc) · 4.04 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"context"
"fmt"
"reflect"
"time"
"github.com/iancoleman/strcase"
"github.com/influxdata/influxdb-client-go/v2/api"
"github.com/influxdata/influxdb-client-go/v2/api/write"
"github.com/tedpearson/ForecastMetrics/v3/source"
)
const ForecastTimeFormat = "2006-01-02:15"
// WriteOptions contains information for writing a point to the database: tags and measurement name
type WriteOptions struct {
ForecastSource string
MeasurementName string
Location string
ForecastTime *string
}
// MetricUpdater provides the ability to write forecasts to the database.
type MetricUpdater struct {
writeApi api.WriteAPIBlocking
overwrite bool
weatherMeasurement string
astroMeasurement string
precipProbability float64
}
// WriteMetrics writes a forecast to the database.
func (m MetricUpdater) WriteMetrics(forecast source.Forecast, location string, src string) {
forecastOptions := WriteOptions{
ForecastSource: src,
MeasurementName: m.weatherMeasurement,
Location: location,
}
if !m.overwrite {
forecastTime := time.Now().Truncate(time.Hour).Format(ForecastTimeFormat)
forecastOptions.ForecastTime = &forecastTime
}
ft := "nil"
if forecastOptions.ForecastTime != nil {
ft = *forecastOptions.ForecastTime
}
records := forecast.WeatherRecords
fmt.Printf(`Writing %d points {loc:"%s", src:"%s", measurement:"%s", forecast_time:"%s"}`+"\n",
len(records), location, src, m.weatherMeasurement, ft)
points := toPoints(records, forecastOptions)
if err := m.writeApi.WritePoint(context.Background(), points...); err != nil {
fmt.Printf("Error writing weather forecast point: %+v\n", err)
}
// write next hour to past forecast measurement
if !m.overwrite {
nextHour := time.Now().Truncate(time.Hour).Add(time.Hour)
for _, record := range records {
if nextHour.Equal(record.Time) {
nextHourRecord := []source.WeatherRecord{record}
nextHourOptions := forecastOptions
f := "0"
nextHourOptions.ForecastTime = &f
points = toPoints(nextHourRecord, nextHourOptions)
if err := m.writeApi.WritePoint(context.Background(), points...); err != nil {
fmt.Printf("Error writing weather forecast point: %+v\n", err)
}
break
}
}
}
if len(forecast.AstroEvents) > 0 {
// write astronomy
astronomyOptions := forecastOptions
astronomyOptions.MeasurementName = m.astroMeasurement
astronomyOptions.ForecastTime = nil
fmt.Printf(`Writing %d points {loc:"%s", src:"%s", measurement:"%s"}`+"\n",
len(forecast.AstroEvents), location, src, m.astroMeasurement)
points := toPoints(forecast.AstroEvents, astronomyOptions)
if err := m.writeApi.WritePoint(context.Background(), points...); err != nil {
fmt.Printf("Error writing astronomy forecast point: %+v\n", err)
return
}
}
}
// toPoints converts a slice of source.InfluxPointer to influx client points.
func toPoints[IP source.InfluxPointer](ip []IP, options WriteOptions) []*write.Point {
points := make([]*write.Point, 0, len(ip))
for _, item := range ip {
t := reflect.ValueOf(item).FieldByName("Time").Interface().(time.Time)
// only send future datapoints.
ft := options.ForecastTime
if ft != nil && *ft != "0" && t.Before(time.Now().Add(time.Hour+1)) {
continue
}
points = append(points, toPoint(t, item, options))
}
return points
}
// toPoint converts a struct to an influx client point.
func toPoint(t time.Time, i interface{}, options WriteOptions) *write.Point {
tags := map[string]string{
"source": options.ForecastSource,
"location": options.Location,
}
if options.ForecastTime != nil {
tags["forecast_time"] = *options.ForecastTime
}
fields := make(map[string]interface{})
e := reflect.ValueOf(i)
for i := 0; i < e.NumField(); i++ {
name := strcase.ToSnake(e.Type().Field(i).Name)
// note: skip time field (added when creating the point)
if name == "time" {
continue
}
ptr := e.Field(i)
if ptr.IsNil() {
// don't dereference nil pointers
continue
}
val := ptr.Elem().Interface()
fields[name] = val
}
return write.NewPoint(options.MeasurementName, tags, fields, t)
}