forked from keybase/managed-bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grouper.go
208 lines (185 loc) · 4.09 KB
/
grouper.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package elastiwatch
import (
"encoding/json"
"fmt"
"sort"
"strings"
"time"
)
type entry struct {
Message string `json:"message"`
Severity string `json:"severity"`
UID string `json:"uid"`
Time string `json:"time"`
Program string `json:"program"`
parts []string
}
func newEntry(dat []byte) (*entry, error) {
entry := new(entry)
if err := json.Unmarshal(dat, entry); err != nil {
return nil, err
}
entry.parts = append(entry.parts, entry.Program)
entry.parts = append(entry.parts, entry.UID)
entry.parts = append(entry.parts, strings.Split(entry.Message, " ")...)
return entry, nil
}
func (e *entry) words() []string {
return e.parts
}
func (e *entry) numWords() int {
return len(e.parts)
}
func (e *entry) String() string {
return fmt.Sprintf("%s %s %s %s %s", e.Time, e.Program, e.Severity, e.UID, e.Message)
}
type chunk struct {
Severity string
Count int
Message string
Items []string
Time string
}
type grouper interface {
Group([]*entry) []chunk
}
type tree struct {
entry *entry
count int
subtrees []*tree
words []string
time time.Time
}
func newTree(e *entry) *tree {
etime, err := time.Parse(time.RFC3339, e.Time)
if err != nil {
etime = time.Time{}
}
return &tree{
entry: e,
count: 1,
words: e.words(),
time: etime,
}
}
func (t *tree) toChunk() chunk {
c := chunk{}
c.Severity = t.entry.Severity
c.Count = t.count
c.Message = strings.Join(t.words, " ")
c.Time = t.time.Format("3:04PM")
for _, t := range t.subtrees {
c.Items = append(c.Items, t.entry.String())
}
return c
}
type treeifyGrouper struct {
maxGroups int
}
var _ grouper = (*treeifyGrouper)(nil)
func newTreeifyGrouper(maxGroups int) *treeifyGrouper {
return &treeifyGrouper{
maxGroups: maxGroups,
}
}
func (t *treeifyGrouper) key(e *entry) string {
return fmt.Sprintf("%s %d", e.Severity, e.numWords())
}
func (t *treeifyGrouper) commonAncestor(aparts, bparts []string) (res []string) {
for index, ap := range aparts {
if ap != bparts[index] {
res = append(res, "___")
} else {
res = append(res, ap)
}
}
return res
}
func (t *treeifyGrouper) errDist(aparts, bparts []string) (res int) {
for index, ap := range aparts {
if ap != bparts[index] {
res++
}
}
return res
}
func (t *treeifyGrouper) buildForest(trees []*tree) (forest []*tree) {
if len(trees) == 0 {
return trees
}
numParts := len(trees[0].words)
minDist := numParts
if minDist > t.maxGroups {
minDist = t.maxGroups
}
for _, loneTree := range trees {
consumed := false
for _, forestTree := range forest {
if t.errDist(forestTree.words, loneTree.words) <= minDist {
forestTree.subtrees = append(forestTree.subtrees, loneTree)
forestTree.count++
forestTree.words = t.commonAncestor(forestTree.words, loneTree.words)
if loneTree.time.After(forestTree.time) {
forestTree.time = loneTree.time
}
consumed = true
break
}
}
if !consumed {
loneTree.subtrees = append(loneTree.subtrees, loneTree)
forest = append(forest, loneTree)
}
}
return forest
}
func (t *treeifyGrouper) treeify(entries []*entry) (res []*tree) {
groups := make(map[string][]*tree)
for _, e := range entries {
key := t.key(e)
tree := newTree(e)
groups[key] = append(groups[key], tree)
}
for _, group := range groups {
res = append(res, t.buildForest(group)...)
}
return res
}
func severityRank(severity string) int {
switch severity {
case "DEBUG":
return 0
case "INFO":
return 1
case "WARNING":
return 2
case "ERROR":
return 3
case "CRITICAL":
return 4
default:
return 0
}
}
type byValue []chunk
func (b byValue) Len() int { return len(b) }
func (b byValue) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b byValue) Less(i, j int) bool {
irank := severityRank(b[i].Severity)
jrank := severityRank(b[j].Severity)
if irank > jrank {
return true
}
if irank < jrank {
return false
}
return len(b[i].Items) > len(b[j].Items)
}
func (t *treeifyGrouper) Group(entries []*entry) (res []chunk) {
trees := t.treeify(entries)
for _, tree := range trees {
res = append(res, tree.toChunk())
}
sort.Sort(byValue(res))
return res
}