ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/alarms/checks/l02_one_open.ts

checks/l02_one_open.ts

30 líneas, 1.6 KB.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. // ALR-R02. One open alarm per watch: while an alarm of a watch is open, the same trouble is that alarm, not a new
  2. // one. Drills are counted apart from real alarms. Every open alarm after the earliest of its watch is a finding.
  3. import { main } from '../../collections/checks/lib/runner.ts';
  4. import { type Project, type Rec, type Result, addr, phase, recordsOf, text, time } from '../../agents/lib/factlib.ts';
  5. export const RULE = {
  6. id: 'ALR-R02', title: 'One open alarm per watcher',
  7. scope: 'open alarms',
  8. };
  9. export function check(p: Project, res: Result): void {
  10. const open = recordsOf(p, 'ALARM').filter((r) => phase(r.fm.status) === 'open' && text(r.fm.watch));
  11. res.scope = open.map((r) => addr(r.id));
  12. res.scopeText = `open alarms ${p.coll('ALARM') || '—'} (${open.length})`;
  13. if (!res.part('alarms', p.coll('ALARM') ? null : 'no alarms collection')) return;
  14. const groups = new Map<string, Rec[]>();
  15. for (const r of open) {
  16. const key = `${text(r.fm.watch)}${r.fm.drill === true ? ' (drill)' : ''}`;
  17. groups.set(key, [...(groups.get(key) ?? []), r]);
  18. }
  19. for (const [watch, recs] of groups) {
  20. if (recs.length < 2) continue;
  21. const sorted = [...recs].sort((a, b) => (time(a.fm.foundAt) ?? Infinity) - (time(b.fm.foundAt) ?? Infinity) || a.id.localeCompare(b.id));
  22. const first = addr(sorted[0].id);
  23. for (const r of sorted.slice(1))
  24. 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');
  25. }
  26. }
  27. if (import.meta.main) main({ RULE, check });