forked from capitancambio/gocassa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeseries_table.go
67 lines (57 loc) · 1.87 KB
/
timeseries_table.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 gocassa
import (
"time"
)
const bucketFieldName = "bucket"
type timeSeriesT struct {
Table
timeField string
idField string
bucketSize time.Duration
}
func (o *timeSeriesT) Set(v interface{}) Op {
m, ok := toMap(v)
if !ok {
panic("Can't set: not able to convert")
}
if tim, ok := m[o.timeField].(time.Time); !ok {
panic("timeField is not actually a time.Time")
} else {
m[bucketFieldName] = o.bucket(tim.Unix())
}
return o.Table.Set(m)
}
func (o *timeSeriesT) bucket(secs int64) int64 {
return (secs - secs%int64(o.bucketSize/time.Second)) * 1000
}
func (o *timeSeriesT) Update(timeStamp time.Time, id interface{}, m map[string]interface{}) Op {
bucket := o.bucket(timeStamp.Unix())
return o.Where(Eq(bucketFieldName, bucket), Eq(o.timeField, timeStamp), Eq(o.idField, id)).Update(m)
}
func (o *timeSeriesT) Delete(timeStamp time.Time, id interface{}) Op {
bucket := o.bucket(timeStamp.Unix())
return o.Where(Eq(bucketFieldName, bucket), Eq(o.timeField, timeStamp), Eq(o.idField, id)).Delete()
}
func (o *timeSeriesT) Read(timeStamp time.Time, id, pointer interface{}) Op {
bucket := o.bucket(timeStamp.Unix())
return o.Where(Eq(bucketFieldName, bucket), Eq(o.timeField, timeStamp), Eq(o.idField, id)).ReadOne(pointer)
}
func (o *timeSeriesT) List(startTime time.Time, endTime time.Time, pointerToASlice interface{}) Op {
buckets := []interface{}{}
start := o.bucket(startTime.Unix())
for i := start; ; i += int64(o.bucketSize/time.Second) * 1000 {
if i >= endTime.Unix()*1000 {
break
}
buckets = append(buckets, i)
}
return o.Where(In(bucketFieldName, buckets...), GTE(o.timeField, startTime), LTE(o.timeField, endTime)).Read(pointerToASlice)
}
func (o *timeSeriesT) WithOptions(opt Options) TimeSeriesTable {
return &timeSeriesT{
Table: o.Table.WithOptions(opt),
timeField: o.timeField,
idField: o.idField,
bucketSize: o.bucketSize,
}
}