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.
64 lines
3.6 KiB
SQL
64 lines
3.6 KiB
SQL
-- Events: something that happened and explains incidents, such as a release, a
|
|
-- change, a maintenance window or an outage at a vendor. An event is a context
|
|
-- that incidents are attached to; the incidents themselves are untouched, keep
|
|
-- their own timeline and stay separate work items. See docs/events.md.
|
|
--
|
|
-- Team-scoped like incidents: an event only holds incidents of its own team
|
|
-- (enforced by the handlers). ended_at is NULL for a marker with no known end
|
|
-- or for one still going. Events are hard-deleted; deleting one removes the
|
|
-- links and notes, never the incidents.
|
|
|
|
CREATE TABLE events (
|
|
id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
|
team_id bigint NOT NULL REFERENCES teams(id) ON DELETE CASCADE,
|
|
kind text NOT NULL,
|
|
title text NOT NULL,
|
|
summary text NOT NULL DEFAULT '',
|
|
started_at bigint NOT NULL,
|
|
ended_at bigint,
|
|
-- manual: created by a person. api: created by a service account (CI).
|
|
source text NOT NULL DEFAULT 'manual',
|
|
-- A caller-chosen identifier (a pipeline id, say) so a retried POST finds
|
|
-- the event it already made instead of creating a second one.
|
|
external_id text,
|
|
external_url text,
|
|
created_by_user_id bigint REFERENCES users(id) ON DELETE SET NULL,
|
|
created_by_service_account_id bigint REFERENCES service_accounts(id) ON DELETE SET NULL,
|
|
created_at bigint NOT NULL DEFAULT floor(EXTRACT(epoch FROM now()))::bigint,
|
|
CONSTRAINT events_kind_check CHECK (kind IN ('release', 'change', 'maintenance', 'outage', 'other')),
|
|
CONSTRAINT events_source_check CHECK (source IN ('manual', 'api')),
|
|
CONSTRAINT events_window_chk CHECK (ended_at IS NULL OR ended_at >= started_at),
|
|
CONSTRAINT events_creator_xor_chk CHECK (created_by_user_id IS NULL OR created_by_service_account_id IS NULL)
|
|
);
|
|
CREATE INDEX events_team_started_idx ON events (team_id, started_at DESC);
|
|
CREATE UNIQUE INDEX events_external_idx ON events (team_id, external_id) WHERE external_id IS NOT NULL;
|
|
|
|
-- Which incidents belong to which event. Many to many: one incident can sit
|
|
-- under a release and under a node upgrade. At most one incident per event is
|
|
-- its trigger, the first domino.
|
|
CREATE TABLE event_incidents (
|
|
event_id bigint NOT NULL REFERENCES events(id) ON DELETE CASCADE,
|
|
incident_id bigint NOT NULL REFERENCES incidents(id) ON DELETE CASCADE,
|
|
is_trigger boolean NOT NULL DEFAULT false,
|
|
attached_at bigint NOT NULL DEFAULT floor(EXTRACT(epoch FROM now()))::bigint,
|
|
attached_by_user_id bigint REFERENCES users(id) ON DELETE SET NULL,
|
|
attached_by_service_account_id bigint REFERENCES service_accounts(id) ON DELETE SET NULL,
|
|
PRIMARY KEY (event_id, incident_id),
|
|
CONSTRAINT event_incidents_actor_xor_chk CHECK (attached_by_user_id IS NULL OR attached_by_service_account_id IS NULL)
|
|
);
|
|
CREATE INDEX event_incidents_incident_idx ON event_incidents (incident_id);
|
|
CREATE UNIQUE INDEX event_incidents_one_trigger_idx ON event_incidents (event_id) WHERE is_trigger;
|
|
|
|
-- Notes on the event itself. Each incident keeps its own notes on its own
|
|
-- timeline; these are for what spans them, such as the postmortem thread.
|
|
CREATE TABLE event_notes (
|
|
id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
|
event_id bigint NOT NULL REFERENCES events(id) ON DELETE CASCADE,
|
|
content text NOT NULL,
|
|
user_id bigint REFERENCES users(id) ON DELETE SET NULL,
|
|
service_account_id bigint REFERENCES service_accounts(id) ON DELETE SET NULL,
|
|
created_at bigint NOT NULL DEFAULT floor(EXTRACT(epoch FROM now()))::bigint,
|
|
CONSTRAINT event_notes_actor_xor_chk CHECK (user_id IS NULL OR service_account_id IS NULL)
|
|
);
|
|
CREATE INDEX event_notes_event_idx ON event_notes (event_id, created_at);
|