fix(admin-ui): derive dashboard KPI row from stats summary aggregates

Top KPI cards used the paginated incidents list, so totals stuck near the API list cap. When operational stats load, sum byProject rollups from GET /v1/stats/summary instead.

Paperclip-Issue: BIT-2202
Paperclip-Run: 4c8f9b0e-4283-4fd7-9c20-f032f14f3719
This commit is contained in:
David Santos committed 2026-04-10 23:00:26 +00:00
1 parent 1533b359df
commit 5fede9ddd9
2 files changed
+32 -3

No files matched your search

+10 -3
View File
@@ -3,7 +3,7 @@ import { AppShell } from "@/components/AppShell";
import { DataAlert } from "@/components/DataAlert";
import { KpiCard } from "@/components/KpiCard";
import { computeKpis, fetchIncidentList } from "@/lib/incidents-data";
import { fetchOperationalStats } from "@/lib/stats-data";
import { aggregateIncidentKpisFromSummary, fetchOperationalStats } from "@/lib/stats-data";
/** Respect runtime env (e.g. ERROR_REPORTING_UI_DEMO) — avoid stale prerender behind CDN. */
export const dynamic = "force-dynamic";
@@ -35,7 +35,10 @@ export default async function DashboardPage() {
);
}
const _kpis = computeKpis(_list.incidents);
const _kpiFromStats = _stats.ok;
const _kpis = _kpiFromStats
? aggregateIncidentKpisFromSummary(_stats.summary)
: computeKpis(_list.incidents);
const _sourceHint =
_list.source === "demo" ? (
<p className="text-muted small">
@@ -48,7 +51,11 @@ export default async function DashboardPage() {
<AppShell title="Painel" breadcrumb={[{ label: "Painel" }]}>
{_sourceHint}
<div className="row g-3 mb-4">
<KpiCard label="Total (vista actual)" value={_kpis.total} variant="dark" />
<KpiCard
label={_kpiFromStats ? "Total (inventário)" : "Total (lista, limitada)"}
value={_kpis.total}
variant="dark"
/>
<KpiCard label="Abertos" value={_kpis.open} variant="danger" />
<KpiCard label="Em investigação" value={_kpis.investigating} variant="warning" />
<KpiCard label="Resolvidos" value={_kpis.resolved} variant="success" />
+22
View File
@@ -1,4 +1,5 @@
import { cookies } from "next/headers";
import type { IncidentState } from "./types";
function _envStr(key: string): string | undefined {
const v = Reflect.get(process.env, key);
@@ -103,6 +104,27 @@ export type OperationalStatsResult =
}
| { ok: false; message: string };
/** Global incident counts from SQL aggregates — not limited by the incidents list page size. */
export function aggregateIncidentKpisFromSummary(
summary: StatsSummaryPayload,
): Record<IncidentState | "total", number> {
const _acc: Record<IncidentState | "total", number> = {
total: 0,
open: 0,
investigating: 0,
resolved: 0,
ignored: 0,
};
for (const _r of summary.byProject) {
_acc.total += _r.total;
_acc.open += _r.open;
_acc.investigating += _r.investigating;
_acc.resolved += _r.resolved;
_acc.ignored += _r.ignored;
}
return _acc;
}
export async function fetchOperationalStats(): Promise<OperationalStatsResult> {
if (!_apiBase()) {
return { ok: false, message: "API não configurada." };