-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimeseries.go
246 lines (213 loc) · 6.22 KB
/
timeseries.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
package main
import (
"math"
data "github.com/gtfierro/mdal/capnp"
"github.com/pkg/errors"
capnp "zombiezen.com/go/capnproto2"
)
type TimeseriesInfo struct {
Aligned bool
NumStreams int
Streams []StreamInfo
}
type StreamInfo struct {
NumTimes int
NumValues int
}
// notes
// msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
// if err != nil {
// panic(err)
// }
//
// // Create a new Book struct. Every message must have a root struct.
// book, err := books.NewRootBook(seg)
// if err != nil {
// panic(err)
// }
// book.SetTitle("War and Peace")
// book.SetPageCount(1440)
//
// // Write the message to stdout.
// err = capnp.NewEncoder(os.Stdout).Encode(msg)
// if err != nil {
// panic(err)
// }
// `
// for below
// RawStream
// msg, seg, err := capnp.NewMessage(capnp.SingleSegment
type Timeseries struct {
//New(starttime, endtime int64, num int)
//GetTimes() []int64
//GetValues() []float64
//SetTime(idx int, time int64)
//SetValue(idx int, value float64)
//SetReading(time int64, value float64)
msg *capnp.Message
collection data.StreamCollection
list data.Stream_List
}
func NewTimeseries(numStreams int) (t *Timeseries, err error) {
msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
if err != nil {
return nil, errors.Wrap(err, "Could not allocate root capnp struct")
}
t = new(Timeseries)
t.msg = msg
if t.collection, err = data.NewRootStreamCollection(seg); err != nil {
return nil, errors.Wrap(err, "Could not allocate timeseries")
}
if t.list, err = t.collection.NewStreams(int32(numStreams)); err != nil {
return nil, errors.Wrap(err, "Could not allocate timeseries")
}
for idx := 0; idx < numStreams; idx++ {
stream, err := data.NewStream(t.collection.Struct.Segment())
if err != nil {
return nil, errors.Wrap(err, "Could not allocate timeseries (internal stream)")
}
if err := t.list.Set(idx, stream); err != nil {
return nil, errors.Wrap(err, "Could not assign timeseries (internal stream)")
}
}
return t, nil
}
func (t *Timeseries) AddStreamWithTime(idx int, iv_times *iovec, iv_values *iovec) error {
stream := t.list.At(idx)
times, err := stream.NewTimes(int32(iv_times.count()))
if err != nil {
return errors.Wrap(err, "Could not allocate time array")
}
addtimes := func(idx int, time int64) {
times.Set(idx, time)
}
iv_times.iterTimes(addtimes)
if err := stream.SetTimes(times); err != nil {
return errors.Wrap(err, "Could not assign time array")
}
values, err := stream.NewValues(int32(iv_values.count()))
if err != nil {
return errors.Wrap(err, "Could not allocate value array")
}
addvalues := func(idx int, value float64) {
values.Set(idx, value)
}
iv_values.iterValues(addvalues)
if err := stream.SetValues(values); err != nil {
return errors.Wrap(err, "Could not assign value array")
}
t.list.Set(idx, stream)
return nil
}
func (t *Timeseries) AddAlignedStream(idx int, iv_times, iv_values *iovec) error {
times, err := t.collection.Times()
if err != nil {
return errors.Wrap(err, "Could not retrieve collection times")
}
val_count := iv_values.count()
stream := t.list.At(idx)
values, err := stream.NewValues(int32(times.Len()))
if err != nil {
return errors.Wrap(err, "Could not allocate value array")
}
// we need to leverage the timestamp of each value in order to align it properly
// with the windows defined by the larger timeseries structure (t.collection.Times()).
colIdx := 0
align := func(idx int, time int64) {
// check if timestamp for data stream @ idx is within colIdx window.
// If it is, add the data here. If its not, we look elsewhere
if colIdx >= times.Len() {
return // skip the extra ones
}
if times.At(colIdx) <= time && (colIdx == times.Len()-1 || time < times.At(colIdx+1)) {
values.Set(colIdx, iv_values.getValue(idx))
} else if times.At(colIdx) > time && colIdx > 0 { // before
values.Set(colIdx-1, iv_values.getValue(idx))
} else if time >= times.At(colIdx+1) {
values.Set(colIdx+1, iv_values.getValue(idx))
} else {
log.Error("bad value")
}
colIdx += 1
}
iv_times.iterTimes(align)
for i := val_count; i < times.Len(); i++ {
values.Set(i, math.NaN())
}
if err := stream.SetValues(values); err != nil {
return errors.Wrap(err, "Could not assign value array")
}
if t.list.Len() <= idx {
return errors.Wrapf(err, "Index %d out of range %d", idx, t.list.Len())
}
t.list.Set(idx, stream)
return nil
}
func (t *Timeseries) AddCollectionTimes(iv_times *iovec) error {
times, err := t.collection.NewTimes(int32(iv_times.count()))
if err != nil {
return errors.Wrap(err, "Could not allocate time array")
}
addtimes := func(idx int, time int64) {
times.Set(idx, time)
}
iv_times.iterTimes(addtimes)
if err := t.collection.SetTimes(times); err != nil {
return errors.Wrap(err, "Could not assign time array")
}
return nil
}
func (t *Timeseries) Info() TimeseriesInfo {
var infos []StreamInfo
for idx := 0; idx < t.list.Len(); idx++ {
stream := t.list.At(idx)
var info StreamInfo
if stream.HasTimes() {
t, _ := stream.Times()
info.NumTimes = t.Len()
}
if stream.HasValues() {
v, _ := stream.Values()
info.NumValues = v.Len()
}
infos = append(infos, info)
}
info := TimeseriesInfo{
Aligned: false,
NumStreams: t.list.Len(),
Streams: infos,
}
return info
}
// we have fewer values returned from the timeseries database then we do
// possible timestamps. Here we trim the rest off
func (t *Timeseries) Trim() error {
times, err := t.collection.Times()
if err != nil {
return errors.Wrap(err, "Could not retrieve times field")
}
var maxvalues int32 = math.MaxInt32
for idx := 0; idx < t.list.Len(); idx++ {
stream := t.list.At(idx)
if stream.HasValues() {
v, _ := stream.Values()
if int32(v.Len()) < maxvalues {
maxvalues = int32(v.Len())
}
}
}
if maxvalues == math.MaxInt32 {
return nil //no-op
}
newtimes, err := t.collection.NewTimes(maxvalues)
if err != nil {
return errors.Wrap(err, "Could not reallocate time array")
}
for idx := 0; idx < newtimes.Len(); idx++ {
newtimes.Set(idx, times.At(idx))
}
if err := t.collection.SetTimes(newtimes); err != nil {
return errors.Wrap(err, "Could not assign time array")
}
return nil
}