-
Notifications
You must be signed in to change notification settings - Fork 2
/
gauges.go
97 lines (72 loc) · 2.17 KB
/
gauges.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package components
import (
"fmt"
"math"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
"github.com/aenix-io/talm/internal/pkg/dashboard/apidata"
)
// SystemGauges quickly show CPU/mem load.
type SystemGauges struct {
*TermUIWrapper
inner *systemGaugesInner
}
// NewSystemGauges creates SystemGauges.
func NewSystemGauges() *SystemGauges {
inner := systemGaugesInner{
Block: *ui.NewBlock(),
}
inner.cpuGauge = widgets.NewGauge()
inner.cpuGauge.Border = false
inner.cpuGauge.Title = "CPU"
inner.memGauge = widgets.NewGauge()
inner.memGauge.Title = "MEM"
inner.memGauge.Border = false
wrapper := NewTermUIWrapper(&inner)
widget := &SystemGauges{
TermUIWrapper: wrapper,
inner: &inner,
}
widget.SetBorderPadding(1, 0, 0, 0)
return widget
}
// OnAPIDataChange implements the APIDataListener interface.
func (widget *SystemGauges) OnAPIDataChange(node string, data *apidata.Data) {
nodeData := data.Nodes[node]
if nodeData == nil {
widget.inner.cpuGauge.Label = noData
widget.inner.cpuGauge.Percent = 0
widget.inner.memGauge.Label = noData
widget.inner.memGauge.Percent = 0
} else {
memUsed := nodeData.MemUsage()
widget.inner.memGauge.Percent = int(math.Round(memUsed * 100.0))
widget.inner.memGauge.Label = fmt.Sprintf("%.1f%%", memUsed*100.0)
cpuUsed := nodeData.CPUUsageByName("usage")
widget.inner.cpuGauge.Percent = int(math.Round(cpuUsed * 100.0))
widget.inner.cpuGauge.Label = fmt.Sprintf("%.1f%%", cpuUsed*100.0)
}
}
type systemGaugesInner struct {
ui.Block
cpuGauge *widgets.Gauge
memGauge *widgets.Gauge
}
// Draw implements io.Drawable.
func (widget *systemGaugesInner) Draw(buf *ui.Buffer) {
width := widget.Dx()
height := widget.Dy()
y := 0
itemHeight := 2
for _, item := range []ui.Drawable{widget.cpuGauge, widget.memGauge} {
item.SetRect(widget.Min.X, widget.Min.Y+y, widget.Min.X+width, widget.Min.Y+y+itemHeight+1)
item.Draw(buf)
y += itemHeight
if y > height {
break
}
}
}