@@ -105,9 +105,6 @@ func monitor(ctx *cli.Context) {
105
105
charts := make ([]* termui.LineChart , len (monitored ))
106
106
units := make ([]int , len (monitored ))
107
107
data := make ([][]float64 , len (monitored ))
108
- for i := 0 ; i < len (data ); i ++ {
109
- data [i ] = make ([]float64 , 512 )
110
- }
111
108
for i := 0 ; i < len (monitored ); i ++ {
112
109
charts [i ] = createChart ((termui .TermHeight () - footer .Height ) / rows )
113
110
row := termui .Body .Rows [i % rows ]
@@ -238,7 +235,11 @@ func fetchMetric(metrics map[string]interface{}, metric string) float64 {
238
235
func refreshCharts (xeth * rpc.Xeth , metrics []string , data [][]float64 , units []int , charts []* termui.LineChart , ctx * cli.Context , footer * termui.Par ) (realign bool ) {
239
236
values , err := retrieveMetrics (xeth )
240
237
for i , metric := range metrics {
241
- data [i ] = append ([]float64 {fetchMetric (values , metric )}, data [i ][:len (data [i ])- 1 ]... )
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
+ }
242
243
if updateChart (metric , data [i ], & units [i ], charts [i ], err ) {
243
244
realign = true
244
245
}
@@ -255,12 +256,16 @@ func updateChart(metric string, data []float64, base *int, chart *termui.LineCha
255
256
colors := []termui.Attribute {termui .ColorBlue , termui .ColorCyan , termui .ColorGreen , termui .ColorYellow , termui .ColorRed , termui .ColorRed }
256
257
257
258
// Extract only part of the data that's actually visible
258
- data = data [:chart .Width * 2 ]
259
-
259
+ if chart .Width * 2 < len (data ) {
260
+ data = data [:chart .Width * 2 ]
261
+ }
260
262
// Find the maximum value and scale under 1K
261
- high := data [0 ]
262
- for _ , value := range data [1 :] {
263
- 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
+ }
264
269
}
265
270
unit , scale := 0 , 1.0
266
271
for high >= 1000 {
@@ -271,6 +276,10 @@ func updateChart(metric string, data []float64, base *int, chart *termui.LineCha
271
276
realign , * base , * chart = true , unit , * createChart (chart .Height )
272
277
}
273
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 )]
274
283
for i , value := range data {
275
284
chart .Data [i ] = value / scale
276
285
}
@@ -296,7 +305,6 @@ func createChart(height int) *termui.LineChart {
296
305
if runtime .GOOS == "windows" {
297
306
chart .Mode = "dot"
298
307
}
299
- chart .Data = make ([]float64 , 512 )
300
308
chart .DataLabels = []string {"" }
301
309
chart .Height = height
302
310
chart .AxesColor = termui .ColorWhite
0 commit comments