-
Notifications
You must be signed in to change notification settings - Fork 18
/
range.go
88 lines (78 loc) · 2.74 KB
/
range.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
package redis_timeseries_go
import (
"fmt"
"strconv"
)
// MultiRangeOptions represent the options for querying across multiple time-series
type RangeOptions struct {
AggType AggregationType
TimeBucket int
Count int64
Align int64
FilterByTs []int64
FilterByValueMin *float64
FilterByValueMax *float64
}
func NewRangeOptions() *RangeOptions {
return &RangeOptions{
AggType: "",
TimeBucket: -1,
Count: -1,
Align: -1,
FilterByTs: []int64{},
FilterByValueMin: nil,
FilterByValueMax: nil,
}
}
// DefaultRangeOptions are the default options for querying across a time-series range
var DefaultRangeOptions = *NewRangeOptions()
func (rangeopts *RangeOptions) SetCount(count int64) *RangeOptions {
rangeopts.Count = count
return rangeopts
}
// SetAlign sets the time bucket alignment control for AGGREGATION.
// This will control the time bucket timestamps by changing the reference timestamp on which a bucket is defined.
func (rangeopts *RangeOptions) SetAlign(byTimeStamp int64) *RangeOptions {
rangeopts.Align = byTimeStamp
return rangeopts
}
// SetFilterByTs sets a list of timestamps to filter the result by specific timestamps
func (rangeopts *RangeOptions) SetFilterByTs(filterByTS []int64) *RangeOptions {
rangeopts.FilterByTs = filterByTS
return rangeopts
}
// SetFilterByValue filters result by value using minimum and maximum ( inclusive )
func (rangeopts *RangeOptions) SetFilterByValue(min, max float64) *RangeOptions {
rangeopts.FilterByValueMin = &min
rangeopts.FilterByValueMax = &max
return rangeopts
}
func (rangeopts *RangeOptions) SetAggregation(aggType AggregationType, timeBucket int) *RangeOptions {
rangeopts.AggType = aggType
rangeopts.TimeBucket = timeBucket
return rangeopts
}
func createRangeCmdArguments(key string, fromTimestamp int64, toTimestamp int64, rangeOptions RangeOptions) []interface{} {
args := []interface{}{key, strconv.FormatInt(fromTimestamp, 10), strconv.FormatInt(toTimestamp, 10)}
if rangeOptions.FilterByValueMin != nil {
args = append(args, "FILTER_BY_VALUE",
fmt.Sprintf("%f", *rangeOptions.FilterByValueMin),
fmt.Sprintf("%f", *rangeOptions.FilterByValueMax))
}
if len(rangeOptions.FilterByTs) > 0 {
args = append(args, "FILTER_BY_TS")
for _, timestamp := range rangeOptions.FilterByTs {
args = append(args, strconv.FormatInt(timestamp, 10))
}
}
if rangeOptions.AggType != "" {
args = append(args, "AGGREGATION", rangeOptions.AggType, strconv.Itoa(rangeOptions.TimeBucket))
}
if rangeOptions.Count != -1 {
args = append(args, "COUNT", strconv.FormatInt(rangeOptions.Count, 10))
}
if rangeOptions.Align != -1 {
args = append(args, "ALIGN", strconv.FormatInt(rangeOptions.Align, 10))
}
return args
}