WIP components (as built)

This commit is contained in:
ZacharyZhang-NY committed 2026-10-05 15:51:57 -04:00
1 parent 1095ed2b9d
commit 7e4f275f0a
80 files changed
+2754 -132

No files matched your search

+53 -14
View File
@@ -9,7 +9,7 @@ import (
"mujicaui/internal"
)
// UptimeDay is one day's availability, 0 to 1; NaN means no data.
// UptimeDay holds availability; NaN marks missing data.
type UptimeDay struct {
Date Date
Uptime float64
@@ -20,11 +20,11 @@ type UptimeDay struct {
// UptimeBarOptions configure an UptimeBar; Frame.Height 0 means 36.
type UptimeBarOptions struct {
Frame ChartFrame
// Degraded and Down are the uptime limits below which a day is marked; zero means 0.999 and 0.95.
// Degraded and Down default to 0.999 and 0.95.
Degraded, Down float64
}
// UptimeBar draws one bar per day by availability; hover or arrows read a day's detail.
// UptimeBar shows daily availability with pointer and keyboard readouts.
func UptimeBar(c *ui.Context, days []UptimeDay, opts UptimeBarOptions) ChartResult {
deg, down := opts.Degraded, opts.Down
if deg == 0 {
@@ -36,17 +36,22 @@ func UptimeBar(c *ui.Context, days []UptimeDay, opts UptimeBarOptions) ChartResu
if !(down < deg && deg <= 1 && down > 0) {
panic("mujica: UptimeBar needs 0 < Down < Degraded <= 1")
}
for i, d := range days {
if !math.IsNaN(d.Uptime) && (d.Uptime < 0 || d.Uptime > 1) {
panic(fmt.Sprintf("mujica: UptimeBar day %d uptime %v outside 0..1", i, d.Uptime))
}
}
if opts.Frame.Height == 0 {
opts.Frame.Height = 36
}
k := Tokens(c)
ch := newChart(c, opts.Frame)
n := len(days)
cache := ui.Local(ch.res.Element, "mujica.uptime", func() uptimeCache { return uptimeCache{} })
if cache.key != chartID(days) {
for i, d := range days {
if !math.IsNaN(d.Uptime) && (d.Uptime < 0 || d.Uptime > 1) {
panic(fmt.Sprintf("mujica: UptimeBar day %d outside 0..1", i))
}
}
cache.key, cache.width = chartID(days), -1
}
band := func(r ui.Rect) internal.Band { return internal.Band{N: n, R0: r.X, R1: r.X + r.W, Pad: 0.25} }
level := func(v float64) int { // 0 ok, 1 degraded, 2 down, 3 no data
switch {
@@ -59,19 +64,27 @@ func UptimeBar(c *ui.Context, days []UptimeDay, opts UptimeBarOptions) ChartResu
}
return 0
}
pal := internal.Palette(k)
words := [...]string{msg(c, "uptime_bar.ok"), msg(c, "uptime_bar.degraded"), msg(c, "uptime_bar.down"), msg(c, "uptime_bar.nodata")}
res, _ := ch.build(chartSpec{
n: n,
draw: func(p *ui.Painter, r ui.Rect, active int) {
b := band(r)
for i, d := range days {
x, w := internal.Snap(p, b.Start(i)), max(b.Width(), 1)
cache.prepare(days, max(1, int(math.Ceil(float64(r.W*p.Scale())))))
for _, slot := range cache.slots {
d := days[slot.index]
x, w := internal.Snap(p, b.Start(slot.first)), max(b.Start(slot.last)+b.Width()-b.Start(slot.first), 1)
lv := level(d.Uptime)
col := [...]ui.Color{k.Success, k.Warning, k.Danger, k.Border}[lv]
// Shorter bars mark bad days, so color is not the only cue.
col := [...]ui.Color{pal[2], pal[3], k.Danger, k.TextMuted}[lv]
// Shorter bars distinguish degraded days and outages.
h := r.H * [...]float32{1, 0.75, 0.5, 1}[lv]
p.Fill(ui.Rect{X: x, Y: r.Y + r.H - h, W: w, H: h}, col, 1)
if i == active {
bar := ui.Rect{X: x, Y: r.Y + r.H - h, W: w, H: h}
if lv == 3 {
p.Stroke(bar, col, 1, 1)
} else {
p.Fill(bar, col, 1)
}
if active >= slot.first && active <= slot.last {
p.Stroke(ui.Rect{X: x - 2, Y: r.Y - 2, W: w + 4, H: r.H + 4}, k.Focus, 2, 1.5)
}
}
@@ -94,3 +107,29 @@ func UptimeBar(c *ui.Context, days []UptimeDay, opts UptimeBarOptions) ChartResu
})
return res
}
type uptimeSlot struct{ first, last, index int }
type uptimeCache struct {
key string
width int
slots []uptimeSlot
}
func (s *uptimeCache) prepare(days []UptimeDay, width int) {
if s.width == width {
return
}
s.width, s.slots = width, s.slots[:0]
n := len(days)
for first := 0; first < n; {
last := min(n-1, max(first, ((first*width/n+1)*n-1)/width))
index := first
for i := first + 1; i <= last; i++ {
if math.IsNaN(days[index].Uptime) || !math.IsNaN(days[i].Uptime) && days[i].Uptime < days[index].Uptime {
index = i
}
}
s.slots = append(s.slots, uptimeSlot{first, last, index})
first = last + 1
}
}