-
Notifications
You must be signed in to change notification settings - Fork 18
/
client.go
449 lines (399 loc) · 17 KB
/
client.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package redis_timeseries_go
import (
"strings"
"time"
"github.com/gomodule/redigo/redis"
)
// TODO: refactor this hard limit and revise client locking
// Client Max Connections
var maxConns = 500
// NewClient creates a new client connecting to the redis host, and using the given name as key prefix.
// Addr can be a single host:port pair, or a comma separated list of host:port,host:port...
// In the case of multiple hosts we create a multi-pool and select connections at random
func NewClient(addr, name string, authPass *string) *Client {
addrs := strings.Split(addr, ",")
var pool ConnPool
if len(addrs) == 1 {
pool = NewSingleHostPool(addrs[0], authPass)
} else {
pool = NewMultiHostPool(addrs, authPass)
}
ret := &Client{
Pool: pool,
Name: name,
}
return ret
}
// NewClientFromPool creates a new Client with the given pool and client name
func NewClientFromPool(pool *redis.Pool, name string) *Client {
ret := &Client{
Pool: pool,
Name: name,
}
return ret
}
// CreateKey create a new time-series
// Deprecated: This function has been deprecated, use CreateKeyWithOptions instead
func (client *Client) CreateKey(key string, retentionTime time.Duration) (err error) {
conn := client.Pool.Get()
defer conn.Close()
opts := DefaultCreateOptions
opts.RetentionMSecs = retentionTime
return client.CreateKeyWithOptions(key, opts)
}
func (client *Client) CreateKeyWithOptions(key string, options CreateOptions) (err error) {
conn := client.Pool.Get()
defer conn.Close()
args := []interface{}{key}
args, err = options.SerializeSeriesOptions(CREATE_CMD, args)
if err != nil {
return
}
_, err = conn.Do(CREATE_CMD, args...)
return err
}
// Update the retention, labels of an existing key. The parameters are the same as TS.CREATE.
func (client *Client) AlterKeyWithOptions(key string, options CreateOptions) (err error) {
conn := client.Pool.Get()
defer conn.Close()
args := []interface{}{key}
args, err = options.SerializeSeriesOptions(ALTER_CMD, args)
if err != nil {
return
}
_, err = conn.Do(ALTER_CMD, args...)
return err
}
// Add - Append (or create and append) a new sample to the series
// args:
// key - time series key name
// timestamp - time of value
// value - value
func (client *Client) Add(key string, timestamp int64, value float64) (storedTimestamp int64, err error) {
conn := client.Pool.Get()
defer conn.Close()
return redis.Int64(conn.Do(ADD_CMD, key, timestamp, floatToStr(value)))
}
// AddAutoTs - Append (or create and append) a new sample to the series, with DB automatic timestamp (using the system clock)
// args:
// key - time series key name
// value - value
func (client *Client) AddAutoTs(key string, value float64) (storedTimestamp int64, err error) {
conn := client.Pool.Get()
defer conn.Close()
return redis.Int64(conn.Do(ADD_CMD, key, "*", floatToStr(value)))
}
// AddWithOptions - Append (or create and append) a new sample to the series, with the specified CreateOptions
// args:
// key - time series key name
// timestamp - time of value
// value - value
// options - define options for create key on add
func (client *Client) AddWithOptions(key string, timestamp int64, value float64, options CreateOptions) (storedTimestamp int64, err error) {
conn := client.Pool.Get()
defer conn.Close()
args := []interface{}{key, timestamp, floatToStr(value)}
args, err = options.SerializeSeriesOptions(ADD_CMD, args)
if err != nil {
return
}
return redis.Int64(conn.Do(ADD_CMD, args...))
}
// AddAutoTsWithOptions - Append (or create and append) a new sample to the series, with the specified CreateOptions and DB automatic timestamp (using the system clock)
// args:
// key - time series key name
// value - value
// options - define options for create key on add
func (client *Client) AddAutoTsWithOptions(key string, value float64, options CreateOptions) (storedTimestamp int64, err error) {
conn := client.Pool.Get()
defer conn.Close()
args := []interface{}{key, "*", floatToStr(value)}
args, err = options.SerializeSeriesOptions(ADD_CMD, args)
if err != nil {
return
}
return redis.Int64(conn.Do(ADD_CMD, args...))
}
// AddWithRetention - append a new value to the series with a duration
// args:
// key - time series key name
// timestamp - time of value
// value - value
// duration - value
// Deprecated: This function has been deprecated, use AddWithOptions instead
func (client *Client) AddWithRetention(key string, timestamp int64, value float64, duration int64) (storedTimestamp int64, err error) {
conn := client.Pool.Get()
defer conn.Close()
options := DefaultCreateOptions
options.RetentionMSecs = time.Duration(duration)
return client.AddWithOptions(key, timestamp, value, options)
}
// DeleteSerie - deletes series given the time series key name. This API is sugar coating on top of redis DEL command
// args:
// key - time series key name
func (client *Client) DeleteSerie(key string) (err error) {
conn := client.Pool.Get()
defer conn.Close()
_, err = conn.Do(DEL_CMD, key)
return err
}
// DeleteRange - Delete data points for a given timeseries and interval range in the form of start and end delete timestamps.
// Returns the total deleted datapoints.
// args:
// key - time series key name
// fromTimestamp - start of range. You can use TimeRangeMinimum to express the minimum possible timestamp.
// toTimestamp - end of range. You can use TimeRangeFull or TimeRangeMaximum to express the maximum possible timestamp.
func (client *Client) DeleteRange(key string, fromTimestamp int64, toTimestamp int64) (totalDeletedSamples int64, err error) {
conn := client.Pool.Get()
defer conn.Close()
totalDeletedSamples, err = redis.Int64(conn.Do(TS_DEL_CMD, key, fromTimestamp, toTimestamp))
return
}
// CreateRule - create a compaction rule
// args:
// sourceKey - key name for source time series
// aggType - AggregationType
// bucketSizeMSec - Time bucket for aggregation in milliseconds
// destinationKey - key name for destination time series
func (client *Client) CreateRule(sourceKey string, aggType AggregationType, bucketSizeMSec uint, destinationKey string) (err error) {
conn := client.Pool.Get()
defer conn.Close()
_, err = conn.Do(CREATERULE_CMD, sourceKey, destinationKey, "AGGREGATION", aggType, bucketSizeMSec)
return err
}
// DeleteRule - delete a compaction rule
// args:
// sourceKey - key name for source time series
// destinationKey - key name for destination time series
func (client *Client) DeleteRule(sourceKey string, destinationKey string) (err error) {
conn := client.Pool.Get()
defer conn.Close()
_, err = conn.Do(DELETERULE_CMD, sourceKey, destinationKey)
return err
}
// Range - ranged query
// args:
// key - time series key name
// fromTimestamp - start of range. You can use TimeRangeMinimum to express the minimum possible timestamp.
// toTimestamp - end of range. You can use TimeRangeFull or TimeRangeMaximum to express the maximum possible timestamp.
// Deprecated: This function has been deprecated, use RangeWithOptions instead
func (client *Client) Range(key string, fromTimestamp int64, toTimestamp int64) (dataPoints []DataPoint, err error) {
return client.RangeWithOptions(key, fromTimestamp, toTimestamp, DefaultRangeOptions)
}
// AggRange - aggregation over a ranged query
// args:
// key - time series key name
// fromTimestamp - start of range. You can use TimeRangeMinimum to express the minimum possible timestamp.
// toTimestamp - end of range. You can use TimeRangeFull or TimeRangeMaximum to express the maximum possible timestamp.
// aggType - aggregation type
// bucketSizeSec - time bucket for aggregation
// Deprecated: This function has been deprecated, use RangeWithOptions instead
func (client *Client) AggRange(key string, fromTimestamp int64, toTimestamp int64, aggType AggregationType,
bucketSizeSec int) (dataPoints []DataPoint, err error) {
rangeOptions := NewRangeOptions()
rangeOptions = rangeOptions.SetAggregation(aggType, bucketSizeSec)
return client.RangeWithOptions(key, fromTimestamp, toTimestamp, *rangeOptions)
}
// RangeWithOptions - Query a timestamp range on a specific time-series
// args:
// key - time-series key name
// fromTimestamp - start of range. You can use TimeRangeMinimum to express the minimum possible timestamp.
// toTimestamp - end of range. You can use TimeRangeFull or TimeRangeMaximum to express the maximum possible timestamp.
// rangeOptions - RangeOptions options. You can use the default DefaultRangeOptions
func (client *Client) RangeWithOptions(key string, fromTimestamp int64, toTimestamp int64, rangeOptions RangeOptions) (dataPoints []DataPoint, err error) {
return client.rangeWithOptions(RANGE_CMD, key, fromTimestamp, toTimestamp, rangeOptions)
}
// ReverseRangeWithOptions - Query a timestamp range on a specific time-series in reverse order
// args:
// key - time-series key name
// fromTimestamp - start of range. You can use TimeRangeMinimum to express the minimum possible timestamp.
// toTimestamp - end of range. You can use TimeRangeFull or TimeRangeMaximum to express the maximum possible timestamp.
// rangeOptions - RangeOptions options. You can use the default DefaultRangeOptions
func (client *Client) ReverseRangeWithOptions(key string, fromTimestamp int64, toTimestamp int64, rangeOptions RangeOptions) (dataPoints []DataPoint, err error) {
return client.rangeWithOptions(REVRANGE_CMD, key, fromTimestamp, toTimestamp, rangeOptions)
}
// rangeWithOptions - Query a timestamp range on a specific time-series in some order
// args:
// command - range command to run
// key - time-series key name
// fromTimestamp - start of range. You can use TimeRangeMinimum to express the minimum possible timestamp.
// toTimestamp - end of range. You can use TimeRangeFull or TimeRangeMaximum to express the maximum possible timestamp.
// rangeOptions - RangeOptions options. You can use the default DefaultRangeOptions
func (client *Client) rangeWithOptions(command string, key string, fromTimestamp int64, toTimestamp int64, rangeOptions RangeOptions) (dataPoints []DataPoint, err error) {
conn := client.Pool.Get()
defer conn.Close()
var reply interface{}
args := createRangeCmdArguments(key, fromTimestamp, toTimestamp, rangeOptions)
reply, err = conn.Do(command, args...)
if err != nil {
return
}
dataPoints, err = ParseDataPoints(reply)
return
}
// AggMultiRange - Query a timestamp range across multiple time-series by filters.
// args:
// fromTimestamp - start of range. You can use TimeRangeMinimum to express the minimum possible timestamp.
// toTimestamp - end of range. You can use TimeRangeFull or TimeRangeMaximum to express the maximum possible timestamp.
// aggType - aggregation type
// bucketSizeSec - time bucket for aggregation
// filters - list of filters e.g. "a=bb", "b!=aa"
// Deprecated: This function has been deprecated, use MultiRangeWithOptions instead
func (client *Client) AggMultiRange(fromTimestamp int64, toTimestamp int64, aggType AggregationType,
bucketSizeSec int, filters ...string) (ranges []Range, err error) {
mrangeOptions := NewMultiRangeOptions()
mrangeOptions = mrangeOptions.SetAggregation(aggType, bucketSizeSec)
return client.MultiRangeWithOptions(fromTimestamp, toTimestamp, *mrangeOptions, filters...)
}
// MultiRangeWithOptions - Query a timestamp range across multiple time-series by filters.
// args:
// fromTimestamp - start of range. You can use TimeRangeMinimum to express the minimum possible timestamp.
// toTimestamp - end of range. You can use TimeRangeFull or TimeRangeMaximum to express the maximum possible timestamp.
// mrangeOptions - MultiRangeOptions options. You can use the default DefaultMultiRangeOptions
// filters - list of filters e.g. "a=bb", "b!=aa"
func (client *Client) MultiRangeWithOptions(fromTimestamp int64, toTimestamp int64, mrangeOptions MultiRangeOptions, filters ...string) (ranges []Range, err error) {
return client.multiRangeWithOptions(MRANGE_CMD, fromTimestamp, toTimestamp, mrangeOptions, filters)
}
// MultiReverseRangeWithOptions - Query a timestamp range across multiple time-series by filters, in reverse direction.
// args:
// fromTimestamp - start of range. You can use TimeRangeMinimum to express the minimum possible timestamp.
// toTimestamp - end of range. You can use TimeRangeFull or TimeRangeMaximum to express the maximum possible timestamp.
// mrangeOptions - MultiRangeOptions options. You can use the default DefaultMultiRangeOptions
// filters - list of filters e.g. "a=bb", "b!=aa"
func (client *Client) MultiReverseRangeWithOptions(fromTimestamp int64, toTimestamp int64, mrangeOptions MultiRangeOptions, filters ...string) (ranges []Range, err error) {
return client.multiRangeWithOptions(MREVRANGE_CMD, fromTimestamp, toTimestamp, mrangeOptions, filters)
}
func (client *Client) multiRangeWithOptions(cmd string, fromTimestamp int64, toTimestamp int64, mrangeOptions MultiRangeOptions, filters []string) (ranges []Range, err error) {
conn := client.Pool.Get()
defer conn.Close()
var reply interface{}
args := createMultiRangeCmdArguments(fromTimestamp, toTimestamp, mrangeOptions, filters)
reply, err = conn.Do(cmd, args...)
if err != nil {
return
}
ranges, err = ParseRanges(reply)
return
}
// Get - Get the last sample of a time-series.
// args:
// key - time-series key name
func (client *Client) Get(key string) (dataPoint *DataPoint,
err error) {
conn := client.Pool.Get()
defer conn.Close()
resp, err := conn.Do(GET_CMD, key)
if err != nil {
return nil, err
}
dataPoint, err = ParseDataPoint(resp)
return
}
// MultiGet - Get the last sample across multiple time-series, matching the specific filters.
// args:
// filters - list of filters e.g. "a=bb", "b!=aa"
func (client *Client) MultiGet(filters ...string) (ranges []Range, err error) {
return client.MultiGetWithOptions(DefaultMultiGetOptions, filters...)
}
// MultiGetWithOptions - Get the last samples matching the specific filters.
// args:
// multiGetOptions - MultiGetOptions options. You can use the default DefaultMultiGetOptions
// filters - list of filters e.g. "a=bb", "b!=aa"
func (client *Client) MultiGetWithOptions(multiGetOptions MultiGetOptions, filters ...string) (ranges []Range, err error) {
conn := client.Pool.Get()
defer conn.Close()
var reply interface{}
if len(filters) == 0 {
return
}
args := createMultiGetCmdArguments(multiGetOptions, filters)
reply, err = conn.Do(MGET_CMD, args...)
if err != nil {
return
}
ranges, err = ParseRangesSingleDataPoint(reply)
return
}
// Returns information and statistics on the time-series.
// args:
// key - time-series key name
func (client *Client) Info(key string) (res KeyInfo, err error) {
conn := client.Pool.Get()
defer conn.Close()
res, err = ParseInfo(conn.Do(INFO_CMD, key))
return res, err
}
// Get all the keys matching the filter list.
func (client *Client) QueryIndex(filters ...string) (keys []string, err error) {
conn := client.Pool.Get()
defer conn.Close()
if len(filters) == 0 {
return
}
args := redis.Args{}
for _, filter := range filters {
args = args.Add(filter)
}
return redis.Strings(conn.Do(QUERYINDEX_CMD, args...))
}
// Creates a new sample that increments the latest sample's value
func (client *Client) IncrBy(key string, timestamp int64, value float64, options CreateOptions) (int64, error) {
conn := client.Pool.Get()
defer conn.Close()
args, err := AddCounterArgs(key, timestamp, value, options)
if err != nil {
return -1, err
}
return redis.Int64(conn.Do(INCRBY_CMD, args...))
}
// Creates a new sample that increments the latest sample's value with an auto timestamp
func (client *Client) IncrByAutoTs(key string, value float64, options CreateOptions) (int64, error) {
conn := client.Pool.Get()
defer conn.Close()
args, err := AddCounterArgs(key, -1, value, options)
if err != nil {
return -1, err
}
return redis.Int64(conn.Do(INCRBY_CMD, args...))
}
// Creates a new sample that decrements the latest sample's value
func (client *Client) DecrBy(key string, timestamp int64, value float64, options CreateOptions) (int64, error) {
conn := client.Pool.Get()
defer conn.Close()
args, err := AddCounterArgs(key, timestamp, value, options)
if err != nil {
return -1, err
}
return redis.Int64(conn.Do(DECRBY_CMD, args...))
}
// Creates a new sample that decrements the latest sample's value with auto timestamp
func (client *Client) DecrByAutoTs(key string, value float64, options CreateOptions) (int64, error) {
conn := client.Pool.Get()
defer conn.Close()
args, err := AddCounterArgs(key, -1, value, options)
if err != nil {
return -1, err
}
return redis.Int64(conn.Do(DECRBY_CMD, args...))
}
// Add counter args for command TS.INCRBY/TS.DECRBY
func AddCounterArgs(key string, timestamp int64, value float64, options CreateOptions) (redis.Args, error) {
args := redis.Args{key, value}
if timestamp > 0 {
args = args.Add("TIMESTAMP", timestamp)
}
return options.Serialize(args)
}
// Append new samples to a list of series.
func (client *Client) MultiAdd(samples ...Sample) (timestamps []interface{}, err error) {
conn := client.Pool.Get()
defer conn.Close()
if len(samples) == 0 {
return
}
args := redis.Args{}
for _, sample := range samples {
args = args.Add(sample.Key, sample.DataPoint.Timestamp, sample.DataPoint.Value)
}
return redis.Values(conn.Do(MADD_CMD, args...))
}