// 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 "