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:
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user