caelum0x 1bc04fa81b feat(insights): the Insights fork behind the add-on, site entries, tenant summaries, caps that say what happens (PY1.3)
Railyard side of PY1.3 (RYP-D2, RYP-E1, SC-E3). Acceptance:
1. apps/insights: umami-software/umami v3.4.0 (MIT, commit ec0ff503),
   vendored --depth 1, LICENSE kept, NOTICE added. Fork additions only:
   src/railyard/{ingest,summary,tenancy,api-only}.ts, /v1/ingest/batch (the
   edge collector contract v1, bearer INSIGHTS_INGEST_TOKEN, no IP ever,
   retried batches de-duplicated by migration 90_railyard_ingest_receipts),
   /api/v1/tenants/{org}/summary (bearer INSIGHTS_API_TOKEN), src/proxy.ts
   (INSIGHTS_API_ONLY=1 serves only the Railyard API and the heartbeat).
   Tenant isolation is tested at the API level (an app id owned by another org
   is refused) and at the SQL level (upstream migrations 01-26 plus 90 on a
   throwaway cluster: org B never reads org A's rows, even naming its app).
   Upstream vitest suite: not run on the Mac (pnpm 12.3.4 pinned upstream,
   disk floor); it runs in the image build on box 2 or CI.
2. No upstream product name in anything a customer loads: the page tag, the
   console pages and the fork's additions (tests); the upstream dashboard is
   never served in API-only mode.
3. The console writes the collector's site entry on "Add app", on
   auto-created apps from deploys, and rewrites every entry on add-on, plan or
   pause changes (lib/sites.ts, SITES KV binding). The bridge gains
   `sites` (the project's app, site key, page tag and collector URLs, entry
   written) and the analytics proof is the first event from Insights. The
   console's Insights page shows visitors, events, first event and the
   visit -> signup -> first action funnel.
5. insights-edge/src/caps.ts: at 100% of the cap events are still counted and
   not stored until the next UTC month; the console shows the same message
   from a byte-identical copy (lib/caps.ts, tested).
4 (384 MB at 50 events a second for 10 min) is measured on staging before deploy.

Env: INSIGHTS_API_URL, INSIGHTS_API_TOKEN, SITES KV binding (console);
INSIGHTS_INGEST_TOKEN, INSIGHTS_API_TOKEN, INSIGHTS_API_ONLY (Insights).

Tests: console 54/54 (tsc clean), insights fork 7/7 (incl. SQL isolation),
insights-edge caps + collector + sim 14/14, tests/contract 3/3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 21:23:38 +03:00

139 lines
4.0 KiB
TypeScript

import { existsSync, readdirSync, readFileSync } from 'node:fs';
import path from 'node:path';
import { COVERAGE_DIR } from '../paths';
import { allowlist } from './allowlist';
import { type ApiOperation, loadOperations } from './oracle';
import { UNMATCHED_PREFIX } from './recorder';
export interface CoverageSummary {
total: number;
covered: string[];
allowlisted: string[];
uncovered: ApiOperation[];
/** Allowlisted operations that were actually called — the entry can be removed. */
staleAllowlist: string[];
/** Allowlist keys that don't exist in the OpenAPI document. */
unknownAllowlist: string[];
/** Calls whose path could not be mapped to any documented operation. */
unmatched: string[];
}
export function readRecordedCalls() {
const covered = new Set<string>();
const unmatched = new Set<string>();
if (!existsSync(COVERAGE_DIR)) {
return { covered, unmatched };
}
for (const file of readdirSync(COVERAGE_DIR)) {
if (!file.endsWith('.log')) {
continue;
}
for (const line of readFileSync(path.join(COVERAGE_DIR, file), 'utf8').split('\n')) {
const entry = line.trim();
if (!entry) {
continue;
}
if (entry.startsWith(UNMATCHED_PREFIX)) {
unmatched.add(entry.slice(UNMATCHED_PREFIX.length));
} else {
covered.add(entry);
}
}
}
return { covered, unmatched };
}
export function computeCoverage(): CoverageSummary {
const operations = loadOperations();
const { covered, unmatched } = readRecordedCalls();
const known = new Set(operations.map(operation => operation.key));
const db = process.env.UMAMI_TEST_DB === 'clickhouse' ? 'clickhouse' : 'postgres';
const allowed = new Set(
allowlist.filter(entry => !entry.db || entry.db === db).map(entry => entry.key),
);
return {
total: operations.length,
covered: operations.filter(op => covered.has(op.key)).map(op => op.key),
allowlisted: [...allowed].filter(key => known.has(key) && !covered.has(key)),
uncovered: operations.filter(op => !covered.has(op.key) && !allowed.has(op.key)),
staleAllowlist: [...allowed].filter(key => covered.has(key)),
unknownAllowlist: [...allowed].filter(key => !known.has(key)),
unmatched: [...unmatched].sort(),
};
}
function groupByTag(operations: ApiOperation[]) {
const groups = new Map<string, ApiOperation[]>();
for (const operation of operations) {
const group = groups.get(operation.tag) ?? [];
group.push(operation);
groups.set(operation.tag, group);
}
return [...groups.entries()].sort(([a], [b]) => a.localeCompare(b));
}
export function formatCoverage(summary: CoverageSummary): string {
const lines: string[] = [];
const { total, covered, allowlisted, uncovered } = summary;
lines.push('');
lines.push(
`API coverage: ${covered.length + allowlisted.length}/${total} operations ` +
`(${covered.length} covered, ${allowlisted.length} allowlisted, ${uncovered.length} uncovered)`,
);
if (uncovered.length) {
lines.push('');
lines.push('Uncovered operations:');
for (const [tag, operations] of groupByTag(uncovered)) {
lines.push(` ${tag}`);
for (const operation of operations) {
const source = operation.source ? ` <- ${operation.source}` : '';
lines.push(` ${operation.key}${source}`);
}
}
}
if (summary.staleAllowlist.length) {
lines.push('');
lines.push('Allowlisted operations that were called (remove them from coverage/allowlist.ts):');
for (const key of summary.staleAllowlist) {
lines.push(` ${key}`);
}
}
if (summary.unknownAllowlist.length) {
lines.push('');
lines.push('Allowlist entries that do not exist in public/openapi.json:');
for (const key of summary.unknownAllowlist) {
lines.push(` ${key}`);
}
}
if (summary.unmatched.length) {
lines.push('');
lines.push('Calls that did not match any documented operation:');
for (const entry of summary.unmatched) {
lines.push(` ${entry}`);
}
}
lines.push('');
return lines.join('\n');
}