-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.go
245 lines (208 loc) · 5.08 KB
/
html.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"strings"
"time"
// "github.com/coreos/go-systemd/v22/journal"
"github.com/mileusna/useragent"
)
func (j JournalEntry) GetField(key string) string {
return j.E.Fields[key]
}
func (j JournalEntry) GetTime() string {
return time.UnixMicro(int64(j.E.RealtimeTimestamp)).Format(time.RFC3339)
}
func (j JournalEntry) GetTimeTime() time.Time {
return time.UnixMicro(int64(j.E.RealtimeTimestamp))
}
func (j JournalEntry) GetLogLevel() string {
switch j.GetField("PRIORITY") {
case "0":
return "Emergency"
case "1":
return "Alert"
case "2":
return "Critical"
case "3":
return "Error"
case "4":
return "Warning"
case "5":
return "Notice"
case "6":
return "Informational"
case "7":
return "Debug"
}
return "UNKNOWN"
}
// These are a collection of journal entries.
type LogEntry struct {
LastSeen time.Time
Entries []*JournalEntry
Id string
}
func (j LogEntry) EntriesReverse() []*JournalEntry {
var entries []*JournalEntry
for i := len(j.Entries) - 1; i >= 0; i-- {
entries = append(entries, j.Entries[i])
}
return entries
}
func (j LogEntry) GetTime() string {
return j.LastSeen.Format(time.RFC3339)
}
func (e LogEntry) GetProject() string {
return e.Entries[0].E.Fields["SENTRY_KEY"]
}
func (e LogEntry) GetMessage() string {
return e.Entries[0].E.Fields["MESSAGE"]
}
func (e LogEntry) GetMessageKey() string {
return e.Entries[0].E.Fields["SENTRY_MESSAGE_KEY"]
}
func (e LogEntry) HasStacktrace() bool {
_, ok := e.Entries[0].E.Fields["SENTRY_STACKTRACE"]
return ok
}
func (e LogEntry) GetStacktrace() SentryStackTrace {
var s SentryStackTrace
json.Unmarshal([]byte(e.Entries[0].E.Fields["SENTRY_STACKTRACE"]), &s)
return s
}
func (e LogEntry) GetId() string {
m := e.GetMessageKey()
// base64 encode this:
return base64.StdEncoding.EncodeToString([]byte(m))
}
func (e LogEntry) GetBrowserMeta(attr string) map[string]float64 {
browsers := make(map[string]int)
for _, entry := range e.Entries {
header_map := make(map[string]string)
json.Unmarshal([]byte(entry.E.Fields["REQUEST_HEADERS"]), &header_map)
// lowercase every key in header_map
for k, v := range header_map {
if k != strings.ToLower(k) {
header_map[strings.ToLower(k)] = v
delete(header_map, k)
}
}
ua := useragent.Parse(header_map["user-agent"])
var k string
switch attr {
case "os":
k = fmt.Sprintf("%s %s", ua.OS, ua.OSVersion)
case "version":
k = fmt.Sprintf("%s %s", ua.Name, ua.Version)
case "browser":
k = ua.Name
case "device":
k = ua.Device
}
if _, ok := browsers[k]; ok {
browsers[k] = browsers[k] + 1
} else {
browsers[k] = 1
}
}
// convert to percentages
total := len(e.Entries)
results := make(map[string]float64)
for k, v := range browsers {
if k == "" || k == " " {
continue
}
results[k] = float64(v) / float64(total)
}
return results
}
func (e LogEntry) GetMeta(attr string) map[string]float64 {
browsers := make(map[string]int)
for _, entry := range e.Entries {
var k = entry.E.Fields[attr]
if _, ok := browsers[k]; ok {
browsers[k] = browsers[k] + 1
} else {
browsers[k] = 1
}
}
// convert to percentages
total := len(e.Entries)
results := make(map[string]float64)
for k, v := range browsers {
if k == "" {
continue
}
results[k] = float64(v) / float64(total)
}
return results
}
func (e LogEntry) GetCount() int {
return len(e.Entries)
}
func (e LogEntry) GetLogLevel() string {
return e.Entries[0].GetLogLevel()
}
func (e LogEntry) GetHistogram12h() []float64 {
// 12 hours
hist := make([]int, 12)
m := 0
for _, entry := range e.Entries {
// hours ago
hour := int(time.Now().Sub(entry.GetTimeTime()).Hours())
if hour > 11 {
continue
}
hist[11-hour] = hist[11-hour] + 1
if hist[11-hour] > m {
m = hist[11-hour]
}
}
results := make([]float64, 12)
for i, v := range hist {
results[i] = float64(v) / float64(m)
}
return results
}
func (e LogEntry) GetStringyContextKeys() []string {
context_keys := make(map[string]string)
json.Unmarshal([]byte(e.Entries[0].E.Fields["SENTRY_CONTEXTS"]), &context_keys)
res := make([]string, 0)
for k, _ := range context_keys {
if context_keys[k] == "" {
// maybe re-parse/serialise as a json string?
continue
}
res = append(res, k)
}
return res
}
func (e LogEntry) GetContextDistribution(attr string) map[string]float64 {
browsers := make(map[string]int)
for _, entry := range e.Entries {
context_keys := make(map[string]string)
json.Unmarshal([]byte(entry.E.Fields["SENTRY_CONTEXTS"]), &context_keys)
k := context_keys[attr]
if _, ok := browsers[k]; ok {
browsers[k] = browsers[k] + 1
} else {
browsers[k] = 1
}
}
// convert to percentages
total := len(e.Entries)
results := make(map[string]float64)
for k, v := range browsers {
if k == "" {
continue
}
results[k] = float64(v) / float64(total)
}
return results
}
type ByAge []LogEntry
func (a ByAge) Len() int { return len(a) }
func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByAge) Less(i, j int) bool { return a[i].LastSeen.After(a[j].LastSeen) }