-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsenmldatastore.go
264 lines (220 loc) · 6.1 KB
/
senmldatastore.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
263
264
package datastore
import (
"encoding/json"
"fmt"
"time"
tsdb "github.com/dschowta/lite.tsdb"
"github.com/farshidtz/senml"
)
const (
ASC = "asc"
DESC = "desc"
)
type SenmlDataStore struct {
tsdb tsdb.TSDB
}
type DenormMask int
const (
FName DenormMask = 1 << iota
FTime
FUnit
FValue
FSum
)
type Query struct {
//A comma separated senml names
Series string
From float64
To float64
//Sorting order:
//Possible values are ASC and DESC
//ASC : The time Series will have the oldest data first
//DESC: The time Series will have the latest data first.
Sort string
//Number of entries to be returned per request. This is used for pagination. The next sequence is found out using NextEntry function
MaxEntries int
//comma separated fields for denormalization (see https://tools.ietf.org/html/rfc8428#section-4.6). Currently only "name" and "time" are supported.
Denormalize DenormMask
}
type SenMLDBRecord struct {
Unit string `json:"u,omitempty" `
UpdateTime float64 `json:"ut,omitempty"`
Value *float64 `json:"v,omitempty" `
StringValue string `json:"vs,omitempty" `
DataValue string `json:"vd,omitempty" `
BoolValue *bool `json:"vb,omitempty" `
Sum *float64 `json:"s,omitempty" `
}
func (bdb *SenmlDataStore) Connect(path string) error {
config := tsdb.BoltDBConfig{Path: path}
var err error
bdb.tsdb, err = tsdb.Open(config)
return err
}
func (bdb SenmlDataStore) Disconnect() error {
return bdb.tsdb.Close()
}
func NewBoltSenMLRecord(record senml.Record) SenMLDBRecord {
return SenMLDBRecord{
record.Unit,
record.UpdateTime,
record.Value,
record.StringValue,
record.DataValue,
record.BoolValue,
record.Sum,
}
}
func ToSenmlTime(t time.Time) float64 {
if t.IsZero() {
return 0
}
return int64ToFloatTime(t.UnixNano())
}
func FromSenmlTime(t float64) time.Time {
return time.Unix(0, floatTimeToInt64(t))
}
//This function converts a floating point number (which is supported by senml) to a bytearray
func floatTimeToInt64(senmlTime float64) int64 {
//sec, frac := math.Modf(senmlTime)
return int64(senmlTime * (1e9)) //time.Unix(int64(sec), int64(frac*(1e9))).UnixNano()
}
//This function converts a bytearray floating point number (which is supported by senml)
func int64ToFloatTime(timeVal int64) float64 {
return float64(timeVal) / 1e9
}
//Create a new bucket
func (bdb SenmlDataStore) Create(name string) error {
return bdb.tsdb.Create(name)
}
func (bdb SenmlDataStore) Add(senmlPack senml.Pack) error {
// Fill the data map with provided data points
pack := senmlPack.Normalize()
seriesMap := make(map[string][]tsdb.TimeEntry)
for _, r := range pack {
if "" != r.Name {
b, err := json.Marshal(NewBoltSenMLRecord(r))
if err != nil {
return err
}
entry := tsdb.TimeEntry{floatTimeToInt64(r.Time), b}
seriesMap[r.Name] = append(seriesMap[r.Name], entry)
} else {
return fmt.Errorf("Senml record with Empty name")
}
}
for name, series := range seriesMap {
err := bdb.tsdb.Add(name, series)
if err != nil {
return err
}
}
return nil
}
func (bdb SenmlDataStore) Get(series string) (senml.Pack, error) {
var senmlPack senml.Pack
timeSeriesCh, errCh := bdb.tsdb.GetOnChannel(series)
senmlPack = iterateOverChannel(timeSeriesCh, 0, series)
//Check the error channel
err := <-errCh
return senmlPack, err
}
//Query the data store for a particular range. This gives the response in multiple pages
func (bdb SenmlDataStore) Query(query Query) (senml.Pack, *float64, error) {
var senmlPack senml.Pack
tsQuery := tsdb.Query{
MaxEntries: query.MaxEntries,
Series: query.Series,
Sort: query.Sort,
To: floatTimeToInt64(query.To),
From: floatTimeToInt64(query.From),
}
timeSeriesCh, nextEntryCh, errCh := bdb.tsdb.QueryOnChannel(tsQuery)
senmlPack = iterateOverChannel(timeSeriesCh, query.Denormalize, query.Series)
//Check the error channel
nextEntry := <-nextEntryCh
err := <-errCh
if nextEntry != nil {
nextEntryf := int64ToFloatTime(*nextEntry)
return senmlPack, &nextEntryf, err
} else {
return senmlPack, nil, err
}
}
func iterateOverChannel(timeSeriesCh <-chan tsdb.TimeEntry, denormalize DenormMask, seriesName string) (senmlPack senml.Pack) {
var baseTime float64
for timeEntry := range timeSeriesCh {
var timeRecord SenMLDBRecord
var curBaseName, curName string
var curTime float64
var curBaseTime float64
err := json.Unmarshal(timeEntry.Value, &timeRecord)
if err != nil {
fmt.Printf("Error while unmarshalling %s", err)
continue
}
if denormalize&FName != 0 {
if senmlPack == nil { //First time
curBaseName = seriesName
} else {
curBaseName = ""
}
curName = ""
} else {
curName = seriesName
}
if denormalize&FTime != 0 {
if senmlPack == nil { //First time
baseTime = int64ToFloatTime(timeEntry.Time)
curBaseTime = baseTime
curTime = 0
} else {
curBaseTime = 0
curTime = int64ToFloatTime(timeEntry.Time) - baseTime
}
} else {
curBaseTime = 0
curTime = int64ToFloatTime(timeEntry.Time)
}
record := senml.Record{
BaseName: curBaseName,
Name: curName,
Unit: timeRecord.Unit,
BaseTime: curBaseTime,
Time: curTime,
UpdateTime: timeRecord.UpdateTime,
Value: timeRecord.Value,
StringValue: timeRecord.StringValue,
DataValue: timeRecord.DataValue,
BoolValue: timeRecord.BoolValue,
Sum: timeRecord.Sum,
}
senmlPack = append(senmlPack, record)
}
return senmlPack
}
func (bdb SenmlDataStore) GetPages(query Query) ([]float64, int, error) {
tsQuery := tsdb.Query{
MaxEntries: query.MaxEntries,
Series: query.Series,
Sort: query.Sort,
To: floatTimeToInt64(query.To),
From: floatTimeToInt64(query.From),
}
pages, count, err := bdb.tsdb.GetPages(tsQuery)
if err != nil {
return nil, 0, err
}
fpages := make([]float64, 0, len(pages))
for _, page := range pages {
fpages = append(fpages, int64ToFloatTime(page))
}
return fpages, count, nil
}
func (bdb *SenmlDataStore) Delete(series string) error {
err := bdb.tsdb.Delete(series)
if err == tsdb.ErrSeriesNotFound {
err = ErrSeriesNotFound
}
return err
}