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>
86 lines
2.2 KiB
TypeScript
86 lines
2.2 KiB
TypeScript
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
export interface WeightedOption<T> {
|
|
value: T;
|
|
weight: number;
|
|
}
|
|
|
|
export function weightedRandom<T>(options: WeightedOption<T>[]): T {
|
|
const totalWeight = options.reduce((sum, opt) => sum + opt.weight, 0);
|
|
let random = Math.random() * totalWeight;
|
|
|
|
for (const option of options) {
|
|
random -= option.weight;
|
|
if (random <= 0) {
|
|
return option.value;
|
|
}
|
|
}
|
|
|
|
return options[options.length - 1].value;
|
|
}
|
|
|
|
export function randomInt(min: number, max: number): number {
|
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
}
|
|
|
|
export function randomFloat(min: number, max: number): number {
|
|
return Math.random() * (max - min) + min;
|
|
}
|
|
|
|
export function pickRandom<T>(array: T[]): T {
|
|
return array[Math.floor(Math.random() * array.length)];
|
|
}
|
|
|
|
export function shuffleArray<T>(array: T[]): T[] {
|
|
const result = [...array];
|
|
for (let i = result.length - 1; i > 0; i--) {
|
|
const j = Math.floor(Math.random() * (i + 1));
|
|
[result[i], result[j]] = [result[j], result[i]];
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export function uuid(): string {
|
|
return uuidv4();
|
|
}
|
|
|
|
export function generateDatesBetween(startDate: Date, endDate: Date): Date[] {
|
|
const dates: Date[] = [];
|
|
const current = new Date(startDate);
|
|
current.setHours(0, 0, 0, 0);
|
|
|
|
while (current <= endDate) {
|
|
dates.push(new Date(current));
|
|
current.setDate(current.getDate() + 1);
|
|
}
|
|
|
|
return dates;
|
|
}
|
|
|
|
export function addHours(date: Date, hours: number): Date {
|
|
return new Date(date.getTime() + hours * 60 * 60 * 1000);
|
|
}
|
|
|
|
export function addMinutes(date: Date, minutes: number): Date {
|
|
return new Date(date.getTime() + minutes * 60 * 1000);
|
|
}
|
|
|
|
export function addSeconds(date: Date, seconds: number): Date {
|
|
return new Date(date.getTime() + seconds * 1000);
|
|
}
|
|
|
|
export function subDays(date: Date, days: number): Date {
|
|
return new Date(date.getTime() - days * 24 * 60 * 60 * 1000);
|
|
}
|
|
|
|
export function formatNumber(num: number): string {
|
|
return num.toLocaleString();
|
|
}
|
|
|
|
export function progressBar(current: number, total: number, width = 30): string {
|
|
const percent = current / total;
|
|
const filled = Math.round(width * percent);
|
|
const empty = width - filled;
|
|
return `[${'█'.repeat(filled)}${'░'.repeat(empty)}] ${Math.round(percent * 100)}%`;
|
|
}
|