/
influxdb.go
264 lines (211 loc) · 7.11 KB
/
influxdb.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
package piot
/*
https://github.com/influxdata/line-protocol/blob/master/metric.go
dlfjsdlfjkkkaaaajjjkkkttps://github.com/influxdata/influxdb-client-go/blob/develop/write.go
*/
import (
"bytes"
"path"
"fmt"
"net/url"
"strconv"
"strings"
"time"
"github.com/mnezerka/go-piot/model"
"github.com/op/go-logging"
proto "github.com/influxdata/line-protocol"
)
type IInfluxDb interface {
PostMeasurement(thing *model.Thing, value string)
PostSwitchState(thing *model.Thing, value string)
PostLocation(thing *model.Thing, lat, lng float64, sat, ts int32)
}
type InfluxDb struct {
log *logging.Logger
orgs *Orgs
httpClient IHttpClient
Uri string
Username string
Password string
}
type RowMetric struct {
name string
tags []*proto.Tag
fields []*proto.Field
ts time.Time
}
func NewInfluxDb(log *logging.Logger, orgs *Orgs, httpClient IHttpClient, uri, username, password string) IInfluxDb {
db := &InfluxDb{log: log, orgs: orgs, httpClient: httpClient}
db.Uri = uri
db.Username = username
db.Password = password
return db
}
func InfluxDbEscapeString(str string) string {
return strings.ReplaceAll(str, " ", "\\ ")
}
func paramToString(name string, value interface{}) (string, error) {
valueStr, err := PrimitiveToString(value)
if err != nil {
return "", err
}
result := fmt.Sprintf("%s=%s", InfluxDbEscapeString(name), InfluxDbEscapeString(valueStr))
return result, nil
}
func (db *InfluxDb) PostMeasurement(thing *model.Thing, value string) {
db.log.Debugf("Posting measurement to InfluxDB, thing: %s, val: %s", thing.Name, value)
// get thing org -> get influxdb assigned to org
org, err := db.orgs.Get(thing.OrgId)
if err != nil {
return
}
db.log.Debugf("Going to post to InfluxDB %s as %s", org.InfluxDb, org.InfluxDbUsername)
if thing.Type != model.THING_TYPE_SENSOR {
// ignore things which don't represent sensor
return
}
// get thing name, use alias if set
name := thing.Name
if thing.Alias != "" {
name = thing.Alias
}
valueFloat, err := strconv.ParseFloat(value, 64)
if err != nil {
db.log.Warningf("Ignoring write measurement to influxdb for device %s due to invalid float value %s", thing.Id.Hex(), value)
return
}
fields := map[string]interface{}{ "value": valueFloat}
tags := map[string]string{"id": thing.Id.Hex(), "name": name, "class": thing.Sensor.Class}
rm := NewRowMetric("sensor", tags, fields, time.Now())
body, err := rm.Encode()
//body := fmt.Sprintf("sensor,id=%s,name=%s,class=%s value=%s", thing.Id.Hex(), name, thing.Sensor.Class, value)
url, err := url.Parse(db.Uri)
if err != nil {
db.log.Errorf("Cannot decode InfluxDB url from %s (%s)", db.Uri, err.Error())
return
}
url.Path = path.Join(url.Path, "write")
params := url.Query()
params.Add("db", org.InfluxDb)
url.RawQuery = params.Encode()
db.httpClient.PostString(url.String(), body.String(), &db.Username, &db.Password)
}
func (db *InfluxDb) PostSwitchState(thing *model.Thing, value string) {
db.log.Debugf("Posting switch state to InfluxDB, thing: %s, val: %s", thing.Name, value)
// get thing org -> get influxdb assigned to org
org, err := db.orgs.Get(thing.OrgId)
if err != nil {
return
}
db.log.Debugf("Going to post to InfluxDB %s as %s", org.InfluxDb, org.InfluxDbUsername)
if thing.Type != model.THING_TYPE_SWITCH {
// ignore things which don't represent switch
return
}
// get thing name, use alias if set
name := thing.Name
if thing.Alias != "" {
name = thing.Alias
}
fields := map[string]interface{}{ "value": value}
tags := map[string]string{"id": thing.Id.Hex(), "name": name}
rm := NewRowMetric("switch", tags, fields, time.Now())
body, err := rm.Encode()
//body := fmt.Sprintf("switch,id=%s,name=%s value=%s", thing.Id.Hex(), name, value)
url, err := url.Parse(db.Uri)
if err != nil {
db.log.Errorf("Cannot decode InfluxDB url from %s (%s)", db.Uri, err.Error())
return
}
url.Path = path.Join(url.Path, "write")
params := url.Query()
params.Add("db", org.InfluxDb)
url.RawQuery = params.Encode()
db.httpClient.PostString(url.String(), body.String(), &db.Username, &db.Password)
}
func (db *InfluxDb) PostLocation(thing *model.Thing, lat, lng float64, sat, ts int32) {
db.log.Debugf("Posting thing location to InfluxDB, thing: %s, lat: %f, lng: %f, sat: %d, ts: %d", thing.Name, lat, lng, sat, ts)
// get thing org -> get influxdb assigned to org
org, err := db.orgs.Get(thing.OrgId)
if err != nil {
return
}
db.log.Debugf("Going to post location to InfluxDB %s as %s", org.InfluxDb, org.InfluxDbUsername)
// get thing name, use alias if set
name := thing.Name
if thing.Alias != "" {
name = thing.Alias
}
fields := map[string]interface{}{
"lat": lat,
"lng": lng,
"sat": int64(sat),
}
tags := map[string]string{
"id": thing.Id.Hex(),
"name": name,
}
rm := NewRowMetric("location", tags, fields, time.Unix(int64(ts), 0))
buf, err := rm.Encode()
if err != nil {
db.log.Errorf("Cannot encode tags and fields into InfluxDB line protocol format: %s", err.Error())
return
}
url, err := url.Parse(db.Uri)
if err != nil {
db.log.Errorf("Cannot decode InfluxDB url from %s (%s)", db.Uri, err.Error())
return
}
url.Path = path.Join(url.Path, "write")
params := url.Query()
params.Add("db", org.InfluxDb)
url.RawQuery = params.Encode()
db.httpClient.PostString(url.String(), buf.String(), &db.Username, &db.Password)
}
func NewRowMetric(
name string,
tags map[string]string,
fields map[string]interface{},
ts time.Time,
) *RowMetric {
m := &RowMetric{
name: name,
tags: nil,
fields: nil,
ts: ts,
}
// convert tags to protocol format
if len(tags) > 0 {
m.tags = make([]*proto.Tag, 0, len(tags))
for k, v := range tags {
m.tags = append(m.tags,
&proto.Tag{Key: k, Value: v})
}
}
// convert fields to protocol format
m.fields = make([]*proto.Field, 0, len(fields))
for k, v := range fields {
/*v := convertField(v)
if v == nil {
continue
}
*/
m.fields = append(m.fields, &proto.Field{Key: k, Value: v})
}
return m
}
func (rm *RowMetric) Time() time.Time { return rm.ts }
func (rm *RowMetric) Name() string { return rm.name }
func (rm *RowMetric) TagList() []*proto.Tag { return rm.tags }
func (rm *RowMetric) FieldList() []*proto.Field { return rm.fields }
func (rm *RowMetric) Encode() (*bytes.Buffer, error) {
buf := &bytes.Buffer{}
e := proto.NewEncoder(buf)
e.SetFieldTypeSupport(proto.UintSupport)
e.SetFieldSortOrder(proto.SortFields)
e.FailOnFieldErr(true)
if _, err := e.Encode(rm); err != nil {
return nil, err
}
return buf, nil
}