-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui.go
477 lines (397 loc) · 13.3 KB
/
gui.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// gui.go contains types, functions and methods for creating
// and updating the graphical and interactive UI to the doctor program
package main
import (
"context"
"fmt"
"log"
"math"
"time"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
"github.com/kava-labs/doctor/collect"
dconfig "github.com/kava-labs/doctor/config"
"github.com/kava-labs/doctor/metric"
"github.com/spf13/viper"
)
// GUIConfig wraps values
// used to configure the GUI
// display mode of the doctor program
type GUIConfig struct {
DebugLoggingEnabled bool
KavaURL string
RefreshRateSeconds int
MaxMetricSamplesToRetainPerNode int
MetricSamplesForSyntheticMetricCalculation int
MetricCollectors []string
AWSRegion string
MetricNamespace string
}
// GUI controls the display
// mode of the doctor program
// using asci interactive tty
// output devices
type GUI struct {
grid *ui.Grid
updateParagraph func(count int)
draw func(count int, paragraph string)
newMessageFunc func(message string)
updateUptimeFunc func(uptime float32)
kavaEndpoint *Endpoint
metricCollectors []collect.Collector
refreshRateSeconds int
debugMode bool
*log.Logger
}
// Watch watches for new measurements and log messages for the kava node with the
// specified rpc api url, outputting them to the gui device in the desired format
func (g *GUI) Watch(metricReadOnlyChannels MetricReadOnlyChannels, logMessages <-chan string, kavaNodeRPCURL string) error {
tickerCount := 1
// create channel to subscribe to
// user input
uiEvents := ui.PollEvents()
// create channel that will emit
// an event every second
ticker := time.NewTicker(time.Second * time.Duration(g.refreshRateSeconds)).C
// handle logging in separate go-routines to avoid
// congestion with metric event emission
go func() {
// events triggered by debug worthy events
for logMessage := range logMessages {
// TODO: separate channels
// for debug only log messages?
if !g.debugMode {
continue
}
g.newMessageFunc(logMessage)
}
}()
for {
select {
// events triggered by user input
// or action such as keyboard strokes
// mouse movements or window changes
case e := <-uiEvents:
switch e.ID {
case "q", "<C-c>":
ui.Close()
return nil
case "c":
updatedParagraph := fmt.Sprintf(
`Current Config %+v
`, viper.AllSettings())
g.newMessageFunc(updatedParagraph)
time.Sleep(1 * time.Second)
case "l":
// TODO: allow paging through metrics per node
message := fmt.Sprintf("Accumulated Metrics %+v", g.kavaEndpoint.PerNodeMetrics)
g.newMessageFunc(message)
time.Sleep(3 * time.Second)
case "<Resize>":
payload := e.Payload.(ui.Resize)
g.grid.SetRect(0, 0, payload.Width, payload.Height)
ui.Clear()
ui.Render(g.grid)
}
// events triggered by new metric data
case syncStatusMetrics := <-metricReadOnlyChannels.SyncStatusMetrics:
// record sample in-memory for use in synthetic metric calculation
g.kavaEndpoint.AddSample(syncStatusMetrics.NodeId, NodeMetrics{
SyncStatusMetrics: &syncStatusMetrics,
})
// calculate hash rate for this node
nodeId := syncStatusMetrics.NodeId
hashRatePerSecond, err := g.kavaEndpoint.CalculateNodeHashRatePerSecond(nodeId)
if err != nil {
g.newMessageFunc(fmt.Sprintf("error %s calculating hash rate for node %s\n", err, nodeId))
}
latestBlockHeight := syncStatusMetrics.SyncStatus.LatestBlockHeight
secondsBehindLive := syncStatusMetrics.SecondsBehindLive
syncStatusLatencyMilliseconds := syncStatusMetrics.SampleLatencyMilliseconds
updatedParagraph := fmt.Sprintf(
`Node %s
Latest Block Height %d
Seconds Behind Live %d
Blocks Hashed (per second) %f
Sync Status Latency (milliseconds) %d
`, nodeId, latestBlockHeight, secondsBehindLive, hashRatePerSecond, syncStatusLatencyMilliseconds)
g.draw(tickerCount, updatedParagraph)
// collect metrics to external storage backends
var metrics []metric.Metric
hashRateMetric := metric.Metric{
Name: "BlocksHashedPerSecond",
Dimensions: map[string]string{
"node_id": nodeId,
},
Data: metric.HashRateMetric{
NodeId: nodeId,
BlocksPerSecond: hashRatePerSecond,
},
Value: float64(hashRatePerSecond),
Timestamp: syncStatusMetrics.SampledAt,
CollectToFile: true,
CollectToCloudwatch: true,
}
metrics = append(metrics, hashRateMetric)
syncStatusMetric := metric.Metric{
Name: "SyncStatus",
Dimensions: map[string]string{
"node_id": nodeId,
},
Data: syncStatusMetrics,
CollectToFile: true,
CollectToCloudwatch: false,
}
metrics = append(metrics, syncStatusMetric)
latestBlockHeightMetric := metric.Metric{
Name: "LatestBlockHeight",
Dimensions: map[string]string{
"node_id": nodeId,
},
Value: float64(latestBlockHeight),
Timestamp: syncStatusMetrics.SampledAt,
CollectToFile: false,
CollectToCloudwatch: true,
}
metrics = append(metrics, latestBlockHeightMetric)
secondsBehindLiveMetric := metric.Metric{
Name: "SecondsBehindLive",
Dimensions: map[string]string{
"node_id": nodeId,
},
Value: float64(secondsBehindLive),
Timestamp: syncStatusMetrics.SampledAt,
CollectToFile: false,
CollectToCloudwatch: true,
}
metrics = append(metrics, secondsBehindLiveMetric)
statusCheckMillisecondLatencyMetric := metric.Metric{
Name: "StatusCheckLatencyMilliseconds",
Dimensions: map[string]string{
"node_id": nodeId,
},
Value: float64(syncStatusLatencyMilliseconds),
Timestamp: syncStatusMetrics.SampledAt,
CollectToFile: false,
CollectToCloudwatch: true,
}
metrics = append(metrics, statusCheckMillisecondLatencyMetric)
for _, collector := range g.metricCollectors {
for _, metric := range metrics {
err := collector.Collect(metric)
if err != nil {
g.newMessageFunc(fmt.Sprintf("error %s collecting metric %+v\n", err, metric))
}
}
}
// events triggered by new metric data
case uptimeMetric := <-metricReadOnlyChannels.UptimeMetrics:
endpointURL := uptimeMetric.EndpointURL
// record sample in-memory for use in synthetic metric calculation
g.kavaEndpoint.AddSample(uptimeMetric.EndpointURL, NodeMetrics{
UptimeMetric: &uptimeMetric,
})
// calculate uptime
uptime, err := g.kavaEndpoint.CalculateUptime(uptimeMetric.EndpointURL)
if err != nil {
g.newMessageFunc(fmt.Sprintf("error %s calculating uptime for %s\n", err, uptimeMetric.EndpointURL))
continue
}
// update uptime gauge
g.updateUptimeFunc(uptime)
// collect metrics to external storage backends
var metrics []metric.Metric
uptimeMetric.RollingAveragePercentAvailable = uptime * 100
uptimeMetricForCollection := metric.Metric{
Name: "Uptime",
Dimensions: map[string]string{
"endpoint_url": endpointURL,
},
Data: uptimeMetric,
Value: float64(uptimeMetric.RollingAveragePercentAvailable),
Timestamp: uptimeMetric.SampledAt,
CollectToFile: true,
CollectToCloudwatch: true,
}
metrics = append(metrics, uptimeMetricForCollection)
for _, collector := range g.metricCollectors {
for _, metric := range metrics {
err := collector.Collect(metric)
if err != nil {
g.newMessageFunc(fmt.Sprintf("error %s collecting metric %+v\n", err, metric))
}
}
}
// events triggered on a regular time based interval
case <-ticker:
g.updateParagraph(tickerCount)
g.draw(tickerCount, "")
tickerCount++
}
}
}
func NewGUI(config GUIConfig) (*GUI, error) {
if err := ui.Init(); err != nil {
panic(fmt.Errorf("failed to initialize termui: %v", err))
}
// upper left box
syncMetrics := widgets.NewParagraph()
syncMetrics.Title = "Sync Metrics"
syncMetrics.Text = `PRESS q TO QUIT
PRESS c TO VIEW CONFIG
PRESS l TO LIST SAMPLES
`
syncMetrics.SetRect(0, 0, 50, 5)
syncMetrics.TextStyle.Fg = ui.ColorWhite
syncMetrics.BorderStyle.Fg = ui.ColorCyan
updateParagraph := func(count int) {
if count%2 == 0 {
syncMetrics.TextStyle.Fg = ui.ColorRed
} else {
syncMetrics.TextStyle.Fg = ui.ColorWhite
}
}
// upper right box
messages := widgets.NewParagraph()
messages.Title = "Messages"
messages.Text = ""
messages.SetRect(0, 0, 50, 5)
messages.TextStyle.Fg = ui.ColorYellow
messages.BorderStyle.Fg = ui.ColorMagenta
// lower left box
uptimeMetric := widgets.NewGauge()
uptimeMetric.Title = "Uptime Metric"
uptimeMetric.Percent = 0
uptimeMetric.SetRect(0, 12, 50, 15)
uptimeMetric.BarColor = ui.ColorRed
uptimeMetric.BorderStyle.Fg = ui.ColorWhite
uptimeMetric.TitleStyle.Fg = ui.ColorCyan
sparklineData := []float64{4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6, 4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6, 4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6, 4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6}
sl := widgets.NewSparkline()
sl.Title = "srv 0:"
sl.Data = sparklineData
sl.LineColor = ui.ColorCyan
sl.TitleStyle.Fg = ui.ColorWhite
sl2 := widgets.NewSparkline()
sl2.Title = "srv 1:"
sl2.Data = sparklineData
sl2.TitleStyle.Fg = ui.ColorWhite
sl2.LineColor = ui.ColorRed
slg := widgets.NewSparklineGroup(sl, sl2)
slg.Title = "Sparkline"
slg.SetRect(25, 5, 50, 12)
sinData := (func() []float64 {
n := 220
ps := make([]float64, n)
for i := range ps {
ps[i] = 1 + math.Sin(float64(i)/5)
}
return ps
})()
lc := widgets.NewPlot()
lc.Title = "dot-marker Line Chart"
lc.Data = make([][]float64, 1)
lc.Data[0] = sinData
lc.SetRect(0, 15, 50, 25)
lc.AxesColor = ui.ColorWhite
lc.LineColors[0] = ui.ColorRed
lc.Marker = widgets.MarkerDot
barchartData := []float64{3, 2, 5, 3, 9, 5, 3, 2, 5, 8, 3, 2, 4, 5, 3, 2, 5, 7, 5, 3, 2, 6, 7, 4, 6, 3, 6, 7, 8, 3, 6, 4, 5, 3, 2, 4, 6, 4, 8, 5, 9, 4, 3, 6, 5, 3, 6}
bc := widgets.NewBarChart()
bc.Title = "Bar Chart"
bc.SetRect(50, 0, 75, 10)
bc.Labels = []string{"S0", "S1", "S2", "S3", "S4", "S5"}
bc.BarColors[0] = ui.ColorGreen
bc.NumStyles[0] = ui.NewStyle(ui.ColorBlack)
// lower right box
lc2 := widgets.NewPlot()
lc2.Title = "braille-mode Line Chart"
lc2.Data = make([][]float64, 1)
lc2.Data[0] = sinData
lc2.SetRect(50, 15, 75, 25)
lc2.AxesColor = ui.ColorWhite
lc2.LineColors[0] = ui.ColorYellow
// set up 4 x 4 grid
grid := ui.NewGrid()
termWidth, termHeight := ui.TerminalDimensions()
grid.SetRect(0, 0, termWidth, termHeight)
grid.Set(
ui.NewRow(1.0/2,
ui.NewCol(1.0/2, syncMetrics),
ui.NewCol(1.0/2, messages),
),
ui.NewRow(1.0/2,
ui.NewCol(1.0/4, uptimeMetric),
ui.NewCol(1.0/4,
ui.NewRow(.9/3, slg),
ui.NewRow(.9/3, lc),
ui.NewRow(1.2/3, bc),
),
ui.NewCol(1.0/2, lc2),
),
)
// setup function to call whenever
// there is new data
draw := func(count int, paragraph string) {
slg.Sparklines[0].Data = sparklineData[:30+count%50]
slg.Sparklines[1].Data = sparklineData[:35+count%50]
lc.Data[0] = sinData[count/2%220:]
lc2.Data[0] = sinData[2*count%220:]
bc.Data = barchartData[count/2%10:]
if paragraph != "" {
syncMetrics.Text = paragraph
}
ui.Render(grid)
}
// setup function to call whenever
// the uptime metric needs to be updated
updateUptime := func(uptime float32) {
uptimeMetric.Percent = int(math.Round(float64(uptime * 100)))
ui.Render(grid)
}
// setup function to call whenever there
// is new debug / log messages to show
newMessage := func(message string) {
messages.Text = message
ui.Render(grid)
}
// show the initial ui to the user
ui.Render(grid)
endpoint := NewEndpoint(EndpointConfig{URL: config.KavaURL,
MetricSamplesToKeepPerNode: config.MaxMetricSamplesToRetainPerNode,
MetricSamplesForSyntheticMetricCalculation: config.MetricSamplesForSyntheticMetricCalculation,
})
collectors := []collect.Collector{}
for _, collector := range config.MetricCollectors {
switch collector {
case dconfig.FileMetricCollector:
fileCollector, err := collect.NewFileCollector(collect.FileCollectorConfig{})
if err != nil {
return nil, err
}
collectors = append(collectors, fileCollector)
case dconfig.CloudwatchMetricCollector:
cloudwatchConfig := collect.CloudWatchCollectorConfig{
Ctx: context.Background(),
AWSRegion: config.AWSRegion,
MetricNamespace: config.MetricNamespace,
}
cloudwatchCollector, err := collect.NewCloudWatchCollector(cloudwatchConfig)
if err != nil {
return nil, err
}
collectors = append(collectors, cloudwatchCollector)
}
}
return &GUI{
refreshRateSeconds: config.RefreshRateSeconds,
debugMode: config.DebugLoggingEnabled,
grid: grid,
updateParagraph: updateParagraph,
updateUptimeFunc: updateUptime,
draw: draw,
newMessageFunc: newMessage,
kavaEndpoint: endpoint,
metricCollectors: collectors,
}, nil
}