package file→ .shtukbase/packages/alarms/checks/l02_one_open.ts
checks/l02_one_open.ts
30 lines, 1.6 KB.
- // ALR-R02. One open alarm per watch: while an alarm of a watch is open, the same trouble is that alarm, not a new
- // one. Drills are counted apart from real alarms. Every open alarm after the earliest of its watch is a finding.
- import { main } from '../../collections/checks/lib/runner.ts';
- import { type Project, type Rec, type Result, addr, phase, recordsOf, text, time } from '../../agents/lib/factlib.ts';
- export const RULE = {
- id: 'ALR-R02', title: 'One open alarm per watcher',
- scope: 'open alarms',
- };
- export function check(p: Project, res: Result): void {
- const open = recordsOf(p, 'ALARM').filter((r) => phase(r.fm.status) === 'open' && text(r.fm.watch));
- res.scope = open.map((r) => addr(r.id));
- res.scopeText = `open alarms ${p.coll('ALARM') || '—'} (${open.length})`;
- if (!res.part('alarms', p.coll('ALARM') ? null : 'no alarms collection')) return;
- const groups = new Map<string, Rec[]>();
- for (const r of open) {
- const key = `${text(r.fm.watch)}${r.fm.drill === true ? ' (drill)' : ''}`;
- groups.set(key, [...(groups.get(key) ?? []), r]);
- }
- for (const [watch, recs] of groups) {
- if (recs.length < 2) continue;
- const sorted = [...recs].sort((a, b) => (time(a.fm.foundAt) ?? Infinity) - (time(b.fm.foundAt) ?? Infinity) || a.id.localeCompare(b.id));
- const first = addr(sorted[0].id);
- for (const r of sorted.slice(1))
- res.hole(addr(r.id), `watcher ${watch} already has an open alarm ${first}`, `close this one as a repeat (status: dismissed, closedBy: "repeat of ${first}") or close the earlier one`, 'second-open');
- }
- }
- if (import.meta.main) main({ RULE, check });