forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_by.go
160 lines (150 loc) · 3.79 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
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
package kapacitor
import (
"log"
"sort"
"sync"
"time"
"github.com/influxdata/kapacitor/expvar"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/pipeline"
"github.com/influxdata/kapacitor/tick/ast"
)
type GroupByNode struct {
node
g *pipeline.GroupByNode
dimensions []string
allDimensions bool
byName 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,
byName: n.ByMeasurementFlag,
}
gn.node.runF = gn.runGroupBy
gn.allDimensions, gn.dimensions = determineDimensions(n.Dimensions)
return gn, nil
}
func (g *GroupByNode) runGroupBy([]byte) error {
dims := models.Dimensions{
ByName: g.g.ByMeasurementFlag,
}
switch g.Wants() {
case pipeline.StreamEdge:
dims.TagNames = g.dimensions
for pt, ok := g.ins[0].NextPoint(); ok; pt, ok = g.ins[0].NextPoint() {
g.timer.Start()
pt = setGroupOnPoint(pt, g.allDimensions, dims, g.g.ExcludedDimensions)
g.timer.Stop()
for _, child := range g.outs {
err := child.CollectPoint(pt)
if err != nil {
return err
}
}
}
default:
var mu sync.RWMutex
var lastTime time.Time
groups := make(map[models.GroupID]*models.Batch)
valueF := func() int64 {
mu.RLock()
l := len(groups)
mu.RUnlock()
return int64(l)
}
g.statMap.Set(statCardinalityGauge, expvar.NewIntFuncGauge(valueF))
for b, ok := g.ins[0].NextBatch(); ok; b, ok = g.ins[0].NextBatch() {
g.timer.Start()
if !b.TMax.Equal(lastTime) {
lastTime = b.TMax
// Emit all groups
mu.RLock()
for id, group := range groups {
for _, child := range g.outs {
err := child.CollectBatch(*group)
if err != nil {
return err
}
}
mu.RUnlock()
mu.Lock()
// Remove from groups
delete(groups, id)
mu.Unlock()
mu.RLock()
}
mu.RUnlock()
}
for _, p := range b.Points {
if g.allDimensions {
dims.TagNames = filterExcludedDimensions(p.Tags, dims, g.g.ExcludedDimensions)
} else {
dims.TagNames = g.dimensions
}
groupID := models.ToGroupID(b.Name, p.Tags, dims)
mu.RLock()
group, ok := groups[groupID]
mu.RUnlock()
if !ok {
tags := make(map[string]string, len(dims.TagNames))
for _, dim := range dims.TagNames {
tags[dim] = p.Tags[dim]
}
group = &models.Batch{
Name: b.Name,
Group: groupID,
TMax: b.TMax,
ByName: b.ByName,
Tags: tags,
}
mu.Lock()
groups[groupID] = group
mu.Unlock()
}
group.Points = append(group.Points, p)
}
g.timer.Stop()
}
}
return nil
}
func determineDimensions(dimensions []interface{}) (allDimensions bool, realDimensions []string) {
for _, dim := range dimensions {
switch d := dim.(type) {
case string:
realDimensions = append(realDimensions, d)
case *ast.StarNode:
allDimensions = true
}
}
sort.Strings(realDimensions)
return
}
func filterExcludedDimensions(tags models.Tags, dimensions models.Dimensions, excluded []string) []string {
dimensions.TagNames = models.SortedKeys(tags)
filtered := dimensions.TagNames[0:0]
for _, t := range dimensions.TagNames {
found := false
for _, x := range excluded {
if x == t {
found = true
break
}
}
if !found {
filtered = append(filtered, t)
}
}
return filtered
}
func setGroupOnPoint(p models.Point, allDimensions bool, dimensions models.Dimensions, excluded []string) models.Point {
if allDimensions {
dimensions.TagNames = filterExcludedDimensions(p.Tags, dimensions, excluded)
}
p.Group = models.ToGroupID(p.Name, p.Tags, dimensions)
p.Dimensions = dimensions
return p
}