-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
109 lines (80 loc) · 2.61 KB
/
db.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
package main
import (
"github.com/influxdata/influxdb1-client/v2"
"time"
"errors"
)
const (
USERNAME string = "admin"
PASSWORD string = "test12345"
DATABASE string = "iot_db"
)
func createPoint(params map[string]interface{}) (map[string]interface{}, error) {
influxClient, err := client.NewHTTPClient(client.HTTPConfig{
Addr: "http://localhost:8086",
Username: USERNAME,
Password: PASSWORD,
})
if err != nil {
return nil, err
}
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: DATABASE,
})
if err != nil {
return nil, err
}
clientId := params["gpu_id"].(string)
location := params["rig_id"].(string)
currentWatts := params["current_watts"]
pt, err := client.NewPoint("total_watts", map[string]string{"gpu_id": gpuId, "rig_id": rigId},
map[string]interface{}{"current_watts": currentWatts},
time.Now())
if err != nil {
return nil, err
}
bp.AddPoint(pt)
err = influxClient.Write(bp)
if err != nil {
return nil, err
}
resp := map[string]interface{}{"data" : "Added"}
return resp, nil
}
func getPoints() (map[string]interface{}, error) {
influxClient, err := client.NewHTTPClient(client.HTTPConfig{
Addr: "http://localhost:8086",
Username: USERNAME,
Password: PASSWORD,
})
if err != nil {
return nil, err
}
queryString := "SELECT gpu_id, rig_id, current_watts FROM total_watts"
q := client.NewQuery(queryString, DATABASE, "ns")
response, err := influxClient.Query(q)
if err != nil {
return nil, err
}
err = response.Error()
if err != nil {
return nil, errors.New("Empty data not allowed")
} else {
res := response.Results
if (len(res) == 0) {
return nil, err
}
columns := response.Results[0].Series[0].Columns
points := response.Results[0].Series[0].Values
data := []map[string]interface{}{}
for i := 0; i <= len(points) - 1 ; i++ {
record := map[string]interface{}{}
for j := 0; j <= len(columns) - 1; j++ {
record[string(columns[j])] = points[i][j]
}
data = append(data, record)
}
resp := map[string]interface{}{"data" : data}
return resp, nil
}
}