forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.go
74 lines (67 loc) · 1.61 KB
/
sample.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
package kapacitor
import (
"errors"
"log"
"time"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/pipeline"
)
type SampleNode struct {
node
s *pipeline.SampleNode
counts map[models.GroupID]int64
duration time.Duration
}
// Create a new SampleNode which filters data from a source.
func newSampleNode(et *ExecutingTask, n *pipeline.SampleNode, l *log.Logger) (*SampleNode, error) {
sn := &SampleNode{
node: node{Node: n, et: et, logger: l},
s: n,
counts: make(map[models.GroupID]int64),
duration: n.Duration,
}
sn.node.runF = sn.runSample
if n.Duration == 0 && n.Count == 0 {
return nil, errors.New("invalid sample rate: must be positive integer or duration")
}
return sn, nil
}
func (s *SampleNode) runSample([]byte) error {
switch s.Wants() {
case pipeline.StreamEdge:
for p, ok := s.ins[0].NextPoint(); ok; p, ok = s.ins[0].NextPoint() {
if s.shouldKeep(p.Group, p.Time) {
for _, child := range s.outs {
err := child.CollectPoint(p)
if err != nil {
return err
}
}
}
}
case pipeline.BatchEdge:
for b, ok := s.ins[0].NextBatch(); ok; b, ok = s.ins[0].NextBatch() {
if s.shouldKeep(b.Group, b.TMax) {
for _, child := range s.outs {
err := child.CollectBatch(b)
if err != nil {
return err
}
}
}
}
}
return nil
}
func (s *SampleNode) shouldKeep(group models.GroupID, t time.Time) bool {
if s.duration != 0 {
keepTime := t.Truncate(s.duration)
return t.Equal(keepTime)
} else {
count := s.counts[group]
keep := count%s.s.Count == 0
count++
s.counts[group] = count
return keep
}
}