-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeneral.go
207 lines (187 loc) · 4.56 KB
/
general.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
package redists
import (
"context"
"fmt"
"reflect"
"strconv"
"time"
)
type Rules map[string]Aggregation
func parseRules(is []interface{}) map[string]Aggregation {
rs := make(map[string]Aggregation)
for _, v := range is {
is := v.([]interface{})
rs[parseString(is[0])] = Aggregation{
Bucket: time.Duration(is[1].(int64)) * time.Millisecond,
Type: parseAggregationType(is[2]),
}
}
return rs
}
type ChunkInfo struct {
StartTimestamp time.Time
EndTimestamp time.Time
Samples int64
Size int64
BytesPerSample float64
}
func parseChunkInfo(is []interface{}) ChunkInfo {
var inf ChunkInfo
for i := 0; i < len(is); i += 2 {
key := parseString(is[i])
val := is[i+1]
if val == nil {
continue
}
switch key {
case "startTimestamp":
inf.StartTimestamp = time.UnixMilli(val.(int64))
case "endTimestamp":
inf.EndTimestamp = time.UnixMilli(val.(int64))
case "samples":
inf.Samples = val.(int64)
case "size":
inf.Size = val.(int64)
case "bytesPerSample":
inf.BytesPerSample, _ = strconv.ParseFloat(parseString(val), 64)
}
}
return inf
}
type Info struct {
TotalSamples int64
MemoryUsage int64
FirstTimestamp time.Time
LastTimestamp time.Time
RetentionTime time.Duration
ChunkCount int64
ChunkSize int64
ChunkType Encoding
DuplicatePolicy *DuplicatePolicy
Labels Labels
SourceKey string
Rules Rules
Chunks []ChunkInfo
}
func parseInfo(is []interface{}) Info {
var inf Info
for i := 0; i < len(is); i += 2 {
key := parseString(is[i])
val := is[i+1]
if val == nil {
continue
}
// some clients (e.g. radix) decode nil as []uint8(nil) instead of nil(nil)
if v := reflect.ValueOf(val); v.Kind() == reflect.Slice && v.IsNil() {
continue
}
switch key {
case "totalSamples":
inf.TotalSamples = val.(int64)
case "memoryUsage":
inf.MemoryUsage = val.(int64)
case "firstTimestamp":
inf.FirstTimestamp = time.UnixMilli(val.(int64))
case "lastTimestamp":
inf.LastTimestamp = time.UnixMilli(val.(int64))
case "retentionTime":
inf.RetentionTime = time.Duration(val.(int64)) * time.Millisecond
case "chunkCount":
inf.ChunkCount = val.(int64)
case "chunkSize":
inf.ChunkSize = val.(int64)
case "chunkType":
inf.ChunkType = parseEncoding(val)
case "duplicatePolicy":
policy := parseDuplicatePolicy(val)
inf.DuplicatePolicy = &policy
case "labels":
inf.Labels = parseLabels(val.([]interface{}))
case "sourceKey":
inf.SourceKey = parseString(val)
case "rules":
inf.Rules = parseRules(val.([]interface{}))
case "Chunks":
inf.Chunks = []ChunkInfo{}
for _, v := range val.([]interface{}) {
inf.Chunks = append(inf.Chunks, parseChunkInfo(v.([]interface{})))
}
}
}
return inf
}
func parseString(val interface{}) string {
switch val.(type) {
case []byte:
return string(val.([]byte))
case string: // some clients decodes values as string
return val.(string)
default:
panic(fmt.Sprintf("val %T not convertible to string", val))
}
}
type cmdInfo struct {
key string
debug bool
}
func newCmdInfo(key string) *cmdInfo {
return &cmdInfo{key: key}
}
func (c *cmdInfo) Name() string {
return "TS.INFO"
}
func (c *cmdInfo) Args() []interface{} {
args := []interface{}{c.key}
if c.debug {
args = append(args, optionNameDebug)
}
return args
}
type OptionInfo func(cmd *cmdInfo)
// Info returns information and statistics on the time-series.
func (c *Client) Info(ctx context.Context, key string, options ...OptionInfo) (Info, error) {
cmd := newCmdInfo(key)
for i := range options {
options[i](cmd)
}
i, err := c.d.Do(ctx, cmd.Name(), cmd.Args()...)
var inf Info
if is, ok := i.([]interface{}); ok {
inf = parseInfo(is)
}
return inf, err
}
func InfoWithDebug() OptionInfo {
return func(cmd *cmdInfo) {
cmd.debug = true
}
}
type cmdQueryIndex struct {
filters []Filter
}
func newCmdQueryIndex(filters []Filter) *cmdQueryIndex {
return &cmdQueryIndex{filters: filters}
}
func (c *cmdQueryIndex) Name() string {
return "TS.QUERYINDEX"
}
func (c *cmdQueryIndex) Args() []interface{} {
args := []interface{}{}
for _, f := range c.filters {
args = append(args, f.Arg())
}
return args
}
// QueryIndex lists all the keys matching the filter list.
func (c *Client) QueryIndex(ctx context.Context, filters []Filter) ([]string, error) {
cmd := newCmdQueryIndex(filters)
res, err := c.d.Do(ctx, cmd.Name(), cmd.Args()...)
var keys []string
if is, ok := res.([]interface{}); ok {
keys = make([]string, len(is))
for i := range is {
keys[i] = parseString(is[i])
}
}
return keys, err
}