-
Notifications
You must be signed in to change notification settings - Fork 1
/
downsample.go
68 lines (56 loc) · 1.75 KB
/
downsample.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
package redists
import (
"context"
"time"
)
type cmdCreateRule struct {
srcKey, destKey string
agg Aggregation
alignTimestamp *time.Time
}
func (c *cmdCreateRule) Name() string {
return "TS.CREATERULE"
}
func (c *cmdCreateRule) Args() []interface{} {
args := []interface{}{c.srcKey, c.destKey, optionNameAggregation, string(c.agg.Type), c.agg.Bucket.Milliseconds()}
if c.alignTimestamp != nil {
args = append(args, c.alignTimestamp.UnixMilli())
}
return args
}
func newCmdCreateRule(srcKey, destKey string, t AggregationType, bucket Duration) *cmdCreateRule {
return &cmdCreateRule{srcKey: srcKey, destKey: destKey, agg: Aggregation{Type: t, Bucket: bucket}}
}
type OptionCreateRule func(cmd *cmdCreateRule)
// CreateRule creates a compaction rule.
func (c *Client) CreateRule(ctx context.Context, srcKey, destKey string, a AggregationType, bucket Duration, options ...OptionCreateRule) error {
cmd := newCmdCreateRule(srcKey, destKey, a, bucket)
for i := range options {
options[i](cmd)
}
_, err := c.d.Do(ctx, cmd.Name(), cmd.Args()...)
return err
}
func CreateRuleWithAlignTimestamp(t time.Time) OptionCreateRule {
return func(cmd *cmdCreateRule) {
cmd.alignTimestamp = &t
}
}
type cmdDeleteRule struct {
srcKey, destKey string
}
func (c *cmdDeleteRule) Name() string {
return "TS.DELETERULE"
}
func (c *cmdDeleteRule) Args() []interface{} {
return []interface{}{c.srcKey, c.destKey}
}
func newCmdDeleteRule(srcKey, destKey string) *cmdDeleteRule {
return &cmdDeleteRule{srcKey: srcKey, destKey: destKey}
}
// DeleteRule deletes a compaction rule.
func (c *Client) DeleteRule(ctx context.Context, srcKey, destKey string) error {
cmd := newCmdDeleteRule(srcKey, destKey)
_, err := c.d.Do(ctx, cmd.Name(), cmd.Args()...)
return err
}