Commit ed77ce3
feat(#103): metrics page — per-user, per-box draggable layout (GridStack)
Closes #103. The Metrics page's chart grid becomes drag + resize
editable, with the layout persisted per (user, box) so each operator
gets their own view per box. Reuses Home gear's GridStack vendor
bundle — no new dependency.
DB (new metrics_layouts table):
- Primary key (user_id, server_id) → layout_json blob. JSON beats
per-tile rows because every read/write is the whole layout and
there's no querying inside it. The blob is GridStack's save()
output verbatim.
- New file: internal/framework/database/metrics_layouts.go with
GetMetricsLayout / SaveMetricsLayout / DeleteMetricsLayout +
ErrNoMetricsLayout sentinel for the "no row" case.
- Schema wired into database.New() right after the Home schema.
Handler + routes:
- New file: internal/framework/handler/api_metrics_layout.go.
- GET /api/{boxID}/metrics/layout — returns saved JSON or 204
(caller falls back to template defaults; mid-rollout-safe).
- PATCH /api/{boxID}/metrics/layout — upsert. Body validated as
a JSON array of {id,x,y,w,h}; capped at 16 KiB. Tile IDs aren't
re-validated against the known set because the dashboard
silently ignores unknown ids — keeps the storage layer
agnostic to GridStack version changes.
- DELETE /api/{boxID}/metrics/layout — drops the row so the next
load uses defaults. Drives the "Reset layout" button.
- Routes registered in cmd/server/main.go alongside the existing
/metrics endpoints.
Templ (metrics.templ):
- Wrapper panel removed; each chart card now sits inside a
.grid-stack-item with default gs-x / gs-y / gs-w / gs-h
attributes. Card order rearranged to the layout proposed in
#103: CPU/Memory · Network/Response Time · Health/Errors ·
Sessions & Requests (full-width) · per-source rows.
- Edit toggle button (.metrics-edit-off-label / .metrics-edit-
on-label) + Reset button (.metrics-edit-only class, hidden
outside edit mode) added to the controls bar. Mirrors Home's
edit-toggle UX.
- GridStack vendor CSS loaded inline; vendor JS + new
metrics-layout.js script tags added to the page bottom (defer).
- applyCapabilities() now dispatches a 'metrics:capabilities-
applied' CustomEvent on every run so metrics-layout.js can
reflow GridStack without either script knowing about the
other's globals.
JS (new static/js/metrics-layout.js):
- Initialises GridStack on #charts-grid (read-only by default).
- Loads saved layout via GET; 204 falls back to template defaults
so first-visit users see the documented order without an
endpoint round-trip's worth of flicker.
- Edit-mode toggle calls setStatic(false), shows .metrics-edit-
only controls, and persists on exit. PATCH is debounced so a
multi-tile drag flush ends in one save call, not N.
- Capability-driven reflow: listens for the CustomEvent above
and calls gs.removeWidget(item, false) / gs.makeWidget(item)
per-tile based on each card's .hidden class. Unavailable
sources drop out of the grid (other tiles compact up) and
return to their saved positions when the source reappears.
- Reset button DELETEs the row and reloads, restoring template
defaults.
Out of scope (deferred):
- Layout import/export. Single (user, box) layouts only — bulk
ops can land as a follow-up when multi-box editing is a thing.
- Sharing layouts between users.
- Mobile-specific layouts. GridStack auto-compacts on narrow
viewports — same behaviour Home relies on, no metrics-specific
treatment needed.
go build / go vet / go test / templ generate all clean. ~580 LOC
new code (DB + handler + JS), plus templ edits across the page.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 0384a1e commit ed77ce3
6 files changed
Lines changed: 760 additions & 62 deletions
File tree
- gearbox
- cmd/server
- internal/framework
- database
- handler
- templates/pages
- static/js
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
19 | | - | |
20 | 18 | | |
| 19 | + | |
21 | 20 | | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
24 | 27 | | |
25 | 28 | | |
26 | 29 | | |
27 | 30 | | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| |||
383 | 383 | | |
384 | 384 | | |
385 | 385 | | |
386 | | - | |
387 | | - | |
388 | | - | |
389 | | - | |
390 | | - | |
391 | | - | |
392 | | - | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
393 | 393 | | |
394 | 394 | | |
395 | 395 | | |
| |||
540 | 540 | | |
541 | 541 | | |
542 | 542 | | |
543 | | - | |
| 543 | + | |
544 | 544 | | |
545 | 545 | | |
546 | 546 | | |
| |||
704 | 704 | | |
705 | 705 | | |
706 | 706 | | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
707 | 715 | | |
708 | 716 | | |
709 | 717 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
105 | 111 | | |
106 | 112 | | |
107 | 113 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
0 commit comments