changes
This commit is contained in:
@@ -439,7 +439,9 @@ export async function POST(req: Request) {
|
||||
|
||||
// If the payload carries a `reason`, create the corresponding ReasonEntry.
|
||||
// If it doesn't, still create an "UNCLASSIFIED" downtime ReasonEntry for stop events so the dashboard can show coverage.
|
||||
if (evReason || finalType === "microstop" || finalType === "macrostop" || finalType === "downtime-acknowledged") {
|
||||
if (evRecord.is_update || evRecord.is_auto_ack || dataObj.is_update || dataObj.is_auto_ack){
|
||||
// skip duplicate reasonEntry for refresh/ack
|
||||
} else if (evReason || finalType === "microstop" || finalType === "macrostop" || finalType === "downtime-acknowledged"){
|
||||
const reasonRaw: Record<string, unknown> =
|
||||
evReason ??
|
||||
({
|
||||
|
||||
29
app/api/recap/route.ts
Normal file
29
app/api/recap/route.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { getRecapDataCached, parseRecapQuery } from "@/lib/recap/getRecapData";
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await requireSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const url = new URL(req.url);
|
||||
const query = parseRecapQuery({
|
||||
machineId: url.searchParams.get("machineId"),
|
||||
start: url.searchParams.get("start"),
|
||||
end: url.searchParams.get("end"),
|
||||
shift: url.searchParams.get("shift"),
|
||||
});
|
||||
|
||||
const recap = await getRecapDataCached({
|
||||
orgId: session.orgId,
|
||||
machineId: query.machineId,
|
||||
start: query.start ?? undefined,
|
||||
end: query.end ?? undefined,
|
||||
shift: query.shift ?? undefined,
|
||||
});
|
||||
|
||||
return NextResponse.json(recap);
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { createHash } from "crypto";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { logLine } from "@/lib/logger";
|
||||
@@ -42,6 +43,10 @@ function pickRange(req: NextRequest) {
|
||||
return { start: new Date(now.getTime() - ms), end: now };
|
||||
}
|
||||
|
||||
function toMs(value?: Date | null) {
|
||||
return value ? value.getTime() : 0;
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const perfEnabled = PERF_LOGS_ENABLED;
|
||||
const totalStart = nowMs();
|
||||
@@ -67,6 +72,32 @@ export async function GET(req: NextRequest) {
|
||||
|
||||
if (perfEnabled) timings.preQuery = elapsedMs(preQueryStart);
|
||||
|
||||
const versionStart = nowMs();
|
||||
const cycleMax = await prisma.machineCycle.aggregate({
|
||||
where: baseWhere,
|
||||
_max: { tsServer: true },
|
||||
});
|
||||
if (perfEnabled) timings.version = elapsedMs(versionStart);
|
||||
|
||||
const versionParts = [
|
||||
session.orgId,
|
||||
range,
|
||||
machineId ?? "",
|
||||
toMs(cycleMax._max.tsServer),
|
||||
];
|
||||
const etag = `W/"${createHash("sha1").update(versionParts.join("|")).digest("hex")}"`;
|
||||
const responseHeaders = new Headers({
|
||||
"Cache-Control": "private, no-cache, max-age=0, must-revalidate",
|
||||
ETag: etag,
|
||||
"Last-Modified": new Date(toMs(cycleMax._max.tsServer) || 0).toUTCString(),
|
||||
Vary: "Cookie",
|
||||
});
|
||||
|
||||
const ifNoneMatch = req.headers.get("if-none-match");
|
||||
if (ifNoneMatch && ifNoneMatch === etag) {
|
||||
return new NextResponse(null, { status: 304, headers: responseHeaders });
|
||||
}
|
||||
|
||||
const workOrdersStart = nowMs();
|
||||
const workOrderRows = await prisma.machineCycle.findMany({
|
||||
where: { ...baseWhere, workOrderId: { not: null } },
|
||||
@@ -90,7 +121,6 @@ export async function GET(req: NextRequest) {
|
||||
|
||||
const payload = { ok: true, workOrders, skus };
|
||||
|
||||
const responseHeaders = new Headers();
|
||||
if (perfEnabled) {
|
||||
timings.postQuery = elapsedMs(postQueryStart);
|
||||
timings.total = elapsedMs(totalStart);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { createHash } from "crypto";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { logLine } from "@/lib/logger";
|
||||
@@ -46,6 +47,10 @@ function safeNum(v: unknown) {
|
||||
return typeof v === "number" && Number.isFinite(v) ? v : null;
|
||||
}
|
||||
|
||||
function toMs(value?: Date | null) {
|
||||
return value ? value.getTime() : 0;
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const perfEnabled = PERF_LOGS_ENABLED;
|
||||
const totalStart = nowMs();
|
||||
@@ -73,6 +78,52 @@ export async function GET(req: NextRequest) {
|
||||
|
||||
if (perfEnabled) timings.preQuery = elapsedMs(preQueryStart);
|
||||
|
||||
const versionStart = nowMs();
|
||||
const [kpiMax, cycleMax, eventMax] = await Promise.all([
|
||||
prisma.machineKpiSnapshot.aggregate({
|
||||
where: { ...baseWhere, ts: { gte: start, lte: end } },
|
||||
_max: { tsServer: true },
|
||||
}),
|
||||
prisma.machineCycle.aggregate({
|
||||
where: { ...baseWhere, ts: { gte: start, lte: end } },
|
||||
_max: { tsServer: true },
|
||||
}),
|
||||
prisma.machineEvent.aggregate({
|
||||
where: { ...baseWhere, ts: { gte: start, lte: end } },
|
||||
_max: { tsServer: true },
|
||||
}),
|
||||
]);
|
||||
if (perfEnabled) timings.version = elapsedMs(versionStart);
|
||||
|
||||
const lastModifiedMs = Math.max(
|
||||
toMs(kpiMax._max.tsServer),
|
||||
toMs(cycleMax._max.tsServer),
|
||||
toMs(eventMax._max.tsServer)
|
||||
);
|
||||
|
||||
const versionParts = [
|
||||
session.orgId,
|
||||
range,
|
||||
machineId ?? "",
|
||||
workOrderId ?? "",
|
||||
sku ?? "",
|
||||
toMs(kpiMax._max.tsServer),
|
||||
toMs(cycleMax._max.tsServer),
|
||||
toMs(eventMax._max.tsServer),
|
||||
];
|
||||
const etag = `W/"${createHash("sha1").update(versionParts.join("|")).digest("hex")}"`;
|
||||
const responseHeaders = new Headers({
|
||||
"Cache-Control": "private, no-cache, max-age=0, must-revalidate",
|
||||
ETag: etag,
|
||||
"Last-Modified": new Date(lastModifiedMs || 0).toUTCString(),
|
||||
Vary: "Cookie",
|
||||
});
|
||||
|
||||
const ifNoneMatch = req.headers.get("if-none-match");
|
||||
if (ifNoneMatch && ifNoneMatch === etag) {
|
||||
return new NextResponse(null, { status: 304, headers: responseHeaders });
|
||||
}
|
||||
|
||||
const kpiStart = nowMs();
|
||||
const kpiRows = await prisma.machineKpiSnapshot.findMany({
|
||||
where: { ...baseWhere, ts: { gte: start, lte: end } },
|
||||
@@ -405,7 +456,6 @@ export async function GET(req: NextRequest) {
|
||||
},
|
||||
};
|
||||
|
||||
const responseHeaders = new Headers();
|
||||
if (perfEnabled) {
|
||||
timings.postQuery = elapsedMs(postQueryStart);
|
||||
timings.total = elapsedMs(totalStart);
|
||||
|
||||
Reference in New Issue
Block a user