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.
This commit is contained in:
2026-05-04 10:44:31 +01:00
parent 6589f23313
commit 93ab0ae84f
7 changed files with 201 additions and 4 deletions
@@ -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)
}
})
}
}
+14
View File
@@ -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)
}
+36 -2
View File
@@ -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 {