P6-03 repo size trend + agent-update UI fix + dashboard polish #21
@@ -195,6 +195,7 @@ func (s *Server) routes(r chi.Router) {
|
||||
r.Get("/hosts/{id}/sources", s.handleUIHostSources)
|
||||
r.Get("/hosts/{id}/sources/new", s.handleUISourceGroupNewGet)
|
||||
r.Get("/hosts/{id}/sources/{gid}/edit", s.handleUISourceGroupEditGet)
|
||||
r.Get("/hosts/{id}/jobs", s.handleUIHostJobs)
|
||||
r.Get("/hosts/{id}/repo", s.handleUIHostRepo)
|
||||
r.Get("/hosts/{id}/repo/trend", s.handleUIRepoTrend)
|
||||
r.Get("/hosts/{id}/schedules", s.handleUISchedulesList)
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
stdhttp "net/http"
|
||||
|
||||
"gitea.dcglab.co.uk/steve/restic-manager/internal/store"
|
||||
)
|
||||
|
||||
// hostJobsPage is the page-data struct for /hosts/{id}/jobs.
|
||||
type hostJobsPage struct {
|
||||
hostChromeData
|
||||
Jobs []store.Job
|
||||
}
|
||||
|
||||
// handleUIHostJobs renders the per-host jobs list. Read-only — no
|
||||
// actions, just a click-through to the existing /jobs/{id} detail
|
||||
// page for any row.
|
||||
func (s *Server) handleUIHostJobs(w stdhttp.ResponseWriter, r *stdhttp.Request) {
|
||||
u := s.requireUIUser(w, r)
|
||||
if u == nil {
|
||||
return
|
||||
}
|
||||
host, ok := s.loadHostForUI(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
jobs, err := s.deps.Store.ListJobsByHost(r.Context(), host.ID, 100)
|
||||
if err != nil {
|
||||
slog.Error("ui host jobs: list", "host_id", host.ID, "err", err)
|
||||
stdhttp.Error(w, "internal", stdhttp.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
page := hostJobsPage{
|
||||
hostChromeData: s.loadHostChrome(r, *host, "jobs", "jobs"),
|
||||
Jobs: jobs,
|
||||
}
|
||||
view := s.baseView(r, u)
|
||||
view.Title = host.Name + " jobs · restic-manager"
|
||||
view.Page = page
|
||||
if err := s.deps.UI.Render(w, "host_jobs", view); err != nil {
|
||||
slog.Error("ui: render host_jobs", "err", err)
|
||||
stdhttp.Error(w, "internal", stdhttp.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
stdhttp "net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.dcglab.co.uk/steve/restic-manager/internal/store"
|
||||
)
|
||||
|
||||
func TestUIHostJobs_RendersList(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, baseURL, st := newTestServerWithUI(t)
|
||||
cookie := loginAsAdmin(t, st)
|
||||
hostID := makeHost(t, st, "h-jobs-render")
|
||||
|
||||
// Two jobs with distinct kinds + statuses.
|
||||
now := time.Now().UTC()
|
||||
ctx := context.Background()
|
||||
if err := st.CreateJob(ctx, store.Job{
|
||||
ID: "01HZZZZZZZZZZZZZZZZZZZZZ10", HostID: hostID, Kind: "backup",
|
||||
ActorKind: "user", CreatedAt: now.Add(-time.Hour),
|
||||
}); err != nil {
|
||||
t.Fatalf("create job: %v", err)
|
||||
}
|
||||
if err := st.MarkJobFinished(ctx, "01HZZZZZZZZZZZZZZZZZZZZZ10", "succeeded", 0, nil, "", now.Add(-time.Hour+time.Minute)); err != nil {
|
||||
t.Fatalf("finish job: %v", err)
|
||||
}
|
||||
if err := st.CreateJob(ctx, store.Job{
|
||||
ID: "01HZZZZZZZZZZZZZZZZZZZZZ11", HostID: hostID, Kind: "prune",
|
||||
ActorKind: "schedule", CreatedAt: now,
|
||||
}); err != nil {
|
||||
t.Fatalf("create job: %v", err)
|
||||
}
|
||||
if err := st.MarkJobFinished(ctx, "01HZZZZZZZZZZZZZZZZZZZZZ11", "failed", 1, nil, "boom", now.Add(time.Minute)); err != nil {
|
||||
t.Fatalf("finish job: %v", err)
|
||||
}
|
||||
|
||||
body := getHostJobsPage(t, baseURL, hostID, cookie)
|
||||
for _, want := range []string{"backup", "prune", "succeeded", "failed", "schedule", "user", `class="jobs-row`} {
|
||||
if !strings.Contains(body, want) {
|
||||
t.Errorf("expected %q in body, missing", want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUIHostJobs_EmptyState(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, baseURL, st := newTestServerWithUI(t)
|
||||
cookie := loginAsAdmin(t, st)
|
||||
hostID := makeHost(t, st, "h-jobs-empty")
|
||||
|
||||
body := getHostJobsPage(t, baseURL, hostID, cookie)
|
||||
if !strings.Contains(body, "No jobs yet.") {
|
||||
t.Error("expected empty-state heading")
|
||||
}
|
||||
}
|
||||
|
||||
// getHostJobsPage fetches /hosts/{id}/jobs and returns the body string.
|
||||
func getHostJobsPage(t *testing.T, baseURL, hostID string, cookie *stdhttp.Cookie) string {
|
||||
t.Helper()
|
||||
client := &stdhttp.Client{
|
||||
CheckRedirect: func(_ *stdhttp.Request, _ []*stdhttp.Request) error {
|
||||
return stdhttp.ErrUseLastResponse
|
||||
},
|
||||
}
|
||||
req, err := stdhttp.NewRequest("GET", baseURL+"/hosts/"+hostID+"/jobs", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("new request: %v", err)
|
||||
}
|
||||
req.AddCookie(cookie)
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("GET /hosts/%s/jobs: %v", hostID, err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != stdhttp.StatusOK {
|
||||
t.Fatalf("GET /hosts/%s/jobs: want 200, got %d", hostID, res.StatusCode)
|
||||
}
|
||||
raw, _ := io.ReadAll(res.Body)
|
||||
return string(raw)
|
||||
}
|
||||
@@ -75,6 +75,28 @@ func funcMap() template.FuncMap {
|
||||
return *p
|
||||
},
|
||||
"sub": func(a, b int) int { return a - b },
|
||||
// durationHuman formats the elapsed time between two *time.Time
|
||||
// values as a short human string: "350ms", "4.2s", "2m 15s",
|
||||
// "1h 4m". Returns "—" when either pointer is nil.
|
||||
"durationHuman": func(start, end *time.Time) string {
|
||||
if start == nil || end == nil {
|
||||
return "—"
|
||||
}
|
||||
d := end.Sub(*start)
|
||||
if d < 0 {
|
||||
d = -d
|
||||
}
|
||||
if d < time.Second {
|
||||
return fmt.Sprintf("%dms", d.Milliseconds())
|
||||
}
|
||||
if d < time.Minute {
|
||||
return fmt.Sprintf("%.1fs", d.Seconds())
|
||||
}
|
||||
if d < time.Hour {
|
||||
return fmt.Sprintf("%dm %ds", int(d.Minutes()), int(d.Seconds())%60)
|
||||
}
|
||||
return fmt.Sprintf("%dh %dm", int(d.Hours()), int(d.Minutes())%60)
|
||||
},
|
||||
// joinComma joins a slice with ", ". Used by the schedule list
|
||||
// to render retention summaries.
|
||||
"joinComma": func(parts []string) string { return strings.Join(parts, ", ") },
|
||||
|
||||
@@ -288,6 +288,87 @@ func (s *Store) HasJobOfKind(ctx context.Context, hostID, kind string) (bool, er
|
||||
return n > 0, nil
|
||||
}
|
||||
|
||||
// ListJobsByHost returns recent jobs for hostID, ordered by
|
||||
// created_at DESC, limited to at most `limit` rows. limit ≤ 0 is
|
||||
// treated as no limit.
|
||||
func (s *Store) ListJobsByHost(ctx context.Context, hostID string, limit int) ([]Job, error) {
|
||||
q := `SELECT id, host_id, kind, status, scheduled_id, source_group_id,
|
||||
actor_kind, actor_id, started_at, finished_at, exit_code,
|
||||
stats, error, created_at
|
||||
FROM jobs
|
||||
WHERE host_id = ?
|
||||
ORDER BY created_at DESC`
|
||||
args := []any{hostID}
|
||||
if limit > 0 {
|
||||
q += ` LIMIT ?`
|
||||
args = append(args, limit)
|
||||
}
|
||||
rows, err := s.db.QueryContext(ctx, q, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("store: list jobs by host: %w", err)
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
|
||||
var out []Job
|
||||
for rows.Next() {
|
||||
var (
|
||||
j Job
|
||||
schedID sql.NullString
|
||||
groupID sql.NullString
|
||||
actorID sql.NullString
|
||||
startedAt sql.NullString
|
||||
finishedAt sql.NullString
|
||||
exitCode sql.NullInt64
|
||||
stats sql.NullString
|
||||
errMsg sql.NullString
|
||||
createdAt string
|
||||
)
|
||||
if err := rows.Scan(&j.ID, &j.HostID, &j.Kind, &j.Status, &schedID, &groupID,
|
||||
&j.ActorKind, &actorID, &startedAt, &finishedAt,
|
||||
&exitCode, &stats, &errMsg, &createdAt); err != nil {
|
||||
return nil, fmt.Errorf("store: scan job row: %w", err)
|
||||
}
|
||||
if schedID.Valid {
|
||||
v := schedID.String
|
||||
j.ScheduledID = &v
|
||||
}
|
||||
if groupID.Valid {
|
||||
v := groupID.String
|
||||
j.SourceGroupID = &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 = json.RawMessage(stats.String)
|
||||
}
|
||||
if errMsg.Valid {
|
||||
v := errMsg.String
|
||||
j.Error = &v
|
||||
}
|
||||
t, _ := time.Parse(time.RFC3339Nano, createdAt)
|
||||
j.CreatedAt = t
|
||||
out = append(out, j)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("store: iterate jobs by host: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func nullableStr(s string) any {
|
||||
if s == "" {
|
||||
return nil
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestListJobsByHost_OrderingAndLimit(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := openTestStore(t)
|
||||
ctx := context.Background()
|
||||
|
||||
const hostID = "h-jobs-1"
|
||||
seedHost(t, s, hostID)
|
||||
|
||||
// Create three jobs with explicit CreatedAt offsets.
|
||||
base := time.Now().UTC().Truncate(time.Second)
|
||||
for i, d := range []time.Duration{-3 * time.Hour, -1 * time.Hour, -2 * time.Hour} {
|
||||
j := Job{
|
||||
ID: "j-" + string(rune('a'+i)) + "0000000000000000000000000",
|
||||
HostID: hostID,
|
||||
Kind: "backup",
|
||||
ActorKind: "user",
|
||||
CreatedAt: base.Add(d),
|
||||
}
|
||||
// Truncate ID to 26 chars (ULID width); the test only needs it
|
||||
// to be unique and stable across rows.
|
||||
j.ID = j.ID[:26]
|
||||
if err := s.CreateJob(ctx, j); err != nil {
|
||||
t.Fatalf("create job %d: %v", i, err)
|
||||
}
|
||||
}
|
||||
|
||||
jobs, err := s.ListJobsByHost(ctx, hostID, 100)
|
||||
if err != nil {
|
||||
t.Fatalf("list: %v", err)
|
||||
}
|
||||
if len(jobs) != 3 {
|
||||
t.Fatalf("want 3 jobs, got %d", len(jobs))
|
||||
}
|
||||
// Newest first ordering by created_at DESC.
|
||||
for i := 0; i < len(jobs)-1; i++ {
|
||||
if !jobs[i].CreatedAt.After(jobs[i+1].CreatedAt) && !jobs[i].CreatedAt.Equal(jobs[i+1].CreatedAt) {
|
||||
t.Fatalf("ordering broken at %d: %v then %v", i, jobs[i].CreatedAt, jobs[i+1].CreatedAt)
|
||||
}
|
||||
}
|
||||
|
||||
// Limit clamps results.
|
||||
limited, err := s.ListJobsByHost(ctx, hostID, 2)
|
||||
if err != nil {
|
||||
t.Fatalf("list limit: %v", err)
|
||||
}
|
||||
if len(limited) != 2 {
|
||||
t.Fatalf("limit 2: want 2 jobs, got %d", len(limited))
|
||||
}
|
||||
}
|
||||
|
||||
func TestListJobsByHost_OnlyThisHost(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := openTestStore(t)
|
||||
ctx := context.Background()
|
||||
|
||||
const a, b = "h-jobs-a", "h-jobs-b"
|
||||
seedHost(t, s, a)
|
||||
seedHost(t, s, b)
|
||||
|
||||
now := time.Now().UTC()
|
||||
if err := s.CreateJob(ctx, Job{ID: "01HZZZZZZZZZZZZZZZZZZZZZ01", HostID: a, Kind: "backup", ActorKind: "user", CreatedAt: now}); err != nil {
|
||||
t.Fatalf("create a: %v", err)
|
||||
}
|
||||
if err := s.CreateJob(ctx, Job{ID: "01HZZZZZZZZZZZZZZZZZZZZZ02", HostID: b, Kind: "backup", ActorKind: "user", CreatedAt: now}); err != nil {
|
||||
t.Fatalf("create b: %v", err)
|
||||
}
|
||||
|
||||
jobs, err := s.ListJobsByHost(ctx, a, 100)
|
||||
if err != nil {
|
||||
t.Fatalf("list a: %v", err)
|
||||
}
|
||||
if len(jobs) != 1 || jobs[0].HostID != a {
|
||||
t.Fatalf("expected 1 job for host a, got %d (%v)", len(jobs), jobs)
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -439,6 +439,31 @@
|
||||
.schd-row.clickable > .row-link { pointer-events: auto; }
|
||||
.schd-row.clickable > .row-action { pointer-events: auto; }
|
||||
|
||||
/* ---------- jobs rows (Jobs tab) ---------- */
|
||||
.jobs-row {
|
||||
display: grid;
|
||||
grid-template-columns: 110px 110px 90px 1fr 1fr 28px;
|
||||
gap: 14px;
|
||||
align-items: center;
|
||||
padding: 9px 14px;
|
||||
font-size: 12.5px;
|
||||
}
|
||||
.jobs-row.head {
|
||||
font-size: 11px;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.08em;
|
||||
color: var(--ink-mid);
|
||||
padding-top: 11px;
|
||||
padding-bottom: 11px;
|
||||
}
|
||||
.jobs-row.clickable { position: relative; }
|
||||
.jobs-row.clickable .row-link {
|
||||
position: absolute; inset: 0; display: block; z-index: 0;
|
||||
}
|
||||
.jobs-row.clickable:hover { background: var(--panel-hi); cursor: pointer; }
|
||||
.jobs-row.clickable > * { position: relative; z-index: 1; pointer-events: none; }
|
||||
.jobs-row.clickable > .row-link { pointer-events: auto; }
|
||||
|
||||
/* ---------- cron preset chips ---------- */
|
||||
.preset-chip {
|
||||
font-family: 'JetBrains Mono', monospace; font-size: 11.5px;
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
{{define "title"}}{{.Title}}{{end}}
|
||||
|
||||
{{define "content"}}
|
||||
{{template "host_chrome" .}}
|
||||
{{$page := .Page}}
|
||||
{{$host := $page.Host}}
|
||||
<div class="max-w-[1280px] mx-auto px-8 pb-14 pt-6">
|
||||
|
||||
<div class="flex items-center justify-between mb-4">
|
||||
<p class="text-pretty text-[12.5px] text-ink-mute leading-[1.6] max-w-[760px]">
|
||||
Recent jobs for this host — backups, prunes, checks, restores, repo init/probe, agent updates.
|
||||
Newest first, limited to the last 100. Click a row for the full log.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{{if eq (len $page.Jobs) 0}}
|
||||
<div class="panel rounded-[7px] empty-state" style="border-radius: 7px;">
|
||||
<h3 class="text-base font-medium tracking-[-0.005em]">No jobs yet.</h3>
|
||||
<p class="text-pretty text-ink-mute text-[13px] mt-2 mx-auto max-w-[480px] leading-[1.65]">
|
||||
Trigger a backup from the Sources tab, or wait for a schedule to fire — jobs appear here as soon as they're queued.
|
||||
</p>
|
||||
</div>
|
||||
{{else}}
|
||||
<div class="panel rounded-[7px] overflow-hidden">
|
||||
<div class="jobs-row head hairline">
|
||||
<div>Kind</div>
|
||||
<div>Status</div>
|
||||
<div>Actor</div>
|
||||
<div>Started</div>
|
||||
<div>Duration</div>
|
||||
<div></div>
|
||||
</div>
|
||||
{{range $i, $j := $page.Jobs}}
|
||||
<div class="jobs-row clickable {{if not (eq $i 0)}}hairline{{end}}">
|
||||
<a href="/jobs/{{$j.ID}}" class="row-link" aria-label="Open job"></a>
|
||||
<div class="mono text-ink">{{$j.Kind}}</div>
|
||||
<div>
|
||||
{{if eq $j.Status "succeeded"}}
|
||||
<span class="mono text-[11px] text-ok">succeeded</span>
|
||||
{{else if eq $j.Status "failed"}}
|
||||
<span class="mono text-[11px] text-bad">failed</span>
|
||||
{{else if eq $j.Status "cancelled"}}
|
||||
<span class="mono text-[11px] text-warn">cancelled</span>
|
||||
{{else if eq $j.Status "running"}}
|
||||
<span class="mono text-[11px] text-accent">running</span>
|
||||
{{else}}
|
||||
<span class="mono text-[11px] text-ink-mid">{{$j.Status}}</span>
|
||||
{{end}}
|
||||
</div>
|
||||
<div class="mono text-[11.5px] text-ink-mid">{{$j.ActorKind}}</div>
|
||||
<div class="mono text-[11.5px] {{if $j.StartedAt}}text-ink-mid{{else}}text-ink-fade{{end}}"
|
||||
{{if $j.StartedAt}}title="{{$j.StartedAt.Format "2006-01-02 15:04:05 MST"}}"{{end}}>
|
||||
{{if $j.StartedAt}}{{relTime $j.StartedAt}}{{else}}<span class="text-ink-fade">queued</span>{{end}}
|
||||
</div>
|
||||
<div class="mono text-[11.5px] text-ink-mid">
|
||||
{{if and $j.StartedAt $j.FinishedAt}}{{durationHuman $j.StartedAt $j.FinishedAt}}{{else}}<span class="text-ink-fade">—</span>{{end}}
|
||||
</div>
|
||||
<div class="text-right text-ink-fade row-action">→</div>
|
||||
</div>
|
||||
{{end}}
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
</div>
|
||||
{{end}}
|
||||
@@ -176,8 +176,7 @@
|
||||
<a class="sub-tab {{if eq $page.SubTab "sources"}}active{{end}}" href="/hosts/{{$host.ID}}/sources">Sources <span class="mono text-ink-fade text-[11px] ml-1">{{$page.SourceGroupCount}}</span></a>
|
||||
<a class="sub-tab {{if eq $page.SubTab "schedules"}}active{{end}}" href="/hosts/{{$host.ID}}/schedules">Schedules <span class="mono text-ink-fade text-[11px] ml-1">{{$page.ScheduleCount}}</span></a>
|
||||
<a class="sub-tab {{if eq $page.SubTab "repo"}}active{{end}}" href="/hosts/{{$host.ID}}/repo">Repo</a>
|
||||
<div class="sub-tab" title="lands later">Jobs</div>
|
||||
<div class="sub-tab" title="lands later">Settings</div>
|
||||
<a class="sub-tab {{if eq $page.SubTab "jobs"}}active{{end}}" href="/hosts/{{$host.ID}}/jobs">Jobs</a>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
Reference in New Issue
Block a user