-
Notifications
You must be signed in to change notification settings - Fork 3
/
numeric.go
265 lines (222 loc) · 7.37 KB
/
numeric.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
265
package gosnowth
import (
"bytes"
"context"
"encoding/json"
"fmt"
"path"
"strconv"
"time"
)
// NumericAllValueResponse values represent numeric data responses from IRONdb.
type NumericAllValueResponse struct {
Data []NumericAllValue
}
// UnmarshalJSON decodes a JSON format byte slice into a
// NumericAllValueResponse.
func (nv *NumericAllValueResponse) UnmarshalJSON(b []byte) error {
nv.Data = []NumericAllValue{}
values := [][]interface{}{}
if err := json.Unmarshal(b, &values); err != nil {
return fmt.Errorf("failed to deserialize numeric average response: %w",
err)
}
for _, entry := range values {
nav := NumericAllValue{}
if m, ok := entry[1].(map[string]interface{}); ok {
valueBytes, err := json.Marshal(m)
if err != nil {
return fmt.Errorf(
"failed to marshal intermediate value from tuple: %w", err)
}
if err := json.Unmarshal(valueBytes, &nav); err != nil {
return fmt.Errorf("failed to unmarshal value from tuple: %w",
err)
}
}
// grab the timestamp
if v, ok := entry[0].(float64); ok {
nav.Time = time.Unix(int64(v), 0)
}
nv.Data = append(nv.Data, nav)
}
return nil
}
// NumericAllValue values represent numeric data.
type NumericAllValue struct {
Time time.Time `json:"-"`
Count int64 `json:"count"`
Value int64 `json:"value"`
StdDev int64 `json:"stddev"`
Derivative int64 `json:"derivative"`
DerivativeStdDev int64 `json:"derivative_stddev"`
Counter int64 `json:"counter"`
CounterStdDev int64 `json:"counter_stddev"`
Derivative2 int64 `json:"derivative2"`
Derivative2StdDev int64 `json:"derivative2_stddev"`
Counter2 int64 `json:"counter2"`
Counter2StdDev int64 `json:"counter2_stddev"`
}
// NumericValueResponse values represent responses containing numeric data.
type NumericValueResponse struct {
Data []NumericValue
}
// UnmarshalJSON decodes a JSON format byte slice into a NumericValueResponse.
func (nv *NumericValueResponse) UnmarshalJSON(b []byte) error {
nv.Data = []NumericValue{}
values := [][]int64{}
if err := json.Unmarshal(b, &values); err != nil {
return fmt.Errorf("failed to deserialize numeric average response: %w",
err)
}
for _, tuple := range values {
nv.Data = append(nv.Data, NumericValue{
Time: time.Unix(tuple[0], 0),
Value: tuple[1],
})
}
return nil
}
// NumericValue values represent individual numeric data values.
type NumericValue struct {
Time time.Time
Value int64
}
// NumericWrite values represent numeric data.
type NumericWrite struct {
Count int64 `json:"count"`
Value int64 `json:"value"`
Derivative int64 `json:"derivative"`
Counter int64 `json:"counter"`
StdDev int64 `json:"stddev"`
DerivativeStdDev int64 `json:"derivative_stddev"`
CounterStdDev int64 `json:"counter_stddev"`
Metric string `json:"metric"`
ID string `json:"id"`
Offset int64 `json:"offset"`
Parts NumericParts `json:"parts"`
}
// NumericPartsData values represent numeric base data parts.
type NumericPartsData struct {
Count int64 `json:"count"`
Value int64 `json:"value"`
Derivative int64 `json:"derivative"`
Counter int64 `json:"counter"`
StdDev int64 `json:"stddev"`
DerivativeStdDev int64 `json:"derivative_stddev"`
CounterStdDev int64 `json:"counter_stddev"`
}
// NumericParts values contain the NumericWrite submission parts of an
// numeric rollup.
type NumericParts struct {
Period int64 `json:"period"`
Data []NumericPartsData `json:"data"`
}
// MarshalJSON marshals a NumericParts value into a JSON format byte slice.
func (p *NumericParts) MarshalJSON() ([]byte, error) {
tuple := []interface{}{}
tuple = append(tuple, p.Period, p.Data)
buf := bytes.NewBuffer([]byte{})
enc := json.NewEncoder(buf)
if err := enc.Encode(tuple); err != nil {
return buf.Bytes(), err
}
return buf.Bytes(), nil
}
// WriteNumeric writes numeric data to a node.
func (sc *SnowthClient) WriteNumeric(data []NumericWrite,
nodes ...*SnowthNode,
) error {
return sc.WriteNumericContext(context.Background(), data, nodes...)
}
// WriteNumericContext is the context aware version of WriteNumeric.
func (sc *SnowthClient) WriteNumericContext(ctx context.Context,
data []NumericWrite, nodes ...*SnowthNode,
) error {
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(data); err != nil {
return fmt.Errorf("failed to encode NumericWrite for write: %w", err)
}
var node *SnowthNode
if len(nodes) > 0 && nodes[0] != nil {
node = nodes[0]
} else if len(data) > 0 {
node = sc.GetActiveNode(sc.FindMetricNodeIDs(data[0].ID,
data[0].Metric))
}
if node == nil {
return fmt.Errorf("unable to get active node")
}
_, _, err := sc.DoRequestContext(ctx, node, "POST",
"/write/numeric", buf, nil)
return err
}
// ReadNumericValues reads numeric data from a node.
func (sc *SnowthClient) ReadNumericValues(start, end time.Time, period int64,
t, id, metric string, nodes ...*SnowthNode,
) ([]NumericValue, error) {
return sc.ReadNumericValuesContext(context.Background(), start, end,
period, t, id, metric, nodes...)
}
// ReadNumericValuesContext is the context aware version of ReadNumericValues.
func (sc *SnowthClient) ReadNumericValuesContext(ctx context.Context,
start, end time.Time, period int64,
t, id, metric string, nodes ...*SnowthNode,
) ([]NumericValue, error) {
var node *SnowthNode
if len(nodes) > 0 && nodes[0] != nil {
node = nodes[0]
} else {
node = sc.GetActiveNode(sc.FindMetricNodeIDs(id, metric))
}
if node == nil {
return nil, fmt.Errorf("unable to get active node")
}
r := &NumericValueResponse{}
body, _, err := sc.DoRequestContext(ctx, node, "GET", path.Join("/read",
strconv.FormatInt(start.Unix(), 10),
strconv.FormatInt(end.Unix(), 10),
strconv.FormatInt(period, 10), id, t, metric), nil, nil)
if err != nil {
return nil, err
}
if err := decodeJSON(body, &r); err != nil {
return nil, fmt.Errorf("unable to decode IRONdb response: %w", err)
}
return r.Data, nil
}
// ReadNumericAllValues reads all numeric data from a node.
func (sc *SnowthClient) ReadNumericAllValues(start, end time.Time, period int64,
id, metric string, nodes ...*SnowthNode,
) ([]NumericAllValue, error) {
return sc.ReadNumericAllValuesContext(context.Background(), start, end,
period, id, metric, nodes...)
}
// ReadNumericAllValuesContext is the context aware version of
// ReadNumericAllValues.
func (sc *SnowthClient) ReadNumericAllValuesContext(ctx context.Context,
start, end time.Time, period int64,
id, metric string, nodes ...*SnowthNode,
) ([]NumericAllValue, error) {
var node *SnowthNode
if len(nodes) > 0 && nodes[0] != nil {
node = nodes[0]
} else {
node = sc.GetActiveNode(sc.FindMetricNodeIDs(id, metric))
}
if node == nil {
return nil, fmt.Errorf("unable to get active node")
}
r := &NumericAllValueResponse{}
body, _, err := sc.DoRequestContext(ctx, node, "GET", path.Join("/read",
strconv.FormatInt(start.Unix(), 10),
strconv.FormatInt(end.Unix(), 10),
strconv.FormatInt(period, 10), id, "all", metric), nil, nil)
if err != nil {
return nil, err
}
if err := decodeJSON(body, &r); err != nil {
return nil, fmt.Errorf("unable to decode IRONdb response: %w", err)
}
return r.Data, nil
}