// 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 { buildRunMeta, gpusUsed } from "@/lib/benchmark-meta"; import { type BenchmarkSnapshot, deployments, models, type RunMeta, runtimes, settings, type SystemSummary, workerState } from "@/lib/db/schema"; import { applyOverrides, HARDWARE_OVERRIDES_KEY, parseOverrides, systemSummary } from "@/lib/hardware"; /** 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(" "), }, }; } /** Kaname's version (the image sets APP_VERSION). */ export const kanameVersion = () => process.env.APP_VERSION ?? "dev"; /** The detected hardware with Settings → Hardware applied, and the host's OS. */ export async function effectiveHardware() { const [ws, ov] = await Promise.all([ db().query.workerState.findFirst({ columns: { docker: true }, where: eq(workerState.id, "worker") }), db().query.settings.findFirst({ where: eq(settings.key, HARDWARE_OVERRIDES_KEY) }), ]); const docker = ws?.docker ?? null; return { hardware: applyOverrides(docker?.hardware, parseOverrides(ov?.value)), nvidia: docker?.gpus ?? [], os: docker?.os ?? null }; } /** Hardware, runtime and model facts of a deployment as it runs now, plus the system summary. */ export async function runSetup(deploymentIds: (string | null)[]): Promise<{ metas: (RunMeta | null)[]; system: SystemSummary }> { const { hardware, nvidia, os } = await effectiveHardware(); const metas = await Promise.all( deploymentIds.map(async (id) => { if (!id) return null; const [d] = await db() .select({ options: deployments.options, engineInfo: deployments.engineInfo, repoId: models.repoId, runtime: runtimes }) .from(deployments) .innerJoin(runtimes, eq(runtimes.id, deployments.runtimeId)) .innerJoin(models, eq(models.id, deployments.modelId)) .where(eq(deployments.id, id)); if (!d) return null; return buildRunMeta({ hardware, gpus: gpusUsed(hardware, d.runtime.accelerator, d.options, nvidia), runtime: { image: d.runtime.image, digest: d.runtime.imageDigest, engine: d.runtime.engine, engineName: d.runtime.engineName, extraArgs: d.runtime.extraArgs }, repoId: d.repoId, options: d.options, engineInfo: d.engineInfo, }); }), ); return { metas, system: systemSummary(hardware, os, kanameVersion()) }; }