import { execFileSync } from "node:child_process" import { readdirSync } from "node:fs" import path from "node:path" import { LOCALES } from "../packages/stats/app/src/lib/language" import { domain, zoneID } from "./stage" //////////////// // DATABASE //////////////// const cluster = planetscale.getDatabaseOutput({ name: "opencode-stats", organization: "anomalyco", }) const branch = $app.stage === "production" ? planetscale.getBranchOutput({ name: "production", organization: cluster.organization, database: cluster.name, }) : new planetscale.Branch("StatsDatabaseBranch", { database: cluster.name, organization: cluster.organization, name: $app.stage, parentBranch: "production", }) const password = new planetscale.Password("StatsDatabasePassword", { name: $app.stage, database: cluster.name, organization: cluster.organization, branch: branch.name, }) const databaseUrl = $interpolate`mysql://${password.username.apply(encodeURIComponent)}:${password.plaintext.apply( encodeURIComponent, )}@${password.accessHostUrl}/${cluster.name}` export const database = new sst.Linkable("StatsDatabase", { properties: { host: password.accessHostUrl, database: cluster.name, username: password.username, password: password.plaintext, port: 3306, url: databaseUrl, }, }) new sst.x.DevCommand("StatsStudio", { link: [database], environment: { DATABASE_URL: databaseUrl, }, dev: { command: "bun db:studio", directory: "packages/stats/core", autostart: false, }, }) //////////////// // APP //////////////// export const publicUrl = `https://${domain}/data` const accountId = "15d29c8639fd3733b1b5486a2acfd968" // The private Worker that renders the pages' Open Graph images (packages/stats/app/src/og/worker.ts). Its Wasm and font // modules can't ship in the Stats Worker, which SST uploads as one file, so it is its own Worker with no URL or route, // reached only through the Stats Worker's OG_WORKER Service Binding. It is built here, before any resource reads its // files. `sst dev` deploys no Worker. const ogWorker = $dev ? undefined : createOgWorker() const app = new sst.cloudflare.x.SolidStart("Stats", { path: "packages/stats/app", buildCommand: "bun run build", link: [database], environment: { PUBLIC_URL: publicUrl, }, transform: { server: (args) => { args.url = false if (!ogWorker) return args.transform = { worker: (worker) => { // The deployment's script name, so the binding waits for the OG Worker to have a live version. worker.bindings = $util .all([$output(worker.bindings ?? []), ogWorker.scriptName]) .apply(([bindings, service]) => [...bindings, { type: "service", name: "OG_WORKER", service }]) }, } }, }, }) function createOgWorker() { const root = path.join($cli.paths.root, "packages/stats/app") execFileSync("bun", ["run", "build:og"], { cwd: root, stdio: "inherit" }) const dir = path.join(root, ".og-worker") const types: Record = { ".mjs": "application/javascript+module", ".wasm": "application/wasm", ".bin": "application/octet-stream", } const worker = new cloudflare.Worker("StatsOg", { accountId, name: `${$app.name}-${$app.stage}-stats-og`, subdomain: { enabled: false, previewsEnabled: false }, observability: { enabled: true }, }) const version = new cloudflare.WorkerVersion("StatsOgVersion", { accountId, workerId: worker.id, mainModule: "index.mjs", compatibilityDate: "2026-07-01", modules: readdirSync(dir).map((name) => ({ name, contentFile: path.join(dir, name), contentType: types[path.extname(name)], })), }) return new cloudflare.WorkersDeployment("StatsOgDeployment", { accountId, scriptName: worker.name, strategy: "percentage", versions: [{ percentage: 100, versionId: version.id }], }) } // The site serves itself under /data and //data, and these routes are its only way in: it has no custom domain // or workers.dev URL. An exact `/data` pattern misses `/data?query`, so each pattern is a `/data*` prefix. Route patterns // can't wildcard the middle of a path, hence one per locale. `sst dev` deploys no Worker. const appServer = app.nodes.server if (appServer) { for (const prefix of ["data", ...LOCALES.map((locale) => `${locale}/data`)]) { new cloudflare.WorkersRoute(`StatsRoute${prefix.replaceAll("/", "")}`, { zoneId: zoneID, pattern: `${domain}/${prefix}*`, script: appServer.nodes.worker.scriptName, }) } } //////////////// // SERVICES //////////////// const statsSyncConfig = new sst.Linkable("StatsSyncConfig", { properties: { dataset: "zen", }, }) // Comma-separated model IDs kept off the public stats site. A secret, so excluding // an unreleased model does not publish its name in git. const statsHiddenModels = new sst.Secret("StatsHiddenModels", "") const r2SqlAuthToken = new sst.Secret("R2SqlAuthToken") const r2Sql = new sst.Linkable("R2Sql", { properties: { accountId, bucket: `platform-${$app.stage}-lake`, namespace: "inference", table: "generation", }, }) // One instance per run: a plan step, then one step per stat period and retention // cohort. See packages/stats/README.md for triggering a run by hand. export const statsSyncWorkflow = new sst.cloudflare.Workflow("StatsSyncWorkflow", { handler: "packages/stats/server/src/workflow.ts", className: "StatsSyncWorkflow", link: [database, r2Sql, r2SqlAuthToken, statsHiddenModels, statsSyncConfig], transform: { worker: { transform: { worker: (args) => { // Raise CPU time from the 30-second default to the 5-minute maximum for // ranking a full week of geo rows. args.limits = { cpuMs: 300_000 } args.observability = { enabled: true } }, }, }, }, }) // Schedules must match packages/stats/server/src/schedule.ts, which maps the // daily one to a history run. if (!$dev) new sst.cloudflare.Cron("StatsSyncCron", { schedules: ["5 * * * *", "20 3 * * *"], worker: { handler: "packages/stats/server/src/cron.ts", link: [statsSyncWorkflow], transform: { worker: (args) => { args.observability = { enabled: true } }, }, }, }) new sst.x.DevCommand("StatsSync", { link: [database, r2Sql, r2SqlAuthToken, statsHiddenModels, statsSyncConfig], dev: { command: "bun src/sync.ts", directory: "packages/stats/server", autostart: false, }, })