|
| 1 | +import { writeFileSync, mkdirSync, readFileSync, existsSync } from "node:fs"; |
| 2 | +import { createHash } from "node:crypto"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { ROOT } from "./lib/registry.mjs"; |
| 5 | + |
| 6 | +const SNAPSHOTS_DIR = join(ROOT, "snapshots"); |
| 7 | +const REGISTRY_EVIDENCE_DIR = join(ROOT, "registry", "evidence"); |
| 8 | + |
| 9 | +function isoWeek(date) { |
| 10 | + const d = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate())); |
| 11 | + const dayNum = (d.getUTCDay() + 6) % 7; |
| 12 | + d.setUTCDate(d.getUTCDate() - dayNum + 3); |
| 13 | + const firstThursday = new Date(Date.UTC(d.getUTCFullYear(), 0, 4)); |
| 14 | + const week = 1 + Math.round(((d - firstThursday) / 86400000 - 3 + ((firstThursday.getUTCDay() + 6) % 7)) / 7); |
| 15 | + return `${d.getUTCFullYear()}-W${String(week).padStart(2, "0")}`; |
| 16 | +} |
| 17 | + |
| 18 | +async function fetchJson(url, headers = {}) { |
| 19 | + try { |
| 20 | + const res = await fetch(url, { headers, signal: AbortSignal.timeout(15000) }); |
| 21 | + if (!res.ok) return { ok: false, status: res.status, url }; |
| 22 | + return { ok: true, data: await res.json(), url }; |
| 23 | + } catch (e) { |
| 24 | + return { ok: false, error: e.message, url }; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +function sha256(text) { |
| 29 | + return createHash("sha256").update(text).digest("hex"); |
| 30 | +} |
| 31 | + |
| 32 | +// Aggiorna last_seen con una sostituzione mirata sul testo grezzo, non un |
| 33 | +// dump YAML completo: preserva formattazione, commenti e ordine dei campi |
| 34 | +// del file esistente (un dump js-yaml li riscriverebbe tutti). |
| 35 | +function updateLastSeen(evdId, dateStr) { |
| 36 | + const file = join(REGISTRY_EVIDENCE_DIR, `${evdId}.yaml`); |
| 37 | + if (!existsSync(file)) return false; |
| 38 | + let text = readFileSync(file, "utf8"); |
| 39 | + if (/^last_seen:/m.test(text)) { |
| 40 | + text = text.replace(/^last_seen:.*$/m, `last_seen: ${dateStr}`); |
| 41 | + } else if (/^collection:.*$/m.test(text)) { |
| 42 | + text = text.replace(/^(collection:.*)$/m, `$1\nlast_seen: ${dateStr}`); |
| 43 | + } else { |
| 44 | + return false; |
| 45 | + } |
| 46 | + writeFileSync(file, text); |
| 47 | + return true; |
| 48 | +} |
| 49 | + |
| 50 | +async function main() { |
| 51 | + const ghHeaders = process.env.GITHUB_TOKEN |
| 52 | + ? { Authorization: `Bearer ${process.env.GITHUB_TOKEN}`, Accept: "application/vnd.github+json" } |
| 53 | + : { Accept: "application/vnd.github+json" }; |
| 54 | + |
| 55 | + const today = new Date(); |
| 56 | + const week = isoWeek(today); |
| 57 | + const dir = join(SNAPSHOTS_DIR, week); |
| 58 | + mkdirSync(dir, { recursive: true }); |
| 59 | + |
| 60 | + const results = {}; |
| 61 | + const evdHits = new Set(); |
| 62 | + |
| 63 | + results.status = await fetchJson("https://imgauth.spaziogenesi.org/api/status"); |
| 64 | + if (results.status.ok) evdHits.add("EVD-status-live"); |
| 65 | + |
| 66 | + results["status-history"] = await fetchJson("https://imgauth.spaziogenesi.org/api/status-history"); |
| 67 | + if (results["status-history"].ok) evdHits.add("EVD-r2-status-history"); |
| 68 | + |
| 69 | + const healthLog = {}; |
| 70 | + for (let i = 0; i < 7; i++) { |
| 71 | + const d = new Date(today); |
| 72 | + d.setUTCDate(d.getUTCDate() - i); |
| 73 | + const day = d.toISOString().slice(0, 10); |
| 74 | + healthLog[day] = await fetchJson(`https://imgauth.spaziogenesi.org/api/health-log?day=${day}`); |
| 75 | + } |
| 76 | + results["health-log"] = healthLog; |
| 77 | + if (Object.values(healthLog).some((r) => r.ok)) evdHits.add("EVD-d1-health-log"); |
| 78 | + |
| 79 | + results["ping-imgauth"] = await fetchJson("https://imgauth.spaziogenesi.org/ping"); |
| 80 | + if (results["ping-imgauth"].ok) evdHits.add("EVD-versions-live"); |
| 81 | + |
| 82 | + results["monitor-issues"] = await fetchJson( |
| 83 | + "https://api.github.com/repos/SPAZIO-GENESI/imgauth/issues?labels=status-alert&state=all&per_page=20", |
| 84 | + ghHeaders |
| 85 | + ); |
| 86 | + if (results["monitor-issues"].ok) evdHits.add("EVD-monitor-issues"); |
| 87 | + |
| 88 | + results["git-imgauth"] = await fetchJson("https://api.github.com/repos/SPAZIO-GENESI/imgauth/commits?per_page=5", ghHeaders); |
| 89 | + if (results["git-imgauth"].ok) evdHits.add("EVD-git-imgauth"); |
| 90 | + |
| 91 | + results["git-imgauthweb"] = await fetchJson("https://api.github.com/repos/SPAZIO-GENESI/imgauthweb/commits?per_page=5", ghHeaders); |
| 92 | + if (results["git-imgauthweb"].ok) evdHits.add("EVD-git-imgauthweb"); |
| 93 | + |
| 94 | + const manifest = { collected_at: today.toISOString(), week, files: {} }; |
| 95 | + for (const [name, data] of Object.entries(results)) { |
| 96 | + const filename = `${name}.json`; |
| 97 | + const text = JSON.stringify(data, null, 2) + "\n"; |
| 98 | + writeFileSync(join(dir, filename), text); |
| 99 | + manifest.files[filename] = sha256(text); |
| 100 | + } |
| 101 | + writeFileSync(join(dir, "manifest.json"), JSON.stringify(manifest, null, 2) + "\n"); |
| 102 | + |
| 103 | + const todayStr = today.toISOString().slice(0, 10); |
| 104 | + let updated = 0; |
| 105 | + for (const id of evdHits) { |
| 106 | + if (updateLastSeen(id, todayStr)) updated++; |
| 107 | + } |
| 108 | + |
| 109 | + console.log(`Snapshot ${week} scritto in ${dir}: ${Object.keys(results).length} file, ${updated} evidenze con last_seen aggiornato.`); |
| 110 | +} |
| 111 | + |
| 112 | +main(); |
0 commit comments