import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { getSettings } from "yt-dlp-transcript-common/lib/settings"; import { locationOfDataDir } from "yt-dlp-transcript-common/lib/storageLocations"; import { driveHealthView, locationHealth, type DriveHealthView, } from "yt-dlp-transcript-common/lib/storageHealth"; import { listChannelConfigs } from "yt-dlp-transcript-common/controller/channels"; import { probeAllLocations } from "yt-dlp-transcript-common/controller/storageLocations"; import { channelsHrefForLocation, probeMounted, } from "yt-dlp-transcript-common/views/storage"; // THE DASHBOARD'S DRIVE BANNER — every storage location whose drive is slow, // not answering or not mounted, and how many channels have their media there. // // NOTHING HERE ASKS A DRIVE IN-PROCESS. The health record is memory (the // health pass and the watchdog write it, lib/storageHealth.ts), and the mount // half is the same probe /storage renders from: memoised 10 s, a stalled // location answered without a call. The channel configs are read only when // some drive is in trouble — the corpus disk, one small file per channel — // because a drive nobody's media is on is not worth a banner when it is merely // unplugged. // // SERVER ONLY (it reaches the controller, which imports execa). export type DriveHealthBannerRow = DriveHealthView & { // Channels whose media is on this location (by `mediaDir`, or a legacy // channel's retired `dataDir`). channels: number; // `/channels?location=`: the list of them. channelsHref: string; }; export type DriveHealthPayload = { drives: DriveHealthBannerRow[] }; export async function buildDriveHealthPayload(): Promise { const paths = getPaths(); const locations = getSettings().storage.locations; if (locations.length === 0) return { drives: [] }; const probes = await probeAllLocations(locations, paths); const now = Date.now(); const troubled = locations .map((loc) => { const h = locationHealth(loc.id); return driveHealthView(loc, h && h.root === loc.root ? h : undefined, { now, mounted: probeMounted(probes[loc.id]?.status), }); }) .filter((v) => v.state !== "ok"); if (troubled.length === 0) return { drives: [] }; const counts = new Map(); for (const { config } of await listChannelConfigs(paths)) { const dir = config.mediaDir?.trim() || config.dataDir?.trim(); const loc = dir ? locationOfDataDir(dir, locations) : null; if (loc) counts.set(loc.id, (counts.get(loc.id) ?? 0) + 1); } return { drives: troubled .map((v) => ({ ...v, channels: counts.get(v.id) ?? 0, channelsHref: channelsHrefForLocation(v.id), })) // An unplugged drive with nobody's media on it is not news. .filter((v) => v.state !== "unmounted" || v.channels > 0), }; }