-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.go
159 lines (142 loc) · 3.42 KB
/
script.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package riverzone
import (
"context"
"fmt"
"os"
"strings"
"github.com/whitewater-guide/gorge/core"
)
type optionsRiverzone struct {
Key string `desc:"Auth key"`
}
type scriptRiverzone struct {
name string
options optionsRiverzone
stationsEndpointURL string
core.LoggingScript
}
func containsStr(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
func (s *scriptRiverzone) fetch(path string, dest interface{}) error {
key := s.options.Key
if key == "" {
key = os.Getenv("RIVERZONE_KEY")
}
if key == "" {
return fmt.Errorf("riverzone api key not found")
}
err := core.Client.GetAsJSON(
s.stationsEndpointURL+path,
&dest,
&core.RequestOptions{
Headers: map[string]string{"X-Key": key},
},
)
if err != nil {
return err
}
return nil
}
func (s *scriptRiverzone) fetchStations() (*stationsResp, error) {
var response *stationsResp
err := s.fetch("", &response)
return response, err
}
func (s *scriptRiverzone) fetchReadings() (*readingsResp, error) {
var response *readingsResp
err := s.fetch("/readings?from=60&to=60", &response)
return response, err
}
func (s *scriptRiverzone) ListGauges() (core.Gauges, error) {
stations, err := s.fetchStations()
if err != nil {
return nil, err
}
var result []core.Gauge
for _, station := range stations.Stations {
if !station.IsActive {
continue
}
var flowUnit, levelUnit string
if containsStr(station.Sensors, "level") {
levelUnit = "cm"
}
if containsStr(station.Sensors, "flow") {
flowUnit = "m3s"
}
g := core.Gauge{
GaugeID: core.GaugeID{
Script: s.name,
Code: station.ID,
},
Name: strings.ReplaceAll(fmt.Sprintf("%s - %s - %s - %s", station.CountryCode, station.State, station.River.value, station.Name), "- -", "-"),
Location: &core.Location{
Latitude: core.TruncCoord(station.Latlng.Lat),
Longitude: core.TruncCoord(station.Latlng.Lng),
Altitude: 0,
},
FlowUnit: flowUnit,
LevelUnit: levelUnit,
URL: station.SourceLinks.value,
Timezone: rzTz[station.CountryCode],
}
result = append(result, g)
}
return result, nil
}
func (s *scriptRiverzone) Harvest(ctx context.Context, recv chan<- *core.Measurement, errs chan<- error, codes core.StringSet, since int64) {
defer close(recv)
defer close(errs)
readings, err := s.fetchReadings()
if err != nil {
errs <- err
return
}
count := 0
s.GetLogger().Debugf("Harvested %d stations", len(readings.Readings))
for id, reading := range readings.Readings {
flowValues := make(map[core.HTime]*core.Measurement)
if reading.M3s != nil {
for _, reading := range reading.M3s {
t := core.HTime{Time: reading.Timestamp.Time}
flowValues[t] = &core.Measurement{
GaugeID: core.GaugeID{
Script: s.name,
Code: id,
},
Flow: reading.Value,
Timestamp: t,
}
}
}
if reading.Cm != nil {
for _, reading := range reading.Cm {
t := core.HTime{Time: reading.Timestamp.Time}
// Trying to find corresponding flow value:
if flowValue, ok := flowValues[t]; ok {
flowValue.Level = reading.Value
} else {
recv <- &core.Measurement{
GaugeID: core.GaugeID{
Script: s.name,
Code: id,
},
Level: reading.Value,
Timestamp: t,
}
}
}
}
for _, v := range flowValues {
count += 1
recv <- v
}
}
s.GetLogger().Debugf("Sent %d readings", count)
}