// What a benchmark run records about the deployment it measures. Shared by the web app and the worker // (sweeps create runs there), so no "server-only" here. import { and, eq, isNull } from "drizzle-orm"; import { db } from "@/lib/db/client"; import { type BenchmarkSnapshot, deployments, models, runtimes, workerState } from "@/lib/db/schema"; /** Host CPU/RAM and the names of the GPUs a deployment uses (all detected ones when none are picked). */ async function hostFacts(uuids: string[] | null, nvidia: boolean) { const ws = await db().query.workerState.findFirst({ columns: { docker: true }, where: eq(workerState.id, "worker") }); const h = ws?.docker; const gpus = h?.gpus ?? []; return { gpuNames: nvidia ? (uuids ? gpus.filter((g) => uuids.includes(g.uuid)) : gpus).map((g) => g.name) : [], host: { cpuModel: h?.cpuModel ?? null, threads: h?.cpus ?? null, memoryBytes: h?.memoryBytes ?? null, os: h?.os ?? null }, }; } /** The deployment as it is now; null when it doesn't exist (or is being deleted). */ export async function deploymentSnapshot(deploymentId: string): Promise<{ id: string; modelId: string; state: string; snapshot: BenchmarkSnapshot } | null> { const [d] = await db() .select({ id: deployments.id, name: deployments.name, state: deployments.state, options: deployments.options, cpus: deployments.cpus, memoryMb: deployments.memoryMb, command: deployments.command, imageId: deployments.imageId, modelId: deployments.modelId, repoId: models.repoId, runtimeName: runtimes.name, engine: runtimes.engine, accelerator: runtimes.accelerator, image: runtimes.image, }) .from(deployments) .innerJoin(runtimes, eq(runtimes.id, deployments.runtimeId)) .innerJoin(models, eq(models.id, deployments.modelId)) .where(and(eq(deployments.id, deploymentId), isNull(deployments.deleteRequestedAt))); if (!d) return null; return { id: d.id, modelId: d.modelId, state: d.state, snapshot: { deploymentName: d.name, repoId: d.repoId, runtimeName: d.runtimeName, engine: d.engine, accelerator: d.accelerator, ...(await hostFacts(d.options.gpus ?? null, d.accelerator === "nvidia")), image: d.image, imageId: d.imageId, options: d.options, cpus: d.cpus, memoryMb: d.memoryMb, command: [d.image, ...(d.command ?? [])].join(" "), }, }; }