-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.go
320 lines (278 loc) · 9.55 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
"github.com/influxdata/influxdb-client-go/v2/api/write"
"github.com/peterbourgon/ff/v3"
"html"
"log"
"os"
"regexp"
"strconv"
"sync"
)
var wg sync.WaitGroup
var wgInflux sync.WaitGroup
func main() {
fs := flag.NewFlagSet("ns-exporter", flag.ContinueOnError)
var (
mongoUri = fs.String("mongo-uri", "", "Mongo-db uri to download from")
mongoDb = fs.String("mongo-db", "", "Mongo-db database name")
nsUri = fs.String("ns-uri", "", "Nightscout server url to download from")
nsToken = fs.String("ns-token", "", "Nigthscout server API Authorization Token")
limit = fs.Int64("limit", 0, "number of records to read from mongo-db")
skip = fs.Int64("skip", 0, "number of records to skip from mongo-db")
influxUri = fs.String("influx-uri", "", "InfluxDb uri to download from")
influxToken = fs.String("influx-token", "", "InfluxDb access token")
influxOrg = fs.String("influx-org", "ns", "InfluxDb organization to use")
influxBucket = fs.String("influx-bucket", "ns", "InfluxDb bucket to use")
configFile = fs.String("config", "", "File to load configuration from")
user = fs.String("user", "", "User name to be set on Influx record")
)
if err := ff.Parse(fs, os.Args[1:], ff.WithEnvVarPrefix("NS_EXPORTER")); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
ctx := context.Background()
deviceStatuses := make(chan NsEntry)
treatments := make(chan NsTreatment)
influx := make(chan write.Point)
if *mongoUri != "" && *mongoDb != "" {
NewExporterFromMongo(*mongoUri, *mongoDb, *user, ctx).processClient(deviceStatuses, treatments, *limit, *skip, ctx)
}
if *nsUri != "" && *nsToken != "" {
NewExporterFromNS(*nsUri, *nsToken, *user).processClient(deviceStatuses, treatments, *limit, *skip, ctx)
}
var config = Config{}
if *configFile != "" {
file, err := os.Open(*configFile)
if err != nil {
log.Fatal("can't open config file: ", err)
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
log.Fatal("can't decode config JSON: ", err)
}
var climit = *limit
if climit == 0 {
climit = config.Limit
}
if climit == 0 {
fail("'limit' must be greater than 0")
}
var cskip = *skip
if cskip == 0 {
cskip = config.Skip
}
for _, entry := range config.Imports {
var fMongoUri = combine(*mongoUri, entry.MongoUri)
if fMongoUri != "" && entry.MongoDb != "" {
NewExporterFromMongo(fMongoUri, entry.MongoDb, entry.User, ctx).processClient(deviceStatuses, treatments, climit, cskip, ctx)
}
if entry.NsUri != "" && entry.NsToken != "" {
NewExporterFromNS(entry.NsUri, entry.NsToken, entry.User).processClient(deviceStatuses, treatments, climit, cskip, ctx)
}
}
}
var wgTransform = &sync.WaitGroup{}
wgTransform.Add(2)
go parseDeviceStatuses(wgTransform, influx, deviceStatuses)
go parseTreatments(wgTransform, influx, treatments)
go func() {
wgInflux.Add(1)
defer wgInflux.Done()
var count = 0
var fInfluxUri = combineOrFail("InfluxDB uri not supplied", *influxUri, config.InfluxUri)
var fInfluxToken = combineOrFail("InfluxDB token not supplied", *influxToken, config.InfluxToken)
var fInfluxOrg = combineOrFail("InfluxDB token not supplied", *influxOrg, config.InfluxOrg)
var fInfluxBucket = combineOrFail("InfluxDB token not supplied", *influxBucket, config.InfluxBucket)
writeAPI := influxdb2.NewClient(fInfluxUri, fInfluxToken).WriteAPIBlocking(fInfluxOrg, fInfluxBucket)
for point := range influx {
if len(point.FieldList()) == 0 && len(point.TagList()) == 0 {
fmt.Println("empty point for time: ", point.Time(), " of type: ", point.Name())
continue
}
err := writeAPI.WritePoint(ctx, &point)
count++
if err != nil {
fmt.Println("error writing: ", point.Time(), ", name: ", point.Name())
}
}
fmt.Println("total writen: ", count)
}()
wg.Wait()
close(deviceStatuses)
close(treatments)
wgTransform.Wait()
close(influx)
wgInflux.Wait()
}
func combineOrFail(message string, values ...string) string {
var result = combine(values...)
if result == "" {
fail(message)
}
return result
}
func combine(values ...string) string {
var result = ""
for _, value := range values {
if value != "" {
result = value
}
}
return result
}
func fail(message string) {
fmt.Fprintf(os.Stderr, "error: %v\n", message)
os.Exit(1)
}
func parseDeviceStatuses(group *sync.WaitGroup, influx chan write.Point, entries chan NsEntry) {
defer group.Done()
reg := regexp.MustCompile("Dev: (?P<dev>[-0-9.]+),.*ISF: (?:(?P<isf_nt>[-0-9.]+)/(?P<isf_bg>[-0-9.]+)+=)?(?P<isf>[-0-9.]+),.*CR: (?P<cr>[-0-9.]+)")
var count = 0
var lastbg = 0.0
var lasttick float64 = 0
for entry := range entries {
point := influxdb2.NewPointWithMeasurement("openaps").
AddField("iob", entry.OpenAps.IOB.IOB).
AddField("basal_iob", entry.OpenAps.IOB.BasalIOB).
AddField("activity", entry.OpenAps.IOB.Activity).
SetTime(entry.OpenAps.IOB.Time)
if entry.User != "" {
point.AddTag("user", entry.User)
}
if entry.OpenAps.Suggested.Bg > 0 {
var tick = entry.OpenAps.Suggested.Tick
if lastbg == entry.OpenAps.Suggested.Bg &&
lasttick == tick &&
tick != 0.0 {
// deduplication, because nightscout still allows duplicate records to be added
fmt.Println("skipping duplicate bg record: ", entry.OpenAps.IOB.Time, ", bg: ", entry.OpenAps.Suggested.Bg, ", tick: ", tick)
continue
}
lastbg = entry.OpenAps.Suggested.Bg
lasttick = tick
point.
AddField("bg", entry.OpenAps.Suggested.Bg).
AddField("tick", tick).
AddField("eventual_bg", entry.OpenAps.Suggested.EventualBG).
AddField("target_bg", entry.OpenAps.Suggested.TargetBG).
AddField("insulin_req", entry.OpenAps.Suggested.InsulinReq).
AddField("cob", entry.OpenAps.Suggested.COB).
AddField("bolus", entry.OpenAps.Suggested.Units).
AddField("tbs_rate", entry.OpenAps.Suggested.Rate).
AddField("tbs_duration", entry.OpenAps.Suggested.Duration).
AddField("sens", entry.OpenAps.Suggested.SensitivityRatio)
if len(entry.OpenAps.Suggested.PredBGs.COB) > 0 {
point.AddField("pred_cob", entry.OpenAps.Suggested.PredBGs.COB[len(entry.OpenAps.Suggested.PredBGs.COB)-1])
}
if len(entry.OpenAps.Suggested.PredBGs.IOB) > 0 {
point.AddField("pred_iob", entry.OpenAps.Suggested.PredBGs.IOB[len(entry.OpenAps.Suggested.PredBGs.IOB)-1])
}
if len(entry.OpenAps.Suggested.PredBGs.UAM) > 0 {
point.AddField("pred_uam", entry.OpenAps.Suggested.PredBGs.UAM[len(entry.OpenAps.Suggested.PredBGs.UAM)-1])
}
if len(entry.OpenAps.Suggested.PredBGs.ZT) > 0 {
point.AddField("pred_zt", entry.OpenAps.Suggested.PredBGs.ZT[len(entry.OpenAps.Suggested.PredBGs.ZT)-1])
}
if len(entry.OpenAps.Suggested.Reason) > 0 {
matches := reg.FindStringSubmatch(entry.OpenAps.Suggested.Reason)
names := reg.SubexpNames()
for i, match := range matches {
if i != 0 {
if len(match) > 0 {
if rvalue, err := strconv.ParseFloat(match, 32); err == nil {
point.AddField(names[i], rvalue)
}
}
}
}
point.AddField("reason", html.UnescapeString(entry.OpenAps.Suggested.Reason))
}
}
count++
influx <- *point
fmt.Println("treatment time+: ", entry.OpenAps.IOB.Time, "iob:", entry.OpenAps.IOB.IOB, ", bg: ", entry.OpenAps.Suggested.Bg)
}
fmt.Println("total devicestatuses parsed: ", count)
}
func parseTreatments(group *sync.WaitGroup, influx chan write.Point, entries chan NsTreatment) {
defer group.Done()
var noted = map[string]bool{
"Site Change": true,
"Insulin Change": true,
"Pump Battery Change": true,
"Sensor Change": true,
"Sensor Start": true,
"Sensor Stop": true,
"BG Check": true,
"Exercise": true,
"Announcement": true,
"Question": true,
//"Note": true,
"OpenAPS Offline": true,
"D.A.D. Alert": true,
"Mbg": true,
//"Carb Correction": true,
//"Bolus Wizard": true,
//"Correction Bolus": true,
//"Meal Bolus": true,
//"Combo Bolus": true,
//"Temporary Target": true,
//"Temporary Target Cancel": true,
"Profile Switch": true,
//"Snack Bolus": true,
//"Temp Basal": true,
//"Temp Basal Start": true,
//"Temp Basal End": true,
}
var count = 0
for entry := range entries {
point := influxdb2.NewPointWithMeasurement("treatments").
SetTime(entry.CreatedAt)
if entry.User != "" {
point.AddTag("user", entry.User)
}
tagName := "type"
if entry.Carbs > 0 {
point.
AddField("carbs", entry.Carbs).
AddTag(tagName, "carbs")
}
if entry.Insulin > 0 {
point.
AddField("bolus", entry.Insulin).
AddTag(tagName, "bolus").
AddTag("smb", strconv.FormatBool(entry.IsSMB))
}
if entry.EventType == "Temp Basal" {
point.
AddField("duration", entry.Duration).
AddField("percent", entry.Percent).
AddField("rate", entry.Rate).
AddTag(tagName, "tbs")
} else if entry.EventType == "Temporary Target" {
point.
AddField("duration", entry.Duration).
AddField("target_top", entry.TargetTop).
AddField("target_bottom", entry.TargetBottom).
AddField("units", entry.Units).
AddField("reason", entry.Reason).
AddTag(tagName, "tt")
} else if len(entry.Notes) > 0 {
point.AddField("notes", entry.Notes)
} else if noted[entry.EventType] {
point.AddField("notes", entry.EventType)
}
count++
influx <- *point
fmt.Println("time: ", point.Time(), ", type: ", entry.EventType)
}
fmt.Println("total treatments parsed: ", count)
}