// Dashboard (mockup Admin.html, BRIEF §14.8): storage, archive, jobs, render nodes, charts, activity, active jobs.
import { t, plural, applyStrings, culture } from "../i18n.js";
import { escapeHtml, attr, formatBytes, formatNumber, formatTime, formatDuration, bar, storageTip, writeVolumeLine, columns, toast, errorText, emptyState, redraw, fitColumns, PROXY_WARNING } from "../ui.js";
import { kindLabel, nodeLabel, progressCell, stateCell, stateLabel, encoders, targetCell, updateStatus } from "../jobs.js";
import { activityText } from "../audit.js";
const SCAN_BUTTON = ``;
// Why a folder of the archive's layout could not be created (DashboardDto.ArchiveProblem), each with its tooltip.
const ARCHIVE_PROBLEM_TIPS = {
unavailable: "Admin.Dashboard.ArchiveIncomplete.Unavailable.Tip",
denied: "Admin.Dashboard.ArchiveIncomplete.Denied.Tip",
readOnly: "Admin.Dashboard.ArchiveIncomplete.ReadOnly.Tip",
failed: "Admin.Dashboard.ArchiveIncomplete.Failed.Tip",
};
/** The warning chip of an incomplete archive layout; empty when every folder is there. */
function archiveProblemChip(problem) {
if (!problem) return "";
const tip = t(ARCHIVE_PROBLEM_TIPS[problem.code] ?? ARCHIVE_PROBLEM_TIPS.failed, problem.folder);
return ``;
}
export async function render(root, { api, setMeta, actions, onEvent }) {
let timer = null;
// The active jobs drawn, by id, so a progress event updates the bar and the status line in place.
let activeJobs = new Map();
const refresh = () => {
clearTimeout(timer);
timer = setTimeout(load, 1500);
};
async function load() {
let d;
try {
d = await api.get("/api/v1/server/dashboard");
} catch (err) {
root.innerHTML = `
${escapeHtml(errorText(err))}
`;
return;
}
redraw(root, () => draw(d));
}
function draw(d) {
const tls = `Admin.Settings.Tls.${d.tlsMode.charAt(0).toUpperCase()}${d.tlsMode.slice(1)}`;
setMeta(t("Admin.Dashboard.Meta", formatDuration(d.uptimeSeconds), d.database, t(tls),
t(d.discovery ? "Admin.Dashboard.DiscoveryOn" : "Admin.Dashboard.DiscoveryOff")));
actions.innerHTML = (d.proxyWarning ? PROXY_WARNING : "") + archiveProblemChip(d.archiveProblem);
applyStrings(actions);
// The space volume's figures (the space path when the server has one), the write volume apart when it differs.
const a = d.archive;
const used = a.capacityBytes > 0 ? (a.capacityBytes - a.freeBytes) / a.capacityBytes : 0;
const spaceTip = attr(storageTip(a));
const online = d.nodes.filter((n) => n.state !== "offline");
const uploads = lastThirtyDays(d.uploads);
const totalUploads = uploads.reduce((s, u) => s + u.value, 0);
const peak = Math.max(0, ...uploads.map((u) => u.value));
activeJobs = new Map(d.activeJobs.map((j) => [j.id, j]));
root.innerHTML = `