WIP components (as built)
This commit is contained in:
80 files changed
+2754
-132
No files matched your search
+23
-6
@@ -2,6 +2,7 @@ package mujica
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"math"
|
||||
|
||||
"github.com/egoist/mygo/ui"
|
||||
@@ -22,10 +23,15 @@ type ResourceGaugeOptions struct {
|
||||
Warn, Critical float64
|
||||
// Height of each dial; zero means 150.
|
||||
Height float32
|
||||
Status DataStatus
|
||||
Error string
|
||||
}
|
||||
|
||||
// ResourceGauge shows CPU, memory and disk dials with named normal, high and critical bands.
|
||||
func ResourceGauge(c *ui.Context, readings []ResourceReading, opts ResourceGaugeOptions) *ui.Element {
|
||||
// ResourceGauge shows resource usage with named threshold bands.
|
||||
func ResourceGauge(c *ui.Context, readings []ResourceReading, opts ResourceGaugeOptions) ChoiceResult {
|
||||
if opts.Status != DataReady && opts.Status != DataLoading && opts.Status != DataFailed {
|
||||
panic("mujica: ResourceGauge needs a known status")
|
||||
}
|
||||
warn, crit := opts.Warn, opts.Critical
|
||||
if warn == 0 {
|
||||
warn = 0.7
|
||||
@@ -42,16 +48,23 @@ func ResourceGauge(c *ui.Context, readings []ResourceReading, opts ResourceGauge
|
||||
}
|
||||
u := Density(c).Unit()
|
||||
row := ui.Row(c).Wrap().Gap(u * 4).Shrink(0)
|
||||
result := ChoiceResult{Element: row}
|
||||
row.Children(func() {
|
||||
if len(readings) == 0 {
|
||||
ui.Column(c).Height(h).Children(func() {
|
||||
result.changed = dataPlaceholder(c, opts.Status, opts.Error, msg(c, "resource_gauge.empty"), "")
|
||||
})
|
||||
return
|
||||
}
|
||||
for i, r := range readings {
|
||||
if r.Name == "" || !(r.Total > 0) || r.Used < 0 || r.Used > r.Total || math.IsNaN(r.Used) {
|
||||
if r.Name == "" || !(r.Total > 0) || r.Used < 0 || r.Used > r.Total || math.IsNaN(r.Used) || math.IsInf(r.Used, 0) || math.IsInf(r.Total, 0) {
|
||||
panic(fmt.Sprintf("mujica: ResourceGauge reading %d needs a Name and 0 <= Used <= Total", i))
|
||||
}
|
||||
frac := r.Used / r.Total
|
||||
ui.Column(c).Key(r.Name).Width(200).Gap(u).AlignItems(ui.Center).Children(func() {
|
||||
ui.Text(c, r.Name).FontWeight(600)
|
||||
Gauge(c, frac*100, GaugeOptions{
|
||||
Frame: ChartFrame{Label: r.Name, Height: h}, Min: 0, Max: 100, Unit: "%",
|
||||
gauge := Gauge(c, frac*100, GaugeOptions{
|
||||
Frame: ChartFrame{Label: r.Name, Height: h, Status: opts.Status, Error: opts.Error}, Min: 0, Max: 100, Unit: "%",
|
||||
Format: func(v float64) string { return fmt.Sprintf("%.0f", v) },
|
||||
Segments: []GaugeSegment{
|
||||
{To: warn * 100, Label: msg(c, "resource_gauge.normal"), Tone: GaugeGood},
|
||||
@@ -59,11 +72,15 @@ func ResourceGauge(c *ui.Context, readings []ResourceReading, opts ResourceGauge
|
||||
{To: 100, Label: msg(c, "resource_gauge.critical"), Tone: GaugeDanger},
|
||||
},
|
||||
})
|
||||
result.changed = result.changed || gauge.Retried()
|
||||
if r.Unit != "" {
|
||||
agentCaption(c, fmt.Sprintf("%s / %s %s", sandboxNum(r.Used), sandboxNum(r.Total), r.Unit))
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
return row
|
||||
if result.changed {
|
||||
slog.Debug("mujica: resource_gauge retry")
|
||||
}
|
||||
return result
|
||||
}
|
||||
Reference in new issue
Block a user