// Archive (BRIEF §14.8): naming templates, proxy policy, scan/rebuild/verify/maintenance, statistics.
import { t, plural, applyStrings } from "../i18n.js";
import { escapeHtml, attr, formatBytes, formatNumber, bar, storageTip, writeVolumeLine, columns, toast, errorText, emptyState, markInvalid, clearInvalid, invalidText, escapeReverts } from "../ui.js";
const PROXY_SIZES = [-1, 1280, 1920, 2560, 3840, 0];
const ACTIONS = ["scan", "rebuild", "verify", "maintenance"];
export async function render(root, { api, setMeta }) {
let settings;
let stats;
try {
[settings, stats] = await Promise.all([api.get("/api/v1/archive/settings"), api.get("/api/v1/archive/stats")]);
} catch (err) {
root.innerHTML = `
${escapeHtml(errorText(err))}
`;
return;
}
setMeta(settings.rootPath, { ltr: true });
// The space volume's figures (the space path when the server has one), the write volume apart when it differs.
const used = stats.capacityBytes > 0 ? (stats.capacityBytes - stats.freeBytes) / stats.capacityBytes : 0;
const spaceTip = attr(storageTip(stats));
const sizes = PROXY_SIZES.includes(settings.proxyLongEdge) ? PROXY_SIZES : [settings.proxyLongEdge, ...PROXY_SIZES];
const proxyOptions = sizes.map((size) => ``).join("");
const statsTable = (rows, labelId) => rows.length === 0 ? "" : `${rows.map((r) =>
`
${escapeHtml(r.label)}${formatNumber(r.count)}${escapeHtml(formatBytes(r.bytes))}
`).join("")}
`;
const scanButton = ``;
root.innerHTML = `
${formatNumber(stats.sequences)}
${escapeHtml(`${plural("Admin.Dashboard.PhotoCount", stats.photos)} · ${plural("Admin.Dashboard.CameraCount", stats.cameras)}`)}
${escapeHtml(formatBytes(stats.bytes))}
${escapeHtml(stats.firstYear ? `${stats.firstYear}–${stats.lastYear}` : "—")}
${escapeHtml(formatBytes(stats.freeBytes))}
${bar(used)}${escapeHtml(t("Admin.Dashboard.Of", formatBytes(stats.capacityBytes)))}${writeVolumeLine(stats)}
${formatNumber(stats.unverifiedSequences)}${escapeHtml(plural("Admin.Archive.UnverifiedUnit", stats.unverifiedSequences))}
${escapeHtml(plural("Admin.Dashboard.WithoutProxies", stats.sequencesWithoutProxies))}
${ACTIONS.map((a) => ``).join("")}
${statsTable(stats.byCamera.slice(0, 8).map((c) => ({ label: c.key || "—", count: c.count, bytes: c.bytes })), "cameras-title")
|| ``}
${stats.byYear.length === 0 ? emptyState("Admin.Dashboard.NoData", scanButton)
: columns(stats.byYear.map((y) => ({ label: y.key, value: y.bytes, title: `${y.key} · ${formatBytes(y.bytes)}` })),
{ label: t("Admin.Dashboard.ByYear"), valueLabel: (i) => formatBytes(i.value) })}
${statsTable(stats.byFormat.map((f) => ({ label: f.key.toUpperCase(), count: f.count, bytes: f.bytes })), "formats-title")
|| emptyState("Admin.Dashboard.NoData", scanButton)}
`;
applyStrings(root);
const form = root.querySelector("form");
escapeReverts(form);
form.addEventListener("submit", async (e) => {
e.preventDefault();
form.querySelectorAll("[aria-invalid]").forEach(clearInvalid);
const data = new FormData(form);
try {
settings = await api.put("/api/v1/archive/settings", {
...settings,
name: String(data.get("name")).trim(),
folderTemplate: String(data.get("folderTemplate")),
fileTemplate: String(data.get("fileTemplate")),
proxyLongEdge: Number(data.get("proxyLongEdge")),
});
// Esc now reverts to what was saved.
form.querySelectorAll("input").forEach((input) => {
input.defaultValue = input.value;
});
form.querySelectorAll("option").forEach((option) => {
option.defaultSelected = option.selected;
});
toast(t("Admin.Common.Saved"));
} catch (err) {
// Errors name the field in details; the field gets the error border and the message as its tooltip.
const field = err.details && form.elements[err.details];
const message = field ? invalidText(field) : errorText(err);
if (field) {
markInvalid(field, message);
field.focus();
}
toast(message, true);
}
});
root.querySelectorAll("[data-action]").forEach((button) => button.addEventListener("click", async () => {
button.disabled = true;
try {
await api.post(`/api/v1/archive/${button.dataset.action}`);
toast(t("Admin.Archive.Queued"));
} catch (err) {
toast(errorText(err), true);
} finally {
button.disabled = false;
button.focus();
}
}));
}
function proxyLabel(size) {
if (size === -1) return t("Admin.Archive.ProxiesOff");
if (size === 0) return t("Admin.Archive.ProxiesFull");
return t("Admin.Archive.ProxiesEdge", size);
}
function cap(s) {
return s.charAt(0).toUpperCase() + s.slice(1);
}