-
Notifications
You must be signed in to change notification settings - Fork 18
/
reply_parser.go
258 lines (239 loc) · 5.89 KB
/
reply_parser.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
package redis_timeseries_go
import (
"errors"
"fmt"
"github.com/gomodule/redigo/redis"
"strconv"
)
func toAggregationType(aggType interface{}) (aggTypeStr AggregationType, err error) {
agg, err := redis.String(aggType, nil)
if err != nil {
return
}
aggTypeStr = (AggregationType)(agg)
return
}
func toDuplicatePolicy(duplicatePolicy interface{}) (duplicatePolicyStr DuplicatePolicyType, err error) {
duplicatePolicyStr = ""
if duplicatePolicy == nil {
return
}
policy, err := redis.String(duplicatePolicy, nil)
if err != nil {
return
}
duplicatePolicyStr = (DuplicatePolicyType)(policy)
return
}
func ParseRules(ruleInterface interface{}, err error) (rules []Rule, retErr error) {
if err != nil {
return nil, err
}
ruleSlice, err := redis.Values(ruleInterface, nil)
if err != nil {
return nil, err
}
for _, ruleSlice := range ruleSlice {
ruleValues, err := redis.Values(ruleSlice, nil)
if err != nil {
return nil, err
}
destKey, err := redis.String(ruleValues[0], nil)
if err != nil {
return nil, err
}
bucketSizeSec, err := redis.Int(ruleValues[1], nil)
if err != nil {
return nil, err
}
aggType, err := toAggregationType(ruleValues[2])
if err != nil {
return nil, err
}
rules = append(rules, Rule{destKey, bucketSizeSec, aggType})
}
return rules, nil
}
func ParseInfo(result interface{}, err error) (info KeyInfo, outErr error) {
values, outErr := redis.Values(result, err)
if outErr != nil {
return KeyInfo{}, err
}
if len(values)%2 != 0 {
return KeyInfo{}, errors.New("ParseInfo expects even number of values result")
}
var key string
for i := 0; i < len(values); i += 2 {
key, outErr = redis.String(values[i], nil)
switch key {
case "rules":
info.Rules, outErr = ParseRules(values[i+1], nil)
case "retentionTime":
info.RetentionTime, outErr = redis.Int64(values[i+1], nil)
case "chunkCount":
info.ChunkCount, outErr = redis.Int64(values[i+1], nil)
// Backwards compatible
case "maxSamplesPerChunk":
var v int64
v, outErr = redis.Int64(values[i+1], nil)
info.MaxSamplesPerChunk = v
info.ChunkSize = 16 * v
case "chunkSize":
info.ChunkSize, outErr = redis.Int64(values[i+1], nil)
case "lastTimestamp":
info.LastTimestamp, outErr = redis.Int64(values[i+1], nil)
case "labels":
info.Labels, outErr = ParseLabels(values[i+1])
case "duplicatePolicy":
info.DuplicatePolicy, outErr = toDuplicatePolicy(values[i+1])
}
if outErr != nil {
return KeyInfo{}, outErr
}
}
return info, nil
}
func ParseDataPoints(info interface{}) (dataPoints []DataPoint, err error) {
dataPoints = make([]DataPoint, 0)
values, err := redis.Values(info, err)
if err != nil {
return
}
for _, rawDataPoint := range values {
var dataPoint *DataPoint = nil //nolint:ineffassign
dataPoint, err = ParseDataPoint(rawDataPoint)
if err != nil {
return
}
if dataPoint != nil {
dataPoints = append(dataPoints, *dataPoint)
}
}
return
}
func ParseDataPoint(rawDataPoint interface{}) (dataPoint *DataPoint, err error) {
dataPoint = nil
iValues, err := redis.Values(rawDataPoint, nil)
if err != nil || len(iValues) == 0 {
return
}
if len(iValues) != 2 {
err = fmt.Errorf("ParseDataPoint expects array reply of size 2 with timestamp and value.Got %v", iValues)
return
}
timestamp, err := redis.Int64(iValues[0], nil)
if err != nil {
return
}
value, err := redis.String(iValues[1], nil)
if err != nil {
return
}
float, err := strconv.ParseFloat(value, 64)
if err != nil {
return
}
dataPoint = NewDataPoint(timestamp, float)
return
}
func ParseLabels(res interface{}) (labels map[string]string, err error) {
values, err := redis.Values(res, err)
if err != nil {
return
}
labels = make(map[string]string, len(values))
for i := 0; i < len(values); i++ {
iValues, err := redis.Values(values[i], err)
if err != nil {
return nil, err
}
if len(iValues) != 2 {
err = errors.New("ParseLabels: expects 2 elements per inner-array")
return nil, err
}
key, okKey := iValues[0].([]byte)
value, okValue := iValues[1].([]byte)
if !okKey || !okValue {
err = errors.New("ParseLabels: StringMap key not a bulk string value")
return nil, err
}
labels[string(key)] = string(value)
}
return
}
func ParseRanges(info interface{}) (ranges []Range, err error) {
values, err := redis.Values(info, err)
if err != nil {
return nil, err
}
if len(values) == 0 {
return []Range{}, nil
}
for _, i := range values {
iValues, err := redis.Values(i, err)
if err != nil {
return nil, err
}
if len(iValues) != 3 {
err = errors.New("ParseRanges: expects 3 elements per inner-array")
return nil, err
}
name, err := redis.String(iValues[0], nil)
if err != nil {
return nil, err
}
labels, err := ParseLabels(iValues[1])
if err != nil {
return nil, err
}
dataPoints, err := ParseDataPoints(iValues[2])
if err != nil {
return nil, err
}
r := Range{name, labels, dataPoints}
ranges = append(ranges, r)
}
return
}
func ParseRangesSingleDataPoint(info interface{}) (ranges []Range, err error) {
values, err := redis.Values(info, err)
if err != nil {
return nil, err
}
if len(values) == 0 {
return []Range{}, nil
}
for _, i := range values {
dataPoints := make([]DataPoint, 0)
iValues, err := redis.Values(i, err)
if err != nil {
return nil, err
}
if len(iValues) != 3 {
err = errors.New("ParseRanges: expects 3 elements per inner-array")
return nil, err
}
name, err := redis.String(iValues[0], nil)
if err != nil {
return nil, err
}
labels, err := ParseLabels(iValues[1])
if err != nil {
return nil, err
}
var dataPoint *DataPoint = nil //nolint:ineffassign
dataPoint, err = ParseDataPoint(iValues[2])
if err != nil {
return nil, err
}
if dataPoint != nil {
dataPoints = append(dataPoints, *dataPoint)
}
if err != nil {
return nil, err
}
r := Range{name, labels, dataPoints}
ranges = append(ranges, r)
}
return
}