A running job now says what it does now: "Verifying photo 812 of 2,400 · Harbour sunset · sequence 3 of 41". A paused or failed job says where it stopped. The line appears in the Jobs table under the target, in the job bar (at most 60 characters, the item dropped first, the bar text also its tooltip), in the Archive's recent maintenance (refreshed at most twice a second), in the editor's server-job line and in the sync tooltip. Byte jobs now count sizes in the bar. While connected, the Server section lists the server's running and paused jobs, live through the jobs API and the jobProgress events. Pause, Resume and Cancel are offered only to an account that manages the archive, which is what the server requires to act on someone else's job; for any other account the list is read-only. The list hides while nothing runs, and the capture gains a server-jobs scene. The admin panel's Jobs page and dashboard show the same line under the target and update it with every progress event. The steps, parts and sub-steps are in English and Italian, and an unknown step shows its counts only. Refs #65 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
182 lines
11 KiB
JavaScript
182 lines
11 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, targetCell, updateStatus } 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>`;
|
|
|
|
// 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 `<span class="chip warn" title="${attr(tip)}" data-i18n="Admin.Dashboard.ArchiveIncomplete"></span>`;
|
|
}
|
|
|
|
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 = `<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 : "") + 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 = `
|
|
<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="target-cell" role="cell">${targetCell(j)}</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 job = activeJobs.get(e.jobId) ?? { kind: "", state: "running" };
|
|
job.progressDone = e.done;
|
|
job.progressTotal = e.total;
|
|
job.itemsPerSecond = e.itemsPerSecond;
|
|
job.etaSeconds = e.etaSeconds;
|
|
if (e.detail) job.detail = e.detail;
|
|
const row = root.querySelector(`[data-key="${CSS.escape(e.jobId)}"]`);
|
|
const cell = row?.querySelector(".progress");
|
|
if (cell) cell.innerHTML = progressCell(job);
|
|
updateStatus(row, job);
|
|
} 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;
|
|
}
|