-
Notifications
You must be signed in to change notification settings - Fork 0
/
drain.go
280 lines (232 loc) · 6.63 KB
/
drain.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package logmunch
import (
"encoding/json"
"fmt"
"io"
"sort"
"strings"
"time"
)
type Drainer func(<-chan LogLine, io.WriteCloser)
func DrainStandard() func(<-chan LogLine, io.WriteCloser) {
return func(in <-chan LogLine, out io.WriteCloser) {
defer out.Close()
for l := range in {
out.Write([]byte(l.String()))
out.Write([]byte{'\n'})
}
}
}
func DrainJson() Drainer {
return func(in <-chan LogLine, out io.WriteCloser) {
defer out.Close()
for l := range in {
j, err := json.Marshal(l)
if err == nil {
out.Write(j)
out.Write([]byte{'\n'})
}
}
}
}
// Helper to sort list of timestamps
type timeList []time.Time
func (t timeList) Len() int { return len(t) }
func (t timeList) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t timeList) Less(i, j int) bool { return t[i].Before(t[j]) }
func DrainCountOverTime(key string) func(<-chan LogLine, io.WriteCloser) {
return func(in <-chan LogLine, out io.WriteCloser) {
defer out.Close()
keyValues := make(map[string]bool)
data := make(map[time.Time]map[string]int)
curTime := time.Unix(0, 0)
for l := range in {
// Fish out different values of the keys
keyValues[l.Entries[key]] = true
// Keep track of current time
if !curTime.Equal(l.Time) {
curTime = l.Time
// FIXME: Check if key already exists
data[curTime] = make(map[string]int)
}
// Make sure the counter has been initalized
if _, ok := data[curTime][l.Entries[key]]; !ok {
data[curTime][l.Entries[key]] = 0
}
data[curTime][l.Entries[key]] += 1
}
// Sort the keys
sortedKeys := make([]string, 0, len(keyValues))
for k := range keyValues {
sortedKeys = append(sortedKeys, k)
}
sort.Strings(sortedKeys)
// Sort the timestamps
var sortedTimestamps timeList = make([]time.Time, 0, len(data))
for t := range data {
sortedTimestamps = append(sortedTimestamps, t)
}
sort.Sort(sortedTimestamps)
// Loop over timestamps, then keys and print it all
for _, t := range sortedTimestamps {
out.Write([]byte(t.Format(time.RFC3339Nano)))
out.Write([]byte{'\n'})
for _, k := range sortedKeys {
v := "0"
if val, ok := data[t][k]; ok {
v = fmt.Sprintf("%d", val)
}
out.Write([]byte(fmt.Sprintf("\t%s:%s\n", k, v)))
}
}
}
}
func DrainGnuplotDistinctKeyCount(key string) func(<-chan LogLine, io.WriteCloser) {
return func(in <-chan LogLine, out io.WriteCloser) {
defer out.Close()
keyValues := make(map[string]bool)
data := make(map[time.Time]map[string]int)
curTime := time.Unix(0, 0)
for l := range in {
// Fish out different values of the keys
keyValues[l.Entries[key]] = true
// Keep track of current time
if !curTime.Equal(l.Time) {
curTime = l.Time
// FIXME: Check if key already exists
data[curTime] = make(map[string]int)
}
// Make sure the counter has been initalized
if _, ok := data[curTime][l.Entries[key]]; !ok {
data[curTime][l.Entries[key]] = 0
}
data[curTime][l.Entries[key]] += 1
}
// Sort the keys
sortedKeys := make([]string, 0, len(keyValues))
for k := range keyValues {
sortedKeys = append(sortedKeys, k)
}
sort.Strings(sortedKeys)
// Sort the timestamps
var sortedTimestamps timeList = make([]time.Time, 0, len(data))
for t := range data {
sortedTimestamps = append(sortedTimestamps, t)
}
sort.Sort(sortedTimestamps)
// Print GNUPLOT stuffs
out.Write([]byte(
`set terminal png
set output 'x.png'
set timefmt "%s"
set xdata time
plot`))
for i, name := range sortedKeys {
out.Write([]byte(fmt.Sprintf(" '-' using 1:2 with linespoints title '%s'", name)))
if i != len(sortedKeys)-1 {
out.Write([]byte(", \\\n"))
}
}
out.Write([]byte("\n"))
// Loop over timestamps, then keys and print it all
for _, k := range sortedKeys {
for _, t := range sortedTimestamps {
v := 0
if val, ok := data[t][k]; ok {
v = val
}
out.Write([]byte(fmt.Sprintf(" %d\t%d\n", t.Unix(), v)))
}
out.Write([]byte("EOF\n"))
}
}
}
func DrainSqlite3() func(<-chan LogLine, io.WriteCloser) {
return func(in <-chan LogLine, out io.WriteCloser) {
defer out.Close()
data := make([]LogLine, 0)
keyNames := make(map[string]bool)
for l := range in {
// Fish out different values of the keys
for keyName, _ := range l.Entries {
keyNames[keyName] = true
}
data = append(data, l)
}
// Sort the keys
sortedKeys := make([]string, 0, len(keyNames))
for k := range keyNames {
sortedKeys = append(sortedKeys, k)
}
sort.Strings(sortedKeys)
// Print GNUPLOT stuffs
out.Write([]byte("CREATE TABLE logs (time, unix, name, "))
// Join keys and rewrite `.` to `_` (so we don't need escpaing in all cases).
out.Write([]byte(strings.Replace(strings.Join(sortedKeys, ", "), ".", "_", -1)))
out.Write([]byte(");\n\n"))
// Loop over timestamps, then keys and print it all
for _, k := range data {
arr := make([]string, len(sortedKeys))
for i, name := range sortedKeys {
arr[i] = ""
if val, ok := k.Entries[name]; ok {
// In Sqlite (and SQL in general, I believe), `'`s are escaped with `''`.
arr[i] = strings.Replace(val, "'", "''", -1)
}
}
out.Write([]byte(fmt.Sprintf(
"INSERT INTO logs VALUES('%s', %d, '%s', '%s');\n",
k.Time.Format(time.RFC3339Nano),
k.Time.Unix(),
k.Name,
strings.Join(arr, "', '"),
)))
}
}
}
func DrainCSV(join string) func(<-chan LogLine, io.WriteCloser) {
return func(in <-chan LogLine, out io.WriteCloser) {
defer out.Close()
data := make([]LogLine, 0)
keyNames := make(map[string]bool)
for l := range in {
// Fish out different values of the keys
for keyName, _ := range l.Entries {
keyNames[keyName] = true
}
data = append(data, l)
}
// Sort the keys
sortedKeys := make([]string, 0, len(keyNames))
for k := range keyNames {
sortedKeys = append(sortedKeys, k)
}
sort.Strings(sortedKeys)
// Print GNUPLOT stuffs
out.Write([]byte("#"))
// Join keys and rewrite `.` to `_` (so we don't need escpaing in all cases).
out.Write([]byte("time"))
out.Write([]byte(join))
out.Write([]byte("unixtime"))
out.Write([]byte(join))
out.Write([]byte("name"))
out.Write([]byte(join))
out.Write([]byte(strings.Join(sortedKeys, join)))
out.Write([]byte("\n"))
// Loop over timestamps, then keys and print it all
for _, k := range data {
arr := make([]string, len(sortedKeys)+3)
arr[0] = k.Time.Format(time.RFC3339Nano)
arr[1] = fmt.Sprint(k.Time.Unix())
arr[2] = k.Name
for i, name := range sortedKeys {
arr[i+3] = ""
if val, ok := k.Entries[name]; ok {
arr[i+3] = val
}
}
out.Write([]byte(strings.Join(arr, join)))
out.Write([]byte("\n"))
}
}
}