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
44 changes: 34 additions & 10 deletions scripts/analytics-report.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,41 @@ export const analyticsReportQuery = `
ORDER BY report_section, event_count DESC, item
`;

/** Cloudflare D1 limits compound SELECT terms more tightly than local SQLite. */
export function analyticsReportQueries(maxTerms = 4) {
if (!Number.isInteger(maxTerms) || maxTerms < 2) {
throw new Error("Analytics report chunks require at least two SELECT terms");
}
const body = analyticsReportQuery
.replace(/\n\s*ORDER BY report_section, event_count DESC, item\s*$/, "")
.trim();
const terms = body.split(/\n\s*UNION ALL\s*\n/);
const first = terms.shift();
const maintenance = terms.pop();
if (!first || !maintenance || !maintenance.includes("analytics_maintenance_status")) {
throw new Error("Analytics report query structure is invalid");
}

const chunks = [[first, maintenance]];
while (terms.length > 0) chunks.push(terms.splice(0, maxTerms));
return chunks.map(
(chunk) =>
`${chunk.join("\n\n UNION ALL\n\n")}\n\n ORDER BY report_section, event_count DESC, item`,
);
}

// Importing the query for local fixture tests must never contact production.
if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
const result = spawnSync(
"wrangler",
["d1", "execute", "scientfactory-downloads", "--remote", "--command", analyticsReportQuery],
{ stdio: "inherit", timeout: 60_000 },
);
if (result.error) {
console.error("Analytics aggregate report failed or timed out");
process.exitCode = 1;
} else {
process.exitCode = result.status ?? 1;
for (const query of analyticsReportQueries()) {
const result = spawnSync(
"wrangler",
["d1", "execute", "scientfactory-downloads", "--remote", "--command", query],
{ stdio: "inherit", timeout: 60_000 },
);
if (result.error || result.status !== 0) {
console.error("Analytics aggregate report failed or timed out");
process.exitCode = result.status ?? 1;
break;
}
}
}
16 changes: 15 additions & 1 deletion scripts/analytics-report.test.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import { expect, it } from "vitest";
import { analyticsReportQuery } from "./analytics-report.mjs";
import { analyticsReportQueries, analyticsReportQuery } from "./analytics-report.mjs";
import { testDatabase } from "../workers/events/src/sqlite.testSupport.ts";

const fixedQuery = analyticsReportQuery.replaceAll("'now'", "'2026-08-31T12:00:00.000Z'");

it("keeps remote report chunks within the D1 compound SELECT limit", () => {
const store = testDatabase();
try {
const queries = analyticsReportQueries();
expect(queries.length).toBeGreaterThan(1);
for (const query of queries) {
expect(query.match(/\bSELECT\b/g)?.length).toBeLessThanOrEqual(4);
expect(() => store.sqlite.prepare(query).all()).not.toThrow();
}
} finally {
store.close();
}
});

it("reports centrally retained diagnostics separately from PostHog delivery", () => {
const store = testDatabase();
try {
Expand Down
6 changes: 3 additions & 3 deletions workers/events/worker-configuration.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable */
// Generated by Wrangler by running `wrangler types --config=workers/events/wrangler.jsonc --include-runtime=false --env-interface=AnalyticsWorkerBindings workers/events/worker-configuration.d.ts` (hash: ee9d44a6ab6f6fd522cbd77158166bc2)
// Generated by Wrangler by running `wrangler types --config=workers/events/wrangler.jsonc --include-runtime=false --env-interface=AnalyticsWorkerBindings workers/events/worker-configuration.d.ts` (hash: 81f6792a18d83d567c47b9a872304e01)
interface __BaseEnv_AnalyticsWorkerBindings {
ANALYTICS_DB: D1Database;
ANALYTICS_INGESTION_RATE_LIMITER: RateLimit;
CF_VERSION_METADATA: WorkerVersionMetadata;
DESKTOP_INGESTION_ENABLED: "false";
DESKTOP_POSTHOG_EXPORT_ENABLED: "false";
DESKTOP_INGESTION_ENABLED: "true";
DESKTOP_POSTHOG_EXPORT_ENABLED: "true";
POSTHOG_PROJECT_ID: "228610";
POSTHOG_PROJECT_TOKEN: string;
POSTHOG_PERSONAL_API_KEY: string;
Expand Down
4 changes: 2 additions & 2 deletions workers/events/wrangler.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"compatibility_date": "2026-07-20",
"compatibility_flags": ["nodejs_compat"],
"vars": {
"DESKTOP_INGESTION_ENABLED": "false",
"DESKTOP_POSTHOG_EXPORT_ENABLED": "false",
"DESKTOP_INGESTION_ENABLED": "true",
"DESKTOP_POSTHOG_EXPORT_ENABLED": "true",
"POSTHOG_PROJECT_ID": "228610",
},
"secrets": {
Expand Down