caelum0x 6baaadf91a feat(insights): identify and people, saved cohorts, retention by property, breakdowns, CSV and JSON export, delete by user (PY3.2)
RYP-F1 in Wave P. Everything scoped by the org through website.team_id with
bound parameters only; tested at the SQL level on a throwaway cluster with
the upstream migrations plus the fork's 90 and 91.

- Identify: the edge collector (contract v1.1, additive) and the page tag
  accept `identify(userId, traits)` and a `user` on any event. Insights links
  the session (upstream session_link and session.distinct_id) and keeps one
  profile per (app, user) with first and last seen and merged traits
  (migration 91_railyard_people). Before this, an identify from the collector
  made Insights refuse the whole batch.
- Saved cohorts: performed (with min times), not performed, trait equals and
  first seen within; counts are live. On fixtures a cohort equals a
  hand-written SQL count (two definitions).
- Retention by property (country, language, screen, trait.<key>; day or week
  periods), breakdowns (path, referrer, country, language, screen, event,
  hostname, data.<key>, trait.<key>).
- Export: events (31 days) or profiles, JSON or CSV, 100,000 rows; formula
  cells defused.
- Delete by user id: one statement removes events, event data, session data,
  revenue, replay and heatmap rows, links, sessions and the profile in the
  org's apps only; recorded in railyard_erasure; a pending request is retried
  by POST /api/v1/erasure/sweep (hourly), so it is done within 24 h.
- Console /api/v1/insights/* for an org's ry_ token: the org comes from the
  token, app names resolve to the org's own app, the platform token never
  leaves; served only with RAILYARD_REPLAY_LIVE=1 (denylist rule
  railyard-slot-replay), else 503 "arriving".
- New packages: @railyard/insights-react-native (offline queue, identify kept
  across restarts, reset, consent) and @railyard/insights-server (server
  track and identify; profiles, cohorts, retention, breakdown, export,
  deleteUser through the console). No dependencies.

Env: RAILYARD_REPLAY_LIVE (console). Migration: insights
91_railyard_people (idempotent; scratch Postgres only).

Tests: insights fork 9/9 (depth SQL incl. isolation, cohort vs hand SQL,
erasure), edge 18/18, console 125/125 (tsc clean), RN 3/3, server 5/5
(incl. copy lint), CLI 18/18. The upstream vitest suite runs in the image
build on box 2 or CI (not on the Mac).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 03:35:37 +03:00

50 lines
2.7 KiB
TypeScript

// The Insights depth docs and messages (PY3.2) pass the vendored copy denylist.
// Profiles and cohorts are claims only with RAILYARD_REPLAY_LIVE on (rule
// railyard-slot-replay); the console serves them only then (checked below), so
// these docs are linted as live, and every other rule still applies.
// node --test test/*.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { errors, formatFindings, lintUnits, markdownUnits } from '../../../policy/copy-lint.mjs';
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '../../..');
const R = (p: string) => readFileSync(join(ROOT, p), 'utf8');
const strings = (src: string) =>
[...src.replace(/^\s*(\/\/|\*).*$/gm, '').matchAll(/["'`]([^"'`\n]{12,})["'`]/g)].map((m) => m[1] as string).filter((s) => !/^[\^[/.]|^ry_|https?:|\$\{|^[a-z_]+\.[a-z]/.test(s));
const LIVE = { RAILYARD_REPLAY_LIVE: '1' };
test('package READMEs pass the copy lint (docs surface, replay live)', () => {
const units = ['packages/insights-server/README.md', 'packages/insights-react-native/README.md'].flatMap((f) => markdownUnits(R(f), { where: f }));
const found = errors(lintUnits(units, { flags: LIVE }));
assert.deepEqual(found, [], formatFindings(found));
});
test('messages people can read pass the copy lint and name no upstream product', () => {
const files: [string, string, Record<string, string>][] = [
['packages/insights-server/src/index.ts', 'api-public', LIVE],
['packages/insights-react-native/src/index.ts', 'api-public', LIVE],
['apps/console/lib/insights-depth/forward.ts', 'api-public', {}],
['apps/insights/src/railyard/people.ts', 'api-public', LIVE],
['apps/insights/src/railyard/cohorts.ts', 'api-public', LIVE],
['apps/insights/src/railyard/analysis.ts', 'api-public', LIVE],
['apps/insights/src/railyard/export.ts', 'api-public', LIVE],
];
for (const [f, surface, flags] of files) {
const units = strings(R(f)).map((text) => ({ text, surface, where: f }));
const found = errors(lintUnits(units, { flags }));
assert.deepEqual(found, [], formatFindings(found));
assert.doesNotMatch(R(f).replace(/^\s*(\/\/|\*).*$/gm, ''), /umami/i, f);
}
});
test('the console serves Insights depth only with RAILYARD_REPLAY_LIVE on', () => {
const src = R('apps/console/lib/insights-depth/forward.ts');
assert.match(src, /if \(!deps\.live\) return reply\(\{ error: ARRIVING \}, 503\)/);
assert.match(R('apps/console/lib/config.ts'), /replayLive: env\.RAILYARD_REPLAY_LIVE === "1"/);
assert.match(R('apps/console/app/api/v1/insights/[...path]/route.ts'), /live: cfg\.replayLive/);
});