-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilter.go
152 lines (134 loc) · 3.08 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
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
package justgrep
import (
"context"
"regexp"
"strconv"
"time"
)
type UserMatchType uint8
const (
DontMatch UserMatchType = iota
MatchRegex
MatchExact
)
type Filter struct {
// StartDate < EndDate
StartDate time.Time
EndDate time.Time
HasMessageType bool
MessageTypes []string
HasMessageRegex bool
MessageRegex *regexp.Regexp
UserMatchType UserMatchType
UserRegex *regexp.Regexp
NegativeUserRegex *regexp.Regexp
UserName string
NegativeUserName string
Count int
}
type FilterResult uint8
const (
ResultOk FilterResult = iota
ResultDateBeforeStart
ResultDateAfterEnd
ResultType
ResultContent
ResultUser
ResultMaxCountReached
ResultCount
)
func (res FilterResult) String() string {
switch res {
case ResultOk:
return "ok"
case ResultDateBeforeStart:
return "date before start"
case ResultDateAfterEnd:
return "date after end"
case ResultType:
return "type"
case ResultContent:
return "content"
case ResultUser:
return "user"
case ResultMaxCountReached:
return "limit reached"
default:
return strconv.FormatInt(int64(res), 10)
}
}
// StreamFilter performs Filter on every message from the input channel and puts every message that matched onto the
// output channel, if the max count of results is reached cancel() is called and results[ResultsMaxCountReached] is set.
// If the messages are too old, cancel() is called and results[ResultDateBeforeStart] is set.
func (f Filter) StreamFilter(
cancel context.CancelFunc,
input chan *Message,
output chan *Message,
progress *ProgressState,
) []int {
results := make([]int, ResultCount)
for msg := range input {
if msg == nil {
break
}
if f.Count != 0 && progress.TotalResults[ResultOk]+results[ResultOk] >= f.Count {
results[ResultMaxCountReached] = 1
cancel() // HTTP request is still going, kill it
break
}
result := f.Filter(msg)
results[result]++
if result == ResultOk {
output <- msg
}
if result == ResultDateBeforeStart {
cancel() // HTTP request is still going, kill it
break
}
}
close(output)
return results
}
// Filter performs all checks necessary to know if a given msg matches the Filter predicates.
func (f Filter) Filter(msg *Message) FilterResult {
if msg.Timestamp.After(f.EndDate) {
return ResultDateAfterEnd
}
if msg.Timestamp.Before(f.StartDate) {
return ResultDateBeforeStart
}
if f.HasMessageType {
ok := false
for _, messageType := range f.MessageTypes {
if messageType == msg.Action {
ok = true
break
}
}
if !ok {
return ResultType
}
}
if f.HasMessageRegex && !f.MessageRegex.MatchString(msg.Args[len(msg.Args)-1]) {
return ResultContent
}
switch f.UserMatchType {
case DontMatch:
break
case MatchRegex:
if f.UserName != "" && !f.UserRegex.MatchString(msg.User) {
return ResultUser
}
if f.NegativeUserName != "" && f.NegativeUserRegex.MatchString(msg.User) {
return ResultUser
}
case MatchExact:
if f.UserName != "" && f.UserName != msg.User {
return ResultUser
}
if f.NegativeUserName != "" && f.NegativeUserName == msg.User {
return ResultUser
}
}
return ResultOk
}