Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/app/api/v1/_lib/api-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ import {
type ServiceAccountContext,
} from "@/server/middleware/api-auth";

/** BigInt-safe NextResponse.json() — converts BigInts to numbers before serialization. */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function jsonResponse(data: any, init?: { status?: number }) {
const body = JSON.stringify(data, (_key, value) =>
typeof value === "bigint" ? Number(value) : value,
);
return new NextResponse(body, {
status: init?.status ?? 200,
headers: { "Content-Type": "application/json" },
});
}

const TRPC_TO_HTTP: Record<string, number> = {
NOT_FOUND: 404,
BAD_REQUEST: 400,
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/v1/nodes/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { apiRoute } from "../../_lib/api-handler";
import { apiRoute, jsonResponse } from "../../_lib/api-handler";

export const GET = apiRoute("nodes.read", async (_req, ctx, params) => {
const id = params?.id;
Expand Down Expand Up @@ -41,5 +41,5 @@ export const GET = apiRoute("nodes.read", async (_req, ctx, params) => {
return NextResponse.json({ error: "Node not found" }, { status: 404 });
}

return NextResponse.json({ node });
return jsonResponse({ node });
});
4 changes: 2 additions & 2 deletions src/app/api/v1/pipelines/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { apiRoute } from "../../_lib/api-handler";
import { apiRoute, jsonResponse } from "../../_lib/api-handler";

export const GET = apiRoute("pipelines.read", async (_req, ctx, params) => {
const id = params?.id;
Expand Down Expand Up @@ -58,5 +58,5 @@ export const GET = apiRoute("pipelines.read", async (_req, ctx, params) => {
);
}

return NextResponse.json({ pipeline });
return jsonResponse({ pipeline });
});
31 changes: 24 additions & 7 deletions src/server/routers/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export const dashboardRouter = router({
memoryUsedBytes: true,
memoryTotalBytes: true,
cpuSecondsTotal: true,
cpuSecondsIdle: true,
},
})
: [];
Expand Down Expand Up @@ -227,11 +228,22 @@ export const dashboardRouter = router({
unhealthyPipelines,
rates: { eventsIn: eventsInRate, eventsOut: eventsOutRate, bytesIn: bytesInRate, bytesOut: bytesOutRate, errors: errorsRate },
totals: { eventsIn: totalEventsIn, eventsOut: totalEventsOut, bytesIn: totalBytesIn, bytesOut: totalBytesOut, errors: totalErrors },
sparkline: (metricsByNode.get(node.id) ?? []).map((m) => ({
t: m.timestamp.getTime(),
mem: m.memoryTotalBytes ? Number(m.memoryUsedBytes) / Number(m.memoryTotalBytes) * 100 : 0,
cpu: Number(m.cpuSecondsTotal ?? 0),
})),
sparkline: (metricsByNode.get(node.id) ?? []).map((m, i, arr) => {
let cpu = 0;
if (i > 0) {
const prev = arr[i - 1];
const totalDelta = m.cpuSecondsTotal - prev.cpuSecondsTotal;
const idleDelta = m.cpuSecondsIdle - prev.cpuSecondsIdle;
if (totalDelta > 0) {
cpu = Math.max(0, Math.min(100, ((totalDelta - idleDelta) / totalDelta) * 100));
}
}
return {
t: m.timestamp.getTime(),
mem: m.memoryTotalBytes ? Number(m.memoryUsedBytes) / Number(m.memoryTotalBytes) * 100 : 0,
cpu,
};
}),
};
});
}),
Expand Down Expand Up @@ -683,6 +695,7 @@ export const dashboardRouter = router({
nodeId: true,
timestamp: true,
cpuSecondsTotal: true,
cpuSecondsIdle: true,
memoryUsedBytes: true,
memoryTotalBytes: true,
diskReadBytes: true,
Expand Down Expand Up @@ -803,6 +816,7 @@ export const dashboardRouter = router({
nodeId: string;
timestamp: Date;
cpuSecondsTotal: number;
cpuSecondsIdle: number;
memoryUsedBytes: bigint;
memoryTotalBytes: bigint;
diskReadBytes: bigint;
Expand All @@ -826,8 +840,11 @@ export const dashboardRouter = router({
const dtSec = (t - new Date(prev.timestamp).getTime()) / 1000;
if (dtSec <= 0) continue;

const cpuDelta = curr.cpuSecondsTotal - prev.cpuSecondsTotal;
const cpuPct = Math.max(0, Math.min(100, (cpuDelta / dtSec) * 100));
const cpuTotalDelta = curr.cpuSecondsTotal - prev.cpuSecondsTotal;
const cpuIdleDelta = curr.cpuSecondsIdle - prev.cpuSecondsIdle;
const cpuPct = cpuTotalDelta > 0
? Math.max(0, Math.min(100, ((cpuTotalDelta - cpuIdleDelta) / cpuTotalDelta) * 100))
: 0;
addPoint(cpu, label, t, cpuPct);

const memTotal = Number(curr.memoryTotalBytes);
Expand Down
15 changes: 5 additions & 10 deletions src/server/services/alert-evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,17 @@ async function getCpuUsage(nodeId: string): Promise<number | null> {
where: { nodeId },
orderBy: { timestamp: "desc" },
take: 2,
select: { cpuSecondsTotal: true, timestamp: true },
select: { cpuSecondsTotal: true, cpuSecondsIdle: true },
});

if (rows.length < 2) return null;

const [newer, older] = rows;
const dtSeconds =
(newer.timestamp.getTime() - older.timestamp.getTime()) / 1000;
if (dtSeconds <= 0) return null;
const totalDelta = newer.cpuSecondsTotal - older.cpuSecondsTotal;
if (totalDelta <= 0) return null; // counter reset or no change

// cpuSecondsTotal is cumulative; the delta / wall-clock-delta gives
// fraction of one core used. Multiply by 100 for a percentage.
const cpuDelta = newer.cpuSecondsTotal - older.cpuSecondsTotal;
if (cpuDelta < 0) return null; // counter reset

return (cpuDelta / dtSeconds) * 100;
const idleDelta = newer.cpuSecondsIdle - older.cpuSecondsIdle;
return Math.max(0, Math.min(100, ((totalDelta - idleDelta) / totalDelta) * 100));
}

/** Compute memory usage percentage from the latest NodeMetric row. */
Expand Down
Loading