Files
Niklas Ye acb23c2395 Add events: a shared context for incidents that belong together
Incidents stay separate until Alertmanager groups their alerts, but a bad
release can break the API, the connection pool and a dozen pods in
different groups, and three months later each incident on its own says
little. An event is the context they share: a release, a change, a
maintenance window or an outage. Incidents attach to it, many to many,
and one of them can be marked as the trigger. Incidents are never merged,
so each keeps its own timeline, assignee and metrics; merging would have
thrown that away and could not be undone.

Nothing attaches by itself. An incident's page and
GET /api/incidents/{id}/related-events suggest events within two hours of
it, and a person decides. Events are team-scoped like incidents, kind is
a closed set (a CHECK constraint, so the UI can colour and filter by it),
and they are hard-deleted: the incidents stay and each one's timeline
records the detach. A repeated external_id returns the event already
recorded, so CI can post a release marker and retry safely. Figures such
as time to first ack and the first alarm to last resolution are computed
when an event is read and never stored.

The web UI gets an Events page and an event page with a merged timeline,
an Events card on the incident, and a tag on queue rows. make dev seeds a
release and a maintenance window.

All of it is additive. Migration 004 only creates tables, so v0.53.1
reads the same database and ignores them. The incident JSON gains
event_count (always present) and events (omitted when empty, so clients
detect support from event_count). terdut-tui keeps working unchanged and
simply does not show events yet; no TUI version is required and none is
excluded. The operator's client is untouched. The Prometheus allowlist, the CSP and the
resource limits do not change.
2026-11-04 15:35:40 +01:00

139 lines
5.6 KiB
Go

package models
import "time"
// Event kinds. Closed on purpose: it is a CHECK constraint (migration 004), so
// the web UI can colour and filter by it, and adding one is a migration.
const (
EventKindRelease = "release"
EventKindChange = "change"
EventKindMaintenance = "maintenance"
EventKindOutage = "outage"
EventKindOther = "other"
)
// Event sources: who recorded the event.
const (
EventSourceManual = "manual" // a person
EventSourceAPI = "api" // a service account, typically CI
)
// ValidEventKind reports whether k is one of the kinds an event may have.
func ValidEventKind(k string) bool {
switch k {
case EventKindRelease, EventKindChange, EventKindMaintenance, EventKindOutage, EventKindOther:
return true
}
return false
}
// Event is a context that incidents are attached to: a release, a change, a
// maintenance window, an outage. It explains incidents without merging them;
// each incident keeps its own timeline, assignee and metrics.
type Event struct {
ID int64 `json:"id"`
TeamID int64 `json:"team_id"`
TeamName string `json:"team_name,omitempty"`
Kind string `json:"kind"`
Title string `json:"title"`
Summary string `json:"summary"`
StartedAt time.Time `json:"started_at"`
// EndedAt is absent for a marker with no known end, or one still going.
EndedAt *time.Time `json:"ended_at,omitempty"`
Source string `json:"source"`
ExternalID *string `json:"external_id,omitempty"`
ExternalURL *string `json:"external_url,omitempty"`
// CreatedBy* and CreatedByServiceAccount* are mutually exclusive, and both
// absent once the creator has been deleted.
CreatedByID *int64 `json:"created_by_id,omitempty"`
CreatedBy *string `json:"created_by,omitempty"`
CreatedByServiceAccountID *int64 `json:"created_by_service_account_id,omitempty"`
CreatedByServiceAccount *string `json:"created_by_service_account,omitempty"`
CreatedAt time.Time `json:"created_at"`
// Everything below is derived from the attached incidents each time the
// event is read; none of it is stored.
IncidentCount int `json:"incident_count"`
OpenIncidentCount int `json:"open_incident_count"`
// WorstSeverity is the highest severity among the attached incidents.
WorstSeverity *string `json:"worst_severity,omitempty"`
// FirstTriggeredAt is when the earliest attached incident opened.
FirstTriggeredAt *time.Time `json:"first_triggered_at,omitempty"`
// TimeToFirstAckSeconds runs from the first incident opening to the first
// acknowledgement of any of them. Absent while none has been acknowledged.
TimeToFirstAckSeconds *int64 `json:"time_to_first_ack_seconds,omitempty"`
// AllResolvedAt and SpanSeconds are set once every attached incident is
// resolved: the last resolution, and the time from the first incident
// opening to it.
AllResolvedAt *time.Time `json:"all_resolved_at,omitempty"`
SpanSeconds *int64 `json:"span_seconds,omitempty"`
// Incidents is every attached incident on GET /api/events/{id}, and only
// the first three (trigger first) in a list, where IncidentCount says how
// many there are in all.
Incidents []EventIncident `json:"incidents,omitempty"`
}
// EventIncident is one incident attached to an event, with what a list or the
// event page needs to show it without fetching the incident.
type EventIncident struct {
IncidentID int64 `json:"incident_id"`
Title string `json:"title"`
Status string `json:"status"`
Severity *string `json:"severity,omitempty"`
TriggeredAt time.Time `json:"triggered_at"`
AcknowledgedAt *time.Time `json:"acknowledged_at,omitempty"`
ResolvedAt *time.Time `json:"resolved_at,omitempty"`
IsTrigger bool `json:"is_trigger"`
AttachedAt time.Time `json:"attached_at"`
}
// EventNote is a note on the event itself.
type EventNote struct {
ID int64 `json:"id"`
EventID int64 `json:"event_id"`
Content string `json:"content"`
UserID *int64 `json:"user_id,omitempty"`
Username *string `json:"username,omitempty"`
ServiceAccountID *int64 `json:"service_account_id,omitempty"`
ServiceAccountName *string `json:"service_account_name,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
// EventTimelineEntry is one line of an event's merged timeline: the event's own
// start and end, its notes, and the timeline of every attached incident.
//
// Source is "event" or "incident". For "event", Type is started, ended or note;
// for "incident" it is the incident timeline event's type and IncidentID names
// the incident. Treat Type as an open set.
type EventTimelineEntry struct {
ID int64 `json:"id,omitempty"`
Source string `json:"source"`
Type string `json:"type"`
IncidentID *int64 `json:"incident_id,omitempty"`
IncidentTitle *string `json:"incident_title,omitempty"`
UserID *int64 `json:"user_id,omitempty"`
Username *string `json:"username,omitempty"`
ServiceAccountID *int64 `json:"service_account_id,omitempty"`
ServiceAccountName *string `json:"service_account_name,omitempty"`
Detail *string `json:"detail,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
// IncidentEventRef is an event as an incident lists it.
type IncidentEventRef struct {
ID int64 `json:"id"`
Kind string `json:"kind"`
Title string `json:"title"`
StartedAt time.Time `json:"started_at"`
IsTrigger bool `json:"is_trigger"`
}