pulse stop
This commit is contained in:
@@ -19,6 +19,12 @@ type MachineRow = {
|
|||||||
ip?: string | null;
|
ip?: string | null;
|
||||||
fwVersion?: string | null;
|
fwVersion?: string | null;
|
||||||
};
|
};
|
||||||
|
latestMacrostop?: null | {
|
||||||
|
machineId: string;
|
||||||
|
ts: string;
|
||||||
|
status: "active" | "resolved" | "unknown";
|
||||||
|
startedAtMs: number;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
const LIVE_REFRESH_MS = 5000;
|
const LIVE_REFRESH_MS = 5000;
|
||||||
const OFFLINE_MS = RECAP_HEARTBEAT_STALE_MS;
|
const OFFLINE_MS = RECAP_HEARTBEAT_STALE_MS;
|
||||||
@@ -51,6 +57,21 @@ function badgeClass(status?: string, offline?: boolean) {
|
|||||||
return "bg-white/10 text-white";
|
return "bg-white/10 text-white";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const MACROSTOP_FRESH_MS = 2 * 60 * 1000;
|
||||||
|
|
||||||
|
function isMacrostopActive(macrostop: MachineRow["latestMacrostop"]) {
|
||||||
|
if (!macrostop) return false;
|
||||||
|
if (macrostop.status !== "active") return false;
|
||||||
|
// Fresh if last refresh was within 2 min — Node-RED refreshes every 10s,
|
||||||
|
// so anything older means the stoppage already ended without resolution event.
|
||||||
|
return Date.now() - new Date(macrostop.ts).getTime() <= MACROSTOP_FRESH_MS;
|
||||||
|
}
|
||||||
|
|
||||||
|
function ongoingMacrostopMin(macrostop: MachineRow["latestMacrostop"]) {
|
||||||
|
if (!macrostop) return 0;
|
||||||
|
return Math.max(0, Math.floor((Date.now() - macrostop.startedAtMs) / 60000));
|
||||||
|
}
|
||||||
|
|
||||||
export default function MachinesClient({ initialMachines = [] }: { initialMachines?: MachineRow[] }) {
|
export default function MachinesClient({ initialMachines = [] }: { initialMachines?: MachineRow[] }) {
|
||||||
const { t, locale } = useI18n();
|
const { t, locale } = useI18n();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
@@ -292,9 +313,28 @@ export default function MachinesClient({ initialMachines = [] }: { initialMachin
|
|||||||
const hbTs = hb?.tsServer ?? hb?.ts;
|
const hbTs = hb?.tsServer ?? hb?.ts;
|
||||||
const offline = isOffline(hbTs);
|
const offline = isOffline(hbTs);
|
||||||
const normalizedStatus = normalizeStatus(hb?.status);
|
const normalizedStatus = normalizeStatus(hb?.status);
|
||||||
const statusLabel = offline ? t("machines.status.offline") : (normalizedStatus || t("machines.status.unknown"));
|
|
||||||
const lastSeen = secondsAgo(hbTs, locale, t("common.never"));
|
const lastSeen = secondsAgo(hbTs, locale, t("common.never"));
|
||||||
|
|
||||||
|
const macrostopActive = isMacrostopActive(m.latestMacrostop);
|
||||||
|
const stoppedMin = macrostopActive ? ongoingMacrostopMin(m.latestMacrostop) : 0;
|
||||||
|
|
||||||
|
// Production-state badge: STOPPED if active macrostop, else heartbeat-based.
|
||||||
|
const productionBadgeLabel = offline
|
||||||
|
? t("machines.status.offline")
|
||||||
|
: macrostopActive
|
||||||
|
? t("machines.status.stopped")
|
||||||
|
: (normalizedStatus || t("machines.status.unknown"));
|
||||||
|
|
||||||
|
const productionBadgeClass = offline
|
||||||
|
? "bg-white/10 text-zinc-300"
|
||||||
|
: macrostopActive
|
||||||
|
? "bg-red-500/20 text-red-200 ring-2 ring-red-500/50 animate-pulse"
|
||||||
|
: badgeClass(normalizedStatus, offline);
|
||||||
|
|
||||||
|
const cardClass = macrostopActive
|
||||||
|
? "cursor-pointer rounded-2xl border border-red-500/60 bg-red-500/10 p-5 ring-2 ring-red-500/40 animate-pulse hover:bg-red-500/15"
|
||||||
|
: "cursor-pointer rounded-2xl border border-white/10 bg-white/5 p-5 hover:bg-white/10";
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
key={m.id}
|
key={m.id}
|
||||||
@@ -302,7 +342,7 @@ export default function MachinesClient({ initialMachines = [] }: { initialMachin
|
|||||||
tabIndex={0}
|
tabIndex={0}
|
||||||
onClick={() => router.push(`/machines/${m.id}`)}
|
onClick={() => router.push(`/machines/${m.id}`)}
|
||||||
onKeyDown={(event) => handleCardKeyDown(event, m.id)}
|
onKeyDown={(event) => handleCardKeyDown(event, m.id)}
|
||||||
className="cursor-pointer rounded-2xl border border-white/10 bg-white/5 p-5 hover:bg-white/10"
|
className={cardClass}
|
||||||
>
|
>
|
||||||
<div className="flex items-center justify-between gap-3">
|
<div className="flex items-center justify-between gap-3">
|
||||||
<div className="min-w-0">
|
<div className="min-w-0">
|
||||||
@@ -310,15 +350,17 @@ export default function MachinesClient({ initialMachines = [] }: { initialMachin
|
|||||||
<div className="mt-1 text-xs text-zinc-400">
|
<div className="mt-1 text-xs text-zinc-400">
|
||||||
{m.code ? m.code : t("common.na")} - {t("machines.lastSeen", { time: lastSeen })}
|
{m.code ? m.code : t("common.na")} - {t("machines.lastSeen", { time: lastSeen })}
|
||||||
</div>
|
</div>
|
||||||
|
{macrostopActive ? (
|
||||||
|
<div className="mt-1 text-xs font-semibold text-red-200">
|
||||||
|
{t("machines.stoppedFor", { min: stoppedMin })}
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<span
|
<span
|
||||||
className={`shrink-0 rounded-full px-3 py-1 text-xs ${badgeClass(
|
className={`shrink-0 rounded-full px-3 py-1 text-xs ${productionBadgeClass}`}
|
||||||
normalizedStatus,
|
|
||||||
offline
|
|
||||||
)}`}
|
|
||||||
>
|
>
|
||||||
{statusLabel}
|
{productionBadgeLabel}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import { requireSession } from "@/lib/auth/requireSession";
|
|||||||
import {
|
import {
|
||||||
fetchLatestHeartbeats,
|
fetchLatestHeartbeats,
|
||||||
fetchLatestKpis,
|
fetchLatestKpis,
|
||||||
|
fetchLatestMacrostops,
|
||||||
fetchMachineBase,
|
fetchMachineBase,
|
||||||
mergeMachineOverviewRows,
|
mergeMachineOverviewRows,
|
||||||
} from "@/lib/machines/withLatest";
|
} from "@/lib/machines/withLatest";
|
||||||
@@ -58,6 +59,10 @@ export async function GET(req: Request) {
|
|||||||
if (perfEnabled) timings.kpiQuery = elapsedMs(kpiStart);
|
if (perfEnabled) timings.kpiQuery = elapsedMs(kpiStart);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const macrostopStart = nowMs();
|
||||||
|
const macrostops = await fetchLatestMacrostops(session.orgId, machineIds);
|
||||||
|
if (perfEnabled) timings.macrostopsQuery = elapsedMs(macrostopStart);
|
||||||
|
|
||||||
const postQueryStart = nowMs();
|
const postQueryStart = nowMs();
|
||||||
|
|
||||||
// flatten latest heartbeat for UI convenience
|
// flatten latest heartbeat for UI convenience
|
||||||
@@ -65,6 +70,7 @@ export async function GET(req: Request) {
|
|||||||
machines,
|
machines,
|
||||||
heartbeats,
|
heartbeats,
|
||||||
kpis,
|
kpis,
|
||||||
|
macrostops,
|
||||||
includeKpi,
|
includeKpi,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -112,6 +112,8 @@
|
|||||||
"recap.title": "Recap",
|
"recap.title": "Recap",
|
||||||
"recap.subtitle": "Last 24h",
|
"recap.subtitle": "Last 24h",
|
||||||
"recap.card.stoppedFor": "Stopped for {min} min",
|
"recap.card.stoppedFor": "Stopped for {min} min",
|
||||||
|
"machines.status.stopped": "STOPPED",
|
||||||
|
"machines.stoppedFor": "Stopped for {min} min",
|
||||||
"recap.grid.title": "Machine recap",
|
"recap.grid.title": "Machine recap",
|
||||||
"recap.grid.subtitle": "Last 24h · click to open details",
|
"recap.grid.subtitle": "Last 24h · click to open details",
|
||||||
"recap.grid.updatedAgo": "Updated {sec}s ago",
|
"recap.grid.updatedAgo": "Updated {sec}s ago",
|
||||||
|
|||||||
@@ -119,6 +119,8 @@
|
|||||||
"recap.title": "Resumen",
|
"recap.title": "Resumen",
|
||||||
"recap.subtitle": "Últimas 24h",
|
"recap.subtitle": "Últimas 24h",
|
||||||
"recap.card.stoppedFor": "Detenida hace {min} min",
|
"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.title": "Resumen de máquinas",
|
||||||
"recap.grid.subtitle": "Últimas 24h · click para ver detalle",
|
"recap.grid.subtitle": "Últimas 24h · click para ver detalle",
|
||||||
"recap.grid.updatedAgo": "Actualizado hace {sec}s",
|
"recap.grid.updatedAgo": "Actualizado hace {sec}s",
|
||||||
|
|||||||
@@ -31,6 +31,15 @@ type LatestKpiRow = {
|
|||||||
cycleTime?: number | null;
|
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[]> {
|
export async function fetchMachineBase(orgId: string): Promise<MachineBaseRow[]> {
|
||||||
return prisma.machine.findMany({
|
return prisma.machine.findMany({
|
||||||
where: { orgId },
|
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: {
|
export function mergeMachineOverviewRows(params: {
|
||||||
machines: MachineBaseRow[];
|
machines: MachineBaseRow[];
|
||||||
heartbeats: LatestHeartbeatRow[];
|
heartbeats: LatestHeartbeatRow[];
|
||||||
kpis?: LatestKpiRow[];
|
kpis?: LatestKpiRow[];
|
||||||
|
macrostops?: LatestMacrostopRow[];
|
||||||
includeKpi?: boolean;
|
includeKpi?: boolean;
|
||||||
}): OverviewMachineRow[] {
|
}): 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 heartbeatMap = new Map(heartbeats.map((row) => [row.machineId, row]));
|
||||||
const kpiMap = new Map(kpis.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) => ({
|
return machines.map((machine) => ({
|
||||||
...machine,
|
...machine,
|
||||||
latestHeartbeat: (heartbeatMap.get(machine.id) ?? null) as OverviewMachineRow["latestHeartbeat"],
|
latestHeartbeat: (heartbeatMap.get(machine.id) ?? null) as OverviewMachineRow["latestHeartbeat"],
|
||||||
latestKpi: includeKpi ? (kpiMap.get(machine.id) ?? null) : null,
|
latestKpi: includeKpi ? (kpiMap.get(machine.id) ?? null) : null,
|
||||||
|
latestMacrostop: macrostopMap.get(machine.id) ?? null,
|
||||||
heartbeats: undefined,
|
heartbeats: undefined,
|
||||||
kpiSnapshots: undefined,
|
kpiSnapshots: undefined,
|
||||||
}));
|
}));
|
||||||
|
|||||||
Reference in New Issue
Block a user