@@ -103,33 +103,17 @@ func monitor(ctx *cli.Context) {
103
103
footer .Height = 3
104
104
105
105
charts := make ([]* termui.LineChart , len (monitored ))
106
+ units := make ([]int , len (monitored ))
106
107
data := make ([][]float64 , len (monitored ))
107
- for i := 0 ; i < len (data ); i ++ {
108
- data [i ] = make ([]float64 , 512 )
109
- }
110
- for i , metric := range monitored {
111
- charts [i ] = termui .NewLineChart ()
112
- if runtime .GOOS == "windows" {
113
- charts [i ].Mode = "dot"
114
- }
115
- charts [i ].Data = make ([]float64 , 512 )
116
- charts [i ].DataLabels = []string {"" }
117
- charts [i ].Height = (termui .TermHeight () - footer .Height ) / rows
118
- charts [i ].AxesColor = termui .ColorWhite
119
- charts [i ].PaddingBottom = - 2
120
-
121
- charts [i ].Border .Label = metric
122
- charts [i ].Border .LabelFgColor = charts [i ].Border .FgColor | termui .AttrBold
123
- charts [i ].Border .FgColor = charts [i ].Border .BgColor
124
-
108
+ for i := 0 ; i < len (monitored ); i ++ {
109
+ charts [i ] = createChart ((termui .TermHeight () - footer .Height ) / rows )
125
110
row := termui .Body .Rows [i % rows ]
126
111
row .Cols = append (row .Cols , termui .NewCol (12 / cols , 0 , charts [i ]))
127
112
}
128
113
termui .Body .AddRows (termui .NewRow (termui .NewCol (12 , 0 , footer )))
129
- termui .Body .Align ()
130
- termui .Render (termui .Body )
131
114
132
- refreshCharts (xeth , monitored , data , charts , ctx , footer )
115
+ refreshCharts (xeth , monitored , data , units , charts , ctx , footer )
116
+ termui .Body .Align ()
133
117
termui .Render (termui .Body )
134
118
135
119
// Watch for various system events, and periodically refresh the charts
@@ -149,7 +133,9 @@ func monitor(ctx *cli.Context) {
149
133
termui .Render (termui .Body )
150
134
}
151
135
case <- refresh :
152
- refreshCharts (xeth , monitored , data , charts , ctx , footer )
136
+ if refreshCharts (xeth , monitored , data , units , charts , ctx , footer ) {
137
+ termui .Body .Align ()
138
+ }
153
139
termui .Render (termui .Body )
154
140
}
155
141
}
@@ -246,52 +232,88 @@ func fetchMetric(metrics map[string]interface{}, metric string) float64 {
246
232
247
233
// refreshCharts retrieves a next batch of metrics, and inserts all the new
248
234
// values into the active datasets and charts
249
- func refreshCharts (xeth * rpc.Xeth , metrics []string , data [][]float64 , charts []* termui.LineChart , ctx * cli.Context , footer * termui.Par ) {
235
+ func refreshCharts (xeth * rpc.Xeth , metrics []string , data [][]float64 , units [] int , charts []* termui.LineChart , ctx * cli.Context , footer * termui.Par ) ( realign bool ) {
250
236
values , err := retrieveMetrics (xeth )
251
237
for i , metric := range metrics {
252
- data [i ] = append ([]float64 {fetchMetric (values , metric )}, data [i ][:len (data [i ])- 1 ]... )
253
- updateChart (metric , data [i ], charts [i ], err )
238
+ if len (data ) < 512 {
239
+ data [i ] = append ([]float64 {fetchMetric (values , metric )}, data [i ]... )
240
+ } else {
241
+ data [i ] = append ([]float64 {fetchMetric (values , metric )}, data [i ][:len (data [i ])- 1 ]... )
242
+ }
243
+ if updateChart (metric , data [i ], & units [i ], charts [i ], err ) {
244
+ realign = true
245
+ }
254
246
}
255
247
updateFooter (ctx , err , footer )
248
+ return
256
249
}
257
250
258
251
// updateChart inserts a dataset into a line chart, scaling appropriately as to
259
252
// not display weird labels, also updating the chart label accordingly.
260
- func updateChart (metric string , data []float64 , chart * termui.LineChart , err error ) {
253
+ func updateChart (metric string , data []float64 , base * int , chart * termui.LineChart , err error ) ( realign bool ) {
261
254
dataUnits := []string {"" , "K" , "M" , "G" , "T" , "E" }
262
255
timeUnits := []string {"ns" , "µs" , "ms" , "s" , "ks" , "ms" }
263
256
colors := []termui.Attribute {termui .ColorBlue , termui .ColorCyan , termui .ColorGreen , termui .ColorYellow , termui .ColorRed , termui .ColorRed }
264
257
265
258
// Extract only part of the data that's actually visible
266
- data = data [:chart .Width * 2 ]
267
-
259
+ if chart .Width * 2 < len (data ) {
260
+ data = data [:chart .Width * 2 ]
261
+ }
268
262
// Find the maximum value and scale under 1K
269
- high := data [0 ]
270
- for _ , value := range data [1 :] {
271
- high = math .Max (high , value )
263
+ high := 0.0
264
+ if len (data ) > 0 {
265
+ high = data [0 ]
266
+ for _ , value := range data [1 :] {
267
+ high = math .Max (high , value )
268
+ }
272
269
}
273
270
unit , scale := 0 , 1.0
274
271
for high >= 1000 {
275
272
high , unit , scale = high / 1000 , unit + 1 , scale * 1000
276
273
}
274
+ // If the unit changes, re-create the chart (hack to set max height...)
275
+ if unit != * base {
276
+ realign , * base , * chart = true , unit , * createChart (chart .Height )
277
+ }
277
278
// Update the chart's data points with the scaled values
279
+ if cap (chart .Data ) < len (data ) {
280
+ chart .Data = make ([]float64 , len (data ))
281
+ }
282
+ chart .Data = chart .Data [:len (data )]
278
283
for i , value := range data {
279
284
chart .Data [i ] = value / scale
280
285
}
281
286
// Update the chart's label with the scale units
282
- chart .Border .Label = metric
283
-
284
287
units := dataUnits
285
288
if strings .Contains (metric , "/Percentiles/" ) || strings .Contains (metric , "/pauses/" ) {
286
289
units = timeUnits
287
290
}
291
+ chart .Border .Label = metric
288
292
if len (units [unit ]) > 0 {
289
293
chart .Border .Label += " [" + units [unit ] + "]"
290
294
}
291
295
chart .LineColor = colors [unit ] | termui .AttrBold
292
296
if err != nil {
293
297
chart .LineColor = termui .ColorRed | termui .AttrBold
294
298
}
299
+ return
300
+ }
301
+
302
+ // createChart creates an empty line chart with the default configs.
303
+ func createChart (height int ) * termui.LineChart {
304
+ chart := termui .NewLineChart ()
305
+ if runtime .GOOS == "windows" {
306
+ chart .Mode = "dot"
307
+ }
308
+ chart .DataLabels = []string {"" }
309
+ chart .Height = height
310
+ chart .AxesColor = termui .ColorWhite
311
+ chart .PaddingBottom = - 2
312
+
313
+ chart .Border .LabelFgColor = chart .Border .FgColor | termui .AttrBold
314
+ chart .Border .FgColor = chart .Border .BgColor
315
+
316
+ return chart
295
317
}
296
318
297
319
// updateFooter updates the footer contents based on any encountered errors.
0 commit comments