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
|
// match — in that case the row shows "Open →" instead of a Run-now
|
||||||
// button (the operator picks per-group from the host detail).
|
// button (the operator picks per-group from the host detail).
|
||||||
RunAllScheduleID string
|
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
|
// 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)
|
slog.Warn("ui dashboard: list schedules", "host_id", h.ID, "err", serr)
|
||||||
}
|
}
|
||||||
row.RunAllScheduleID = pickRunAllSchedule(scheds, groups)
|
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)
|
rows = append(rows, row)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,10 +25,22 @@ import (
|
|||||||
// the template doesn't need to do per-row store lookups.
|
// the template doesn't need to do per-row store lookups.
|
||||||
type hostSchedulesPage struct {
|
type hostSchedulesPage struct {
|
||||||
hostChromeData
|
hostChromeData
|
||||||
Schedules []store.Schedule
|
Schedules []scheduleRow
|
||||||
GroupNames map[string]string
|
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
|
// scheduleFormData mirrors the form's wire shape — strings + bool for
|
||||||
// round-trip on validation re-render.
|
// round-trip on validation re-render.
|
||||||
type scheduleFormData struct {
|
type scheduleFormData struct {
|
||||||
@@ -74,6 +86,28 @@ func (s *Server) handleUISchedulesList(w stdhttp.ResponseWriter, r *stdhttp.Requ
|
|||||||
names[g.ID] = g.Name
|
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 := s.loadHostChrome(r, *host, "schedules", "schedules")
|
||||||
chrome.ScheduleCount = len(scheds)
|
chrome.ScheduleCount = len(scheds)
|
||||||
chrome.SourceGroupCount = len(groups)
|
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.Title = host.Name + " schedules · restic-manager"
|
||||||
view.Page = hostSchedulesPage{
|
view.Page = hostSchedulesPage{
|
||||||
hostChromeData: chrome,
|
hostChromeData: chrome,
|
||||||
Schedules: scheds,
|
Schedules: rows,
|
||||||
GroupNames: names,
|
GroupNames: names,
|
||||||
}
|
}
|
||||||
if err := s.deps.UI.Render(w, "host_schedules", view); err != nil {
|
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
|
||||||
|
}
|
||||||
@@ -209,8 +209,8 @@
|
|||||||
/* ---------- schedule rows (Schedules tab) ---------- */
|
/* ---------- schedule rows (Schedules tab) ---------- */
|
||||||
.schd-row {
|
.schd-row {
|
||||||
display: grid; align-items: center;
|
display: grid; align-items: center;
|
||||||
grid-template-columns: 90px 1fr 2fr auto;
|
grid-template-columns: 78px 1fr 1.6fr 100px 110px auto;
|
||||||
column-gap: 18px;
|
column-gap: 14px;
|
||||||
padding: 12px 18px; font-size: 13px;
|
padding: 12px 18px; font-size: 13px;
|
||||||
}
|
}
|
||||||
.schd-row.head {
|
.schd-row.head {
|
||||||
|
|||||||
@@ -33,6 +33,8 @@
|
|||||||
<div>Status</div>
|
<div>Status</div>
|
||||||
<div>Cron</div>
|
<div>Cron</div>
|
||||||
<div>Sources</div>
|
<div>Sources</div>
|
||||||
|
<div>Next</div>
|
||||||
|
<div>Last</div>
|
||||||
<div></div>
|
<div></div>
|
||||||
</div>
|
</div>
|
||||||
{{range $i, $sc := $page.Schedules}}
|
{{range $i, $sc := $page.Schedules}}
|
||||||
@@ -52,6 +54,14 @@
|
|||||||
<span class="tag" style="border-color: color-mix(in oklch, var(--accent), transparent 60%); color: var(--accent); {{if not $sc.Enabled}}opacity: 0.6;{{end}}">{{if $name}}{{$name}}{{else}}<span class="text-ink-fade">unknown</span>{{end}}</span>
|
<span class="tag" style="border-color: color-mix(in oklch, var(--accent), transparent 60%); color: var(--accent); {{if not $sc.Enabled}}opacity: 0.6;{{end}}">{{if $name}}{{$name}}{{else}}<span class="text-ink-fade">unknown</span>{{end}}</span>
|
||||||
{{end}}
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
|
<div class="mono text-[11.5px] {{if $sc.NextRun}}text-ink-mid{{else}}text-ink-fade{{end}}"
|
||||||
|
{{if $sc.NextRun}}title="{{$sc.NextRun.Format "2006-01-02 15:04:05 MST"}}"{{end}}>
|
||||||
|
{{if $sc.NextRun}}{{relTime $sc.NextRun}}{{else if not $sc.Enabled}}(paused){{else}}—{{end}}
|
||||||
|
</div>
|
||||||
|
<div class="mono text-[11.5px] {{if $sc.LastRun}}{{if eq $sc.LastStatus "failed"}}text-warn{{else}}text-ink-mid{{end}}{{else}}text-ink-fade{{end}}"
|
||||||
|
{{if $sc.LastRun}}title="{{$sc.LastRun.Format "2006-01-02 15:04:05 MST"}} · {{$sc.LastStatus}}"{{end}}>
|
||||||
|
{{if $sc.LastRun}}{{relTime $sc.LastRun}}{{else}}—{{end}}
|
||||||
|
</div>
|
||||||
<div class="flex gap-1.5 justify-end row-action">
|
<div class="flex gap-1.5 justify-end row-action">
|
||||||
{{if eq $host.Status "online"}}
|
{{if eq $host.Status "online"}}
|
||||||
{{if $sc.Enabled}}
|
{{if $sc.Enabled}}
|
||||||
|
|||||||
@@ -30,6 +30,9 @@
|
|||||||
{{- else -}}
|
{{- else -}}
|
||||||
<span class="text-ink-fade italic">never run</span>
|
<span class="text-ink-fade italic">never run</span>
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
|
{{- if .NextRun -}}
|
||||||
|
<br><span class="mono text-[10.5px] text-ink-fade" title="{{.NextRun.Format "2006-01-02 15:04:05 MST"}}">next {{relTime .NextRun}}</span>
|
||||||
|
{{- end -}}
|
||||||
</div>
|
</div>
|
||||||
<div class="text-right mono {{if eq $h.Status "offline"}}text-ink-mid{{else}}text-ink{{end}}">{{bytes $h.RepoSizeBytes}}</div>
|
<div class="text-right mono {{if eq $h.Status "offline"}}text-ink-mid{{else}}text-ink{{end}}">{{bytes $h.RepoSizeBytes}}</div>
|
||||||
<div class="text-right mono {{if eq $h.Status "offline"}}text-ink-mute{{else}}text-ink-mid{{end}}">
|
<div class="text-right mono {{if eq $h.Status "offline"}}text-ink-mute{{else}}text-ink-mid{{end}}">
|
||||||
|
|||||||
Reference in New Issue
Block a user