|
| 1 | +import { readFileSync, readdirSync, existsSync } from "node:fs"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import { loadRegistry } from "./registry.mjs"; |
| 4 | +import { TENANT_SNAPSHOTS_DIR } from "./tenant.mjs"; |
| 5 | + |
| 6 | +// Sorgente unica delle scadenze ricorrenti. Esiste perché due consumatori |
| 7 | +// devono raccontare la stessa identica storia: l'allarme Telegram |
| 8 | +// (check-cadences.mjs) e la scheda Manutenzione del pannello, alimentata dal |
| 9 | +// file pubblicato da build-cadences.mjs. Quando la logica stava solo nel |
| 10 | +// primo, il secondo avrebbe potuto dire "tutto a posto" mentre il bot |
| 11 | +// segnalava una scadenza — la classe di divergenza che il framework esiste |
| 12 | +// per evitare. |
| 13 | + |
| 14 | +const ANCHORS_DIR = join(TENANT_SNAPSHOTS_DIR, "anchors"); |
| 15 | + |
| 16 | +// Grazia oltre il mese per il ciclo di ancoraggio (GTF-ARCH §6.4): un |
| 17 | +// pacchetto mensile non cade mai a giorno fisso. |
| 18 | +export const ANCHOR_GRACE_DAYS = 35; |
| 19 | + |
| 20 | +const DAY_MS = 86400000; |
| 21 | + |
| 22 | +function isoDay(ms) { |
| 23 | + return new Date(ms).toISOString().slice(0, 10); |
| 24 | +} |
| 25 | + |
| 26 | +function toMs(dateStr) { |
| 27 | + return new Date(dateStr).getTime(); |
| 28 | +} |
| 29 | + |
| 30 | +// Ultimo pacchetto di ancoraggio committato. È la fonte di verità di quel |
| 31 | +// ciclo: nessuna data da aggiornare a mano, quindi nessuna data che possa |
| 32 | +// mentire — il pacchetto o c'è o non c'è. |
| 33 | +function latestAnchorDate() { |
| 34 | + if (!existsSync(ANCHORS_DIR)) return null; |
| 35 | + const bundles = readdirSync(ANCHORS_DIR) |
| 36 | + .filter((f) => /^\d{4}-\d{2}-bundle\.json$/.test(f)) |
| 37 | + .sort(); |
| 38 | + if (bundles.length === 0) return null; |
| 39 | + try { |
| 40 | + const data = JSON.parse(readFileSync(join(ANCHORS_DIR, bundles[bundles.length - 1]), "utf8")); |
| 41 | + return data.generated_at ?? null; |
| 42 | + } catch { |
| 43 | + return null; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +function registryUrl(cfg, rel) { |
| 48 | + const base = cfg.operations?.registry_blob_base; |
| 49 | + return base ? `${base}/${rel.split("\\").join("/")}` : null; |
| 50 | +} |
| 51 | + |
| 52 | +function makeItem({ id, title, record, rel, cfg, lastDone, dueMs, neverRun }) { |
| 53 | + const now = Date.now(); |
| 54 | + const overdueDays = dueMs !== null && now > dueMs ? Math.floor((now - dueMs) / DAY_MS) : null; |
| 55 | + return { |
| 56 | + id, |
| 57 | + title, |
| 58 | + who: record?.who ?? null, |
| 59 | + needs_technical: record?.needs_technical ?? null, |
| 60 | + duration_minutes: record?.duration_minutes ?? null, |
| 61 | + frequency_days: record?.frequency_days ?? null, |
| 62 | + last_done: lastDone, |
| 63 | + never_run: Boolean(neverRun), |
| 64 | + next_due: dueMs !== null ? isoDay(dueMs) : null, |
| 65 | + overdue_days: overdueDays, |
| 66 | + steps: record?.steps ?? [], |
| 67 | + registry_url: rel ? registryUrl(cfg, rel) : null, |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +// Elenco completo delle manutenzioni ricorrenti, scadute e non, ordinate per |
| 72 | +// urgenza. Chi vuole solo le scadute filtra su overdue_days !== null: la |
| 73 | +// scheda del pannello ha bisogno anche delle altre, per dire "nulla da fare" |
| 74 | +// invece di non dire niente. |
| 75 | +export function computeCadences(cfg, records = loadRegistry()) { |
| 76 | + const birth = cfg.operations?.birth_date; |
| 77 | + const items = []; |
| 78 | + |
| 79 | + const processes = [...records.values()].filter((r) => r.folder === "processes"); |
| 80 | + const byId = new Map(processes.map((r) => [r.record.id, r])); |
| 81 | + |
| 82 | + // 1. Il ciclo di ancoraggio: la cadenza si legge dai pacchetti, i passi dal |
| 83 | + // processo che lo descrive. Due sorgenti perché sono due cose diverse — |
| 84 | + // quando è stato fatto, e come si fa. |
| 85 | + const anchorProcessId = cfg.operations?.anchor_process_id; |
| 86 | + const anchorEntry = anchorProcessId ? byId.get(anchorProcessId) : null; |
| 87 | + if (anchorEntry) { |
| 88 | + const anchorDate = latestAnchorDate(); |
| 89 | + items.push( |
| 90 | + makeItem({ |
| 91 | + id: anchorEntry.record.id, |
| 92 | + title: anchorEntry.record.title, |
| 93 | + record: anchorEntry.record, |
| 94 | + rel: anchorEntry.rel, |
| 95 | + cfg, |
| 96 | + lastDone: anchorDate ? isoDay(toMs(anchorDate)) : null, |
| 97 | + dueMs: anchorDate ? toMs(anchorDate) + ANCHOR_GRACE_DAYS * DAY_MS : 0, |
| 98 | + neverRun: !anchorDate, |
| 99 | + }) |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + // 2. I processi a cadenza dichiarata. Senza last_run si parte dalla nascita |
| 104 | + // del registro: un processo mai eseguito deve risultare scaduto, non |
| 105 | + // perennemente "non ancora dovuto". |
| 106 | + for (const entry of processes) { |
| 107 | + const p = entry.record; |
| 108 | + if (!p.frequency_days) continue; |
| 109 | + const ref = p.last_run ?? birth; |
| 110 | + items.push( |
| 111 | + makeItem({ |
| 112 | + id: p.id, |
| 113 | + title: p.title, |
| 114 | + record: p, |
| 115 | + rel: entry.rel, |
| 116 | + cfg, |
| 117 | + lastDone: p.last_run ?? null, |
| 118 | + dueMs: ref ? toMs(ref) + p.frequency_days * DAY_MS : 0, |
| 119 | + neverRun: !p.last_run, |
| 120 | + }) |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + items.sort((a, b) => { |
| 125 | + if (a.next_due === b.next_due) return a.id.localeCompare(b.id); |
| 126 | + if (a.next_due === null) return -1; |
| 127 | + if (b.next_due === null) return 1; |
| 128 | + return a.next_due < b.next_due ? -1 : 1; |
| 129 | + }); |
| 130 | + return items; |
| 131 | +} |
| 132 | + |
| 133 | +export function overdueOnly(items) { |
| 134 | + return items.filter((i) => i.overdue_days !== null); |
| 135 | +} |
0 commit comments