From 93ab0ae84fd90ae00c30cefbe12e886d1ff7266c Mon Sep 17 00:00:00 2001 From: Steve Cliff Date: Mon, 4 May 2026 10:44:31 +0100 Subject: [PATCH] ui+server: schedule next-run / last-run on dashboard + schedules tab P2R-14. New store.LatestJobBySchedule query (per-schedule fired job). Schedules-tab handler computes next-fire from cron + last-fire from the jobs table per row. Schedules table grows two columns; dashboard host row prepends 'next 12h ago/from now' to the existing last-backup line when a single covering schedule is the run-now candidate. Embeds store.Schedule into scheduleRow so existing template field references keep working without bulk renames. --- internal/server/http/schedule_nextrun_test.go | 48 ++++++++++ internal/server/http/ui_handlers.go | 14 +++ internal/server/http/ui_schedules.go | 38 +++++++- internal/store/schedule_runs.go | 88 +++++++++++++++++++ web/styles/input.css | 4 +- web/templates/pages/host_schedules.html | 10 +++ web/templates/partials/host_row.html | 3 + 7 files changed, 201 insertions(+), 4 deletions(-) create mode 100644 internal/server/http/schedule_nextrun_test.go create mode 100644 internal/store/schedule_runs.go diff --git a/internal/server/http/schedule_nextrun_test.go b/internal/server/http/schedule_nextrun_test.go new file mode 100644 index 0000000..6088f1f --- /dev/null +++ b/internal/server/http/schedule_nextrun_test.go @@ -0,0 +1,48 @@ +// schedule_nextrun_test.go — pin the cron parser → next-run shape we +// rely on for the dashboard host row + schedules tab (P2R-14). +package http + +import ( + "testing" + "time" +) + +func TestCronParserNext(t *testing.T) { + cases := []struct { + name string + expr string + from time.Time + want time.Time + }{ + { + name: "daily at 03:00", + expr: "0 3 * * *", + from: time.Date(2026, 5, 4, 1, 0, 0, 0, time.UTC), + want: time.Date(2026, 5, 4, 3, 0, 0, 0, time.UTC), + }, + { + name: "daily at 03:00 (after time of day → next day)", + expr: "0 3 * * *", + from: time.Date(2026, 5, 4, 5, 0, 0, 0, time.UTC), + want: time.Date(2026, 5, 5, 3, 0, 0, 0, time.UTC), + }, + { + name: "every 15 minutes", + expr: "*/15 * * * *", + from: time.Date(2026, 5, 4, 1, 7, 0, 0, time.UTC), + want: time.Date(2026, 5, 4, 1, 15, 0, 0, time.UTC), + }, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + parsed, err := cronParser.Parse(c.expr) + if err != nil { + t.Fatalf("parse %q: %v", c.expr, err) + } + got := parsed.Next(c.from) + if !got.Equal(c.want) { + t.Fatalf("Next(%v) = %v, want %v", c.from, got, c.want) + } + }) + } +} diff --git a/internal/server/http/ui_handlers.go b/internal/server/http/ui_handlers.go index 0dd4752..cd4166d 100644 --- a/internal/server/http/ui_handlers.go +++ b/internal/server/http/ui_handlers.go @@ -124,6 +124,9 @@ type dashboardHostRow struct { // match — in that case the row shows "Open →" instead of a Run-now // button (the operator picks per-group from the host detail). RunAllScheduleID string + // NextRun is the next-fire time of RunAllScheduleID (when set), + // computed server-side from its cron. nil otherwise. + NextRun *time.Time } // pickRunAllSchedule returns the ID of the single schedule whose @@ -203,6 +206,17 @@ func (s *Server) handleUIDashboard(w stdhttp.ResponseWriter, r *stdhttp.Request) slog.Warn("ui dashboard: list schedules", "host_id", h.ID, "err", serr) } row.RunAllScheduleID = pickRunAllSchedule(scheds, groups) + if row.RunAllScheduleID != "" { + for _, sc := range scheds { + if sc.ID == row.RunAllScheduleID { + if parsed, perr := cronParser.Parse(sc.CronExpr); perr == nil { + n := parsed.Next(time.Now().UTC()).UTC() + row.NextRun = &n + } + break + } + } + } rows = append(rows, row) } diff --git a/internal/server/http/ui_schedules.go b/internal/server/http/ui_schedules.go index 485e64e..a4daf4d 100644 --- a/internal/server/http/ui_schedules.go +++ b/internal/server/http/ui_schedules.go @@ -25,10 +25,22 @@ import ( // the template doesn't need to do per-row store lookups. type hostSchedulesPage struct { hostChromeData - Schedules []store.Schedule + Schedules []scheduleRow GroupNames map[string]string } +// scheduleRow bundles a schedule with its derived "next run" + "last +// run" data. The Schedule is embedded so existing template field +// references (`$sc.ID`, `$sc.CronExpr`, etc) keep working when we +// switch the iterating slice from []store.Schedule to []scheduleRow. +type scheduleRow struct { + store.Schedule + NextRun *time.Time + LastRun *time.Time + LastJobID string + LastStatus string // succeeded|failed|running|queued — empty when never fired +} + // scheduleFormData mirrors the form's wire shape — strings + bool for // round-trip on validation re-render. type scheduleFormData struct { @@ -74,6 +86,28 @@ func (s *Server) handleUISchedulesList(w stdhttp.ResponseWriter, r *stdhttp.Requ names[g.ID] = g.Name } + now := time.Now().UTC() + rows := make([]scheduleRow, 0, len(scheds)) + for _, sc := range scheds { + row := scheduleRow{Schedule: sc} + if sc.Enabled { + if sched, err := cronParser.Parse(sc.CronExpr); err == nil { + next := sched.Next(now).UTC() + row.NextRun = &next + } + } + if j, jerr := s.deps.Store.LatestJobBySchedule(r.Context(), host.ID, sc.ID); jerr == nil && j != nil { + t := j.CreatedAt + if j.StartedAt != nil { + t = *j.StartedAt + } + row.LastRun = &t + row.LastJobID = j.ID + row.LastStatus = j.Status + } + rows = append(rows, row) + } + chrome := s.loadHostChrome(r, *host, "schedules", "schedules") chrome.ScheduleCount = len(scheds) chrome.SourceGroupCount = len(groups) @@ -82,7 +116,7 @@ func (s *Server) handleUISchedulesList(w stdhttp.ResponseWriter, r *stdhttp.Requ view.Title = host.Name + " schedules · restic-manager" view.Page = hostSchedulesPage{ hostChromeData: chrome, - Schedules: scheds, + Schedules: rows, GroupNames: names, } if err := s.deps.UI.Render(w, "host_schedules", view); err != nil { diff --git a/internal/store/schedule_runs.go b/internal/store/schedule_runs.go new file mode 100644 index 0000000..a42f504 --- /dev/null +++ b/internal/store/schedule_runs.go @@ -0,0 +1,88 @@ +// schedule_runs.go — derived "next run" / "last run" helpers for the +// dashboard host row + schedules tab (P2R-14). +// +// Both are derived data: NextRun is computed from the cron expression +// at request time; LatestJobBySchedule reads the most recent job that +// fired against this schedule. Neither is persisted — the cost of the +// query is small relative to a page render. +package store + +import ( + "context" + "database/sql" + "errors" + "fmt" + "time" +) + +// LatestJobBySchedule returns the most recent job fired by this +// schedule (actor_kind='schedule' AND scheduled_id=schedID), or +// (nil, ErrNotFound) when the schedule has never fired. Includes +// queued/running rows because the operator wants to see "running +// now" too. +func (s *Store) LatestJobBySchedule(ctx context.Context, hostID, schedID string) (*Job, error) { + row := s.db.QueryRowContext(ctx, + `SELECT id, host_id, kind, status, scheduled_id, actor_kind, actor_id, + started_at, finished_at, exit_code, stats, error, created_at + FROM jobs + WHERE host_id = ? AND scheduled_id = ? AND actor_kind = 'schedule' + ORDER BY created_at DESC + LIMIT 1`, hostID, schedID) + return scanJobRow(row) +} + +// scanJobRow is the shared scan used by LatestJobBySchedule. Mirrors +// the columns LatestJobByKind reads. Kept in this file (vs jobs.go) +// to avoid disturbing the stable API surface exported there. +func scanJobRow(row *sql.Row) (*Job, error) { + var ( + j Job + schedID sql.NullString + actorID sql.NullString + startedAt sql.NullString + finishedAt sql.NullString + exitCode sql.NullInt64 + stats sql.NullString + errMsg sql.NullString + createdAt string + ) + if err := row.Scan(&j.ID, &j.HostID, &j.Kind, &j.Status, &schedID, + &j.ActorKind, &actorID, &startedAt, &finishedAt, + &exitCode, &stats, &errMsg, &createdAt); err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, ErrNotFound + } + return nil, fmt.Errorf("store: scan job: %w", err) + } + if schedID.Valid { + v := schedID.String + j.ScheduledID = &v + } + if actorID.Valid { + v := actorID.String + j.ActorID = &v + } + if startedAt.Valid { + t, _ := time.Parse(time.RFC3339Nano, startedAt.String) + j.StartedAt = &t + } + if finishedAt.Valid { + t, _ := time.Parse(time.RFC3339Nano, finishedAt.String) + j.FinishedAt = &t + } + if exitCode.Valid { + i := int(exitCode.Int64) + j.ExitCode = &i + } + if stats.Valid && stats.String != "" { + j.Stats = []byte(stats.String) + } + if errMsg.Valid { + v := errMsg.String + j.Error = &v + } + if t, err := time.Parse(time.RFC3339Nano, createdAt); err == nil { + j.CreatedAt = t + } + return &j, nil +} diff --git a/web/styles/input.css b/web/styles/input.css index 565c5e2..7f0ff24 100644 --- a/web/styles/input.css +++ b/web/styles/input.css @@ -209,8 +209,8 @@ /* ---------- schedule rows (Schedules tab) ---------- */ .schd-row { display: grid; align-items: center; - grid-template-columns: 90px 1fr 2fr auto; - column-gap: 18px; + grid-template-columns: 78px 1fr 1.6fr 100px 110px auto; + column-gap: 14px; padding: 12px 18px; font-size: 13px; } .schd-row.head { diff --git a/web/templates/pages/host_schedules.html b/web/templates/pages/host_schedules.html index 764ae2d..4a49e79 100644 --- a/web/templates/pages/host_schedules.html +++ b/web/templates/pages/host_schedules.html @@ -33,6 +33,8 @@
Status
Cron
Sources
+
Next
+
Last
{{range $i, $sc := $page.Schedules}} @@ -52,6 +54,14 @@ {{if $name}}{{$name}}{{else}}unknown{{end}} {{end}} +
+ {{if $sc.NextRun}}{{relTime $sc.NextRun}}{{else if not $sc.Enabled}}(paused){{else}}—{{end}} +
+
+ {{if $sc.LastRun}}{{relTime $sc.LastRun}}{{else}}—{{end}} +
{{if eq $host.Status "online"}} {{if $sc.Enabled}} diff --git a/web/templates/partials/host_row.html b/web/templates/partials/host_row.html index 98b27ea..9c7799e 100644 --- a/web/templates/partials/host_row.html +++ b/web/templates/partials/host_row.html @@ -30,6 +30,9 @@ {{- else -}} never run {{- end -}} + {{- if .NextRun -}} +
next {{relTime .NextRun}} + {{- end -}}
{{bytes $h.RepoSizeBytes}}