forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.go
134 lines (118 loc) · 2.71 KB
/
batch.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
package kapacitor
import (
"time"
"github.com/influxdb/influxdb/client"
"github.com/influxdb/kapacitor/models"
"github.com/influxdb/kapacitor/pipeline"
)
type BatchNode struct {
node
b *pipeline.BatchNode
query *Query
ticker *time.Ticker
}
func newBatchNode(et *ExecutingTask, n *pipeline.BatchNode) (*BatchNode, error) {
bn := &BatchNode{
node: node{Node: n, et: et},
b: n,
}
bn.node.runF = bn.runBatch
bn.node.stopF = bn.stopBatch
q, err := NewQuery(n.Query)
if err != nil {
return nil, err
}
bn.query = q
err = bn.query.Dimensions(n.Dimensions)
if err != nil {
return nil, err
}
return bn, nil
}
// Query InfluxDB return Edge with data
func (b *BatchNode) Query(batch BatchCollector) {
defer batch.Close()
b.ticker = time.NewTicker(b.b.Period)
for now := range b.ticker.C {
// Update times for query
b.query.Start(now.Add(-1 * b.b.Period))
b.query.Stop(now)
b.logger.Println("D! starting next batch query:", b.query.String())
// Connect
con, err := b.et.tm.InfluxDBService.NewClient()
if err != nil {
b.logger.Println("E! " + err.Error())
break
}
q := client.Query{
Command: b.query.String(),
}
// Execute query
resp, err := con.Query(q)
if err != nil {
b.logger.Println("E! " + err.Error())
return
}
if resp.Err != nil {
b.logger.Println("E! " + resp.Err.Error())
return
}
// Collect batches
for _, res := range resp.Results {
if res.Err != nil {
b.logger.Println("E! " + res.Err.Error())
return
}
for _, series := range res.Series {
groupID := models.TagsToGroupID(
models.SortedKeys(series.Tags),
series.Tags,
)
bch := models.Batch{
Name: series.Name,
Group: groupID,
Tags: series.Tags,
}
bch.Points = make([]models.TimeFields, len(series.Values))
for i, v := range series.Values {
fields := make(models.Fields)
var t time.Time
for i, c := range series.Columns {
if c == "time" {
tStr, ok := v[i].(string)
if !ok {
b.logger.Printf("E! unexpected time value: %v", v[i])
return
}
t, err = time.Parse(time.RFC3339, tStr)
if err != nil {
b.logger.Println("E! unexpected time format: " + err.Error())
return
}
} else {
fields[c] = v[i]
}
}
bch.Points[i] = models.TimeFields{Time: t, Fields: fields}
}
batch.CollectBatch(bch)
}
}
}
}
func (b *BatchNode) stopBatch() {
if b.ticker != nil {
b.ticker.Stop()
}
}
func (b *BatchNode) runBatch() error {
for bt, ok := b.ins[0].NextBatch(); ok; bt, ok = b.ins[0].NextBatch() {
for _, child := range b.outs {
err := child.CollectBatch(bt)
if err != nil {
return err
}
}
}
return nil
}