P1-24: live dashboard — fleet summary tiles + host table
CI / Test (linux/amd64) (push) Has been cancelled
CI / Lint (push) Has been cancelled
CI / Build (windows/amd64) (push) Has been cancelled
CI / Build (linux/amd64) (push) Has been cancelled
CI / Build (linux/arm64) (push) Has been cancelled

Server-rendered HTML view backed by:
  - new store.FleetSummary aggregating host counts + repo bytes +
    snapshot total + open alerts + last-24h job rollup in two queries.
  - GET /api/hosts (JSON list of hosts in the dashboard projection).
  - GET /api/fleet/summary (JSON aggregate, same shape as above).

The HTML page (web/templates/pages/dashboard.html) renders the four
summary tiles + host table directly from store data — no separate
fetch. Per-row state colour comes from .host-row.{degraded,failed,
offline} which paint a 3px left edge so problem hosts are scannable
without reading. HTMX is loaded into the base layout so per-row
"Run now" buttons can hx-post to /hosts/{id}/run-backup, a thin
HTML wrapper that funnels into a new dispatchJob helper shared
with the JSON /api/hosts/{id}/jobs endpoint.

Empty state (zero hosts) collapses to the "no hosts yet" prompt
with the + Add host CTA — matches the v1 mockup.

Template helpers (internal/server/ui/funcs.go) added for byte
formatting (412 GB / 3.7 TB), relative time (3m ago / 2d ago), and
comma grouping (1,847). Pure Go, no template-magic dependency.

Browser-verified end-to-end with seeded fixture data: five hosts
across all four states render with correct dots, accents, last-
backup pills, sizes, snapshot counts, alerts, tags, and the right
action button (Run now / Retry / Run first / View → / offline).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-01 19:29:11 +01:00
parent 229f89fee2
commit ee16bc7ce7
13 changed files with 597 additions and 48 deletions
+134
View File
@@ -0,0 +1,134 @@
package ui
import (
"fmt"
"html/template"
"strconv"
"strings"
"time"
)
// funcMap returns the template functions every page can call.
// Kept small on purpose: anything fancier belongs in the handler,
// which can pre-compute and pass primitives into the view.
func funcMap() template.FuncMap {
return template.FuncMap{
"bytes": formatBytes,
"relTime": formatRelTime,
"comma": formatComma,
"deref": derefStr,
"timeNotZero": func(t *time.Time) bool { return t != nil && !t.IsZero() },
}
}
// formatBytes renders a byte count as a short human string —
// "412 GB", "3.7 TB", "8.4 GB". Single decimal place for sub-1000
// values, none above. Returns "—" for zero so the dashboard's
// "never run" rows read clean.
func formatBytes(n int64) template.HTML {
if n == 0 {
return template.HTML(`<span class="text-ink-fade">—</span>`)
}
const (
kb = 1000
mb = 1000 * kb
gb = 1000 * mb
tb = 1000 * gb
)
var (
val float64
unit string
)
switch {
case n >= tb:
val, unit = float64(n)/float64(tb), "TB"
case n >= gb:
val, unit = float64(n)/float64(gb), "GB"
case n >= mb:
val, unit = float64(n)/float64(mb), "MB"
case n >= kb:
val, unit = float64(n)/float64(kb), "kB"
default:
return template.HTML(fmt.Sprintf(`%d <span class="text-ink-mute text-[11px]">B</span>`, n))
}
num := strconv.FormatFloat(val, 'f', -1, 64)
if val < 100 && !strings.Contains(num, ".") {
num += ".0"
} else if val < 100 && strings.Contains(num, ".") {
// One decimal max, e.g. "3.7" not "3.74".
idx := strings.Index(num, ".")
if len(num) > idx+2 {
num = num[:idx+2]
}
} else {
// Above 100, no decimals — "412" not "412.4".
if idx := strings.Index(num, "."); idx > 0 {
num = num[:idx]
}
}
return template.HTML(fmt.Sprintf(`%s <span class="text-ink-mute text-[11px]">%s</span>`, num, unit))
}
// formatRelTime renders a *time.Time as a short relative string like
// "3m ago" / "2d ago" / "5w ago". Future times render as "in Xs".
// Nil pointer returns "—".
func formatRelTime(t *time.Time) string {
if t == nil || t.IsZero() {
return "—"
}
d := time.Since(*t)
suffix := "ago"
if d < 0 {
d = -d
suffix = "from now"
}
switch {
case d < time.Minute:
return fmt.Sprintf("%ds %s", int(d.Seconds()), suffix)
case d < time.Hour:
return fmt.Sprintf("%dm %s", int(d.Minutes()), suffix)
case d < 24*time.Hour:
return fmt.Sprintf("%dh %s", int(d.Hours()), suffix)
case d < 7*24*time.Hour:
return fmt.Sprintf("%dd %s", int(d.Hours()/24), suffix)
default:
return fmt.Sprintf("%dw %s", int(d.Hours()/(24*7)), suffix)
}
}
// formatComma renders 1847 as "1,847". Used for snapshot counts and
// any other count that benefits from grouping at this scale.
func formatComma(n int) string {
s := strconv.Itoa(n)
if n < 1000 && n > -1000 {
return s
}
// Hand-rolled grouping; Go has no builtin and we don't want a
// dep for this. Negative numbers handled by stripping the sign,
// grouping, then putting it back.
neg := false
if s[0] == '-' {
neg = true
s = s[1:]
}
var b strings.Builder
for i, c := range s {
if i > 0 && (len(s)-i)%3 == 0 {
b.WriteByte(',')
}
b.WriteRune(c)
}
if neg {
return "-" + b.String()
}
return b.String()
}
// derefStr returns the string a pointer points to, or "" if nil.
// Avoids "<nil>" appearing in templates that hit a missing FK.
func derefStr(p *string) string {
if p == nil {
return ""
}
return *p
}
+3 -1
View File
@@ -88,6 +88,7 @@ func New() (*Renderer, error) {
"templates/layouts/base.html",
"templates/layouts/chromeless.html",
"templates/partials/nav.html",
"templates/partials/host_row.html",
}
pageEntries, err := fs.Glob(web.FS, "templates/pages/*.html")
@@ -101,7 +102,8 @@ func New() (*Renderer, error) {
r := &Renderer{pages: make(map[string]*template.Template, len(pageEntries))}
for _, p := range pageEntries {
base := strings.TrimSuffix(path.Base(p), ".html")
t, err := template.New(base).ParseFS(web.FS, append(append([]string{}, commonPaths...), p)...)
t, err := template.New(base).Funcs(funcMap()).
ParseFS(web.FS, append(append([]string{}, commonPaths...), p)...)
if err != nil {
return nil, fmt.Errorf("ui: parse %s: %w", p, err)
}