diff --git a/docs/operations/observability.md b/docs/operations/observability.md index f9f52f77..554808e6 100644 --- a/docs/operations/observability.md +++ b/docs/operations/observability.md @@ -279,7 +279,7 @@ columns and a non-null CTA target. Raw events are not copied, modified, or given a longer retention period. Current and previous UTC days are refreshed by the existing scheduler; retained historical documentation dates are rebuilt once during promotion. Site-derived input remains subject to the existing ninety-day -site retention and legacy docs input to its existing seven-day retention. On December 4 the retained production D1 database still contained docs-scoped events older than 91 days. Production Cron targets PostgreSQL; cleanup of retained D1 requires data-owner approval. +site retention and legacy docs input to its existing seven-day retention. The [retained D1 Worker](../../workers/retained-d1/wrangler.json) runs daily at 22:00 UTC against `omg-platform` independently of the PostgreSQL API Cron. Its completion log records the cutoff and deleted count. Remove its Cron when retiring that database. The six ignored site issues are specific low-risk scanner fingerprints. Their retained occurrences returned 403 or 404 and reported no matching outbound fetch. diff --git a/package.json b/package.json index bece14ff..0db3c34f 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "format": "prettier --write --ignore-unknown .snyk site/.snyk workers/api/.snyk contracts docs/operations shared site/e2e site/static/docs.js site/static/terminal site/tools site/playwright.config.ts site/src site/alchemy.run.ts site/alchemy.environment.mjs site/vite.config.ts site/package.json site/tsconfig.json workers/api/src workers/api/tests workers/api/tools workers/cve .github/workflows/ci.yml .github/workflows/production.yml tools/check-*.mjs tools/drill-*.mjs tools/release-staging.mjs tools/release tools/postgres tools/check-npm-audits.ts tools/oxlint/sync-anti-slop.mjs oxlint.config.ts prettier.config.js package.json workers/api/package.json tsconfig.json workers/api/tsconfig.json workers/api/wrangler.toml workers/api/wrangler.test.toml", "format:check": "prettier --check --ignore-unknown .snyk site/.snyk workers/api/.snyk contracts docs/operations shared site/e2e site/static/docs.js site/static/terminal site/tools site/playwright.config.ts site/src site/alchemy.run.ts site/alchemy.environment.mjs site/vite.config.ts site/package.json site/tsconfig.json workers/api/src workers/api/tests workers/api/tools workers/cve .github/workflows/ci.yml .github/workflows/production.yml tools/check-*.mjs tools/drill-*.mjs tools/release-staging.mjs tools/release tools/postgres tools/check-npm-audits.ts tools/oxlint/sync-anti-slop.mjs oxlint.config.ts prettier.config.js package.json workers/api/package.json tsconfig.json workers/api/tsconfig.json workers/api/wrangler.toml workers/api/wrangler.test.toml", "lint": "npm run lint:oxlint", - "lint:oxlint": "oxlint --config oxlint.config.ts shared site/e2e site/static/docs.js site/static/terminal site/tools site/playwright.config.ts site/alchemy.run.ts site/alchemy.environment.mjs site/vite.config.ts site/src workers/api/src workers/api/tests workers/cve tools/check-*.mjs tools/drill-*.mjs tools/release-staging.mjs tools/release tools/postgres tools/check-npm-audits.ts", + "lint:oxlint": "oxlint --config oxlint.config.ts shared site/e2e site/static/docs.js site/static/terminal site/tools site/playwright.config.ts site/alchemy.run.ts site/alchemy.environment.mjs site/vite.config.ts site/src workers/api/src workers/api/tests workers/cve workers/retained-d1 tools/check-*.mjs tools/drill-*.mjs tools/release-staging.mjs tools/release tools/postgres tools/check-npm-audits.ts", "lint:workers": "oxlint --config oxlint.config.ts workers/api/src workers/api/tests", "lint:workers:fix": "oxlint --fix --config oxlint.config.ts workers/api/src workers/api/tests", "sync:anti-slop": "node tools/oxlint/sync-anti-slop.mjs", diff --git a/tools/release/retained-d1.test.mjs b/tools/release/retained-d1.test.mjs new file mode 100644 index 00000000..e14eb784 --- /dev/null +++ b/tools/release/retained-d1.test.mjs @@ -0,0 +1,77 @@ +import assert from 'node:assert/strict'; +import { readFileSync } from 'node:fs'; +import { DatabaseSync } from 'node:sqlite'; +import { test } from 'node:test'; +import worker from '../../workers/retained-d1/worker.mjs'; + +test('retained D1 cleanup deletes only expired docs source rows and can be retried', async () => { + const db = new DatabaseSync(':memory:'); + try { + db.exec( + 'CREATE TABLE site_analytics_events (id INTEGER, event_type TEXT, properties TEXT, created_at INTEGER)' + ); + const insert = db.prepare('INSERT INTO site_analytics_events VALUES (?, ?, ?, ?)'); + const cutoff = Date.UTC(2026, 8, 5); + const scheduledTime = Date.UTC(2026, 11, 4); + const fixtures = [ + ['pageview', '/docs', -1], + ['click', '/docs/', -1], + ['performance', '/docs/a', -1], + ['pageview', '/docs/a', 0], + ['pageview', '/docs/a', 1], + ['pageview', '/docs-old', -1], + ['error', '/docs/a', -1], + ['form', '/docs', -1], + ['pageview', null, -1], + ['click', '/', -1], + ]; + fixtures.forEach(([type, path, offset], id) => { + insert.run(id, type, JSON.stringify({ path }), cutoff + offset); + }); + const changes = []; + const env = { + RETAINED_DB: { + prepare(sql) { + const statement = db.prepare(sql); + return { + bind: value => ({ + run() { + const meta = statement.run(value); + changes.push(meta.changes); + return { meta }; + }, + }), + }; + }, + }, + }; + await worker.scheduled({ scheduledTime }, env); + await worker.scheduled({ scheduledTime }, env); + assert.deepEqual(changes, [3, 0]); + assert.deepEqual( + db + .prepare('SELECT id FROM site_analytics_events ORDER BY id') + .all() + .map(row => row.id), + [3, 4, 5, 6, 7, 8, 9] + ); + } finally { + db.close(); + } +}); + +test('retained cleanup runs daily against the production D1 binding only', () => { + const config = JSON.parse( + readFileSync(new URL('../../workers/retained-d1/wrangler.json', import.meta.url), 'utf8') + ); + assert.deepEqual(config.triggers.crons, ['0 22 * * *']); + assert.equal(config.workers_dev, false); + assert.equal(config.preview_urls, false); + assert.deepEqual(config.d1_databases, [ + { + binding: 'RETAINED_DB', + database_name: 'omg-platform', + database_id: 'fee8ddab-fb4a-4be4-b8d2-8abb7c2db188', + }, + ]); +}); diff --git a/workers/retained-d1/worker.mjs b/workers/retained-d1/worker.mjs new file mode 100644 index 00000000..bcea48fc --- /dev/null +++ b/workers/retained-d1/worker.mjs @@ -0,0 +1,17 @@ +const retentionMs = 90 * 24 * 60 * 60 * 1000; + +export default { + async scheduled(controller, env) { + const cutoff = controller.scheduledTime - retentionMs; + const result = await env.RETAINED_DB.prepare( + `DELETE FROM site_analytics_events + WHERE created_at < ? + AND event_type IN ('pageview', 'click', 'performance') + AND (json_extract(properties, '$.path') = '/docs' + OR substr(json_extract(properties, '$.path'), 1, 6) = '/docs/')` + ) + .bind(cutoff) + .run(); + console.log({ event: 'retained_d1.docs_cleanup', cutoff, deleted: result.meta.changes }); + }, +}; diff --git a/workers/retained-d1/wrangler.json b/workers/retained-d1/wrangler.json new file mode 100644 index 00000000..e1ce6e8a --- /dev/null +++ b/workers/retained-d1/wrangler.json @@ -0,0 +1,17 @@ +{ + "name": "omg-retained-d1-docs-retention", + "main": "worker.mjs", + "account_id": "f1e95b3e1b502cf366dfc81a863695fa", + "compatibility_date": "2026-03-17", + "workers_dev": false, + "preview_urls": false, + "triggers": { "crons": ["0 22 * * *"] }, + "observability": { "enabled": true, "head_sampling_rate": 1 }, + "d1_databases": [ + { + "binding": "RETAINED_DB", + "database_name": "omg-platform", + "database_id": "fee8ddab-fb4a-4be4-b8d2-8abb7c2db188" + } + ] +}