forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
where.go
74 lines (68 loc) · 1.45 KB
/
where.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
package pipeline
import (
"encoding/json"
"fmt"
"github.com/influxdata/kapacitor/tick/ast"
)
// The WhereNode filters the data stream by a given expression.
//
// Example:
// var sums = stream
// |from()
// .groupBy('service', 'host')
// |sum('value')
// //Watch particular host for issues.
// sums
// |where(lambda: "host" == 'h001.example.com')
// |alert()
// .crit(lambda: TRUE)
// .email().to('user@example.com')
//
type WhereNode struct {
chainnode `json:"-"`
// The expression predicate.
// tick:ignore
Lambda *ast.LambdaNode `json:"lambda"`
}
func newWhereNode(wants EdgeType, predicate *ast.LambdaNode) *WhereNode {
return &WhereNode{
chainnode: newBasicChainNode("where", wants, wants),
Lambda: predicate,
}
}
// MarshalJSON converts WhereNode to JSON
// tick:ignore
func (n *WhereNode) MarshalJSON() ([]byte, error) {
type Alias WhereNode
var raw = &struct {
TypeOf
*Alias
}{
TypeOf: TypeOf{
Type: "where",
ID: n.ID(),
},
Alias: (*Alias)(n),
}
return json.Marshal(raw)
}
// UnmarshalJSON converts JSON to an WhereNode
// tick:ignore
func (n *WhereNode) UnmarshalJSON(data []byte) error {
type Alias WhereNode
var raw = &struct {
TypeOf
*Alias
}{
Alias: (*Alias)(n),
}
err := json.Unmarshal(data, raw)
if err != nil {
return err
}
if raw.Type != "where" {
return fmt.Errorf("error unmarshaling node %d of type %s as WhereNode", raw.ID, raw.Type)
}
n.setID(raw.ID)
return nil
}