import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { getSettings } from "yt-dlp-transcript-common/lib/settings"; import { getRegistry } from "yt-dlp-transcript-common/jobs/registry"; import { readSpaceSuggestions } from "yt-dlp-transcript-common/controller/spaceScan"; import { INTERNAL_LOCATION_ID, volumeFreeBytes } from "yt-dlp-transcript-common/controller/storageLocations"; import { SPACE_SCAN_JOB_KIND, diskNeedOrder, fullestDiskId, groupSuggestions, leadKindsFor, suggestionsForDisk, type SpaceScanResult, } from "yt-dlp-transcript-common/lib/spaceSuggestions"; // WHAT /cleanup/space, its summary line and `get space-suggestions` read: the // scan's file (never a walk, a stat of the corpus or an ffprobe), the live // free space of each disk (two syscalls a location — tables never probe), and // whether a scan is queued or running. export type SpaceScanJob = { jobId: string; status: string } | null; export function spaceScanJob(): SpaceScanJob { const j = getRegistry() .list() .filter((r) => r.kind === SPACE_SCAN_JOB_KIND && (r.status === "queued" || r.status === "running")) .sort((a, b) => (b.queuedAt ?? 0) - (a.queuedAt ?? 0))[0]; return j ? { jobId: j.id, status: j.status } : null; } // Free bytes now, by the scan's disk ids: `corpus` is the corpus volume, a // location's disk takes its first location id. export async function liveFreeByDisk(result: SpaceScanResult | null): Promise> { if (!result) return {}; const paths = getPaths(); const free = await volumeFreeBytes({ paths, locations: getSettings().storage.locations }).catch( () => ({}) as Record, ); const out: Record = {}; for (const d of result.disks) { if (d.status !== "ok") continue; const v = d.id === "corpus" ? free[INTERNAL_LOCATION_ID] : d.locationIds.map((id) => free[id]).find((x) => x !== undefined); if (v !== undefined) out[d.id] = v; } return out; } export async function loadSpace(): Promise<{ result: SpaceScanResult | null; live: Record; job: SpaceScanJob; }> { const result = await readSpaceSuggestions(getPaths()); return { result, live: await liveFreeByDisk(result), job: spaceScanJob() }; } // The read the ops route answers: the file less its probe memo, the disks in // order of need, the suggestions for one disk (default: the fullest). export function spaceView(result: SpaceScanResult, disk: string | null) { const { probes: _probes, ...rest } = result; void _probes; const id = disk ?? fullestDiskId(result.disks, result.floorBytes); const suggestions = id ? suggestionsForDisk(result, id) : []; return { ...rest, disks: diskNeedOrder(result.disks, result.floorBytes), disk: id, // The page's groups, in its order (a lead kind first, then by subtotal). groups: groupSuggestions(suggestions, { lead: id ? leadKindsFor(id) : [] }).map((g) => ({ kind: g.kind, label: g.label, bytes: g.bytes, count: g.rows.length, ...(g.laterHigh ? { later: [g.laterLow, g.laterHigh] } : {}), })), suggestions, }; }