-
Notifications
You must be signed in to change notification settings - Fork 1
/
logvelocityview.go
379 lines (327 loc) · 8.35 KB
/
logvelocityview.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package logview
import (
"fmt"
"github.com/gdamore/tcell/v2"
gui "github.com/rivo/tview"
"sync"
"time"
)
// LogVelocityView is a bar chart to display number of log events per time period
type LogVelocityView struct {
*gui.Box
defaultStyle tcell.Style
errorColor tcell.Color
warningColor tcell.Color
showLogLevel LogLevel
bucketWidth int64
infoBuckets map[int64]int
errorBuckets map[int64]int
warnBuckets map[int64]int
height int
width int
anchor *int64
sync.RWMutex
}
const valuesPerBlock = 8
const minWidthToDisplayYAxis = 20
var blocks = []rune{
'\u2581', // U+2581 1/8
'\u2582', // U+2582 2/8
'\u2583', // U+2583 3/8
'\u2584', // U+2584 4/8
'\u2585', // U+2585 5/8
'\u2586', // U+2586 6/8
'\u2587', // U+2587 7/8
'\u2588', // U+2588 8/8
}
// NewLogVelocityView creates a new log velocity view with a defined bucket time frame
func NewLogVelocityView(bucketWidth time.Duration) *LogVelocityView {
return &LogVelocityView{
Box: gui.NewBox(),
bucketWidth: int64(bucketWidth.Seconds()),
infoBuckets: make(map[int64]int),
warnBuckets: make(map[int64]int),
errorBuckets: make(map[int64]int),
defaultStyle: tcell.StyleDefault.Foreground(gui.Styles.PrimaryTextColor).Background(tcell.Color239),
errorColor: tcell.ColorIndianRed,
warningColor: tcell.ColorSaddleBrown,
showLogLevel: LogLevelAll,
anchor: nil,
}
}
// Clear resets all the statistics. Bucket width and anchor do not change
func (lh *LogVelocityView) Clear() {
lh.Lock()
defer lh.Unlock()
lh.reset()
}
var (
bucketSizes = []int64{1, 10, 60, 120, 300, 600, 900, 1800, 3600, 1e10}
)
func (lh *LogVelocityView) ScaleFor(duration time.Duration) {
lh.Lock()
defer lh.Unlock()
lh.scaleForDuration(duration)
}
func (lh *LogVelocityView) AutoScale(from, to time.Time) {
lh.Lock()
defer lh.Unlock()
lh.reset()
if from.After(to) {
from, to = to, from
}
duration := to.Sub(from)
lh.scaleForDuration(duration)
}
// AppendLogEvents adds event to a velocity chart
func (lh *LogVelocityView) AppendLogEvent(event *LogEvent) {
lh.Lock()
defer lh.Unlock()
key := event.Timestamp.Unix() / lh.bucketWidth
var b map[int64]int
switch event.Level {
case LogLevelError:
b = lh.errorBuckets
case LogLevelWarning:
b = lh.warnBuckets
default:
b = lh.infoBuckets
}
if v, ok := b[key]; ok {
b[key] = v + 1
} else {
b[key] = 1
}
}
// SetShowLogLevel sets the log level of events that should be displayed in the velocity view
//
// Supported values are:
//
// - LogLevelInfo - show all events but warning and errors
//
// - LogLevelWarning
//
// - LogLevelError
//
// - LogLevelAll - show all events
func (lh *LogVelocityView) SetShowLogLevel(logLevel LogLevel) {
lh.Lock()
defer lh.Unlock()
lh.showLogLevel = logLevel
}
// GetShowLogLevel returns the log level of events that are be displayed in the velocity view
func (lh *LogVelocityView) GetShowLogLevel() LogLevel {
lh.RLock()
defer lh.RUnlock()
return lh.showLogLevel
}
// SetAnchor sets the max time for the time axis
func (lh *LogVelocityView) SetAnchor(newAnchor time.Time) {
lh.Lock()
defer lh.Unlock()
a := newAnchor.Unix()
lh.anchor = &a
}
// ClearAnchor removes the max time for the time axis. Max time will be equal to the current time
func (lh *LogVelocityView) ClearAnchor() {
lh.Lock()
defer lh.Unlock()
lh.anchor = nil
}
func (lh *LogVelocityView) GetAnchor() *time.Time {
lh.RLock()
defer lh.RUnlock()
if lh.anchor == nil {
return nil
} else {
result := time.Unix(*lh.anchor, 0)
return &result
}
}
// Draw draws this primitive onto the screen.
func (lh *LogVelocityView) Draw(screen tcell.Screen) {
//if !lh.GetVisible() {
// return
//}
lh.Box.Draw(screen)
lh.Lock()
defer lh.Unlock()
// Get the available size.
x, y, width, height := lh.GetInnerRect()
if height == 0 {
return
}
lh.width = width
lh.height = height
var values []int
key := lh.timeAnchor()
if width < minWidthToDisplayYAxis {
values = lh.values(key, width)
} else {
values = lh.values(key, width-6)
}
maxV := lh.max(values)
if width > 20 {
x, width = lh.drawValueAxis(screen, x, y, maxV)
}
if height > 1 {
lh.drawTimeAxis(screen, x, y, width, height, key)
height--
}
lh.drawHistogram(screen, x, y, width, height, values, maxV)
}
// ****************
// Internal methods
func (lh *LogVelocityView) bucketValue(bucket map[int64]int, key int64) int {
if v, ok := bucket[key]; ok {
return v
} else {
return 0
}
}
func (lh *LogVelocityView) values(key int64, count int) []int {
results := make([]int, count)
for i := count - 1; i >= 0; i-- {
var value int
switch lh.showLogLevel {
case LogLevelWarning:
value = lh.bucketValue(lh.warnBuckets, key)
case LogLevelError:
value = lh.bucketValue(lh.errorBuckets, key)
case LogLevelInfo:
value = lh.bucketValue(lh.infoBuckets, key)
case LogLevelAll:
value = lh.bucketValue(lh.infoBuckets, key) + lh.bucketValue(lh.warnBuckets, key) +
lh.bucketValue(lh.errorBuckets, key)
default:
value = 0
}
results[i] = value
key = key - 1
}
return results
}
func (lh *LogVelocityView) drawHistogram(screen tcell.Screen, x, y, width, height int, values []int, maxV int) {
var scale float64
if maxV == 0 {
scale = float64(maxV)
} else {
scale = float64(height*valuesPerBlock) / float64(maxV)
}
// normalize values to available height
for i, v := range values {
values[i] = int(float64(v) * scale)
}
style := lh.defaultStyle
switch lh.showLogLevel {
case LogLevelWarning:
style = style.Foreground(lh.warningColor)
case LogLevelError:
style = style.Foreground(lh.errorColor)
}
index := len(values) - 1
i := x + width - 1
for i >= 0 && index >= 0 {
v := values[index]
for j := y + height - 1; j >= y; j-- {
if v > valuesPerBlock {
screen.SetCell(i, j, style, blocks[7])
} else {
if v > 0 {
block := v - 1
screen.SetCell(i, j, style, blocks[block])
} else {
screen.SetCell(i, j, style, ' ')
}
}
v -= valuesPerBlock
}
i--
index--
}
}
// drawTimeAxis draws X-axis with duration marks
func (lh *LogVelocityView) drawTimeAxis(screen tcell.Screen, x int, y int, width int, height int, key int64) {
tickDuration := lh.bucketWidth * 20
current := key
for i := 0; i < width; i++ {
screen.SetCell(x+i, y+height-1, lh.defaultStyle, ' ')
}
i := width - 1
yp := y + height - 1
var dur string
for i >= 0 {
if lh.anchor != nil && i == width-1 {
dur = time.Unix(*lh.anchor, 0).Format(time.Kitchen)
} else {
dur = "-" + durationToString(key-current)
}
i -= len(dur)
if i <= 0 {
break
}
printString(screen, x+i, yp, dur, lh.defaultStyle)
screen.SetCell(x+i+len(dur), yp, lh.defaultStyle, '⭡')
current -= tickDuration
i -= 20 - (len(dur))
}
}
// drawValueAxis draws Y-axis with marks for zero and max event count per bucket
// it returns new minimal x coordinate. Y-axis takes 6 characters out of screen real estate
func (lh *LogVelocityView) drawValueAxis(screen tcell.Screen, x int, y int, maxValue int) (int, int) {
valueS := formatValue(maxValue)
printString(screen, x, y, valueS, lh.defaultStyle)
printString(screen, x+4, y, " ┬", lh.defaultStyle)
for j := y + 1; j < y+lh.height-1; j++ {
printString(screen, x, j, " │", lh.defaultStyle)
}
printString(screen, x, y+lh.height-1, " 0 ┴", lh.defaultStyle)
return x + 6, lh.width - 6
}
func (lh *LogVelocityView) max(values []int) int {
m := 0
for _, val := range values {
v := val
if v > m {
m = v
}
}
return m
}
func (lh *LogVelocityView) timeAnchor() int64 {
if lh.anchor == nil {
return time.Now().Unix() / lh.bucketWidth
} else {
return *lh.anchor / lh.bucketWidth
}
}
func durationToString(d int64) string {
minutes := d / 60
seconds := d % 60
hours := minutes / 60
minutes = minutes % 60
if hours > 0 {
return fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
}
return fmt.Sprintf("%02d:%02d", minutes, seconds)
}
func (lh *LogVelocityView) reset() {
lh.infoBuckets = make(map[int64]int)
lh.warnBuckets = make(map[int64]int)
lh.errorBuckets = make(map[int64]int)
}
func (lh *LogVelocityView) scaleForDuration(duration time.Duration) {
dur := duration.Seconds() / float64(lh.width)
if dur < 1 {
dur = 1
}
var bucketW int64
for i := 1; i < len(bucketSizes); i++ {
bucketW = int64(dur + 0.5)
if bucketW > bucketSizes[i-1] && bucketW <= bucketSizes[i] {
bucketW = (bucketW / bucketSizes[i-1]) * bucketSizes[i-1]
break
}
}
lh.bucketWidth = bucketW
}