2018-02-19 15:25:02 +08:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
2018-04-11 11:08:02 +08:00
|
|
|
"fmt"
|
2018-02-19 15:25:02 +08:00
|
|
|
"time"
|
|
|
|
|
2020-05-13 07:42:45 +08:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
2020-07-25 20:21:15 +08:00
|
|
|
"github.com/VividCortex/ewma"
|
2020-04-24 03:07:08 +08:00
|
|
|
"github.com/xxxserxxx/gotop/v4/devices"
|
2019-01-31 07:20:09 +08:00
|
|
|
|
2020-07-25 20:21:15 +08:00
|
|
|
"github.com/gizak/termui/v3"
|
2020-04-24 03:07:08 +08:00
|
|
|
ui "github.com/xxxserxxx/gotop/v4/termui"
|
2018-02-19 15:25:02 +08:00
|
|
|
)
|
|
|
|
|
2020-04-28 09:33:41 +08:00
|
|
|
type CPUWidget struct {
|
2018-02-19 15:25:02 +08:00
|
|
|
*ui.LineGraph
|
2020-04-28 09:33:41 +08:00
|
|
|
CPUCount int
|
2019-03-01 08:29:52 +08:00
|
|
|
ShowAverageLoad bool
|
2020-04-28 09:33:41 +08:00
|
|
|
ShowPerCPULoad bool
|
2019-03-01 08:29:52 +08:00
|
|
|
updateInterval time.Duration
|
2020-05-13 07:42:45 +08:00
|
|
|
cpuLoads map[string]float64
|
2020-07-25 20:21:15 +08:00
|
|
|
average ewma.MovingAverage
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 08:51:28 +08:00
|
|
|
var cpuLabels []string
|
|
|
|
|
2020-04-28 09:33:41 +08:00
|
|
|
func NewCPUWidget(updateInterval time.Duration, horizontalScale int, showAverageLoad bool, showPerCPULoad bool) *CPUWidget {
|
|
|
|
self := &CPUWidget{
|
2019-03-01 08:29:52 +08:00
|
|
|
LineGraph: ui.NewLineGraph(),
|
2020-04-28 09:33:41 +08:00
|
|
|
CPUCount: len(cpuLabels),
|
2019-03-01 08:29:52 +08:00
|
|
|
updateInterval: updateInterval,
|
|
|
|
ShowAverageLoad: showAverageLoad,
|
2020-04-28 09:33:41 +08:00
|
|
|
ShowPerCPULoad: showPerCPULoad,
|
2020-05-13 07:42:45 +08:00
|
|
|
cpuLoads: make(map[string]float64),
|
2020-07-25 20:21:15 +08:00
|
|
|
average: ewma.NewMovingAverage(),
|
2018-03-04 09:05:52 +08:00
|
|
|
}
|
2020-07-25 20:21:15 +08:00
|
|
|
self.LabelStyles[AVRG] = termui.ModifierBold
|
2020-06-19 08:51:01 +08:00
|
|
|
self.Title = tr.Value("widget.label.cpu")
|
2019-01-13 08:31:37 +08:00
|
|
|
self.HorizontalScale = horizontalScale
|
2018-08-01 05:24:44 +08:00
|
|
|
|
2020-04-28 09:33:41 +08:00
|
|
|
if !(self.ShowAverageLoad || self.ShowPerCPULoad) {
|
|
|
|
if self.CPUCount <= 8 {
|
|
|
|
self.ShowPerCPULoad = true
|
2018-08-01 05:40:51 +08:00
|
|
|
} else {
|
2019-03-01 08:29:52 +08:00
|
|
|
self.ShowAverageLoad = true
|
2018-08-01 05:40:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
if self.ShowAverageLoad {
|
2020-07-25 20:21:15 +08:00
|
|
|
self.Data[AVRG] = []float64{0}
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
2020-04-28 09:33:41 +08:00
|
|
|
if self.ShowPerCPULoad {
|
2020-02-29 00:03:41 +08:00
|
|
|
cpus := make(map[string]int)
|
2020-04-28 09:33:41 +08:00
|
|
|
devices.UpdateCPU(cpus, self.updateInterval, self.ShowPerCPULoad)
|
2020-02-28 08:51:28 +08:00
|
|
|
for k, v := range cpus {
|
2020-02-29 00:03:41 +08:00
|
|
|
self.Data[k] = []float64{float64(v)}
|
2018-08-01 05:24:44 +08:00
|
|
|
}
|
|
|
|
}
|
2018-04-13 11:00:34 +08:00
|
|
|
|
2018-12-19 10:12:22 +08:00
|
|
|
self.update()
|
2018-11-30 10:17:13 +08:00
|
|
|
|
2018-02-19 15:25:02 +08:00
|
|
|
go func() {
|
2019-03-01 08:29:52 +08:00
|
|
|
for range time.NewTicker(self.updateInterval).C {
|
2018-03-28 05:27:23 +08:00
|
|
|
self.update()
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-03-28 05:27:23 +08:00
|
|
|
return self
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
2020-05-13 07:42:45 +08:00
|
|
|
const AVRG = "AVRG"
|
|
|
|
|
2020-04-28 09:33:41 +08:00
|
|
|
func (cpu *CPUWidget) EnableMetric() {
|
|
|
|
if cpu.ShowAverageLoad {
|
2020-05-13 07:42:45 +08:00
|
|
|
metrics.NewGauge(makeName("cpu", " avg"), func() float64 {
|
|
|
|
return cpu.cpuLoads[AVRG]
|
2020-02-18 23:44:29 +08:00
|
|
|
})
|
|
|
|
} else {
|
2020-02-29 00:03:41 +08:00
|
|
|
cpus := make(map[string]int)
|
2020-04-28 09:33:41 +08:00
|
|
|
devices.UpdateCPU(cpus, cpu.updateInterval, cpu.ShowPerCPULoad)
|
2020-02-28 08:51:28 +08:00
|
|
|
for key, perc := range cpus {
|
2020-05-13 07:42:45 +08:00
|
|
|
kc := key
|
|
|
|
cpu.cpuLoads[key] = float64(perc)
|
|
|
|
metrics.NewGauge(makeName("cpu", key), func() float64 {
|
|
|
|
return cpu.cpuLoads[kc]
|
2020-02-18 23:44:29 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 09:33:41 +08:00
|
|
|
func (cpu *CPUWidget) Scale(i int) {
|
|
|
|
cpu.LineGraph.HorizontalScale = i
|
2020-02-13 10:15:49 +08:00
|
|
|
}
|
|
|
|
|
2020-04-28 09:33:41 +08:00
|
|
|
func (cpu *CPUWidget) update() {
|
2020-07-25 20:21:15 +08:00
|
|
|
go func() {
|
|
|
|
cpus := make(map[string]int)
|
|
|
|
devices.UpdateCPU(cpus, cpu.updateInterval, true)
|
|
|
|
cpu.Lock()
|
|
|
|
defer cpu.Unlock()
|
|
|
|
// AVG = ((AVG*i)+n)/(i+1)
|
|
|
|
var sum int
|
|
|
|
for key, percent := range cpus {
|
|
|
|
sum += percent
|
|
|
|
if cpu.ShowPerCPULoad {
|
2020-04-28 09:33:41 +08:00
|
|
|
cpu.Data[key] = append(cpu.Data[key], float64(percent))
|
2020-07-21 18:49:51 +08:00
|
|
|
cpu.Labels[key] = fmt.Sprintf("%3d%%", percent)
|
2020-05-13 07:42:45 +08:00
|
|
|
cpu.cpuLoads[key] = float64(percent)
|
2018-08-17 07:44:44 +08:00
|
|
|
}
|
2020-07-25 20:21:15 +08:00
|
|
|
}
|
|
|
|
if cpu.ShowAverageLoad {
|
|
|
|
cpu.average.Add(float64(sum) / float64(len(cpus)))
|
|
|
|
avg := cpu.average.Value()
|
|
|
|
cpu.Data[AVRG] = append(cpu.Data[AVRG], avg)
|
|
|
|
cpu.Labels[AVRG] = fmt.Sprintf("%3.0f%%", avg)
|
|
|
|
cpu.cpuLoads[AVRG] = avg
|
|
|
|
}
|
|
|
|
}()
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|