caelum0x 65acbbd9a3 feat(insights): page speed per app with deploy markers, a web package that measures it, and the join to errors (PY3.3)
RYP-G3 in Wave P.
- @railyard/insights-web: page views (with client-side route changes),
  events, identify, and LCP, INP, CLS, FCP and TTFB per page view, sent once
  when the page is hidden or the route changes; no cookies, no storage, Do
  Not Track and consent respected. src/metrics.ts adapts MIT-licensed code
  (NOTICE keeps the licence) with two changes that match Chrome's field
  reporting: LCP stops at the first input, and INP picks its percentile from
  the page's interaction count. On six worked fixtures (after-click paint,
  two shift windows with a post-input shift, prerender activation, 120
  interactions, multi-event interactions) every metric and the p75 are
  within 10% of Chrome's values.
- Edge collector and ingest carry `performance` events (the five metrics,
  in the upstream ranges) into upstream event type 5 rows.
- Insights GET /api/v1/tenants/{org}/vitals: p75 per app, per UTC day, and
  in the day before and after each given deploy time (up to 10); ratings
  from the published thresholds; org-scoped (SQL test with hand p75s).
- Console: the Page speed screen (only with RAILYARD_REPLAY_LIVE=1) and the
  Scribase bridge `insights` answer gain `vitals` (p75 cells and one marker
  per deploy from the console's own deploy records, so a deploy is marked on
  the next read); a vitals failure never hides the rest of the tab.

The Scribase Analytics sub-tab (carve-out) joins these markers to the
environment's error issues: "This deploy raised errors by N" links to the
Errors tab.

Tests: console 128/128 (view model, bridge marker, flag gate, copy lint),
insights-web 12/12 (fixtures, client, copy), insights fork 12/12, edge
19/19, tsc clean.

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

51 lines
2.8 KiB
TypeScript

// Page-speed metrics (Wave P PY3.3): the numbers for each fixture page load
// are within 10% of what Chrome's field reporting gives for the same entries,
// and so is the 75th percentile over all of them (the figure the views show).
// 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 { computeVitals, p75, type PerfEntry, type Vitals } from '../src/metrics.ts';
const HERE = dirname(fileURLToPath(import.meta.url));
const fixture = JSON.parse(readFileSync(join(HERE, 'fixtures/chrome-page-loads.json'), 'utf8')) as {
loads: { name: string; entries: PerfEntry[]; chrome: Vitals; interactionCount?: number }[];
};
const METRICS = ['lcp', 'inp', 'cls', 'fcp', 'ttfb'] as const;
const within10 = (got: number | undefined, want: number) => got !== undefined && (want === 0 ? got === 0 : Math.abs(got - want) / want <= 0.1);
for (const load of fixture.loads) {
test(`within 10% of Chrome: ${load.name}`, () => {
const got = computeVitals(load.entries, { interactionCount: load.interactionCount, layoutShiftSupported: true });
for (const m of METRICS) {
const want = load.chrome[m];
if (want === undefined) assert.equal(got[m], undefined, `${m} should be absent`);
else assert.ok(within10(got[m], want), `${m}: got ${got[m]}, Chrome ${want}`);
}
});
}
test('the p75 over all loads is within 10% of the p75 of the Chrome values', () => {
const ours = fixture.loads.map((l) => computeVitals(l.entries, { interactionCount: l.interactionCount, layoutShiftSupported: true }));
for (const m of METRICS) {
const chrome = fixture.loads.map((l) => l.chrome[m]).filter((v): v is number => v !== undefined);
const mine = ours.map((v) => v[m]).filter((v): v is number => v !== undefined);
assert.equal(mine.length, chrome.length, m);
assert.ok(within10(p75(mine) ?? undefined, p75(chrome) as number), `${m}: p75 ${p75(mine)} vs ${p75(chrome)}`);
}
});
test('without the two corrections the LCP and INP cases would be far off (the fixture is not trivial)', () => {
const lcpCase = fixture.loads[1]!;
const lastCandidate = Math.max(...lcpCase.entries.filter((e) => e.entryType === 'largest-contentful-paint').map((e) => e.startTime));
assert.ok(!within10(lastCandidate, lcpCase.chrome.lcp as number), 'the paint after the click is 4x the real LCP');
const inpCase = fixture.loads[4]!;
const slowest = Math.max(...inpCase.entries.filter((e) => e.interactionId).map((e) => e.duration));
assert.ok(!within10(slowest, inpCase.chrome.inp as number));
// A browser without layout-shift reporting gives no CLS rather than a made-up 0.
assert.equal(computeVitals(fixture.loads[3]!.entries).cls, undefined);
});