* feat(state): durable operator alerts owned by the State Store Alerts dedup on org+kind+subject, resolve themselves when a condition clears, and are raised, resolved, dismissed and listed through the state contract. Task transitions raise needs-human and failed alerts in the same transaction; the store checks service health and prunes closed alerts. Each raise and resolve queues a webhook delivery whose attempts are kept. * feat(notify): deliver operator alerts to the webhook; raise config and release alerts notify.webhook now receives raised and resolved alerts above a severity threshold with HMAC-signed payloads, bounded retry and every attempt recorded on the alert. archied raises model alias, provider and credential-binding alerts from the effective configuration, an update alert from the release channel, and a credential-rejected alert from the egress resolver. The dashboard API lists and dismisses the caller's org's alerts. * feat(ui): alert bell in the top bar and webhook alert settings The bell lists the org's open alerts by severity with dismiss and a link per alert, and refreshes from the event stream. The task settings page gains the webhook severity threshold and signing secret. ui/dist rebuilt.
123 lines
4.1 KiB
Go
123 lines
4.1 KiB
Go
package webui
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
|
"github.com/samcharles93/archie-core/internal/domain/alert"
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
)
|
|
|
|
// AlertSource is the alert surface the dashboard reads and dismisses
|
|
// through. The caller's org is the only one it can reach.
|
|
type AlertSource interface {
|
|
ListAlerts(ctx context.Context, orgID org.OrgID, filter alert.Filter) ([]alert.Alert, error)
|
|
DismissAlert(ctx context.Context, orgID org.OrgID, id int64, by string) error
|
|
}
|
|
|
|
type alertView struct {
|
|
ID int64 `json:"id"`
|
|
Kind string `json:"kind"`
|
|
Subject string `json:"subject"`
|
|
Severity string `json:"severity"`
|
|
Title string `json:"title"`
|
|
Body string `json:"body"`
|
|
Link string `json:"link,omitempty"`
|
|
Condition bool `json:"condition"`
|
|
State string `json:"state"`
|
|
FirstRaised time.Time `json:"first_raised"`
|
|
LastSeen time.Time `json:"last_seen"`
|
|
Count int64 `json:"count"`
|
|
ClosedBy string `json:"closed_by,omitempty"`
|
|
ClosedAt *time.Time `json:"closed_at,omitempty"`
|
|
Deliveries []deliveryView `json:"deliveries"`
|
|
}
|
|
|
|
type deliveryView struct {
|
|
Event string `json:"event"`
|
|
State string `json:"state"`
|
|
Created time.Time `json:"created_at"`
|
|
Attempts []attemptView `json:"attempts"`
|
|
}
|
|
|
|
type attemptView struct {
|
|
N int64 `json:"n"`
|
|
At time.Time `json:"at"`
|
|
StatusCode int `json:"status_code,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
func viewOfAlert(a alert.Alert) alertView {
|
|
view := alertView{
|
|
ID: a.ID, Kind: a.Kind, Subject: a.Subject, Severity: string(a.Severity), Title: a.Title,
|
|
Body: a.Body, Link: a.Link, Condition: a.Condition, State: string(a.State),
|
|
FirstRaised: a.FirstRaised, LastSeen: a.LastSeen, Count: a.Count, ClosedBy: a.ClosedBy,
|
|
Deliveries: make([]deliveryView, 0, len(a.Deliveries)),
|
|
}
|
|
if !a.ClosedAt.IsZero() {
|
|
view.ClosedAt = &a.ClosedAt
|
|
}
|
|
for _, d := range a.Deliveries {
|
|
dv := deliveryView{Event: d.Event, State: d.State, Created: d.CreatedAt, Attempts: make([]attemptView, 0, len(d.Attempts))}
|
|
for _, at := range d.Attempts {
|
|
dv.Attempts = append(dv.Attempts, attemptView{N: at.N, At: at.At, StatusCode: at.StatusCode, Error: at.Error})
|
|
}
|
|
view.Deliveries = append(view.Deliveries, dv)
|
|
}
|
|
return view
|
|
}
|
|
|
|
// 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 {
|
|
http.Error(w, "alerts unavailable", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
filter := alert.Filter{}
|
|
if states := r.URL.Query().Get("state"); states != "" {
|
|
for state := range strings.SplitSeq(states, ",") {
|
|
filter.States = append(filter.States, alert.State(strings.TrimSpace(state)))
|
|
}
|
|
}
|
|
alerts, err := s.Alerts.ListAlerts(r.Context(), policyOrg(r), filter)
|
|
if err != nil {
|
|
s.Log.Error("list alerts", "err", err)
|
|
http.Error(w, "list alerts failed", http.StatusBadGateway)
|
|
return
|
|
}
|
|
views := make([]alertView, len(alerts))
|
|
for i, a := range alerts {
|
|
views[i] = viewOfAlert(a)
|
|
}
|
|
writeJSON(w, map[string]any{"alerts": views})
|
|
}
|
|
|
|
// handleAlertDismiss dismisses one open alert of the caller's org.
|
|
func (s *Server) handleAlertDismiss(w http.ResponseWriter, r *http.Request) {
|
|
if s.Alerts == nil {
|
|
http.Error(w, "alerts unavailable", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
|
if err != nil {
|
|
http.Error(w, "invalid alert id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
err = s.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.Error("dismiss alert", "err", err)
|
|
http.Error(w, "dismiss alert failed", http.StatusBadGateway)
|
|
default:
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
}
|