-
Notifications
You must be signed in to change notification settings - Fork 3
/
expression_processor.go
114 lines (102 loc) · 2.73 KB
/
expression_processor.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
package steps
import (
"bytes"
"github.com/bitflow-stream/go-bitflow/bitflow"
"github.com/bitflow-stream/go-bitflow/script/reg"
)
type ExpressionProcessor struct {
bitflow.NoopProcessor
Filter bool
checker bitflow.HeaderChecker
expressions []*Expression
}
func RegisterExpression(b reg.ProcessorRegistry) {
b.RegisterAnalysisParamsErr("do",
func(p *bitflow.SamplePipeline, params map[string]string) error {
return add_expression(p, params, false)
},
"Execute the given expression on every sample", reg.RequiredParams("expr"))
}
func RegisterFilterExpression(b reg.ProcessorRegistry) {
b.RegisterAnalysisParamsErr("filter",
func(p *bitflow.SamplePipeline, params map[string]string) error {
return add_expression(p, params, true)
},
"Filter the samples based on a boolean expression", reg.RequiredParams("expr"))
}
func add_expression(p *bitflow.SamplePipeline, params map[string]string, filter bool) error {
proc := &ExpressionProcessor{Filter: filter}
err := proc.AddExpression(params["expr"])
if err == nil {
p.Add(proc)
}
return err
}
func (p *ExpressionProcessor) AddExpression(expressionString string) error {
expr, err := NewExpression(expressionString)
if err != nil {
return err
}
p.expressions = append(p.expressions, expr)
return nil
}
func (p *ExpressionProcessor) Sample(sample *bitflow.Sample, header *bitflow.Header) error {
if res, err := p.evaluate(sample, header); err != nil {
return err
} else if res {
return p.NoopProcessor.Sample(sample, header)
}
return nil
}
func (p *ExpressionProcessor) MergeProcessor(otherProcessor bitflow.SampleProcessor) bool {
if other, ok := otherProcessor.(*ExpressionProcessor); !ok {
return false
} else {
if other.Filter != p.Filter {
return false
}
p.expressions = append(p.expressions, other.expressions...)
return true
}
}
func (p *ExpressionProcessor) String() string {
var str bytes.Buffer
for _, expr := range p.expressions {
if str.Len() > 0 {
if p.Filter {
str.WriteString(" && ")
} else {
str.WriteString("; ")
}
}
str.WriteString(expr.expr.String())
}
res := "Expression"
if p.Filter {
res += " filter"
}
return res + ": " + str.String()
}
func (p *ExpressionProcessor) evaluate(sample *bitflow.Sample, header *bitflow.Header) (bool, error) {
if p.checker.HeaderChanged(header) {
for _, expr := range p.expressions {
if err := expr.UpdateHeader(header); err != nil {
return false, err
}
}
}
for _, expr := range p.expressions {
if p.Filter {
if res, err := expr.EvaluateBool(sample, header); err != nil {
return false, err
} else if !res {
return false, nil
}
} else {
if _, err := expr.Evaluate(sample, header); err != nil {
return false, err
}
}
}
return true, nil
}