2018-02-19 15:25:02 +08:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
2018-02-25 13:35:57 +08:00
|
|
|
"fmt"
|
2019-01-01 08:55:50 +08:00
|
|
|
"image"
|
2018-03-04 09:05:52 +08:00
|
|
|
"sort"
|
2018-02-19 15:25:02 +08:00
|
|
|
"time"
|
|
|
|
|
2019-03-08 15:15:41 +08:00
|
|
|
ui "github.com/gizak/termui/v3"
|
2020-02-18 23:44:29 +08:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2019-03-01 08:29:52 +08:00
|
|
|
|
2020-02-14 06:26:28 +08:00
|
|
|
"github.com/xxxserxxx/gotop/utils"
|
2019-03-01 08:29:52 +08:00
|
|
|
)
|
|
|
|
|
2020-01-24 06:17:14 +08:00
|
|
|
type TempScale rune
|
2019-03-01 08:29:52 +08:00
|
|
|
|
|
|
|
const (
|
2020-02-26 04:26:52 +08:00
|
|
|
Celsius TempScale = 'C'
|
2020-01-24 06:17:14 +08:00
|
|
|
Fahrenheit = 'F'
|
2018-02-19 15:25:02 +08:00
|
|
|
)
|
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
type TempWidget struct {
|
|
|
|
*ui.Block // inherits from Block instead of a premade Widget
|
|
|
|
updateInterval time.Duration
|
|
|
|
Data map[string]int
|
|
|
|
TempThreshold int
|
|
|
|
TempLowColor ui.Color
|
|
|
|
TempHighColor ui.Color
|
|
|
|
TempScale TempScale
|
2020-02-18 23:44:29 +08:00
|
|
|
tempsMetric map[string]prometheus.Gauge
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
2020-02-21 08:56:52 +08:00
|
|
|
// TODO: state:deferred 156 Added temperatures for NVidia GPUs (azak-azkaran/master). Crashes on non-nvidia machines.
|
2019-03-08 15:43:10 +08:00
|
|
|
func NewTempWidget(tempScale TempScale) *TempWidget {
|
2019-03-01 08:29:52 +08:00
|
|
|
self := &TempWidget{
|
|
|
|
Block: ui.NewBlock(),
|
|
|
|
updateInterval: time.Second * 5,
|
|
|
|
Data: make(map[string]int),
|
|
|
|
TempThreshold: 80,
|
|
|
|
TempScale: tempScale,
|
2018-02-25 13:35:57 +08:00
|
|
|
}
|
2019-01-01 08:55:50 +08:00
|
|
|
self.Title = " Temperatures "
|
2018-02-19 15:25:02 +08:00
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
if tempScale == Fahrenheit {
|
|
|
|
self.TempThreshold = utils.CelsiusToFahrenheit(self.TempThreshold)
|
2018-11-14 01:29:09 +08:00
|
|
|
}
|
|
|
|
|
2018-04-13 11:00:34 +08:00
|
|
|
self.update()
|
|
|
|
|
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 {
|
2019-03-08 15:43:10 +08:00
|
|
|
self.Lock()
|
2018-03-28 05:27:23 +08:00
|
|
|
self.update()
|
2019-03-08 15:43:10 +08:00
|
|
|
self.Unlock()
|
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-02-18 23:44:29 +08:00
|
|
|
func (self *TempWidget) EnableMetric() {
|
|
|
|
self.tempsMetric = make(map[string]prometheus.Gauge)
|
|
|
|
for k, v := range self.Data {
|
|
|
|
gauge := prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Namespace: "gotop",
|
|
|
|
Subsystem: "temp",
|
|
|
|
Name: k,
|
|
|
|
})
|
|
|
|
gauge.Set(float64(v))
|
|
|
|
prometheus.MustRegister(gauge)
|
|
|
|
self.tempsMetric[k] = gauge
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
// Custom Draw method instead of inheriting from a generic Widget.
|
|
|
|
func (self *TempWidget) Draw(buf *ui.Buffer) {
|
2019-01-01 08:55:50 +08:00
|
|
|
self.Block.Draw(buf)
|
2018-02-25 13:35:57 +08:00
|
|
|
|
2018-03-04 09:05:52 +08:00
|
|
|
var keys []string
|
2018-05-11 11:32:23 +08:00
|
|
|
for key := range self.Data {
|
|
|
|
keys = append(keys, key)
|
2018-03-04 09:05:52 +08:00
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for y, key := range keys {
|
2019-01-01 08:55:50 +08:00
|
|
|
if y+1 > self.Inner.Dy() {
|
2018-02-25 13:35:57 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
var fg ui.Color
|
|
|
|
if self.Data[key] < self.TempThreshold {
|
|
|
|
fg = self.TempLowColor
|
|
|
|
} else {
|
|
|
|
fg = self.TempHighColor
|
2018-02-25 13:35:57 +08:00
|
|
|
}
|
|
|
|
|
2019-01-01 08:55:50 +08:00
|
|
|
s := ui.TrimString(key, (self.Inner.Dx() - 4))
|
|
|
|
buf.SetString(s,
|
|
|
|
ui.Theme.Default,
|
2019-01-15 11:47:44 +08:00
|
|
|
image.Pt(self.Inner.Min.X, self.Inner.Min.Y+y),
|
2019-01-01 08:55:50 +08:00
|
|
|
)
|
2019-03-01 08:29:52 +08:00
|
|
|
|
2020-02-21 08:56:52 +08:00
|
|
|
// TODO: state:merge #184 or #177 degree symbol (BartWillems/master, fleaz/master)
|
2020-02-19 03:47:52 +08:00
|
|
|
if self.tempsMetric != nil {
|
|
|
|
self.tempsMetric[key].Set(float64(self.Data[key]))
|
|
|
|
}
|
2020-01-24 06:17:14 +08:00
|
|
|
temperature := fmt.Sprintf("%3d°%c", self.Data[key], self.TempScale)
|
|
|
|
|
|
|
|
buf.SetString(
|
|
|
|
temperature,
|
|
|
|
ui.NewStyle(fg),
|
|
|
|
image.Pt(self.Inner.Max.X-(len(temperature)-1), self.Inner.Min.Y+y),
|
|
|
|
)
|
2018-02-25 13:35:57 +08:00
|
|
|
}
|
|
|
|
}
|