Skip to content

Commit 1169ec7

Browse files
committed
Merge pull request #1344 from karalabe/monitor-fixes
Monitor fixes
2 parents b0a5be4 + d099a42 commit 1169ec7

File tree

1 file changed

+55
-33
lines changed

1 file changed

+55
-33
lines changed

cmd/geth/monitorcmd.go

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -103,33 +103,17 @@ func monitor(ctx *cli.Context) {
103103
footer.Height = 3
104104

105105
charts := make([]*termui.LineChart, len(monitored))
106+
units := make([]int, len(monitored))
106107
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)
125110
row := termui.Body.Rows[i%rows]
126111
row.Cols = append(row.Cols, termui.NewCol(12/cols, 0, charts[i]))
127112
}
128113
termui.Body.AddRows(termui.NewRow(termui.NewCol(12, 0, footer)))
129-
termui.Body.Align()
130-
termui.Render(termui.Body)
131114

132-
refreshCharts(xeth, monitored, data, charts, ctx, footer)
115+
refreshCharts(xeth, monitored, data, units, charts, ctx, footer)
116+
termui.Body.Align()
133117
termui.Render(termui.Body)
134118

135119
// Watch for various system events, and periodically refresh the charts
@@ -149,7 +133,9 @@ func monitor(ctx *cli.Context) {
149133
termui.Render(termui.Body)
150134
}
151135
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+
}
153139
termui.Render(termui.Body)
154140
}
155141
}
@@ -246,52 +232,88 @@ func fetchMetric(metrics map[string]interface{}, metric string) float64 {
246232

247233
// refreshCharts retrieves a next batch of metrics, and inserts all the new
248234
// 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) {
250236
values, err := retrieveMetrics(xeth)
251237
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+
}
254246
}
255247
updateFooter(ctx, err, footer)
248+
return
256249
}
257250

258251
// updateChart inserts a dataset into a line chart, scaling appropriately as to
259252
// 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) {
261254
dataUnits := []string{"", "K", "M", "G", "T", "E"}
262255
timeUnits := []string{"ns", "µs", "ms", "s", "ks", "ms"}
263256
colors := []termui.Attribute{termui.ColorBlue, termui.ColorCyan, termui.ColorGreen, termui.ColorYellow, termui.ColorRed, termui.ColorRed}
264257

265258
// 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+
}
268262
// 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+
}
272269
}
273270
unit, scale := 0, 1.0
274271
for high >= 1000 {
275272
high, unit, scale = high/1000, unit+1, scale*1000
276273
}
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+
}
277278
// 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)]
278283
for i, value := range data {
279284
chart.Data[i] = value / scale
280285
}
281286
// Update the chart's label with the scale units
282-
chart.Border.Label = metric
283-
284287
units := dataUnits
285288
if strings.Contains(metric, "/Percentiles/") || strings.Contains(metric, "/pauses/") {
286289
units = timeUnits
287290
}
291+
chart.Border.Label = metric
288292
if len(units[unit]) > 0 {
289293
chart.Border.Label += " [" + units[unit] + "]"
290294
}
291295
chart.LineColor = colors[unit] | termui.AttrBold
292296
if err != nil {
293297
chart.LineColor = termui.ColorRed | termui.AttrBold
294298
}
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
295317
}
296318

297319
// updateFooter updates the footer contents based on any encountered errors.

0 commit comments

Comments
 (0)