carlos / platform Public

console: Boxes review fixes: memory shows used over total; the unreadable-hosts test fails S3 through the fake

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev e5fae886d685b3c4f5a2d1b51d7e2e8e04013fa7 parent ec1a16f
4 files changed, +63 −9
  • internal/console/opboxes.go +12 −0
  • internal/console/server.go +3 −1
  • internal/console/server_test.go +47 −7
  • internal/console/templates/ops_boxes.html +1 −1
diff --git a/internal/console/opboxes.go b/internal/console/opboxes.go
index 9dc64626b..442020116 100644
--- a/internal/console/opboxes.go
+++ b/internal/console/opboxes.go
@@ -123,3 +123,15 @@ func memUsedPercent(m *status.BoxMetrics) int {
used := *m.MemTotalBytes - *m.MemAvailBytes
return int(min(max(used*100 / *m.MemTotalBytes, 0), 100))
}
+
+// memUsedBytes is a host's memory in use, MemTotalBytes less MemAvailBytes
+// (never below zero), or nil when memUsedPercent has no figure. It is the
+// template func memUsed: the Memory column's text is used over total, the
+// same fraction its bar draws.
+func memUsedBytes(m *status.BoxMetrics) *int64 {
+ if memUsedPercent(m) < 0 {
+ return nil
+ }
+ used := max(*m.MemTotalBytes-*m.MemAvailBytes, 0)
+ return &used
+}
diff --git a/internal/console/server.go b/internal/console/server.go
index 55e788bce..d0efca5e6 100644
--- a/internal/console/server.go
+++ b/internal/console/server.go
@@ -214,13 +214,15 @@ var templates = template.Must(template.New("").Funcs(template.FuncMap{
"opHeader": func(title, sub string) opHeader { return opHeader{Title: title, Sub: sub} },
"opEmpty": func(title, body string) opEmpty { return opEmpty{Title: title, Body: body} },
// opPill, opStat and opMeter build op_status, op_stat and op_meter's
- // data in a template; memUsedPct is a host's used memory for its meter.
+ // data in a template; memUsedPct is a host's used memory for its meter,
+ // and memUsed the same figure in bytes for the meter's text.
"opPill": func(label, tone string) opPill { return opPill{Label: label, Tone: tone} },
"opStat": func(label, value, note string, lead bool) opStat {
return opStat{Label: label, Value: value, Note: note, Lead: lead}
},
"opMeter": func(percent int, text string) opMeter { return opMeter{Percent: percent, Text: text} },
"memUsedPct": memUsedPercent,
+ "memUsed": memUsedBytes,
// join renders fleetPageBox.Tags back into the tag form's editable
// text input, comma-separated — the exact grammar SetFleetBoxTags
// expects back, so an owner opening the form sees the same string they'd
diff --git a/internal/console/server_test.go b/internal/console/server_test.go
index 00506aae1..3d48d8b19 100644
--- a/internal/console/server_test.go
+++ b/internal/console/server_test.go
@@ -1894,6 +1894,40 @@ func TestBoxesHostMemoryMeter(t *testing.T) {
}
}
+// TestBoxesMemoryTextIsUsedOverTotal: the Memory column's text and its bar
+// measure the same thing. With 0.5 GB available of 2 GB the bar is 75 and
+// the text is the 1.5 GB in use over the total, never the 0.5 GB free (a
+// 50% host cannot tell the two apart).
+func TestBoxesMemoryTextIsUsedOverTotal(t *testing.T) {
+ fx := newConsoleFixture(t)
+ if err := status.WriteBox(context.Background(), fx.rel, status.Box{
+ GeneratedAt: time.Now(), Label: "ada-stargazing", Region: "eu-west-1",
+ Metrics: &status.BoxMetrics{MemTotalBytes: pi64(2 << 30), MemAvailBytes: pi64(512 << 20)},
+ }); err != nil {
+ t.Fatal(err)
+ }
+ c := signInOperator(t, fx)
+ status, _, body := get(t, c, fx.ts.URL+"/fleet/boxes")
+ if status != 200 {
+ t.Fatalf("/fleet/boxes = %d: %s", status, body)
+ }
+ i := strings.Index(body, "<h2>Hosts</h2>")
+ if i < 0 {
+ t.Fatalf("/fleet/boxes has no Hosts table:\n%s", body)
+ }
+ row := body[i:]
+ row = row[strings.Index(row, "ada-stargazing"):]
+ row = row[:strings.Index(row, "</tr>")]
+ for _, want := range []string{`<meter rst-meter-bar value="75"`, "<span rst-meter-num>1.5 GB / 2.0 GB</span>"} {
+ if !strings.Contains(row, want) {
+ t.Errorf("the host's row has no %s:\n%s", want, row)
+ }
+ }
+ if strings.Contains(row, "512.0 MB") {
+ t.Errorf("the host's row shows the free figure:\n%s", row)
+ }
+}
+
// seedHostBoxes writes one fresh and one long-dark box snapshot, both
// carrying a private Metrics block, and publishes the merged feed over them —
// the exact pair /fleet/boxes' Hosts detail has to tell apart.
@@ -1969,18 +2003,24 @@ func TestFleetHostsDetailSuppressesStaleMetrics(t *testing.T) {
// page that already had a job. If the boxes can't be listed, the page still
// renders the merged feed it could read — metrics are never the reason Boxes
// 500s.
+//
+// The failure is injected at the S3 fake (newPerfFixture, recordS3), so the
+// console's own client meets a refused listing of status/boxes/ while
+// status/fleet.json still reads.
func TestFleetHostsDegradeWhenUnreadable(t *testing.T) {
- fx := newConsoleFixture(t)
- seedFleetJSON(t, fx)
- fx.srv.cfg.Releases = listFailStore{fx.rel}
- c := signInPaul(t, fx)
- if err := fx.st.SetOperator("paul@keymail.dev", true); err != nil {
- t.Fatal(err)
- }
+ fx, lat := newPerfFixture(t)
+ c := signInOperator(t, fx)
+ rec := recordS3(lat, func(q s3Req) bool { return q.Key == "" && strings.HasPrefix(q.Prefix, "status/boxes/") })
code, _, body := get(t, c, fx.ts.URL+"/fleet/boxes")
if code != 200 || !strings.Contains(body, "<h1>Boxes</h1>") || !strings.Contains(body, "ada-stargazing") {
t.Fatalf("/fleet/boxes with an unlistable bucket = %d, want the fleet feed still rendered:\n%s", code, body)
}
+ if rec.listings("status/boxes/") == 0 {
+ t.Fatalf("the view never listed %s, so no failure was met: %+v", "status/boxes/", rec.all())
+ }
+ if strings.Contains(body, "<h2>Hosts</h2>") {
+ t.Errorf("the Hosts detail rendered with its listing refused:\n%s", body)
+ }
}
// TestFleetHostsEscapesHostSuppliedStrings: Release and Tunnel are strings a
diff --git a/internal/console/templates/ops_boxes.html b/internal/console/templates/ops_boxes.html
index 07d7894e4..7e62a2372 100644
--- a/internal/console/templates/ops_boxes.html
+++ b/internal/console/templates/ops_boxes.html
@@ -97,7 +97,7 @@
{{if .Metrics}}
<td>{{emptyOrDash .Metrics.Release}}</td>
<td>{{emptyOrDash .Metrics.Tunnel}}</td>
- {{$mem := printf "%s / %s" (bytesOrDash .Metrics.MemAvailBytes) (bytesOrDash .Metrics.MemTotalBytes)}}{{$used := memUsedPct .Metrics}}
+ {{$mem := printf "%s / %s" (bytesOrDash (memUsed .Metrics)) (bytesOrDash .Metrics.MemTotalBytes)}}{{$used := memUsedPct .Metrics}}
<td>{{if ge $used 0}}{{template "op_meter" (opMeter $used $mem)}}{{else}}{{$mem}}{{end}}</td>
<td>{{loadOrDash .Metrics.Load1}}</td>
<td>{{bytesOrDash .Metrics.DiskFreeBytes}}</td>