Add Markdown editor, server statistics and content navigation

This commit is contained in:
SantaSpeen committed 2026-12-02 15:32:14 +03:00
1 parent 03c590e172
commit 1819f35f91
52 files changed
+7190 -28

No files matched your search

+162
View File
@@ -0,0 +1,162 @@
import { mountChart } from "./charts.js";
import { mountMiniLog } from "./mini-log.js";
import { escapeText } from "./text-runtime.js";
export function statisticsMarkup({ title = "", t = (ru) => ru } = {}) {
return `<section class="server-statistics"><div class="statistics-toolbar"><div><h2>${escapeText(title || t("Статистика сервера", "Server statistics"))}</h2><p class="muted">${t("Показатели и события за выбранный период.", "Metrics and events for the selected period.")}</p></div><label>${t("Период", "Period")}<select data-statistics-range><option value="15m">${t("15 минут", "15 minutes")}</option><option value="1h" selected>${t("1 час", "1 hour")}</option><option value="24h">${t("24 часа", "24 hours")}</option></select></label></div><p class="statistics-empty muted" hidden></p><div class="statistics-kpis">${["cpu", "memory", "network"].map((key) => `<div class="statistics-kpi" data-kpi="${key}"><span></span><strong></strong><div data-stat-spark="${key}"></div></div>`).join("")}</div><div class="statistics-grid">${["cpu", "memory", "network", "disk", "requests", "errors", "storage", "gauge"].map((key) => `<div class="statistics-panel" data-stat-chart="${key}"></div>`).join("")}</div><div data-stat-mini-log></div></section>`;
}
export function mountStatistics(
root,
{ data, t = (ru) => ru, onRangeChange, onOpenLogs } = {},
) {
const abort = new AbortController();
let disposed = false,
charts = [],
logs;
function update(next) {
if (disposed) return;
charts.forEach((chart) => chart.dispose());
charts = [];
logs?.dispose();
const empty = root.querySelector(".statistics-empty");
empty.hidden = Boolean(next);
empty.textContent = next ? "" : t("Нет данных.", "No data.");
root.querySelector(".statistics-grid").hidden = !next;
root.querySelector(".statistics-kpis").hidden = !next;
root.querySelector("[data-stat-mini-log]").hidden = !next;
if (!next) return;
const labels = next.labels || [],
series = (name, values) => [{ name, values: values || [] }],
last = (values) => values?.at(-1) ?? 0;
root.querySelector("[data-statistics-range]").value = next.range || "1h";
const definitions = {
cpu: {
type: "area",
title: t("Загрузка CPU", "CPU utilization"),
unit: "%",
max: 100,
series: series("CPU", next.cpu),
},
memory: {
type: "line",
title: t("Оперативная память", "Memory"),
unit: t("ГБ", "GB"),
max: next.memoryTotal || 16,
series: series(t("Используется", "Used"), next.memory),
},
network: {
type: "line",
title: t("Сетевой трафик", "Network traffic"),
unit: t("МБ/с", "MB/s"),
series: [
{ name: t("Входящий", "Inbound"), values: next.networkIn || [] },
{ name: t("Исходящий", "Outbound"), values: next.networkOut || [] },
],
},
disk: {
type: "stacked",
title: t("Дисковые операции", "Disk I/O"),
unit: t("МБ/с", "MB/s"),
series: [
{ name: t("Чтение", "Read"), values: next.diskRead || [] },
{ name: t("Запись", "Write"), values: next.diskWrite || [] },
],
},
requests: {
type: "bar",
title: t("Запросы", "Requests"),
unit: t("запр./с", "req/s"),
series: series("HTTP", next.requests),
},
errors: {
type: "bar",
tone: "danger",
title: t("Ошибки сервиса", "Service errors"),
description: t(
"Количество 5xx в каждом интервале",
"5xx count per interval",
),
unit: t("шт.", "count"),
series: series("5xx", next.errors),
},
storage: {
type: "donut",
title: t("Дисковое пространство", "Storage allocation"),
unit: t("ГБ", "GB"),
series: (next.storage || []).map((item) => ({
name: item.name,
values: [item.value],
})),
},
gauge: {
type: "gauge",
title: t("CPU сейчас", "Current CPU"),
unit: "%",
max: 100,
series: series("CPU", [last(next.cpu)]),
},
};
for (const [key, options] of Object.entries(definitions))
charts.push(
mountChart(root.querySelector(`[data-stat-chart="${key}"]`), {
...options,
labels,
t,
}),
);
const kpis = {
cpu: {
label: t("CPU сейчас", "CPU now"),
value: `${last(next.cpu).toFixed(1)} %`,
values: next.cpu,
},
memory: {
label: t("Память сейчас", "Memory now"),
value: `${last(next.memory).toFixed(1)} / ${next.memoryTotal || 16} ${t("ГБ", "GB")}`,
values: next.memory,
},
network: {
label: t("Входящий трафик", "Inbound traffic"),
value: `${last(next.networkIn).toFixed(1)} ${t("МБ/с", "MB/s")}`,
values: next.networkIn,
},
};
for (const [key, item] of Object.entries(kpis)) {
const card = root.querySelector(`[data-kpi="${key}"]`);
card.querySelector("span").textContent = item.label;
card.querySelector("strong").textContent = item.value;
charts.push(
mountChart(card.querySelector("[data-stat-spark]"), {
type: "sparkline",
title: item.label,
series: series(item.label, item.values),
labels,
t,
}),
);
}
logs = mountMiniLog(root.querySelector("[data-stat-mini-log]"), {
entries: next.logs || [],
limit: 6,
title: t("Последние события сервера", "Recent server events"),
onOpenFull: onOpenLogs,
t,
});
}
root
.querySelector("[data-statistics-range]")
.addEventListener(
"change",
(event) => onRangeChange?.(event.target.value),
{ signal: abort.signal },
);
update(data);
return {
update,
dispose() {
disposed = true;
abort.abort();
charts.forEach((chart) => chart.dispose());
logs?.dispose();
},
};
}