hub/internal/fleet/backups_display.go
trvon f8a3bbadd5
All checks were successful
release / build (push) Successful in 4m13s
hub backups: labelled jobs, not-scheduled state, failure reason; status shows critical alerts
One line per job: label, source -> target, method, state text, age and schedule, from the
RS10-Z /v1/backups metadata. hub status adds critical alerts and a backup summary token
from /v1/summary. Servers without the new fields keep the previous output.
2026-11-21 09:11:56 -07:00

161 lines
4 KiB
Go

// SPDX-License-Identifier: GPL-3.0-or-later
package fleet
import (
"encoding/json"
"fmt"
"io"
"strings"
)
// A job from a server with the RS10-Z fields carries `display_state`; `hub backups` then prints one labelled line per
// job. A job without it (older server) keeps the block format in views.go exactly.
// backupMark is the one-character state mark: ok, running, failed/never/not scheduled, overdue, unknown.
func backupMark(display string) string {
switch display {
case "ok":
return "✓"
case "running":
return "●"
case "failed", "never_succeeded", "not_scheduled":
return "✗"
case "overdue":
return "⚠"
}
return "?"
}
// backupAge renders age_h as "<1 h ago", "5 h ago" or "5 d ago"; "" when the job has no known success.
func backupAge(v any) string {
n, ok := v.(json.Number)
if !ok {
return ""
}
h, err := n.Float64()
if err != nil || h < 0 {
return ""
}
switch {
case h < 1:
return "<1 h ago"
case h < 48:
return fmt.Sprintf("%d h ago", int(h))
}
return fmt.Sprintf("%d d ago", int(h/24))
}
func jobText(m map[string]any, key string) string {
s, _ := m[key].(string)
return printable(s)
}
// backupStateText is the state column: what the job is, why, and when it last succeeded.
func backupStateText(job map[string]any, display string) string {
age := backupAge(job["age_h"])
reason := jobText(job, "reason_label")
switch display {
case "ok":
if age == "" {
return "ok"
}
return "ok " + age
case "running":
text := "running"
if p, ok := job["progress"].(map[string]any); ok {
text += fmt.Sprintf(" %s/%s %s", dash(p["index"]), dash(p["total"]), humanBytes(p["bytes_received"]))
}
return text
case "not_scheduled":
text := "not scheduled"
if doc := jobText(job, "setup_doc"); doc != "" {
text += " (setup: " + doc + ")"
}
return text
case "failed":
text := "failed"
if reason != "" {
text += " " + reason
}
if age != "" {
text += ", last ok " + age
}
return text
case "never_succeeded":
text := "never succeeded"
if reason != "" {
text += " (" + reason + ")"
}
return text
case "overdue":
text := "overdue"
if age != "" {
text += ", last ok " + age
}
return text
}
return "unknown"
}
// writeBackupLine prints "<mark> <label> <source> -> <target> (<method>) <state text> · <schedule>"; absent parts are left out.
func writeBackupLine(w io.Writer, name string, job map[string]any) {
display := jobText(job, "display_state")
label := jobText(job, "label")
if label == "" {
label = printable(name)
}
parts := []string{backupMark(display), label}
if src, tgt := jobText(job, "source_label"), jobText(job, "target_label"); src != "" || tgt != "" {
parts = append(parts, dashStr(src)+" -> "+dashStr(tgt))
}
if method := jobText(job, "method"); method != "" {
parts = append(parts, "("+method+")")
}
line := strings.Join(parts, " ") + " " + backupStateText(job, display)
if schedule := jobText(job, "schedule"); schedule != "" {
line += " · " + schedule
}
fmt.Fprintln(w, line)
}
// backupSummaryToken is the `hub status` headline token from the /v1/summary `backups` block ("" when absent).
func backupSummaryToken(env *Envelope) string {
b, ok := env.Raw["backups"].(map[string]any)
if !ok {
return ""
}
okN, flagged, notSched := countField(b["ok"]), countField(b["flagged"]), countField(b["not_scheduled"])
text := fmt.Sprintf("backups %d ok", okN)
if flagged > 0 {
text += fmt.Sprintf(", %d flagged", flagged)
if notSched > 0 {
text += fmt.Sprintf(" (%d not scheduled)", notSched)
}
}
return text
}
// criticalAlertsToken is "critical alerts N" when the /v1/summary `alerts` block reports open critical alerts.
func criticalAlertsToken(env *Envelope) string {
a, ok := env.Raw["alerts"].(map[string]any)
if !ok {
return ""
}
if n := countField(a["critical"]); n > 0 {
return fmt.Sprintf("critical alerts %d", n)
}
return ""
}
func countField(v any) int {
n, ok := v.(json.Number)
if !ok {
return 0
}
i, err := n.Int64()
if err != nil || i < 0 || i > 1<<30 {
return 0
}
return int(i)
}