ui: per-host Jobs sub-tab; drop unused Settings stub
Adds /hosts/{id}/jobs page listing recent jobs for the host (newest
first, capped at 100) with click-through to /jobs/{id}. Converts the
Jobs placeholder <div> to a real <a> nav link; removes the Settings
stub entirely. Also registers durationHuman template func and a
.jobs-row CSS grid to match the existing .schd-row idiom.
This commit is contained in:
@@ -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, ", ") },
|
||||
|
||||
Reference in New Issue
Block a user