-
Notifications
You must be signed in to change notification settings - Fork 8
/
influx.go
88 lines (76 loc) · 1.55 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
package main
import (
"encoding/json"
"fmt"
"log"
"net/url"
"os"
"strconv"
"time"
"github.com/influxdb/influxdb/client"
)
func queryDB(cmd string) (res []client.Result, err error) {
q := client.Query{
Command: cmd,
Database: os.Getenv("INFLUX_DB"),
}
if response, err := con.Query(q); err == nil {
if response.Error() != nil {
return res, response.Error()
}
res = response.Results
}
return
}
func query(query string) []float64 {
ret := []float64{}
res, err := queryDB(query)
if err != nil {
log.Fatal(err)
}
if len(res) < 1 {
return ret
}
if len(res[0].Series) < 1 {
return ret
}
for i, row := range res[0].Series[0].Values {
t, err := time.Parse(time.RFC3339, row[0].(string))
if err != nil {
log.Fatal(err)
}
if row[1] == nil {
continue
}
val, _ := row[1].(json.Number).Float64()
ret = append(ret, val)
if os.Getenv("DEBUG") == "true" {
log.Printf("[%2d] %s: %d\n", i, t.Format(time.Stamp), val)
}
}
return ret
}
var con *client.Client
func setupInflux() {
influx_port, _ := strconv.ParseInt(os.Getenv("INFLUX_PORT"), 10, 0)
u, err := url.Parse(fmt.Sprintf("http://%s:%d", os.Getenv("INFLUX_HOST"), influx_port))
if err != nil {
log.Fatal(err)
}
conf := client.Config{
URL: *u,
Username: os.Getenv("INFLUX_USER"),
Password: os.Getenv("INFLUX_PASS"),
}
con, err = client.NewClient(conf)
if err != nil {
log.Fatal(err)
}
dur, ver, err := con.Ping()
if err != nil {
log.Fatal(err)
}
if os.Getenv("DEBUG") == "true" {
log.Printf("Connected in %v | Version: %s", dur, ver)
}
}