/
queries.go
74 lines (62 loc) · 1.9 KB
/
queries.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
package main
import (
"encoding/json"
"log"
"time"
clientPkg "github.com/influxdata/influxdb/client/v2"
)
// Possibly returns multiple series if you select across multiple tags
func query(client clientPkg.Client, databaseName, command string) [][]Point {
log.Printf("Query is %s", command)
q := clientPkg.Query{
Command: command,
Database: databaseName,
Precision: "ns",
}
response, err := client.Query(q)
if err != nil {
log.Fatalf("Error from Query with command %s: %s", command, err)
}
if response.Error() != nil {
log.Fatalf("Error from Error with command %s: %s", command, response.Error())
}
if len(response.Results) != 1 {
log.Fatalf("Expected len(Results) to be 1, but was %d in command %s", len(response.Results), command)
}
result := response.Results[0]
if len(result.Messages) > 0 {
log.Fatalf("Unexpected messages in result for command %s: %v", command, result.Messages)
}
if len(result.Err) > 0 {
log.Fatalf("Unexpected Err in result for command %s: %v", command, result.Err)
}
allPoints := [][]Point{}
for _, series := range result.Series {
seriesPoints := []Point{}
if len(series.Columns) != 2 {
log.Fatalf("Expected len(Columns) to be 2, but was %d in command %s", len(series.Columns), command)
}
if series.Columns[0] != "time" {
log.Fatalf("Expected Columns[0] to be 'time', but was %s in command %s", series.Columns[0], command)
}
for _, row := range series.Values {
timeNanos, err := row[0].(json.Number).Int64()
if err != nil {
log.Fatalf("Error from Float64 of %v", row[0])
}
if row[1] != nil {
value, err := row[1].(json.Number).Float64()
if err != nil {
log.Fatalf("Error from Float64 of %v", row[1])
}
point := Point{
Time: time.Unix(0, timeNanos).UTC(),
Value: value,
}
seriesPoints = append(seriesPoints, point)
}
}
allPoints = append(allPoints, seriesPoints)
}
return allPoints
}