/
query.go
186 lines (151 loc) · 3.87 KB
/
query.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package straumur
import (
"strconv"
"strings"
"time"
"unicode"
)
type MatchArray [2][]string
// Given two string arrays, returns true if the
// value array contains all of the query array's values
func (m MatchArray) Match() bool {
qArr := m[0]
eArr := m[1]
//both are empty, ignore
if len(qArr) == 0 && len(eArr) == 0 {
return true
}
if len(qArr) > 0 {
allMatch := true
for _, s := range qArr {
if !stringInSlice(s, eArr) {
allMatch = false
}
}
if !allMatch {
return false
}
}
return true
}
// Returns true if a string is in the slice
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
// Returns true if any of the OR delimited q string matches e:
// orMatchStringAny("foo OR bar or baz", "baz") // returns true
// orMatchStringAny("foo", "foo") // returns true
// orMatchStringAny(" foo ", "foo") // returns true
// orMatchStringAny("foo OR bar or baz", "moo") // returns false
func orMatchStringAny(q, e string) bool {
for _, s := range strings.Split(q, "OR") {
s = strings.TrimSpace(s)
if e == s {
return true
}
}
return false
}
//TODO: Created/Updated, lt & gt
//TODO: Importance, 1+OR+3 gt1 lt4. single param
//TODO: Sort & direction
type Query struct {
Origin string `url:"origin,omitempty" schema:"origin" json:"origin"`
Key string `url:"key,omitempty" schema:"key" json:"key"`
Entities []string `url:"entities,omitempty" schema:"entities" json:"entities"`
OtherReferences []string `url:"other_references,omitempty" schema:"other_references" json:"other_references"`
Actors []string `url:"actors,omitempty" schema:"actors" json:"actors"`
Tags []string `url:"tags,omitempty" schema:"tags" json:"tags"`
Importance string `url:"importance,omitempty" schema:"importance" json:"importance"`
From time.Time `url:"from,omitempty" schema:"from" json:"from"`
To time.Time `url:"to,omitempty" schema:"to" json:"to"`
}
// Return true if s is a valid array type
func (q *Query) IsValidArrayType(s string) bool {
return stringInSlice(s, []string{"entities", "other_references", "actors", "tags"})
}
// Returns true if the query values are empty
func (q *Query) IsEmpty() bool {
for _, s := range []string{q.Origin, q.Key, q.Importance} {
if s != "" {
return false
}
}
for _, arr := range [][]string{q.Entities, q.OtherReferences, q.Actors, q.Tags} {
if len(arr) > 0 {
return false
}
}
if !q.From.IsZero() {
return false
}
if !q.To.IsZero() {
return false
}
return true
}
func (q *Query) matchImportance(i int) bool {
if q.Importance == "" {
return true
}
val, err := strconv.Atoi(strings.TrimFunc(q.Importance, unicode.IsLetter))
if err != nil {
//maybe don't fatal
logger.Criticalf("%+v", err)
return false
}
switch strings.TrimFunc(q.Importance, unicode.IsDigit) {
case "gte":
return (i >= val)
case "gt":
return (i > val)
case "lte":
return (i <= val)
case "lt":
return (i < val)
}
return (val == i)
}
// Determines whether an event matched the query
func (q *Query) Match(e Event) bool {
// empty query matches everything
if q.IsEmpty() {
return true
}
orPairs := [][2]string{
{q.Origin, e.Origin},
{q.Key, e.Key},
}
for _, pair := range orPairs {
//should orMatchStringAny return true if q (pair[0]) is empty?
if pair[0] != "" && !orMatchStringAny(pair[0], pair[1]) {
return false
}
}
arrays := []MatchArray{
{q.Entities, e.Entities},
{q.OtherReferences, e.OtherReferences},
{q.Actors, e.Actors},
{q.Tags, e.Tags},
}
for _, ma := range arrays {
if !ma.Match() {
return false
}
}
if !q.matchImportance(e.Importance) {
return false
}
if !q.From.IsZero() && e.Created.Before(q.From) {
return false
}
if !q.To.IsZero() && e.Created.After(q.To) {
return false
}
return true
}