Skip to content

Commit d099a42

Browse files
committed
cmd/geth: fix monitor panic, don't pre-fill with dummy data
1 parent 7e69392 commit d099a42

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

cmd/geth/monitorcmd.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,6 @@ func monitor(ctx *cli.Context) {
105105
charts := make([]*termui.LineChart, len(monitored))
106106
units := make([]int, len(monitored))
107107
data := make([][]float64, len(monitored))
108-
for i := 0; i < len(data); i++ {
109-
data[i] = make([]float64, 512)
110-
}
111108
for i := 0; i < len(monitored); i++ {
112109
charts[i] = createChart((termui.TermHeight() - footer.Height) / rows)
113110
row := termui.Body.Rows[i%rows]
@@ -238,7 +235,11 @@ func fetchMetric(metrics map[string]interface{}, metric string) float64 {
238235
func refreshCharts(xeth *rpc.Xeth, metrics []string, data [][]float64, units []int, charts []*termui.LineChart, ctx *cli.Context, footer *termui.Par) (realign bool) {
239236
values, err := retrieveMetrics(xeth)
240237
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+
}
242243
if updateChart(metric, data[i], &units[i], charts[i], err) {
243244
realign = true
244245
}
@@ -255,12 +256,16 @@ func updateChart(metric string, data []float64, base *int, chart *termui.LineCha
255256
colors := []termui.Attribute{termui.ColorBlue, termui.ColorCyan, termui.ColorGreen, termui.ColorYellow, termui.ColorRed, termui.ColorRed}
256257

257258
// 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+
}
260262
// 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+
}
264269
}
265270
unit, scale := 0, 1.0
266271
for high >= 1000 {
@@ -271,6 +276,10 @@ func updateChart(metric string, data []float64, base *int, chart *termui.LineCha
271276
realign, *base, *chart = true, unit, *createChart(chart.Height)
272277
}
273278
// 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)]
274283
for i, value := range data {
275284
chart.Data[i] = value / scale
276285
}
@@ -296,7 +305,6 @@ func createChart(height int) *termui.LineChart {
296305
if runtime.GOOS == "windows" {
297306
chart.Mode = "dot"
298307
}
299-
chart.Data = make([]float64, 512)
300308
chart.DataLabels = []string{""}
301309
chart.Height = height
302310
chart.AxesColor = termui.ColorWhite

0 commit comments

Comments
 (0)