-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinflux.go
98 lines (77 loc) · 2.76 KB
/
influx.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
package main
import (
"fmt"
"time"
"github.com/influxdata/influxdb/client/v2"
"github.com/nilsmagnus/grib/griblib"
)
//ConnectionConfig is connection config w/credentials and url
type ConnectionConfig struct {
Hostname string
Port int
User string
Password string
Database string
}
func clientFromConfig(config ConnectionConfig) (client.Client, error) {
return client.NewHTTPClient(client.HTTPConfig{
Addr: fmt.Sprintf("%s:%d", config.Hostname, config.Port),
Username: config.User,
Password: config.Password,
InsecureSkipVerify: true,
})
}
func save(points []*client.Point, influxClient client.Client, database string) error {
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: database,
Precision: "s",
})
if err != nil {
return fmt.Errorf("Error creating batchpoints %s", err.Error())
}
bp.AddPoints(points)
return influxClient.Write(bp)
}
func toInfluxPoints(messages []griblib.Message, forecastOffsetHour int) []*client.Point {
points := []*client.Point{}
for _, message := range messages {
dataTypeName := griblib.ReadProductDisciplineParameters(message.Section0.Discipline,
message.Section4.ProductDefinitionTemplate.ParameterCategory)
forecastStartTime := toGoTime(message.Section1.ReferenceTime)
for counter, data := range message.Section7.Data {
coords := toCoords(counter, message.Section3)
// append forecasts and actuals to all series
points = append(points, singleInfluxDataPoint(data, dataTypeName, forecastStartTime, coords, forecastOffsetHour))
// append actuals to a separate series
if forecastOffsetHour == 0 {
points = append(points, singleInfluxDataPointActuals(data, dataTypeName, forecastStartTime, coords))
}
}
}
return points
}
func singleInfluxDataPoint(data float64, dataname string, forecastTime time.Time, coords Coords, offsetHours int) *client.Point {
fields := map[string]interface{}{
fmt.Sprintf("%s-%06dx%06d", dataname, coords.Lat/10000, coords.Lon/10000): data,
}
serieName := fmt.Sprintf("D%d%02d%02d%02d",
forecastTime.Year(), forecastTime.Month(), forecastTime.Day(), forecastTime.Hour())
valueTime := forecastTime.Add(time.Duration(offsetHours) * time.Hour)
point, err := client.NewPoint(serieName, map[string]string{}, fields, valueTime)
if err != nil {
panic(err)
}
return point
}
// if offsethour ==0, store to "actuals"
func singleInfluxDataPointActuals(data float64, dataname string, forecastTime time.Time, coords Coords) *client.Point {
fields := map[string]interface{}{
fmt.Sprintf("%s-%06dx%06d", dataname, coords.Lat/10000, coords.Lon/10000): data,
}
serieName := dataname
point, err := client.NewPoint(serieName, map[string]string{}, fields, forecastTime)
if err != nil {
panic(err)
}
return point
}