Files
Scrobbler/frontend/js/dom.js
mattandClaude Opus 5.5 ea705589f1
CI / lint (push) Successful in 9s
CI / test (push) Successful in 36s
CI / notify (push) Successful in 5s
Monitoring stack, containers and dashboards
- API image (uv multi-stage, non-root, gunicorn, migrations on start,
  health check) and UI image (nginx, CSP, config.js rendered from
  API_BASE_URL); /healthz documented in OpenAPI
- docker compose runs db, api and ui; the "monitoring" profile adds
  Prometheus, postgres-exporter and Grafana, all provisioned from files
- Grafana dashboards: API overview (RED), Last.fm API, Usage, Database
- Prometheus alert rules: API down, error rate, latency, auth failure
  spike, ignored scrobbles, DB pool saturation, Postgres down
- Pool checkout gauge counts events (the pool's own counters lag the
  checkin event); error ratios show 0 instead of "No data"
- Tests check dashboards and alerts only use metrics that exist
- scripts/generate_traffic.py drives realistic traffic for demos

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
2026-09-25 14:26:19 +01:00

70 lines
2.4 KiB
JavaScript

// DOM helpers. Server data only ever reaches the page as text nodes or attribute
// values, never as HTML, so track names can't inject markup.
export function h(tag, attrs = {}, ...children) {
const el = document.createElement(tag);
for (const [key, value] of Object.entries(attrs ?? {})) {
if (value === false || value === null || value === undefined) continue;
if (key.startsWith("on") && typeof value === "function") el.addEventListener(key.slice(2), value);
else if (key === "class") el.className = value;
// CSSOM, not a style attribute, so it's allowed by the UI's CSP (style-src 'self').
else if (key === "style") el.style.cssText = value;
else if (key === "dataset") Object.assign(el.dataset, value);
else el.setAttribute(key, value === true ? "" : String(value));
}
append(el, children);
return el;
}
function append(el, children) {
for (const child of children.flat(Infinity)) {
if (child === null || child === undefined || child === false) continue;
el.append(child instanceof Node ? child : document.createTextNode(String(child)));
}
}
export function replace(el, ...children) {
el.replaceChildren();
append(el, children);
return el;
}
const numberFormat = new Intl.NumberFormat();
export const num = (n) => numberFormat.format(n);
const relative = new Intl.RelativeTimeFormat(undefined, { numeric: "auto" });
const UNITS = [
["year", 365 * 86400],
["month", 30 * 86400],
["week", 7 * 86400],
["day", 86400],
["hour", 3600],
["minute", 60],
];
export function timeAgo(value) {
const seconds = (new Date(value).getTime() - Date.now()) / 1000;
for (const [unit, size] of UNITS) {
if (Math.abs(seconds) >= size) return relative.format(Math.round(seconds / size), unit);
}
return "just now";
}
export const formatTime = (value) =>
new Date(value).toLocaleTimeString(undefined, { hour: "2-digit", minute: "2-digit" });
export const formatDay = (value) =>
new Date(value).toLocaleDateString(undefined, { weekday: "long", day: "numeric", month: "long", year: "numeric" });
export const formatDate = (value) =>
new Date(value).toLocaleDateString(undefined, { day: "numeric", month: "short", year: "numeric" });
export function formatDuration(seconds) {
const s = Math.max(0, Math.floor(seconds));
return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`;
}
export function errorBox(message) {
return h("p", { class: "notice notice-error", role: "alert" }, message);
}