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

+186 -4
View File
@@ -1,14 +1,24 @@
import Link from "next/link";
import { AppShell } from "@/components/AppShell";
/** Respect runtime env (e.g. ERROR_REPORTING_UI_DEMO) — avoid stale prerender behind CDN. */
export const dynamic = "force-dynamic";
import { DataAlert } from "@/components/DataAlert";
import { KpiCard } from "@/components/KpiCard";
import { computeKpis, fetchIncidentList } from "@/lib/incidents-data";
import { fetchOperationalStats } from "@/lib/stats-data";
/** Respect runtime env (e.g. ERROR_REPORTING_UI_DEMO) — avoid stale prerender behind CDN. */
export const dynamic = "force-dynamic";
function _sumSeriesAt(idx: number, series: Record<string, number[]>): number {
let _t = 0;
for (const _k of Object.keys(series)) {
const _arr = series[_k];
_t += _arr[idx] ?? 0;
}
return _t;
}
export default async function DashboardPage() {
const _list = await fetchIncidentList({});
const [_list, _stats] = await Promise.all([fetchIncidentList({}), fetchOperationalStats()]);
if (!_list.ok) {
return (
@@ -48,6 +58,178 @@ export default async function DashboardPage() {
<KpiCard label="Ignorados" value={_kpis.ignored} variant="secondary" />
</div>
</div>
{_stats.ok ? (
<>
<h2 className="h5 mt-4 mb-3">Métricas operacionais (API)</h2>
<div className="row g-3 mb-4">
<div className="col-md-4">
<KpiCard
label="Ocorrências (24h)"
value={_stats.summary.totalLast24h}
variant="dark"
/>
</div>
<div className="col-md-4">
<KpiCard
label="Incidentes resolvidos (24h)"
value={_stats.summary.resolvedLast24h}
variant="success"
/>
</div>
<div className="col-md-4">
<KpiCard
label="Críticos (todos os estados)"
value={_stats.summary.bySeverity.critical ?? 0}
variant="danger"
/>
</div>
</div>
<div className="card mb-4">
<div className="card-header fw-semibold">Por projeto (inventário de incidentes)</div>
<div className="card-body p-0">
<div className="table-responsive">
<table className="table table-sm table-striped mb-0 align-middle">
<thead className="table-light">
<tr>
<th>Projeto</th>
<th className="text-end">Total</th>
<th className="text-end">Abertos</th>
<th className="text-end">Resolvidos</th>
<th className="text-end">Críticos</th>
</tr>
</thead>
<tbody>
{_stats.summary.byProject.map((r) => (
<tr key={r.slug}>
<td>
<code className="small">{r.slug}</code>
</td>
<td className="text-end font-monospace">{r.total}</td>
<td className="text-end font-monospace">{r.open}</td>
<td className="text-end font-monospace">{r.resolved}</td>
<td className="text-end font-monospace">{r.critical}</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
<div className="row g-3 mb-4">
<div className="col-lg-6">
<div className="card h-100">
<div className="card-header fw-semibold">
Volume horário (UTC, 24h) — ocorrências
</div>
<div className="card-body p-0" style={{ maxHeight: 280, overflowY: "auto" }}>
<table className="table table-sm mb-0 align-middle">
<thead className="table-light sticky-top">
<tr>
<th>Hora (início UTC)</th>
<th className="text-end">Total</th>
<th className="text-end">Crítico</th>
</tr>
</thead>
<tbody>
{_stats.trendHour.hours.map((h, i) => (
<tr key={h}>
<td className="text-nowrap small">{h}</td>
<td className="text-end font-monospace">{_sumSeriesAt(i, _stats.trendHour.series)}</td>
<td className="text-end font-monospace">
{_stats.trendHour.series.critical[i] ?? 0}
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
<div className="col-lg-6">
<div className="card h-100">
<div className="card-header fw-semibold">Tendência diária (14 dias)</div>
<div className="card-body p-0" style={{ maxHeight: 280, overflowY: "auto" }}>
<table className="table table-sm mb-0 align-middle">
<thead className="table-light sticky-top">
<tr>
<th>Dia</th>
<th className="text-end">Total ocorr.</th>
<th className="text-end">Crítico</th>
</tr>
</thead>
<tbody>
{_stats.trendDay.dates.map((d, i) => (
<tr key={d}>
<td className="small">{d}</td>
<td className="text-end font-monospace">{_sumSeriesAt(i, _stats.trendDay.series)}</td>
<td className="text-end font-monospace">
{_stats.trendDay.series.critical[i] ?? 0}
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
</div>
<div className="card mb-4">
<div className="card-header fw-semibold">
Recorrência (top por volume de ocorrências — últimos {_stats.recurrence.windowDays} dias)
</div>
<div className="card-body p-0">
<div className="table-responsive">
<table className="table table-sm table-striped mb-0 align-middle">
<thead className="table-light">
<tr>
<th className="text-end">Hits</th>
<th>Projeto</th>
<th>Severidade</th>
<th>Título</th>
<th />
</tr>
</thead>
<tbody>
{_stats.recurrence.top.length === 0 ? (
<tr>
<td colSpan={5} className="text-muted p-3">
Sem ocorrências no período.
</td>
</tr>
) : (
_stats.recurrence.top.map((r) => (
<tr key={r.id}>
<td className="text-end font-monospace">{r.hitsInWindow}</td>
<td>
<code className="small">{r.projectSlug}</code>
</td>
<td className="small">{r.severity}</td>
<td className="text-break small">{r.title}</td>
<td className="text-end">
<Link
href={`/incidents/${encodeURIComponent(r.id)}`}
className="btn btn-sm btn-outline-primary"
>
Detalhe
</Link>
</td>
</tr>
))
)}
</tbody>
</table>
</div>
</div>
</div>
</>
) : _list.source === "api" ? (
<DataAlert variant="warning" title="Estatísticas indisponíveis" body={_stats.message} />
) : null}
<div className="card">
<div className="card-header d-flex align-items-center justify-content-between">
<span className="fw-semibold">Atalhos</span>