wip-devtools

This commit is contained in:
ZacharyZhang-NY committed 2026-10-05 14:47:28 -04:00
1 parent 92c42620f4
commit 0726d68715
9 files changed
+345 -8

No files matched your search

+114
View File
@@ -0,0 +1,114 @@
package mujica
import (
"fmt"
"time"
"github.com/egoist/mygo/ui"
"mujicaui/internal"
"mujicaui/theme"
)
// IncidentStatus is where an incident stands. The zero value is IncidentInvestigating.
type IncidentStatus int
const (
IncidentInvestigating IncidentStatus = iota
IncidentIdentified
IncidentMonitoring
IncidentResolved
)
// IncidentImpact is how badly users are affected. The zero value is ImpactNone.
type IncidentImpact int
const (
ImpactNone IncidentImpact = iota
ImpactMinor
ImpactMajor
ImpactCritical
)
// IncidentUpdate is one timeline entry, oldest first.
type IncidentUpdate struct {
At time.Time
Status IncidentStatus
Text string
}
// Incident is what the caller reports about an incident.
type Incident struct {
Title string
Status IncidentStatus
Impact IncidentImpact
Started time.Time
Updates []IncidentUpdate
}
// IncidentCard shows an incident's title, status, impact and update timeline.
func IncidentCard(c *ui.Context, inc Incident) *ui.Element {
if inc.Title == "" {
panic("mujica: IncidentCard needs a Title")
}
status := incidentStatusWord(c, inc.Status)
if inc.Impact < ImpactNone || inc.Impact > ImpactCritical {
panic(fmt.Sprintf("mujica: unknown IncidentImpact %d", int(inc.Impact)))
}
k := Tokens(c)
u := Density(c).Unit()
sev := [...]Severity{SeverityInfo, SeverityWarning, SeverityError, SeverityError}[inc.Impact]
impact := msg(c, [...]string{"incident_card.none", "incident_card.minor", "incident_card.major", "incident_card.critical"}[inc.Impact])
card := agentCard(c).Label(inc.Title + ", " + status)
card.Children(func() {
ui.Row(c).Gap(u * 2).AlignItems(ui.Center).Children(func() {
devtoolsTitle(c, inc.Title).Grow(1).Shrink(1)
if inc.Status == IncidentResolved {
devtoolsSeverity(c, SeveritySuccess, status)
} else {
devtoolsSeverity(c, SeverityWarning, status)
}
})
ui.Row(c).Gap(u * 3).AlignItems(ui.Center).Children(func() {
devtoolsSeverity(c, sev, fmt.Sprintf(msg(c, "incident_card.impact"), impact))
if !inc.Started.IsZero() {
agentCaption(c, fmt.Sprintf(msg(c, "incident_card.started"), inc.Started.Format("2006-01-02 15:04")))
}
})
for i, up := range inc.Updates {
incidentUpdate(c, k, up, i == len(inc.Updates)-1)
}
})
return card
}
// incidentUpdate draws one timeline entry on a diamond rail.
func incidentUpdate(c *ui.Context, k theme.Tokens, up IncidentUpdate, last bool) {
u := Density(c).Unit()
ui.Row(c).Gap(u * 3).AlignItems(ui.Stretch).Children(func() {
ui.Box(c).Width(12).Shrink(0).Draw(func(p *ui.Painter, r ui.Rect) {
cx := internal.Snap(p, r.X+r.W/2)
cy := internal.Snap(p, r.Y+9)
if !last {
p.Line(cx, cy+5, cx, r.Y+r.H, 1, k.ControlBorder)
}
p.FillPath(internal.Diamond(cx, cy, 9), k.Ornament)
})
ui.Column(c).Grow(1).Shrink(1).Gap(u / 2).Padding(0, 0, u*2, 0).Children(func() {
ui.Row(c).Gap(u * 2).Children(func() {
ui.Text(c, incidentStatusWord(c, up.Status)).FontWeight(600)
if !up.At.IsZero() {
agentCaption(c, up.At.Format("15:04"))
}
})
ui.Text(c, up.Text).TextColor(k.TextMuted)
})
})
}
func incidentStatusWord(c *ui.Context, s IncidentStatus) string {
if s < IncidentInvestigating || s > IncidentResolved {
panic(fmt.Sprintf("mujica: unknown IncidentStatus %d", int(s)))
}
return msg(c, [...]string{"incident_card.investigating", "incident_card.identified", "incident_card.monitoring", "incident_card.resolved"}[s])
}
+55
View File
@@ -0,0 +1,55 @@
package mujica
import (
"testing"
"time"
"github.com/egoist/mygo/ui"
)
func incidentSample() Incident {
t0 := time.Date(2026, 10, 5, 21, 4, 0, 0, time.UTC)
return Incident{Title: "Checkout errors in eu-west", Status: IncidentMonitoring, Impact: ImpactMajor, Started: t0,
Updates: []IncidentUpdate{
{At: t0, Status: IncidentInvestigating, Text: "Error rate rose to 8%."},
{At: t0.Add(12 * time.Minute), Status: IncidentIdentified, Text: "A bad config push to the payment proxy."},
{At: t0.Add(30 * time.Minute), Status: IncidentMonitoring, Text: "Rolled back; errors are falling."},
}}
}
func TestIncidentCardShowsStatusImpactTimeline(t *testing.T) {
inc := incidentSample()
tt := ui.NewTester(func(c *ui.Context) {
Use(c, Settings{})
IncidentCard(c, inc)
}, 560, 500)
for _, s := range []string{"Checkout errors in eu-west", "Monitoring", "Impact: major", "Started 2026-10-05 21:04",
"Investigating", "21:16", "Rolled back; errors are falling."} {
if !tt.HasText(s) {
t.Fatalf("missing %q in %q", s, tt.Texts())
}
}
inc.Status = IncidentResolved
tt.Frame()
if !tt.HasText("Resolved") {
t.Fatal("status did not update")
}
}
func TestIncidentCardRejectsUnknownStatus(t *testing.T) {
defer func() {
if recover() == nil {
t.Fatal("no panic")
}
}()
ui.NewTester(func(c *ui.Context) {
Use(c, Settings{})
IncidentCard(c, Incident{Title: "x", Status: 9})
}, 200, 200)
}
func TestIncidentCardSnapshot(t *testing.T) {
snapshot(t, "incident_card", 540, 360, func(c *ui.Context) {
ui.Column(c).Padding(16).Children(func() { IncidentCard(c, incidentSample()) })
})
}
+28 -8
View File
@@ -11,13 +11,33 @@ func init() {
"stat_card.change": {"Change %s", "变化 %s"},
"devtools.ago": {"%s ago", "%s 前"},
"alert_list.info": {"Info", "提示"},
"alert_list.warning": {"Warning", "警告"},
"alert_list.critical": {"Critical", "严重"},
"alert_list.ack": {"Acknowledge", "确认"},
"alert_list.acked": {"Acknowledged", "已确认"},
"alert_list.silence": {"Silence 1 h", "静默 1 小时"},
"alert_list.silenced": {"Silenced until %s", "静默至 %s"},
"alert_list.empty": {"No alerts", "没有告警"},
"alert_list.info": {"Info", "提示"},
"alert_list.warning": {"Warning", "警告"},
"alert_list.critical": {"Critical", "严重"},
"alert_list.ack": {"Acknowledge", "确认"},
"alert_list.acked": {"Acknowledged", "已确认"},
"alert_list.silence": {"Silence 1 h", "静默 1 小时"},
"alert_list.silenced": {"Silenced until %s", "静默至 %s"},
"incident_card.investigating": {"Investigating", "调查中"},
"incident_card.identified": {"Identified", "已定位"},
"incident_card.monitoring": {"Monitoring", "观察中"},
"incident_card.resolved": {"Resolved", "已解决"},
"incident_card.none": {"none", "无"},
"incident_card.minor": {"minor", "轻微"},
"incident_card.major": {"major", "严重"},
"incident_card.critical": {"critical", "致命"},
"incident_card.impact": {"Impact: %s", "影响:%s"},
"incident_card.started": {"Started %s", "开始于 %s"},
"service_status.operational": {"Operational", "正常"},
"service_status.degraded": {"Degraded", "降级"},
"service_status.outage": {"Outage", "中断"},
"service_status.all_ok": {"All systems operational", "所有服务正常"},
"service_status.some_degraded": {"Some services degraded", "部分服务降级"},
"service_status.some_down": {"Some services down", "部分服务中断"},
"service_status.counts": {"%d up · %d degraded · %d down", "%d 正常 · %d 降级 · %d 中断"},
"service_status.empty": {"No services", "没有服务"},
"alert_list.empty": {"No alerts", "没有告警"},
})
}
+91
View File
@@ -0,0 +1,91 @@
package mujica
import (
"fmt"
"github.com/egoist/mygo/ui"
"mujicaui/theme"
)
// ServiceHealth is a service's state. The zero value is ServiceOperational.
type ServiceHealth int
const (
ServiceOperational ServiceHealth = iota
ServiceDegraded
ServiceOutage
)
// ServiceItem is one service the caller reports.
type ServiceItem struct {
Name string
Health ServiceHealth
// Note explains a degraded or down service.
Note string
}
// ServiceStatusOptions configure a ServiceStatus.
type ServiceStatusOptions struct {
// MaxHeight caps the scrolling list; zero means 360.
MaxHeight float32
}
// ServiceStatus lists services as operational, degraded or down, under an overall summary.
func ServiceStatus(c *ui.Context, services []ServiceItem, opts ServiceStatusOptions) *ui.Element {
if opts.MaxHeight < 0 {
panic("mujica: ServiceStatus MaxHeight is negative")
}
worst, counts := ServiceOperational, [3]int{}
for i, s := range services {
if s.Name == "" || s.Health < ServiceOperational || s.Health > ServiceOutage {
panic(fmt.Sprintf("mujica: ServiceStatus service %d needs a Name and a known Health", i))
}
worst = max(worst, s.Health)
counts[s.Health]++
}
maxH := opts.MaxHeight
if maxH == 0 {
maxH = 360
}
k := Tokens(c)
u := Density(c).Unit()
summary := msg(c, [...]string{"service_status.all_ok", "service_status.some_degraded", "service_status.some_down"}[worst])
col := ui.Column(c).Shrink(0).Gap(u * 2).Label(summary)
col.Children(func() {
ui.Row(c).Gap(u * 3).AlignItems(ui.Center).Padding(u*3).Radius(theme.CardRadius).Border(theme.BorderWidth, k.Border).
Background(k.Surface).Role(ui.RoleStatus).Label(summary).Children(func() {
devtoolsSeverity(c, serviceSeverity(worst), summary)
ui.Box(c).Grow(1)
agentCaption(c, fmt.Sprintf(msg(c, "service_status.counts"), counts[0], counts[1], counts[2]))
})
if len(services) == 0 {
agentCaption(c, msg(c, "service_status.empty"))
return
}
agentRows(c, len(services), func(i int) {
s := services[i]
word := serviceWord(c, s.Health)
ui.Row(c).Key(i).Gap(u * 3).AlignItems(ui.Center).MinHeight(Density(c).ControlHeight()).Padding(u, u*3).
Label(s.Name + ", " + word).Children(func() {
ui.Column(c).Grow(1).Shrink(1).Children(func() {
ui.Text(c, s.Name).SingleLine()
if s.Note != "" {
agentCaption(c, s.Note)
}
})
devtoolsSeverity(c, serviceSeverity(s.Health), word)
})
}).Shrink(1).MaxHeight(maxH).Background(k.Surface).Border(theme.BorderWidth, k.Border).
Radius(theme.CardRadius).Dividers(theme.BorderWidth, k.Border)
})
return col
}
func serviceSeverity(h ServiceHealth) Severity {
return [...]Severity{SeveritySuccess, SeverityWarning, SeverityError}[h]
}
func serviceWord(c *ui.Context, h ServiceHealth) string {
return msg(c, [...]string{"service_status.operational", "service_status.degraded", "service_status.outage"}[h])
}
+57
View File
@@ -0,0 +1,57 @@
package mujica
import (
"fmt"
"testing"
"github.com/egoist/mygo/ui"
)
var servicesSample = []ServiceItem{
{Name: "API", Health: ServiceOperational},
{Name: "Search", Health: ServiceDegraded, Note: "Indexing is slow"},
{Name: "Payments", Health: ServiceOutage, Note: "Provider unreachable"},
}
func TestServiceStatusSummaryAndRows(t *testing.T) {
services := servicesSample
tt := ui.NewTester(func(c *ui.Context) {
Use(c, Settings{})
ServiceStatus(c, services, ServiceStatusOptions{})
}, 520, 400)
for _, s := range []string{"Some services down", "1 up · 1 degraded · 1 down", "Degraded", "Outage", "Provider unreachable"} {
if !tt.HasText(s) {
t.Fatalf("missing %q in %q", s, tt.Texts())
}
}
services = servicesSample[:1]
tt.Frame()
if !tt.HasText("All systems operational") {
t.Fatalf("all ok: %q", tt.Texts())
}
services = nil
tt.Frame()
if !tt.HasText("No services") {
t.Fatal("empty")
}
}
func TestServiceStatusManyVirtualized(t *testing.T) {
many := make([]ServiceItem, 100000)
for i := range many {
many[i] = ServiceItem{Name: fmt.Sprint("svc ", i)}
}
tt := ui.NewTester(func(c *ui.Context) {
Use(c, Settings{})
ServiceStatus(c, many, ServiceStatusOptions{})
}, 520, 400)
if n := len(tt.Texts()); n > 300 {
t.Fatalf("%d texts", n)
}
}
func TestServiceStatusSnapshot(t *testing.T) {
snapshot(t, "service_status", 520, 280, func(c *ui.Context) {
ui.Column(c).Padding(16).Children(func() { ServiceStatus(c, servicesSample, ServiceStatusOptions{}) })
})
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB