feat(admin): operational stats — hourly trends, recurrence, dashboard

Extend GET /v1/stats/trend with granularity=hour (UTC buckets) and granularity=day
response envelope. Enrich GET /v1/stats/summary per-project totals and critical
counts. Add GET /v1/stats/recurrence for top incidents by occurrence volume in
a sliding window. Add index on er_incident_occurrences(occurred_at) for range
queries. Wire admin dashboard to summary, 24h/14d trends, and recurrence table.
Document contract in docs/ADMIN_STATS_API.md.

Paperclip-Agent: Pedro Alves (urlKey pedro-alves; id d706f4e6-5d05-4cec-b6e8-f8fb1f203aae)
Paperclip-Issue: BIT-728
Paperclip-Run: 948904bb-20d7-4d29-9cd7-4c22544f7570
Made-with: Cursor
This commit is contained in:
David Santos committed 2026-04-02 11:00:51 +00:00
1 parent c4e0b03ed8
commit de68f68103
6 files changed
+643 -23

No files matched your search

+71
View File
@@ -0,0 +1,71 @@
# Admin API — operational statistics (`/v1/stats/*`)
**Audience:** `errors.bitreport.pt` admin UI, internal dashboards, automation.
**Auth:** Same as other admin routes — JWT cookie (`__Host-access`) and permission `errors:read`.
**Base URL:** `ERROR_REPORTING_ADMIN_API_BASE` (Fastify service, typically port `3200` behind `/admin/` proxy).
## `GET /v1/stats/summary`
Aggregates over **all incidents** (current state), optional filter by project.
| Query | Description |
|--------|-------------|
| `project` | Optional. Project `slug`; omit for all projects. |
### Response
- `byProject[]`: per slug — `total` (incident count), `open`, `investigating`, `resolved`, `ignored`, `critical` (incidents with `severity = critical`, any state).
- `bySeverity`: global counts keyed by severity.
- `resolvedLast24h`: distinct incidents moved to `resolved` in the last 24 hours (state history).
- `totalLast24h`: occurrence rows in the last 24 hours.
## `GET /v1/stats/trend`
Occurrence counts bucketed by time and severity.
| Query | Description |
|--------|-------------|
| `granularity` | `day` (default) or `hour`. |
| `days` | With `granularity=day`: window length, default `30`, max `366`. |
| `hours` | With `granularity=hour`: number of UTC hour buckets ending at the current hour, default `24`, max `168`. |
| `project` | Optional project `slug`. |
### Response — daily (`granularity=day`)
- `granularity`: `"day"`
- `dates`: UTC calendar dates `YYYY-MM-DD` (inclusive window).
- `series`: for each severity, an array of counts aligned with `dates`.
### Response — hourly (`granularity=hour`)
- `granularity`: `"hour"`
- `hours`: UTC hour bucket labels `YYYY-MM-DD HH:00:00` (start of bucket).
- `series`: same shape as daily, aligned with `hours`.
Hourly buckets use `CONVERT_TZ(..., @@session.time_zone, '+00:00')` so labels match UTC wall-clock when the DB session timezone is configured consistently.
## `GET /v1/stats/recurrence`
Ranks incidents by **how many occurrence rows** they received in a sliding window (recurrence / noise hotspots).
| Query | Description |
|--------|-------------|
| `project` | Optional project `slug`. |
| `days` | Window length in days, default `7`, max `90`. |
| `limit` | Max rows, default `15`, max `100`. |
### Response
- `windowDays`, `rangeStart`, `rangeEndExclusive`: bounds used for the query (MySQL `DATETIME(6)` strings).
- `top[]`: `id`, `projectSlug`, `fingerprint`, `title`, `severity`, `currentState`, `hitsInWindow`, `lifetimeOccurrences`.
## Indexes
Heavy queries use:
- `er_incident_occurrences (project_id, occurred_at DESC)` — filtered trends and recurrence by project.
- `er_incident_occurrences (occurred_at DESC)` — global time-range scans (see migration `20260402103000_add_occurred_at_for_stats`).
## Related
- Public ingest / `recent-errors`: `docs/` in this repo and Paperclip `ERROR_REPORTING_HUB_CONTRACT.md`.