forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_out.go
124 lines (107 loc) · 2.74 KB
/
http_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
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
package kapacitor
import (
"encoding/json"
"net/http"
"path"
"sync"
"github.com/influxdb/influxdb/influxql"
"github.com/influxdb/kapacitor/models"
"github.com/influxdb/kapacitor/pipeline"
"github.com/influxdb/kapacitor/services/httpd"
)
type HTTPOutNode struct {
node
c *pipeline.HTTPOutNode
result influxql.Result
groupSeriesIdx map[models.GroupID]int
endpoint string
routes []httpd.Route
mu sync.Mutex
}
// Create a new HTTPOutNode which caches the most recent item and exposes it over the HTTP API.
func newHTTPOutNode(et *ExecutingTask, n *pipeline.HTTPOutNode) (*HTTPOutNode, error) {
hn := &HTTPOutNode{
node: node{Node: n, et: et},
c: n,
groupSeriesIdx: make(map[models.GroupID]int),
}
et.registerOutput(hn.c.Endpoint, hn)
hn.node.runF = hn.runOut
hn.node.stopF = hn.stopOut
return hn, nil
}
func (h *HTTPOutNode) Endpoint() string {
return "http://" + h.endpoint
}
func (h *HTTPOutNode) runOut() error {
hndl := func(w http.ResponseWriter, req *http.Request) {
h.mu.Lock()
defer h.mu.Unlock()
if b, err := json.Marshal(h.result); err != nil {
httpd.HttpError(
w,
err.Error(),
true,
http.StatusInternalServerError,
)
} else {
w.Write(b)
}
}
p := path.Join("/", h.et.Task.Name, h.c.Endpoint)
r := []httpd.Route{{
Name: h.Name(),
Method: "GET",
Pattern: p,
Gzipped: true,
Log: true,
HandlerFunc: hndl,
}}
h.endpoint = h.et.tm.HTTPDService.Addr().String() + httpd.APIRoot + p
h.routes = r
err := h.et.tm.HTTPDService.AddRoutes(r)
if err != nil {
return err
}
switch h.Wants() {
case pipeline.StreamEdge:
for p, ok := h.ins[0].NextPoint(); ok; p, ok = h.ins[0].NextPoint() {
h.updateResultWithPoint(p)
}
case pipeline.BatchEdge:
for b, ok := h.ins[0].NextBatch(); ok; b, ok = h.ins[0].NextBatch() {
h.updateResultWithBatch(b)
}
}
return nil
}
// Update the result structure with a single point.
func (h *HTTPOutNode) updateResultWithPoint(p models.Point) {
h.mu.Lock()
defer h.mu.Unlock()
row := models.PointToRow(p)
if idx, ok := h.groupSeriesIdx[p.Group]; !ok {
idx = len(h.result.Series)
h.groupSeriesIdx[p.Group] = idx
h.result.Series = append(h.result.Series, row)
} else {
h.result.Series[idx] = row
}
}
// Update the result structure with a batch.
func (h *HTTPOutNode) updateResultWithBatch(b models.Batch) {
h.mu.Lock()
defer h.mu.Unlock()
row := models.BatchToRow(b)
idx, ok := h.groupSeriesIdx[b.Group]
if !ok {
idx = len(h.result.Series)
h.groupSeriesIdx[b.Group] = idx
h.result.Series = append(h.result.Series, row)
} else {
h.result.Series[idx] = row
}
}
func (h *HTTPOutNode) stopOut() {
h.et.tm.HTTPDService.DelRoutes(h.routes)
}