/** * What each kind of integration has that is read on a schedule and stored (a "sync") — in one place, so that adding an integration, changing one, taking one away and asking for it to be read *now* all * agree on which kinds have one. A kind that is not here (Proxmox, Synology, Tailscale, Gitea, Semaphore, Uptime Kuma, Proxmox Backup Server, osTicket, phpIPAM, Nginx Proxy Manager) has nothing stored: * its pages and the health pass read it live, every time. */ import type { IntegrationType } from "../db/schema.js"; import { loadIntegrationConfig } from "../integrations/loadIntegration.js"; import { createIntegrationAdapter } from "../integrations/registry.js"; import { ARR_KINDS } from "../integrations/arr/adapter.js"; import { DOWNLOAD_KINDS } from "../integrations/downloads/adapter.js"; import { invalidateAlertsCache } from "./alerts.js"; import { forgetAdguard, syncAdguard } from "./adguardSync.js"; import { forgetArr, syncArr } from "./arrSync.js"; import { forgetAuthentik, syncAuthentik } from "./authentikSync.js"; import { forgetDockerVulns, syncDockerVulns } from "./dockerVulnSync.js"; import { forgetDownload, syncDownload } from "./downloadsSync.js"; import { forgetFragnet, syncFragnet } from "./fragnetSync.js"; import { forgetGrafana, syncGrafana } from "./grafanaSync.js"; import { forgetHetzner, syncHetzner } from "./hetznerSync.js"; import { forgetRobot, syncRobot } from "./hetznerRobotSync.js"; import { removeHomeAssistantData, syncHomeAssistant } from "./homeAssistantSync.js"; import { forgetImmich, syncImmich } from "./immichSync.js"; import { forgetJellyfin, syncJellyfin } from "./jellyfinSync.js"; import { forgetN8n, syncN8n } from "./n8nSync.js"; import { forgetNetBox, syncNetBox } from "./netboxSync.js"; import { forgetNextcloud, syncNextcloud } from "./nextcloudSync.js"; import { forgetPaperless, syncPaperless } from "./paperlessSync.js"; import { forgetPrometheus, syncPrometheus } from "./prometheusSync.js"; import { forgetSpeedtest, syncSpeedtest } from "./speedtestSync.js"; import { forgetTechnitium, syncTechnitium } from "./technitiumSync.js"; import { removeIntegrationData, syncUnifiIntegration } from "./unifiSync.js"; import { forgetUps, syncUps } from "./upsSync.js"; /** How a read went, as every sync says it (some say more, which is not needed here). */ export interface ReadStatus { at?: string; ok: boolean; error: string | null; } interface Entry { sync: (id: number) => Promise; forget: (id: number) => Promise; } const BY_TYPE: Partial> = { unifi: { sync: syncUnifiIntegration, forget: removeIntegrationData }, homeassistant: { sync: syncHomeAssistant, forget: removeHomeAssistantData }, ups: { sync: syncUps, forget: forgetUps }, jellyfin: { sync: syncJellyfin, forget: forgetJellyfin }, authentik: { sync: syncAuthentik, forget: forgetAuthentik }, prometheus: { sync: syncPrometheus, forget: forgetPrometheus }, immich: { sync: syncImmich, forget: forgetImmich }, paperless: { sync: syncPaperless, forget: forgetPaperless }, n8n: { sync: syncN8n, forget: forgetN8n }, netbox: { sync: syncNetBox, forget: forgetNetBox }, hetzner: { sync: syncHetzner, forget: forgetHetzner }, hetznerrobot: { sync: syncRobot, forget: forgetRobot }, speedtest: { sync: syncSpeedtest, forget: forgetSpeedtest }, fragnet: { sync: syncFragnet, forget: forgetFragnet }, adguard: { sync: syncAdguard, forget: forgetAdguard }, technitium: { sync: syncTechnitium, forget: forgetTechnitium }, grafana: { sync: syncGrafana, forget: forgetGrafana }, nextcloud: { sync: syncNextcloud, forget: forgetNextcloud }, dockhand: { sync: (id) => syncDockerVulns(id), forget: forgetDockerVulns }, }; for (const k of ARR_KINDS) BY_TYPE[k] = { sync: (id) => syncArr(id), forget: forgetArr }; for (const k of DOWNLOAD_KINDS) BY_TYPE[k] = { sync: (id) => syncDownload(id), forget: forgetDownload }; /** Whether this kind of integration has a reading that is stored. */ export const hasStoredReading = (type: IntegrationType): boolean => BY_TYPE[type] !== undefined; /** A new integration, or one whose settings changed or that was switched on, is read at once (not waited for: a slow system must not slow the request), so what it shows and says is there when the page is opened. */ export function readAfterChange(type: IntegrationType, id: number): void { const entry = BY_TYPE[type]; if (entry) void entry.sync(id).then(() => invalidateAlertsCache()); } /** An integration that is being taken away: what was read from it goes too. */ export async function forgetReading(type: IntegrationType, id: number): Promise { await BY_TYPE[type]?.forget(id); } /** Wait this long between two reads of the same integration asked for by hand: some systems limit how often they may be asked (Hetzner Robot: 200 requests an hour). */ export const READ_NOW_COOLDOWN_MS = 20_000; const lastAsked = new Map(); export type ReadNowResult = | { state: "read"; kind: "stored" | "live"; ok: boolean; error: string | null; at: string; latencyMs: number | null } | { state: "not_found" } | { state: "disabled" } | { state: "too_soon"; retryAfterSeconds: number }; /** * Reads one integration now. A kind with a stored reading is read the way its schedule reads it, and what it says is judged again at once; a kind that is read live (nothing stored) is asked whether it * answers — the same question *Test* asks — which also brings its Working/Failing status up to date, and the alerts, which read it live, are worked out again at the next look. Never throws. */ export async function readIntegrationNow(id: number, now: number = Date.now()): Promise { const loaded = await loadIntegrationConfig(id); if (!loaded) return { state: "not_found" }; if (!loaded.integration.enabled) return { state: "disabled" }; const before = lastAsked.get(id); if (before !== undefined && now - before < READ_NOW_COOLDOWN_MS) return { state: "too_soon", retryAfterSeconds: Math.ceil((READ_NOW_COOLDOWN_MS - (now - before)) / 1000) }; lastAsked.set(id, now); const type = loaded.integration.type; const entry = BY_TYPE[type]; const started = Date.now(); try { if (entry) { const status = await entry.sync(id); invalidateAlertsCache(); return { state: "read", kind: "stored", ok: status.ok, error: status.error, at: status.at ?? new Date().toISOString(), latencyMs: Date.now() - started }; } const result = await createIntegrationAdapter(type, loaded.config).ping(); invalidateAlertsCache(); return { state: "read", kind: "live", ok: result.ok, error: result.ok ? null : (result.error ?? "no answer"), at: new Date().toISOString(), latencyMs: result.latencyMs ?? Date.now() - started }; } catch (err) { return { state: "read", kind: entry ? "stored" : "live", ok: false, error: err instanceof Error ? err.message : String(err), at: new Date().toISOString(), latencyMs: null }; } }