forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
influxdb_out.go
77 lines (70 loc) · 1.68 KB
/
influxdb_out.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
package kapacitor
import (
"github.com/influxdb/influxdb/client"
"github.com/influxdb/kapacitor/models"
"github.com/influxdb/kapacitor/pipeline"
)
type InfluxDBOutNode struct {
node
i *pipeline.InfluxDBOutNode
}
func newInfluxDBOutNode(et *ExecutingTask, n *pipeline.InfluxDBOutNode) (*InfluxDBOutNode, error) {
in := &InfluxDBOutNode{
node: node{Node: n, et: et},
i: n,
}
in.node.runF = in.runOut
return in, nil
}
func (i *InfluxDBOutNode) runOut() error {
switch i.Wants() {
case pipeline.StreamEdge:
for p, ok := i.ins[0].NextPoint(); ok; p, ok = i.ins[0].NextPoint() {
batch := models.Batch{
Name: p.Name,
Group: p.Group,
Tags: p.Tags,
Points: []models.TimeFields{{Time: p.Time, Fields: p.Fields}},
}
err := i.write(batch)
if err != nil {
return err
}
}
case pipeline.BatchEdge:
for b, ok := i.ins[0].NextBatch(); ok; b, ok = i.ins[0].NextBatch() {
err := i.write(b)
if err != nil {
return err
}
}
}
return nil
}
func (i *InfluxDBOutNode) write(batch models.Batch) error {
i.logger.Println("D! batch", batch)
c, err := i.et.tm.InfluxDBService.NewClient()
if err != nil {
return err
}
points := make([]client.Point, len(batch.Points))
for j, p := range batch.Points {
points[j] = client.Point{
Measurement: i.i.Measurement,
Tags: batch.Tags,
Time: p.Time,
Fields: p.Fields,
Precision: i.i.Precision,
}
}
bp := client.BatchPoints{
Points: points,
Database: i.i.Database,
RetentionPolicy: i.i.RetentionPolicy,
WriteConsistency: i.i.WriteConsistency,
Tags: i.i.Tags,
Precision: i.i.Precision,
}
_, err = c.Write(bp)
return err
}