Files
Chronos/src/Chronos.Server/Admin/web/pages/dashboard.js
T
Alby96andClaude Opus 5.5 1f089888e8
CI / Build and test (push) Successful in 5m11s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 6s
Release / Build, test and publish (push) Successful in 8m57s
CI / PostgreSQL 16 integration (push) Successful in 3m10s
CI / PostgreSQL 17 integration (push) Successful in 3m30s
CI / Docker image (push) Successful in 49s
Release / Server image (push) Successful in 1m4s
feat: owner review 2 phase 1: :latest, Unraid array space, Settings with About, whole-folder import (#9)
- :latest follows the highest stable release, or the highest pre-release while none is stable (Chronos.Tools release-channels); bad tags are refused before anything is built.
- Unraid: CHRONOS_SPACE_PATH and the template's Array view show the array's free space; the write volume (cache) is shown apart; completions check the archive volume (507).
- Settings is a gear, docked at the foot of the admin sidebar; About is the last part of Settings on the desktop and the panel.
- Import > Folder > Whole folder: one sequence per format (desktop only).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-27 01:09:59 +01:00

156 lines
9.7 KiB
JavaScript

// 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 } from "../jobs.js";
import { activityText } from "../audit.js";
const SCAN_BUTTON = `<button class="btn small" data-scan data-i18n="Admin.Archive.Scan" data-i18n-title="Admin.Archive.Scan.Tip"></button>`;
export async function render(root, { api, setMeta, actions, onEvent }) {
let timer = null;
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 = `<div class="panel empty">${escapeHtml(errorText(err))}</div>`;
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 : "";
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));
root.innerHTML = `
<div class="tiles">
<section class="panel tile" aria-labelledby="tile-storage">
<h2 class="label" id="tile-storage" data-i18n="Admin.Dashboard.Storage"></h2>
<span class="value" title="${spaceTip}">${escapeHtml(formatBytes(a.capacityBytes - a.freeBytes))}<small>${escapeHtml(t("Admin.Dashboard.Of", formatBytes(a.capacityBytes)))}</small></span>
${bar(used)}
<span class="sub" title="${spaceTip}">${escapeHtml(t("Admin.Dashboard.Free", formatBytes(a.freeBytes)))} · ${escapeHtml(t("Admin.Dashboard.Originals", formatBytes(a.bytes)))}</span>
${writeVolumeLine(a)}
</section>
<section class="panel tile" aria-labelledby="tile-archive">
<h2 class="label" id="tile-archive" data-i18n="Admin.Dashboard.Archive"></h2>
<span class="value num">${formatNumber(a.sequences)}<small>${escapeHtml(plural("Admin.Dashboard.SequenceUnit", a.sequences))}</small></span>
<span class="sub">${escapeHtml(`${plural("Admin.Dashboard.PhotoCount", a.photos)} · ${plural("Admin.Dashboard.CameraCount", a.cameras)}`)}</span>
<span class="sub">${escapeHtml(plural("Admin.Dashboard.WithoutProxies", a.sequencesWithoutProxies))}</span>
</section>
<section class="panel tile" aria-labelledby="tile-jobs">
<h2 class="label" id="tile-jobs" data-i18n="Admin.Dashboard.Jobs"></h2>
<span class="value num">${formatNumber(d.jobs.running)}<small data-i18n="Admin.Dashboard.Running"></small></span>
<span class="sub">${escapeHtml([plural("Admin.Dashboard.QueuedCount", d.jobs.queued), plural("Admin.Dashboard.DoneToday", d.jobs.doneToday),
plural("Admin.Dashboard.FailedToday", d.jobs.failedToday)].join(" · "))}</span>
<span class="sub">${escapeHtml([plural("Admin.Dashboard.Parallelism", d.parallelism), ...(d.priorityQueue ? [t("Admin.Dashboard.PriorityQueue")] : [])].join(" · "))}</span>
</section>
<section class="panel tile" aria-labelledby="tile-nodes">
<h2 class="label" id="tile-nodes" data-i18n="Admin.Dashboard.Nodes"></h2>
<span class="value num">${formatNumber(online.length)}<small data-i18n="Admin.Dashboard.Online"></small></span>
${online.slice(0, 2).map((n) => `<span class="sub"><bdi>${escapeHtml(n.clientName)}</bdi> · ${escapeHtml(encoders(n))} · ${escapeHtml(stateLabel(n.state))}</span>`).join("")}
</section>
</div>
<div class="grid two charts">
<section class="panel">
<div class="panel-head"><h2 data-i18n="Admin.Dashboard.ByYear"></h2></div>
${a.byYear.length === 0 ? emptyState("Admin.Dashboard.NoData", SCAN_BUTTON)
: columns(a.byYear.map((y) => ({ label: y.key, value: y.bytes, title: `${y.key} · ${formatBytes(y.bytes)} · ${formatNumber(y.count)}` })),
{ label: t("Admin.Dashboard.ByYear"), valueLabel: (i) => formatBytes(i.value) })}
</section>
<section class="panel">
<div class="panel-head"><h2 data-i18n="Admin.Dashboard.Uploads"></h2>
<span class="muted ellipsis">${escapeHtml(t("Admin.Dashboard.UploadTotals", formatNumber(totalUploads), formatNumber(peak)))}</span></div>
${columns(uploads, { label: t("Admin.Dashboard.Uploads"), sun: true, edgeLabels: true })}
</section>
</div>
<div class="grid wide-right">
<section class="panel">
<div class="panel-head"><h2 data-i18n="Admin.Dashboard.Activity"></h2><a href="#/logs?view=audit" data-i18n="Admin.Dashboard.AllLogs" data-i18n-title="Admin.Dashboard.AllLogs.Tip"></a></div>
${d.activity.length === 0 ? emptyState("Admin.Dashboard.NoActivity", `<button class="btn small" id="reload" data-i18n="Admin.Common.Refresh" data-i18n-title="Admin.Dashboard.Refresh.Tip"></button>`)
: `<ul class="activity">${d.activity.map((e) => `<li><time class="muted num" datetime="${attr(e.at)}">${escapeHtml(formatTime(e.at))}</time><span dir="auto">${escapeHtml(activityText(e))}</span></li>`).join("")}</ul>`}
</section>
<section class="panel">
<div class="panel-head"><h2 data-i18n="Admin.Dashboard.Jobs"></h2><a href="#/jobs" data-i18n="Admin.Dashboard.Queue" data-i18n-title="Admin.Dashboard.Queue.Tip"></a></div>
${d.activeJobs.length === 0 ? emptyState("Admin.Jobs.Empty", SCAN_BUTTON) : `
<div class="table jobs-mini" data-levels="1" role="table" aria-label="${attr(t("Admin.Dashboard.Jobs"))}">
<div class="thead" role="row"><span role="columnheader" data-i18n="Admin.Jobs.Kind"></span><span role="columnheader" data-i18n="Admin.Jobs.Target"></span>
<span role="columnheader" class="col-node" data-i18n="Admin.Jobs.Node"></span><span role="columnheader" data-i18n="Admin.Jobs.Progress"></span>
<span role="columnheader" data-i18n="Admin.Jobs.State"></span><span role="columnheader"><span class="sr-only" data-i18n="Admin.Common.Actions"></span></span></div>
<div role="rowgroup">${d.activeJobs.map((j) => `
<div class="row" role="row" data-key="${attr(j.id)}">
<span class="ellipsis" role="cell">${escapeHtml(kindLabel(j.kind))}</span>
<span class="ellipsis" role="cell" dir="auto" title="${attr(j.targetLabel ?? "")}">${escapeHtml(j.targetLabel ?? "")}</span>
<span class="col-node ellipsis" role="cell" dir="auto">${escapeHtml(nodeLabel(j, d.nodes))}</span>
<span class="progress" role="cell">${progressCell(j)}</span><span role="cell">${stateCell(j.state)}</span>
<span class="row-actions" role="cell"><button class="btn small" data-cancel="${attr(j.id)}" data-i18n="${j.state === "queued" ? "Admin.Jobs.Remove" : "Admin.Jobs.Cancel"}"
data-i18n-title="${j.state === "queued" ? "Admin.Jobs.Remove.Tip" : "Admin.Jobs.Cancel.Tip"}"></button></span>
</div>`).join("")}</div>
</div>`}
</section>
</div>`;
applyStrings(root);
root.querySelectorAll("[data-cancel]").forEach((button) => button.addEventListener("click", async () => {
button.disabled = true;
try {
await api.post(`/api/v1/jobs/${button.dataset.cancel}/cancel`);
refresh();
} catch (err) {
toast(errorText(err), true);
button.disabled = false;
}
}));
root.querySelectorAll("[data-scan]").forEach((button) => button.addEventListener("click", async () => {
try {
await api.post("/api/v1/archive/scan");
toast(t("Admin.Archive.Queued"));
} catch (err) {
toast(errorText(err), true);
}
}));
root.querySelector("#reload")?.addEventListener("click", load);
root.querySelectorAll(".table[data-levels]").forEach(fitColumns);
}
onEvent((e) => {
if (e.type === "jobProgress") {
const cell = root.querySelector(`[data-key="${CSS.escape(e.jobId)}"] .progress`);
if (cell) cell.innerHTML = progressCell({ progressDone: e.done, progressTotal: e.total });
} else {
refresh();
}
});
await load();
}
/** Frames per UTC day for the last 30 days, zero-filled; the first and last days are labelled (in UTC, like the keys). */
function lastThirtyDays(days) {
const byDay = new Map(days.map((d) => [d.day, d.frames]));
const short = new Intl.DateTimeFormat(culture(), { day: "numeric", month: "short", timeZone: "UTC" });
const long = new Intl.DateTimeFormat(culture(), { dateStyle: "medium", timeZone: "UTC" });
const items = [];
const today = new Date();
for (let i = 29; i >= 0; i--) {
const day = new Date(Date.UTC(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate() - i));
const value = byDay.get(day.toISOString().slice(0, 10)) ?? 0;
items.push({ value, label: i === 29 || i === 0 ? short.format(day) : "", title: `${long.format(day)} · ${formatNumber(value)}` });
}
return items;
}