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>
60 lines
2.4 KiB
TypeScript
60 lines
2.4 KiB
TypeScript
// Site entries for the Insights edge collector (RYP-E1, Wave P PY1.3).
|
|
//
|
|
// The collector reads one KV entry per app, key `site:<site_key>`:
|
|
// `{ org_id, app_id, events_limit, paused }` (contract
|
|
// apps/insights-edge/contract/INGEST-API.md). The console writes it when an
|
|
// app is created and again whenever the org's plan, add-on or pause changes,
|
|
// so caps and pauses reach the edge within the collector's 60 s cache.
|
|
|
|
import { access } from "./ladder.ts";
|
|
import type { Repo } from "./repo.ts";
|
|
import type { App } from "./types.ts";
|
|
|
|
export interface SiteEntry {
|
|
org_id: string;
|
|
app_id: string;
|
|
events_limit: number;
|
|
paused: boolean;
|
|
}
|
|
|
|
export interface SitesStore {
|
|
put(siteKey: string, entry: SiteEntry): Promise<void>;
|
|
}
|
|
|
|
export class MemorySites implements SitesStore {
|
|
entries = new Map<string, SiteEntry>();
|
|
async put(siteKey: string, entry: SiteEntry) {
|
|
this.entries.set(`site:${siteKey}`, structuredClone(entry));
|
|
}
|
|
}
|
|
|
|
/** The Workers KV namespace bound as SITES (wrangler.jsonc), through the OpenNext context. */
|
|
export function kvSites(): SitesStore {
|
|
return {
|
|
async put(siteKey, entry) {
|
|
const { getCloudflareContext } = await import("@opennextjs/cloudflare");
|
|
const { env } = await getCloudflareContext({ async: true });
|
|
const kv = (env as unknown as { SITES?: { put(key: string, value: string): Promise<void> } }).SITES;
|
|
if (!kv) throw new Error("the SITES namespace is not bound to the console");
|
|
await kv.put(`site:${siteKey}`, JSON.stringify(entry));
|
|
},
|
|
};
|
|
}
|
|
|
|
/** The entry for one app from the org's current entitlement. */
|
|
export async function siteEntry(repo: Repo, app: Pick<App, "id" | "org_id">, now: Date): Promise<SiteEntry> {
|
|
const a = access(await repo.getEntitlement(app.org_id), now);
|
|
return { org_id: app.org_id, app_id: app.id, events_limit: a.eventsLimit, paused: a.state === "paused" || !a.canAddApps };
|
|
}
|
|
|
|
export async function registerSite(sites: SitesStore, repo: Repo, app: App, now: Date): Promise<void> {
|
|
await sites.put(app.site_key, await siteEntry(repo, app, now));
|
|
}
|
|
|
|
/** Rewrites every app's entry of the org (after a plan, add-on or pause change). */
|
|
export async function syncSites(sites: SitesStore, repo: Repo, orgId: string, now: Date): Promise<number> {
|
|
const apps = await repo.listApps(orgId);
|
|
for (const app of apps) await registerSite(sites, repo, app, now);
|
|
return apps.length;
|
|
}
|