refactor(webui): extract the schedules and alerts modules (#1778)
This commit is contained in:
4 files changed
+93
-67
No files matched your search
@@ -3,6 +3,7 @@ package webui
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -72,10 +73,23 @@ func viewOfAlert(a alert.Alert) alertView {
|
||||
return view
|
||||
}
|
||||
|
||||
// alertsAPI owns the /api/alerts routes. A nil source answers 503.
|
||||
type alertsAPI struct {
|
||||
alerts AlertSource
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
func newAlertsAPI(s *Server) *alertsAPI { return &alertsAPI{alerts: s.Alerts, log: s.Log} }
|
||||
|
||||
func (a *alertsAPI) register(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /api/alerts", a.handleAlerts)
|
||||
mux.HandleFunc("POST /api/alerts/{id}/dismiss", a.handleAlertDismiss)
|
||||
}
|
||||
|
||||
// handleAlerts lists the caller's org's alerts: open ones unless ?state names
|
||||
// others (comma separated), most severe first.
|
||||
func (s *Server) handleAlerts(w http.ResponseWriter, r *http.Request) {
|
||||
if s.Alerts == nil {
|
||||
func (a *alertsAPI) handleAlerts(w http.ResponseWriter, r *http.Request) {
|
||||
if a.alerts == nil {
|
||||
http.Error(w, "alerts unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
@@ -85,9 +99,9 @@ func (s *Server) handleAlerts(w http.ResponseWriter, r *http.Request) {
|
||||
filter.States = append(filter.States, alert.State(strings.TrimSpace(state)))
|
||||
}
|
||||
}
|
||||
alerts, err := s.Alerts.ListAlerts(r.Context(), policyOrg(r), filter)
|
||||
alerts, err := a.alerts.ListAlerts(r.Context(), policyOrg(r), filter)
|
||||
if err != nil {
|
||||
s.Log.ErrorContext(r.Context(), "list alerts", "err", err)
|
||||
a.log.ErrorContext(r.Context(), "list alerts", "err", err)
|
||||
http.Error(w, "list alerts failed", http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
@@ -99,8 +113,8 @@ func (s *Server) handleAlerts(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// handleAlertDismiss dismisses one open alert of the caller's org.
|
||||
func (s *Server) handleAlertDismiss(w http.ResponseWriter, r *http.Request) {
|
||||
if s.Alerts == nil {
|
||||
func (a *alertsAPI) handleAlertDismiss(w http.ResponseWriter, r *http.Request) {
|
||||
if a.alerts == nil {
|
||||
http.Error(w, "alerts unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
@@ -109,12 +123,12 @@ func (s *Server) handleAlertDismiss(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "invalid alert id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
err = s.Alerts.DismissAlert(r.Context(), policyOrg(r), id, access.ActorFromContext(r.Context()))
|
||||
err = a.alerts.DismissAlert(r.Context(), policyOrg(r), id, access.ActorFromContext(r.Context()))
|
||||
switch {
|
||||
case errors.Is(err, alert.ErrNotFound):
|
||||
http.Error(w, "alert not found", http.StatusNotFound)
|
||||
case err != nil:
|
||||
s.Log.ErrorContext(r.Context(), "dismiss alert", "err", err)
|
||||
a.log.ErrorContext(r.Context(), "dismiss alert", "err", err)
|
||||
http.Error(w, "dismiss alert failed", http.StatusBadGateway)
|
||||
default:
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
@@ -79,15 +80,38 @@ func runView(r scheduling.Run) scheduleRunView {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) ownerView(ctx context.Context, owner scheduling.Owner, names map[org.OrgID]string) scheduleOwnerView {
|
||||
// schedulesAPI owns the /api/schedules routes. They are org-scoped, not
|
||||
// instance-only: the system org sees every schedule, any other org only its
|
||||
// own, and the handlers enforce that ownership.
|
||||
type schedulesAPI struct {
|
||||
schedules ScheduleStore
|
||||
orgs org.API
|
||||
dispatches storecontract.DispatchLedger
|
||||
log *slog.Logger
|
||||
authorize func(http.ResponseWriter, *http.Request) bool
|
||||
}
|
||||
|
||||
func newSchedulesAPI(s *Server) *schedulesAPI {
|
||||
return &schedulesAPI{schedules: s.Schedules, orgs: s.Orgs, dispatches: s.Dispatches, log: s.Log, authorize: s.authorizeTaskMutation}
|
||||
}
|
||||
|
||||
func (a *schedulesAPI) register(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /api/schedules", a.handleSchedules)
|
||||
mux.HandleFunc("PUT /api/schedules/{id}", a.handleSchedulePut)
|
||||
mux.HandleFunc("DELETE /api/schedules/{id}", a.handleScheduleDelete)
|
||||
mux.HandleFunc("GET /api/schedules/{id}/runs", a.handleScheduleRuns)
|
||||
mux.HandleFunc("POST /api/schedules/{id}/runs", a.handleScheduleRunNow)
|
||||
}
|
||||
|
||||
func (a *schedulesAPI) ownerView(ctx context.Context, owner scheduling.Owner, names map[org.OrgID]string) scheduleOwnerView {
|
||||
view := scheduleOwnerView{Kind: owner.Kind, ID: owner.ID}
|
||||
id := org.OrgID(owner.ID)
|
||||
if owner.Kind == scheduling.OwnerInstance {
|
||||
id = org.DefaultOrgID
|
||||
}
|
||||
name, ok := names[id]
|
||||
if !ok && s.Orgs != nil {
|
||||
if found, err := s.Orgs.GetOrg(ctx, id); err == nil {
|
||||
if !ok && a.orgs != nil {
|
||||
if found, err := a.orgs.GetOrg(ctx, id); err == nil {
|
||||
name = found.Name
|
||||
}
|
||||
names[id] = name
|
||||
@@ -96,9 +120,9 @@ func (s *Server) ownerView(ctx context.Context, owner scheduling.Owner, names ma
|
||||
return view
|
||||
}
|
||||
|
||||
func (s *Server) scheduleView(ctx context.Context, sched scheduling.Schedule, names map[org.OrgID]string) scheduleView {
|
||||
func (a *schedulesAPI) scheduleView(ctx context.Context, sched scheduling.Schedule, names map[org.OrgID]string) scheduleView {
|
||||
view := scheduleView{
|
||||
ID: sched.ID, Name: sched.Name, Owner: s.ownerView(ctx, sched.Owner, names),
|
||||
ID: sched.ID, Name: sched.Name, Owner: a.ownerView(ctx, sched.Owner, names),
|
||||
Enabled: sched.Enabled, BuiltIn: sched.BuiltIn, Timing: sched.Timing,
|
||||
NextRun: optionalTime(sched.NextRun),
|
||||
}
|
||||
@@ -109,7 +133,7 @@ func (s *Server) scheduleView(ctx context.Context, sched scheduling.Schedule, na
|
||||
return view
|
||||
}
|
||||
|
||||
func (s *Server) scheduleError(ctx context.Context, w http.ResponseWriter, op string, err error) {
|
||||
func (a *schedulesAPI) scheduleError(ctx context.Context, w http.ResponseWriter, op string, err error) {
|
||||
switch {
|
||||
case errors.Is(err, scheduling.ErrNotFound):
|
||||
http.Error(w, "schedule not found", http.StatusNotFound)
|
||||
@@ -118,13 +142,13 @@ func (s *Server) scheduleError(ctx context.Context, w http.ResponseWriter, op st
|
||||
case errors.Is(err, scheduling.ErrInvalidSpec):
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
default:
|
||||
s.Log.ErrorContext(ctx, op, "err", err)
|
||||
a.log.ErrorContext(ctx, op, "err", err)
|
||||
http.Error(w, op+" failed", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) schedulesReady(w http.ResponseWriter) bool {
|
||||
if s.Schedules == nil {
|
||||
func (a *schedulesAPI) schedulesReady(w http.ResponseWriter) bool {
|
||||
if a.schedules == nil {
|
||||
http.Error(w, "schedules unavailable", http.StatusServiceUnavailable)
|
||||
return false
|
||||
}
|
||||
@@ -152,15 +176,15 @@ func scheduleOwned(instance bool, orgID org.OrgID, sched scheduling.Schedule) bo
|
||||
|
||||
// ownedSchedule reads the named schedule and reports whether the acting org
|
||||
// owns it, answering not found when it does not exist or belongs elsewhere.
|
||||
func (s *Server) ownedSchedule(w http.ResponseWriter, r *http.Request, id string) (scheduling.Schedule, bool) {
|
||||
func (a *schedulesAPI) ownedSchedule(w http.ResponseWriter, r *http.Request, id string) (scheduling.Schedule, bool) {
|
||||
orgID, instance := scheduleScope(r)
|
||||
sched, err := s.Schedules.GetSchedule(r.Context(), id)
|
||||
sched, err := a.schedules.GetSchedule(r.Context(), id)
|
||||
switch {
|
||||
case errors.Is(err, scheduling.ErrNotFound):
|
||||
http.Error(w, "schedule not found", http.StatusNotFound)
|
||||
return scheduling.Schedule{}, false
|
||||
case err != nil:
|
||||
s.scheduleError(r.Context(), w, "get schedule", err)
|
||||
a.scheduleError(r.Context(), w, "get schedule", err)
|
||||
return scheduling.Schedule{}, false
|
||||
case !scheduleOwned(instance, orgID, sched):
|
||||
http.Error(w, "schedule not found", http.StatusNotFound)
|
||||
@@ -169,13 +193,13 @@ func (s *Server) ownedSchedule(w http.ResponseWriter, r *http.Request, id string
|
||||
return sched, true
|
||||
}
|
||||
|
||||
func (s *Server) handleSchedules(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.schedulesReady(w) {
|
||||
func (a *schedulesAPI) handleSchedules(w http.ResponseWriter, r *http.Request) {
|
||||
if !a.schedulesReady(w) {
|
||||
return
|
||||
}
|
||||
list, err := s.Schedules.ListSchedules(r.Context())
|
||||
list, err := a.schedules.ListSchedules(r.Context())
|
||||
if err != nil {
|
||||
s.scheduleError(r.Context(), w, "list schedules", err)
|
||||
a.scheduleError(r.Context(), w, "list schedules", err)
|
||||
return
|
||||
}
|
||||
orgID, instance := scheduleScope(r)
|
||||
@@ -185,7 +209,7 @@ func (s *Server) handleSchedules(w http.ResponseWriter, r *http.Request) {
|
||||
if !scheduleOwned(instance, orgID, sched) {
|
||||
continue
|
||||
}
|
||||
views = append(views, s.scheduleView(r.Context(), sched, names))
|
||||
views = append(views, a.scheduleView(r.Context(), sched, names))
|
||||
}
|
||||
writeJSON(w, map[string]any{"schedules": views})
|
||||
}
|
||||
@@ -194,8 +218,8 @@ func (s *Server) handleSchedules(w http.ResponseWriter, r *http.Request) {
|
||||
// change on it. A new schedule belongs to the instance when the system org
|
||||
// writes it and to the acting org otherwise; an existing schedule must be the
|
||||
// acting org's own.
|
||||
func (s *Server) handleSchedulePut(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.authorizeTaskMutation(w, r) || !s.schedulesReady(w) {
|
||||
func (a *schedulesAPI) handleSchedulePut(w http.ResponseWriter, r *http.Request) {
|
||||
if !a.authorize(w, r) || !a.schedulesReady(w) {
|
||||
return
|
||||
}
|
||||
var input scheduleInput
|
||||
@@ -205,7 +229,7 @@ func (s *Server) handleSchedulePut(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
id := r.PathValue("id")
|
||||
orgID, instance := scheduleScope(r)
|
||||
sched, err := s.Schedules.GetSchedule(r.Context(), id)
|
||||
sched, err := a.schedules.GetSchedule(r.Context(), id)
|
||||
switch {
|
||||
case errors.Is(err, scheduling.ErrNotFound):
|
||||
owner := scheduling.Owner{Kind: scheduling.OwnerInstance}
|
||||
@@ -214,45 +238,45 @@ func (s *Server) handleSchedulePut(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
sched = scheduling.Schedule{ID: id, Owner: owner}
|
||||
case err != nil:
|
||||
s.scheduleError(r.Context(), w, "get schedule", err)
|
||||
a.scheduleError(r.Context(), w, "get schedule", err)
|
||||
return
|
||||
case !scheduleOwned(instance, orgID, sched):
|
||||
http.Error(w, "schedule not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
sched.Name, sched.Enabled, sched.Timing = input.Name, input.Enabled, input.Timing
|
||||
saved, err := s.Schedules.PutSchedule(r.Context(), sched)
|
||||
saved, err := a.schedules.PutSchedule(r.Context(), sched)
|
||||
if err != nil {
|
||||
s.scheduleError(r.Context(), w, "save schedule", err)
|
||||
a.scheduleError(r.Context(), w, "save schedule", err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, s.scheduleView(r.Context(), saved, map[org.OrgID]string{}))
|
||||
writeJSON(w, a.scheduleView(r.Context(), saved, map[org.OrgID]string{}))
|
||||
}
|
||||
|
||||
func (s *Server) handleScheduleDelete(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.authorizeTaskMutation(w, r) || !s.schedulesReady(w) {
|
||||
func (a *schedulesAPI) handleScheduleDelete(w http.ResponseWriter, r *http.Request) {
|
||||
if !a.authorize(w, r) || !a.schedulesReady(w) {
|
||||
return
|
||||
}
|
||||
if _, ok := s.ownedSchedule(w, r, r.PathValue("id")); !ok {
|
||||
if _, ok := a.ownedSchedule(w, r, r.PathValue("id")); !ok {
|
||||
return
|
||||
}
|
||||
if err := s.Schedules.DeleteSchedule(r.Context(), r.PathValue("id")); err != nil {
|
||||
s.scheduleError(r.Context(), w, "delete schedule", err)
|
||||
if err := a.schedules.DeleteSchedule(r.Context(), r.PathValue("id")); err != nil {
|
||||
a.scheduleError(r.Context(), w, "delete schedule", err)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (s *Server) handleScheduleRunNow(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.authorizeTaskMutation(w, r) || !s.schedulesReady(w) {
|
||||
func (a *schedulesAPI) handleScheduleRunNow(w http.ResponseWriter, r *http.Request) {
|
||||
if !a.authorize(w, r) || !a.schedulesReady(w) {
|
||||
return
|
||||
}
|
||||
if _, ok := s.ownedSchedule(w, r, r.PathValue("id")); !ok {
|
||||
if _, ok := a.ownedSchedule(w, r, r.PathValue("id")); !ok {
|
||||
return
|
||||
}
|
||||
run, err := s.Schedules.RequestScheduleRun(r.Context(), r.PathValue("id"))
|
||||
run, err := a.schedules.RequestScheduleRun(r.Context(), r.PathValue("id"))
|
||||
if err != nil {
|
||||
s.scheduleError(r.Context(), w, "run schedule", err)
|
||||
a.scheduleError(r.Context(), w, "run schedule", err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, runView(run))
|
||||
@@ -260,34 +284,34 @@ func (s *Server) handleScheduleRunNow(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// handleScheduleRuns lists a schedule's runs, each with the tasks its firing
|
||||
// started: the binding dispatches recorded against the run's capture.
|
||||
func (s *Server) handleScheduleRuns(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.schedulesReady(w) {
|
||||
func (a *schedulesAPI) handleScheduleRuns(w http.ResponseWriter, r *http.Request) {
|
||||
if !a.schedulesReady(w) {
|
||||
return
|
||||
}
|
||||
if _, ok := s.ownedSchedule(w, r, r.PathValue("id")); !ok {
|
||||
if _, ok := a.ownedSchedule(w, r, r.PathValue("id")); !ok {
|
||||
return
|
||||
}
|
||||
runs, err := s.Schedules.ListScheduleRuns(r.Context(), r.PathValue("id"), scheduleRunsShown)
|
||||
runs, err := a.schedules.ListScheduleRuns(r.Context(), r.PathValue("id"), scheduleRunsShown)
|
||||
if err != nil {
|
||||
s.scheduleError(r.Context(), w, "list schedule runs", err)
|
||||
a.scheduleError(r.Context(), w, "list schedule runs", err)
|
||||
return
|
||||
}
|
||||
views := make([]scheduleRunView, 0, len(runs))
|
||||
for _, run := range runs {
|
||||
view := runView(run)
|
||||
view.TaskIDs = s.runTasks(r.Context(), run.CaptureID)
|
||||
view.TaskIDs = a.runTasks(r.Context(), run.CaptureID)
|
||||
views = append(views, view)
|
||||
}
|
||||
writeJSON(w, map[string]any{"runs": views})
|
||||
}
|
||||
|
||||
func (s *Server) runTasks(ctx context.Context, captureID string) []int64 {
|
||||
if captureID == "" || s.Dispatches == nil {
|
||||
func (a *schedulesAPI) runTasks(ctx context.Context, captureID string) []int64 {
|
||||
if captureID == "" || a.dispatches == nil {
|
||||
return nil
|
||||
}
|
||||
dispatches, err := s.Dispatches.ListDispatches(ctx, storecontract.DispatchFilter{CaptureID: captureID, Limit: 50})
|
||||
dispatches, err := a.dispatches.ListDispatches(ctx, storecontract.DispatchFilter{CaptureID: captureID, Limit: 50})
|
||||
if err != nil {
|
||||
s.Log.WarnContext(ctx, "schedule run tasks unavailable", "capture", captureID, "err", err)
|
||||
a.log.WarnContext(ctx, "schedule run tasks unavailable", "capture", captureID, "err", err)
|
||||
return nil
|
||||
}
|
||||
var ids []int64
|
||||
@@ -99,11 +99,7 @@ func orgSchedule(id, orgID string) scheduling.Schedule {
|
||||
// wrappers the real server mounts.
|
||||
func scheduleRoutes(s *Server) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /api/schedules", s.handleSchedules)
|
||||
mux.HandleFunc("PUT /api/schedules/{id}", s.handleSchedulePut)
|
||||
mux.HandleFunc("DELETE /api/schedules/{id}", s.handleScheduleDelete)
|
||||
mux.HandleFunc("GET /api/schedules/{id}/runs", s.handleScheduleRuns)
|
||||
mux.HandleFunc("POST /api/schedules/{id}/runs", s.handleScheduleRunNow)
|
||||
newSchedulesAPI(s).register(mux)
|
||||
return s.requireToken(s.authorize(mux))
|
||||
}
|
||||
|
||||
@@ -288,22 +288,14 @@ func (s *Server) registerCoreRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /api/skills", instanceOnly(s.handleSkills))
|
||||
mux.HandleFunc("GET /api/curators", instanceOnly(s.handleCurators))
|
||||
mux.HandleFunc("GET /api/captures", s.handleCaptures)
|
||||
// Schedule routes are org-scoped, not instance-only: the system org sees
|
||||
// every schedule, any other org only its own, and the handlers enforce
|
||||
// that ownership.
|
||||
mux.HandleFunc("GET /api/schedules", s.handleSchedules)
|
||||
mux.HandleFunc("PUT /api/schedules/{id}", s.handleSchedulePut)
|
||||
mux.HandleFunc("DELETE /api/schedules/{id}", s.handleScheduleDelete)
|
||||
mux.HandleFunc("GET /api/schedules/{id}/runs", s.handleScheduleRuns)
|
||||
mux.HandleFunc("POST /api/schedules/{id}/runs", s.handleScheduleRunNow)
|
||||
newSchedulesAPI(s).register(mux)
|
||||
newAlertsAPI(s).register(mux)
|
||||
mux.HandleFunc("GET /api/channels", instanceOnly(s.handleChannels))
|
||||
mux.HandleFunc("POST /api/channels/{id}/reload", instanceOnly(s.handleChannelReload))
|
||||
mux.HandleFunc("GET /api/version", s.handleVersion)
|
||||
mux.HandleFunc("GET /api/capabilities", s.handleCapabilities)
|
||||
mux.HandleFunc("GET /api/audit", s.handleAudit)
|
||||
mux.HandleFunc("GET /api/audit/export", s.handleAuditExport)
|
||||
mux.HandleFunc("GET /api/alerts", s.handleAlerts)
|
||||
mux.HandleFunc("POST /api/alerts/{id}/dismiss", s.handleAlertDismiss)
|
||||
}
|
||||
|
||||
func (s *Server) registerTaskRoutes(mux *http.ServeMux) {
|
||||
|
||||
Reference in new issue
Block a user