-
Notifications
You must be signed in to change notification settings - Fork 0
/
influx.go
67 lines (59 loc) · 1.81 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
package database
import (
"context"
"fmt"
"strconv"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
)
type InfluxClient struct {
ip string
token string
organisation string
productionTotalQuery string
}
type Query struct {
ProductionTotal string
}
func NewInfluxClient(ip string, port string, token string, organisation string, productionTotalQuery string) *InfluxClient {
return &InfluxClient{
// Create a new client using an InfluxDB server base URL and an authentication token
ip: fmt.Sprintf("http://%s:%s", ip, port),
token: token,
organisation: organisation,
productionTotalQuery: productionTotalQuery,
}
}
func (i *InfluxClient) InfluxProductionTotal() (float64, error) {
var retValue float64 = -1
var retError error = nil
// Create a new client using an InfluxDB server base URL and an authentication token
client := influxdb2.NewClient(i.ip, i.token)
// Get query client
queryAPI := client.QueryAPI(i.organisation)
// get QueryTableResult
result, err := queryAPI.Query(context.Background(), i.productionTotalQuery)
if err == nil {
// Iterate over query response
for result.Next() {
// Access data
valueString := fmt.Sprintf("%v", result.Record().Value())
// Parse the string value to float64
valueFloat, err := strconv.ParseFloat(valueString, 64)
if err != nil {
retError = fmt.Errorf("error parsing value to float64: %v", err)
} else {
retValue = valueFloat
}
}
// check for an error
if result.Err() != nil {
retError = fmt.Errorf("query parsing error: %s", result.Err().Error())
}
} else {
retError = fmt.Errorf("query error: %s", err)
}
// Ensures background processes finishes
client.Close()
//fmt.Printf("Value: %f\n", retValue)
return retValue, retError
}