SPB Git

Repository Public

fixtures.ts 8.1 KB 105 lines typescript

1import { sql } from "./harness";23// Synthetic rows for the e2e database only (aliases `e2e-…`, environment `test`, documentation IP ranges).4// They are written straight into the tables the API reads, in the shape the agents/ingest produce.56export const NODE = {7  fresh: { id: "0e2e0000-0000-7000-8000-000000000001", alias: "e2e-node-01" },8  stale: { id: "0e2e0000-0000-7000-8000-000000000002", alias: "e2e-node-02" },9  offline: { id: "0e2e0000-0000-7000-8000-000000000003", alias: "e2e-node-03" },10} as const;11export const SERVICE = { api: "0e2e0000-0000-7000-8000-0000000000a1", agentd: "0e2e0000-0000-7000-8000-0000000000a2" } as const;12export const MODEL = { mlx: "0e2e0000-0000-7000-8000-0000000000b1" } as const;1314const GiB = 1024 ** 3;1516/** Makes the "fresh" node fresh again and the "stale" one stale (freshness is computed from elapsed time). */17export function touchNodes(): void {18  sql(`19    update nodes set last_seen_at = now() where id = '${NODE.fresh.id}';20    update node_state set ts = now(), received_at = now() where node_id = '${NODE.fresh.id}';21    update agents set last_seen_at = now() where node_id = '${NODE.fresh.id}';22    update nodes set last_seen_at = now() - interval '60 seconds' where id = '${NODE.stale.id}';23    update node_state set ts = now() - interval '60 seconds', received_at = now() - interval '60 seconds' where node_id = '${NODE.stale.id}';24    update services set last_seen_at = now() where node_id = '${NODE.fresh.id}' and gone_at is null;25    update inference_servers set last_seen_at = now() where node_id = '${NODE.fresh.id}';26    update volumes set last_seen_at = now() where node_id = '${NODE.fresh.id}';27  `);28}2930/** Nodes, agent, latest state, one hour of CPU samples with a hole, services, an inference server, volumes, NAS, backup. */31export function seedCluster(): void {32  const values = JSON.stringify({33    "cpu.usage": 43.5, "cpu.user": 30.1, "cpu.system": 13.4, "load.1": 2.25, "load.5": 2.0, "load.15": 1.8,34    "mem.used": 9 * GiB, "mem.total": 24 * GiB, "mem.wired": 2 * GiB, "mem.compressed": 1 * GiB, "mem.pressure.level": 1, "mem.available.percent": 62,35    "swap.used": 0, "disk.used{/}": 120 * GiB, "disk.total{/}": 460 * GiB, "net.rx{en0}": 125000, "net.tx{en0}": 48000, "gpu.usage": 7, uptime: 432000, "procs.count": 512,36  });37  const availability = JSON.stringify({ "thermal.temp": "unsupported", "power.draw": "unsupported", "battery.charge": "unsupported" });38  const processes = JSON.stringify([39    { pid: 4242, name: "e2e-worker", user: "e2e", cpu: 37.5, rss: 512 * 1024 * 1024 },40    { pid: 4243, name: "e2e-idle", user: "e2e", cpu: 0.4, rss: 64 * 1024 * 1024 },41  ]);42  const inventory = JSON.stringify({ interfaces: [{ name: "en0", kind: "ethernet", mac: "02:00:00:e2:e0:01", addrs: ["192.0.2.11"], media: "1000baseT", up: true, default: true }] });43  sql(`44    insert into nodes(id, hardware_uuid, alias, kind, hostname, site, role, environment, tags, model, chip, arch, cpu_cores, cpu_levels, gpu_cores, mem_bytes, storage_bytes, os_name, os_version, os_build, primary_ip, registered_via, inventory, inventory_at, last_seen_at) values45      ('${NODE.fresh.id}', 'E2E00000-0000-0000-0000-000000000001', '${NODE.fresh.alias}', 'mac', 'e2e-node-01.test', 'e2e-lab', 'worker', 'test', '{e2e,synthetic}', 'Mac18,5', 'Apple M6', 'arm64', 12,46        '[{"name":"Super","cores":2},{"name":"Performance","cores":4},{"name":"Efficiency","cores":6}]', 12, ${24 * GiB}, ${460 * GiB}, 'macOS', '27.0', '27A000', '192.0.2.11', 'agent', '${inventory}', now(), now()),47      ('${NODE.stale.id}', 'E2E00000-0000-0000-0000-000000000002', '${NODE.stale.alias}', 'mac', 'e2e-node-02.test', 'e2e-lab', 'gateway', 'test', '{e2e}', 'Mac16,10', 'Apple M4', 'arm64', 10,48        '[{"name":"Performance","cores":4},{"name":"Efficiency","cores":6}]', 10, ${16 * GiB}, ${228 * GiB}, 'macOS', '26.7', '26H000', '192.0.2.12', 'agent', '{}', now(), now() - interval '60 seconds'),49      ('${NODE.offline.id}', null, '${NODE.offline.alias}', 'linux', '', 'e2e-edge', 'gateway', 'test', '{e2e}', null, null, null, null, null, null, null, null, null, null, null, null, 'manual', '{}', null, null)50    on conflict (id) do nothing;5152    insert into agents(id, node_id, cert_serial, cert_fingerprint, cert_not_after, version, os, capabilities, last_seen_at, overhead) values53      ('0e2e0000-0000-7000-8000-0000000000c1', '${NODE.fresh.id}', 'e2e-serial-01', decode(repeat('ab', 32), 'hex'), now() + interval '30 days', '1.0.0-e2e', 'darwin/arm64',54       '{inventory.refresh,diagnostic.run,service.health_check,service.logs.tail,service.restart}', now(), '{"cpu_pct":0.4,"rss_bytes":31457280}')55    on conflict (id) do nothing;5657    insert into node_state(node_id, ts, received_at, values, availability, processes, processes_at) values58      ('${NODE.fresh.id}', now(), now(), '${values}', '${availability}', '${processes}', now()),59      ('${NODE.stale.id}', now() - interval '60 seconds', now() - interval '60 seconds', '{"cpu.usage": 12.0, "load.1": 0.5}', '{}', null, null)60    on conflict (node_id) do nothing;6162    do $$63    declare d date;64    begin65      for d in select generate_series((now() at time zone 'utc')::date - 1, (now() at time zone 'utc')::date + 1, interval '1 day')::date loop66        execute format('create table if not exists samples_raw_p%s partition of samples_raw for values from (%L) to (%L)', to_char(d, 'YYYYMMDD'), d::text || ' 00:00:00+00', (d + 1)::text || ' 00:00:00+00');67      end loop;68    end $$;6970    -- cpu.usage (catalog id 1) every 10 s over the last 50 minutes, with a 6-minute hole (agent offline)71    insert into samples_raw(node_id, metric_id, label, ts, value)72      select '${NODE.fresh.id}', 1, '', t, 40 + 25 * sin(extract(epoch from t) / 300.0)73      from generate_series(date_trunc('second', now()) - interval '50 minutes', date_trunc('second', now()), interval '10 seconds') t74      where t not between now() - interval '26 minutes' and now() - interval '20 minutes'75    on conflict do nothing;7677    insert into services(id, node_id, manager, name, state, pid, restarts, mem_bytes, cpu_pct, uptime_s, version, ports, app, public_url, health, managed, last_seen_at) values78      ('${SERVICE.api}', '${NODE.fresh.id}', 'pm2', 'e2e-api', 'online', 5001, 3, ${200 * 1024 * 1024}, 2.5, 86400, '1.2.3', '{8700}', 'e2e-app', 'https://e2e-app.example.test', '{"status":"ok","latency_ms":12,"checked_at":"2026-11-18T00:00:00Z"}', true, now()),79      ('${SERVICE.agentd}', '${NODE.fresh.id}', 'launchd', 'test.e2e.agentd', 'errored', null, 17, null, null, null, null, '{}', null, null, null, false, now())80    on conflict (id) do nothing;8182    insert into inference_servers(id, node_id, kind, port, model, quantization, pid, rss_bytes, uptime_s, disk_bytes, health, health_at, health_latency_ms, listed_models, last_seen_at) values83      ('${MODEL.mlx}', '${NODE.fresh.id}', 'mlx_lm', 8081, 'e2e-org/E2E-Model-7B-4bit', '4bit', 6001, ${5 * GiB}, 7200, ${4 * GiB}, 'ok', now(), 35, '["e2e-org/E2E-Model-7B-4bit"]', now())84    on conflict (id) do nothing;8586    insert into volumes(id, node_id, mount, device, fs, kind, total_bytes, used_bytes, smart_status, last_seen_at) values87      ('0e2e0000-0000-7000-8000-0000000000d1', '${NODE.fresh.id}', '/', 'disk3s1', 'apfs', 'internal', ${460 * GiB}, ${120 * GiB}, 'Verified', now()),88      ('0e2e0000-0000-7000-8000-0000000000d2', '${NODE.fresh.id}', '/Volumes/e2e-ssd', 'disk5s1', 'apfs', 'external', ${926 * GiB}, ${900 * GiB}, null, now())89    on conflict (id) do nothing;9091    insert into nas_targets(id, name, host, share, protocol, note, state, state_at) values92      ('0e2e0000-0000-7000-8000-0000000000e1', 'e2e-nas', '192.0.2.173', 'e2e', 'smb', 'synthetic e2e target', 'offline', now())93    on conflict (id) do nothing;9495    insert into backups(id, name, node_id, kind, last_status, detail) values96      ('0e2e0000-0000-7000-8000-0000000000f1', 'e2e-backup', '${NODE.fresh.id}', 'app', 'unknown', 'never observed')97    on conflict (id) do nothing;98  `);99}100101/** Inserts an event exactly as a backend service would: outbox row + NOTIFY. */102export function emitEvent(topic: string, payload: unknown): void {103  sql(`insert into events_outbox(topic, payload) values ('${topic}', '${JSON.stringify(payload).replace(/'/g, "''")}'); select pg_notify('mcc_events', '');`);104}105
Files main
Menu

Theme