-
Notifications
You must be signed in to change notification settings - Fork 491
/
influxql.gen.go.tmpl
226 lines (204 loc) · 5.26 KB
/
influxql.gen.go.tmpl
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package kapacitor
import (
"fmt"
"time"
"reflect"
"github.com/influxdata/influxdb/query"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/edge"
"github.com/influxdata/kapacitor/pipeline"
)
{{/* Define typed Aggregate/Emit types */}}
{{range .}}
func convert{{.Name}}Point(
name string,
p edge.FieldsTagsTimeGetter,
field string,
isSimpleSelector bool,
topBottomInfo *pipeline.TopBottomCallInfo,
) (*query.{{.Name}}Point, error) {
value, ok := p.Fields()[field]
if !ok {
return nil, fmt.Errorf("field %s missing from point cannot aggregate", field)
}
typed, ok := value.({{.Type}})
if !ok {
return nil, fmt.Errorf("field %s has wrong type: got %T exp {{.Type}}", field, value)
}
ap := &query.{{.Name}}Point{
Name: name,
Tags: query.NewTags(p.Tags()),
Time: p.Time().UnixNano(),
Value: typed,
}
if topBottomInfo != nil {
// We need to populate the Aux fields
{{.name}}PopulateAuxFieldsAndTags(ap, topBottomInfo.FieldsAndTags, p.Fields(), p.Tags())
}
if isSimpleSelector {
ap.Aux = []interface{}{ p.Tags(), p.Fields() }
}
return ap, nil
}
type {{.name}}PointAggregator struct {
field string
topBottomInfo *pipeline.TopBottomCallInfo
isSimpleSelector bool
aggregator query.{{.Name}}PointAggregator
}
func {{.name}}PopulateAuxFieldsAndTags(ap *query.{{.Name}}Point, fieldsAndTags []string, fields models.Fields, tags models.Tags) {
ap.Aux = make([]interface{}, len(fieldsAndTags))
for i, name := range fieldsAndTags {
if f, ok := fields[name]; ok {
ap.Aux[i] = f
} else {
ap.Aux[i] = tags[name]
}
}
}
func (a *{{.name}}PointAggregator) AggregatePoint(name string, p edge.FieldsTagsTimeGetter) error {
ap, err := convert{{.Name}}Point(name, p, a.field, a.isSimpleSelector, a.topBottomInfo)
if err != nil {
return err
}
a.aggregator.Aggregate{{.Name}}(ap)
return nil
}
type {{.name}}PointEmitter struct {
baseReduceContext
emitter query.{{.Name}}PointEmitter
isSimpleSelector bool
}
func (e *{{.name}}PointEmitter) EmitPoint() (edge.PointMessage, error) {
slice := e.emitter.Emit()
if len(slice) != 1 {
return nil, nil
}
ap := slice[0]
var t time.Time
if e.pointTimes {
if ap.Time == query.ZeroTime {
t = e.time
} else {
t = time.Unix(0, ap.Time).UTC()
}
} else {
t = e.time
}
var fields models.Fields
var tags models.Tags
if e.isSimpleSelector {
tags = ap.Aux[0].(models.Tags)
fields = ap.Aux[1].(models.Fields)
if e.as != e.field {
fields = fields.Copy()
fields[e.as] = fields[e.field]
delete(fields, e.field)
}
} else {
tags = e.groupInfo.Tags
fields = map[string]interface{}{e.as: ap.Value}
}
return edge.NewPointMessage(
e.name, "", "",
e.groupInfo.Dimensions,
fields,
tags,
t,
), nil
}
func (e *{{.name}}PointEmitter) EmitBatch() edge.BufferedBatchMessage {
slice := e.emitter.Emit()
begin := edge.NewBeginBatchMessage(
e.name,
e.groupInfo.Tags,
e.groupInfo.Dimensions.ByName,
e.time,
len(slice),
)
points := make([]edge.BatchPointMessage, len(slice))
var t time.Time
for i, ap := range slice {
if e.pointTimes {
if ap.Time == query.ZeroTime {
t = e.time
} else {
t = time.Unix(0, ap.Time).UTC()
}
} else {
t = e.time
}
var tags models.Tags
if l := len(ap.Tags.KeyValues()); l > 0 {
// Merge batch and point specific tags
tags = make(models.Tags, len(e.groupInfo.Tags)+l)
for k, v := range e.groupInfo.Tags {
tags[k] = v
}
for k, v := range ap.Tags.KeyValues() {
tags[k] = v
}
} else {
tags = e.groupInfo.Tags
}
points[i] = edge.NewBatchPointMessage(
models.Fields{e.as: ap.Value},
tags,
t,
)
if t.After(begin.Time()) {
begin.SetTime(t)
}
}
return edge.NewBufferedBatchMessage(
begin,
points,
edge.NewEndBatchMessage(),
)
}
{{end}}
{{/* Define composite types for reduceContext */}}
{{with $types := .}}
{{range $a := $types}}
{{range $e := $types}}
// {{$a.name}}{{if ne $a.Name $e.Name}}{{$e.Name}}{{end}}ReduceContext uses composition to implement the reduceContext interface
type {{$a.name}}{{if ne $a.Name $e.Name}}{{$e.Name}}{{end}}ReduceContext struct {
{{$a.name}}PointAggregator
{{$e.name}}PointEmitter
}
{{end}}{{end}}
{{/* Define switch cases for reduceContext contruction */}}
func determineReduceContextCreateFn(method string, kind reflect.Kind, rc pipeline.ReduceCreater) (fn createReduceContextFunc, err error) {
switch kind {
{{range $a := $types}}
case {{.Kind}}:
switch {
{{range $e := $types}}
case rc.Create{{$a.Name}}{{if ne $a.Name $e.Name}}{{$e.Name}}{{end}}Reducer != nil:
fn = func(c baseReduceContext) reduceContext {
a, e := rc.Create{{$a.Name}}{{if ne $a.Name $e.Name}}{{$e.Name}}{{end}}Reducer()
return &{{$a.name}}{{if ne $a.Name $e.Name}}{{$e.Name}}{{end}}ReduceContext{
{{$a.name}}PointAggregator: {{$a.name}}PointAggregator{
field: c.field,
topBottomInfo: rc.TopBottomCallInfo,
isSimpleSelector: rc.IsSimpleSelector,
aggregator: a,
},
{{$e.name}}PointEmitter: {{$e.name}}PointEmitter{
baseReduceContext: c,
emitter: e,
isSimpleSelector: rc.IsSimpleSelector,
},
}
}
{{end}}
default:
err = fmt.Errorf("cannot apply %s to {{$a.Type}} field", method)
}
{{end}}
default:
err = fmt.Errorf("invalid field kind: %v", kind)
}
return
}
{{end}}