forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_by.go
71 lines (63 loc) · 1.63 KB
/
group_by.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
package kapacitor
import (
"log"
"sort"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/pipeline"
"github.com/influxdata/kapacitor/tick"
)
type GroupByNode struct {
node
g *pipeline.GroupByNode
dimensions []string
allDimensions bool
}
// Create a new GroupByNode which splits the stream dynamically based on the specified dimensions.
func newGroupByNode(et *ExecutingTask, n *pipeline.GroupByNode, l *log.Logger) (*GroupByNode, error) {
gn := &GroupByNode{
node: node{Node: n, et: et, logger: l},
g: n,
}
gn.node.runF = gn.runGroupBy
gn.allDimensions, gn.dimensions = determineDimensions(n.Dimensions)
return gn, nil
}
func (g *GroupByNode) runGroupBy([]byte) error {
switch g.Wants() {
case pipeline.StreamEdge:
for pt, ok := g.ins[0].NextPoint(); ok; pt, ok = g.ins[0].NextPoint() {
pt = setGroupOnPoint(pt, g.allDimensions, g.dimensions)
for _, child := range g.outs {
err := child.CollectPoint(pt)
if err != nil {
return err
}
}
}
default:
panic("not implemented")
}
return nil
}
func determineDimensions(dimensions []interface{}) (allDimensions bool, realDimensions []string) {
DIMS:
for _, dim := range dimensions {
switch d := dim.(type) {
case string:
realDimensions = append(realDimensions, d)
case *tick.StarNode:
allDimensions = true
break DIMS
}
}
sort.Strings(realDimensions)
return
}
func setGroupOnPoint(p models.Point, allDimensions bool, dimensions []string) models.Point {
if allDimensions {
dimensions = models.SortedKeys(p.Tags)
}
p.Group = models.TagsToGroupID(dimensions, p.Tags)
p.Dimensions = dimensions
return p
}