From 9387d69edb2eb359b62b115e2f2886ca91b9367d Mon Sep 17 00:00:00 2001 From: Brendan Date: Thu, 17 Sep 2026 05:55:06 +0000 Subject: [PATCH] Batch Cloudflare Tail status lookups --- packages/app/src/tenant.ts | 14 ++++++++----- packages/app/test/run-inspection.test.ts | 25 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/packages/app/src/tenant.ts b/packages/app/src/tenant.ts index 47b26b04..47bea4b1 100644 --- a/packages/app/src/tenant.ts +++ b/packages/app/src/tenant.ts @@ -372,10 +372,13 @@ export class TenantV2 extends DurableObject { return Promise.all(rows.map(async row => ({ ...(JSON.parse(await decrypt(String(row.value), this.env.CREDENTIAL_ENCRYPTION_KEY)) as any), integrationId: String(row.kind).slice(16) }))); } - private async tailIntegrationStatus(): Promise>> { - const installations = await this.tailIntegrations(); - return installations.map(({ signingSecret: _secret, ...installation }) => ({ ...installation, status: "connected", secretConfigured: true, - referencedJobCount: Number((this.one("SELECT count(DISTINCT job_id) AS count FROM triggers WHERE kind='webhook' AND json_extract(config,'$.provider')='cloudflareTail' AND json_extract(config,'$.integrationId')=?", installation.integrationId) as Row)?.count ?? 0) })); + private async tailIntegrationStatus(installations?: Array<{ integrationId: string; name: string; signingSecret: string; createdAt: string; updatedAt: string }>): Promise>> { + const loadedInstallations = installations ?? await this.tailIntegrations(); + const referencedJobCounts = new Map(this.rows( + "SELECT json_extract(config,'$.integrationId') AS integration_id, count(DISTINCT job_id) AS count FROM triggers WHERE kind='webhook' AND json_extract(config,'$.provider')='cloudflareTail' GROUP BY json_extract(config,'$.integrationId')", + ).map(row => [String(row.integration_id), Number(row.count ?? 0)] as const)); + return loadedInstallations.map(({ signingSecret: _secret, ...installation }) => ({ ...installation, status: "connected", secretConfigured: true, + referencedJobCount: referencedJobCounts.get(installation.integrationId) ?? 0 })); } private async saveTailIntegration(input: unknown): Promise { @@ -454,13 +457,14 @@ export class TenantV2 extends DurableObject { const connections = await this.exeConnections(); const exe = await this.connection("exe"); const ampConnections = await this.ampConnections(); + const tailInstallations = await this.tailIntegrations(); return json({ linear: linear ? { organizationName: linear.organizationName ?? null, viewerEmail: linear.viewerEmail ?? null } : null, clickup: clickup ? { teamName: clickup.teamName ?? null } : null, exe: exe ? { vmName: exe.vmName, agentKind: exe.agentKind, cwd: exe.cwd, herdrCommand: exe.herdrCommand ?? "herdr", agentCommand: exe.agentCommand ?? defaultAgentCommand(exe.agentKind) } : null, exeConnections: connections.map(({ apiToken, ...connection }) => connection), ampConnections: ampConnections.map(({ accessToken, ...connection }) => connection), - cloudflareTail: { count: (await this.tailIntegrations()).length, installations: await this.tailIntegrationStatus() }, + cloudflareTail: { count: tailInstallations.length, installations: await this.tailIntegrationStatus(tailInstallations) }, }); } diff --git a/packages/app/test/run-inspection.test.ts b/packages/app/test/run-inspection.test.ts index 1124a2e4..d18ed33c 100644 --- a/packages/app/test/run-inspection.test.ts +++ b/packages/app/test/run-inspection.test.ts @@ -13,6 +13,31 @@ function tenantWithRows(rows: (query: string, ...params: unknown[]) => Record { + it("projects Tail references with one grouped count query", async () => { + const queries: string[] = []; + const tenant = tenantWithRows((query) => { + queries.push(query); + return [ + { integration_id: "tail-1", count: 2 }, + { integration_id: "tail-2", count: 1 }, + ]; + }); + + const installations = [ + { integrationId: "tail-1", name: "Production", signingSecret: "secret-1", createdAt: "2026-09-16", updatedAt: "2026-09-16" }, + { integrationId: "tail-2", name: "Staging", signingSecret: "secret-2", createdAt: "2026-09-16", updatedAt: "2026-09-16" }, + { integrationId: "tail-3", name: "Unused", signingSecret: "secret-3", createdAt: "2026-09-16", updatedAt: "2026-09-16" }, + ]; + + await expect((tenant as any).tailIntegrationStatus(installations)).resolves.toEqual([ + { integrationId: "tail-1", name: "Production", createdAt: "2026-09-16", updatedAt: "2026-09-16", status: "connected", secretConfigured: true, referencedJobCount: 2 }, + { integrationId: "tail-2", name: "Staging", createdAt: "2026-09-16", updatedAt: "2026-09-16", status: "connected", secretConfigured: true, referencedJobCount: 1 }, + { integrationId: "tail-3", name: "Unused", createdAt: "2026-09-16", updatedAt: "2026-09-16", status: "connected", secretConfigured: true, referencedJobCount: 0 }, + ]); + expect(queries).toHaveLength(1); + expect(queries[0]).toContain("GROUP BY json_extract(config,'$.integrationId')"); + }); + it("preserves trigger slugs across edits and reordering", () => { const tenant = Object.create(TenantV2.prototype) as TenantV2; const existing = new Map([