160d788bae
Closes the schedule foundations slice — operator can now drive the
plumbing P2-01..03 landed without touching the JSON API.
* New routes:
- GET /hosts/{id}/schedules (list)
- GET /hosts/{id}/schedules/new (create form)
- POST /hosts/{id}/schedules/new (create)
- GET /hosts/{id}/schedules/{sid}/edit (edit form)
- POST /hosts/{id}/schedules/{sid}/edit (update)
- POST /hosts/{id}/schedules/{sid}/delete (delete, confirm-then-redirect)
* List view (web/templates/pages/schedules_list.html):
status, cron, paths, retention summary, tags, edit/delete buttons.
Header shows "version N · agent in sync" or "agent at vM" when the
push hasn't been ack'd yet — backed by host_schedule_version +
applied_schedule_version. Empty-state CTA points at /schedules/new.
* Create/edit form (web/templates/pages/schedule_edit.html, shared):
cron expression with five quick-pick presets (daily 3am / every 6h
/ @hourly / weekly Sun / monthly 1st), paths textarea (one per
line), excludes textarea, tags (comma-separated), retention as six
numeric fields (mirrors restic's --keep-* flags one-for-one),
bandwidth caps, enabled toggle. Side panel explains the
reconciliation flow so the operator knows what saving actually
does. Validation errors re-render with operator's input intact.
* internal/server/http/ui_schedules.go owns the handlers; reuses
the same validateSchedule + pushScheduleSetAsync used by the JSON
API path. Each save audit-logs schedule.created / schedule.updated
/ schedule.deleted (matching the JSON API actions).
* store.RetentionPolicy gains a Summary() method ("last=7, d=14,
w=4" or "—"). Used by the list view's table cell so templates
don't have to do any conditional retention rendering.
* Two new template helpers: list (string varargs → []string, used
for the cron preset row) and joinComma (sibling to joinDot for
the rare list that wants commas). RetentionPolicy.Summary covers
the schedule-list case but the helpers are general.
* host_detail.html secondary tabs row converted from inert <div>s
into <a> links. Snapshots active by default; Schedules now points
at the new page. Jobs/Repo/Settings remain inert until their
P2 owners ship.
Hooks UI deferred to P2-15 (lands with the hook execution path).
Single-kind UI (backup only) by design — other kinds get a UI when
their job dispatch lands in P2-05..08.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
181 lines
4.5 KiB
Go
181 lines
4.5 KiB
Go
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() },
|
|
"joinDot": func(parts []string) string { return strings.Join(parts, " · ") },
|
|
"absTime": func(t time.Time) string {
|
|
if t.IsZero() {
|
|
return "—"
|
|
}
|
|
return t.Format("2006-01-02 15:04:05")
|
|
},
|
|
"derefInt": func(p *int) int {
|
|
if p == nil {
|
|
return 0
|
|
}
|
|
return *p
|
|
},
|
|
"sub": func(a, b int) int { return a - b },
|
|
// joinComma joins a slice with ", ". Used by the schedule list
|
|
// to render retention summaries.
|
|
"joinComma": func(parts []string) string { return strings.Join(parts, ", ") },
|
|
// list packs strings into a slice — handy for inline ranges
|
|
// in templates (e.g. quick-pick cron presets).
|
|
"list": func(items ...string) []string { return items },
|
|
}
|
|
}
|
|
|
|
// 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 as a short relative string like
|
|
// "3m ago" / "2d ago" / "5w ago". Future times render as
|
|
// "in 5m"-style. Accepts *time.Time or time.Time so templates can
|
|
// pass either without fighting Go's lack of an address-of operator.
|
|
// Anything else returns "—".
|
|
func formatRelTime(v any) string {
|
|
var t time.Time
|
|
switch x := v.(type) {
|
|
case time.Time:
|
|
t = x
|
|
case *time.Time:
|
|
if x == nil {
|
|
return "—"
|
|
}
|
|
t = *x
|
|
default:
|
|
return "—"
|
|
}
|
|
if 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.
|
|
// Accepts int / int64 / int32 — anything else returns "—".
|
|
func formatComma(v any) string {
|
|
var n int64
|
|
switch x := v.(type) {
|
|
case int:
|
|
n = int64(x)
|
|
case int32:
|
|
n = int64(x)
|
|
case int64:
|
|
n = x
|
|
default:
|
|
return "—"
|
|
}
|
|
s := strconv.FormatInt(n, 10)
|
|
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
|
|
}
|