pulse stop

This commit is contained in:
Marcelo
2026-05-01 06:19:30 +00:00
parent 864be8d932
commit 4299ef3478
5 changed files with 124 additions and 8 deletions

View File

@@ -112,6 +112,8 @@
"recap.title": "Recap",
"recap.subtitle": "Last 24h",
"recap.card.stoppedFor": "Stopped for {min} min",
"machines.status.stopped": "STOPPED",
"machines.stoppedFor": "Stopped for {min} min",
"recap.grid.title": "Machine recap",
"recap.grid.subtitle": "Last 24h · click to open details",
"recap.grid.updatedAgo": "Updated {sec}s ago",

View File

@@ -119,6 +119,8 @@
"recap.title": "Resumen",
"recap.subtitle": "Últimas 24h",
"recap.card.stoppedFor": "Detenida hace {min} min",
"machines.status.stopped": "DETENIDA",
"machines.stoppedFor": "Detenida hace {min} min",
"recap.grid.title": "Resumen de máquinas",
"recap.grid.subtitle": "Últimas 24h · click para ver detalle",
"recap.grid.updatedAgo": "Actualizado hace {sec}s",

View File

@@ -31,6 +31,15 @@ type LatestKpiRow = {
cycleTime?: number | null;
};
export type LatestMacrostopRow = {
machineId: string;
ts: Date;
status: "active" | "resolved" | "unknown";
startedAtMs: number;
};
const MACROSTOP_LOOKBACK_MS = 5 * 60 * 1000;
export async function fetchMachineBase(orgId: string): Promise<MachineBaseRow[]> {
return prisma.machine.findMany({
where: { orgId },
@@ -93,20 +102,75 @@ export async function fetchLatestKpis(
});
}
export async function fetchLatestMacrostops(
orgId: string,
machineIds: string[]
): Promise<LatestMacrostopRow[]> {
if (!machineIds.length) return [];
const rows = await prisma.machineEvent.findMany({
where: {
orgId,
machineId: { in: machineIds },
eventType: "macrostop",
ts: { gte: new Date(Date.now() - MACROSTOP_LOOKBACK_MS) },
},
orderBy: [{ machineId: "asc" }, { ts: "desc" }],
select: { machineId: true, ts: true, data: true },
});
const byMachine = new Map<string, LatestMacrostopRow>();
for (const row of rows) {
if (byMachine.has(row.machineId)) continue;
let parsed: unknown = row.data;
if (typeof parsed === "string") {
try { parsed = JSON.parse(parsed); } catch { parsed = null; }
}
const data: Record<string, unknown> =
parsed && typeof parsed === "object" && !Array.isArray(parsed)
? (parsed as Record<string, unknown>)
: {};
const isAutoAck =
data.is_auto_ack === true || data.isAutoAck === true ||
data.is_auto_ack === "true" || data.isAutoAck === "true";
if (isAutoAck) continue;
const rawStatus = String(data.status ?? "").trim().toLowerCase();
const status: LatestMacrostopRow["status"] =
rawStatus === "active" ? "active" : rawStatus === "resolved" ? "resolved" : "unknown";
const lastCycleTs = Number(data.last_cycle_timestamp);
const startedAtMs = Number.isFinite(lastCycleTs) && lastCycleTs > 0
? lastCycleTs
: row.ts.getTime();
byMachine.set(row.machineId, { machineId: row.machineId, ts: row.ts, status, startedAtMs });
}
return Array.from(byMachine.values());
}
export function mergeMachineOverviewRows(params: {
machines: MachineBaseRow[];
heartbeats: LatestHeartbeatRow[];
kpis?: LatestKpiRow[];
macrostops?: LatestMacrostopRow[];
includeKpi?: boolean;
}): OverviewMachineRow[] {
const { machines, heartbeats, kpis = [], includeKpi = false } = params;
const { machines, heartbeats, kpis = [], macrostops = [], includeKpi = false } = params;
const heartbeatMap = new Map(heartbeats.map((row) => [row.machineId, row]));
const kpiMap = new Map(kpis.map((row) => [row.machineId, row]));
const macrostopMap = new Map(macrostops.map((row) => [row.machineId, row]));
return machines.map((machine) => ({
...machine,
latestHeartbeat: (heartbeatMap.get(machine.id) ?? null) as OverviewMachineRow["latestHeartbeat"],
latestKpi: includeKpi ? (kpiMap.get(machine.id) ?? null) : null,
latestMacrostop: macrostopMap.get(machine.id) ?? null,
heartbeats: undefined,
kpiSnapshots: undefined,
}));