This repository has been archived by the owner on Sep 11, 2018. It is now read-only.
forked from cloudflare/unsee
/
filter.go
120 lines (103 loc) · 2.83 KB
/
filter.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
package filters
import (
"fmt"
"regexp"
"github.com/cloudflare/unsee/internal/models"
"github.com/cloudflare/unsee/internal/slices"
)
// FilterT provides methods for interacting with alert filters
type FilterT interface {
init(name string, matcher *matcherT, rawText string, isValid bool, value string)
Match(alert *models.Alert, matches int) bool
GetRawText() string
GetHits() int
GetIsValid() bool
}
type alertFilter struct {
FilterT
Matched string
Matcher matcherT
RawText string
Value interface{}
IsValid bool
Hits int
}
func (filter *alertFilter) init(name string, matcher *matcherT, rawText string, isValid bool, value string) {
filter.Matched = name
if matcher != nil {
filter.Matcher = *matcher
}
filter.RawText = rawText
filter.IsValid = isValid
filter.Value = value
}
func (filter *alertFilter) GetRawText() string {
return filter.RawText
}
func (filter *alertFilter) GetHits() int {
return filter.Hits
}
func (filter *alertFilter) GetIsValid() bool {
return filter.IsValid
}
type newFilterFactory func() FilterT
// NewFilter creates new filter object from filter expression like "key=value"
// expression will be parsed and best filter implementation and value matcher
// will be selected
func NewFilter(expression string) FilterT {
invalid := alwaysInvalidFilter{}
invalid.init("", nil, expression, false, expression)
reExp := fmt.Sprintf("^(?P<matched>(%s))(?P<operator>(%s))(?P<value>(.*))", filterRegex, matcherRegex)
re := regexp.MustCompile(reExp)
match := re.FindStringSubmatch(expression)
result := make(map[string]string)
for i, name := range re.SubexpNames() {
if name != "" && i > 0 && i <= len(match) {
result[name] = match[i]
}
}
matched, _ := result["matched"]
operator, _ := result["operator"]
value, _ := result["value"]
if matched == "" && operator == "" && value == "" {
// no "filter=" part, just the value, use fuzzy filter
f := newFuzzyFilter()
matcher, err := newMatcher(regexpOperator)
if err != nil {
f.init("", nil, expression, false, expression)
} else {
f.init("", &matcher, expression, true, expression)
}
return f
}
if value == "" {
// there's no value, so it's always invalid
return &invalid
}
if operator == "" {
// no operator, no valid filter here
return &invalid
}
// we have "filter=" part, lookup filter that matches
for _, fc := range AllFilters {
f := fc.Factory()
if !fc.LabelRe.MatchString(matched) {
// filter name doesn't match, keep searching
continue
}
if !slices.StringInSlice(fc.SupportedOperators, operator) {
return &invalid
}
matcher, err := newMatcher(operator)
if err != nil {
f.init(matched, nil, expression, false, "")
} else {
if value != "" {
f.init(matched, &matcher, expression, true, value)
return f
}
f.init(matched, &matcher, expression, false, "")
}
}
return &invalid
}