Headroom: homelab capacity dashboard
- FastAPI app med 7 bundled module-typer (proxmox, esxi, truenas, splunk, linux, windows, http-json) - Multi-instance support: flere instanser af samme modultype med adskilte assets - Community/Enterprise licensing med admin enterprise-toggle - Capacity forecasting, trends, what-if simulator, alert policies - MCP server + LLM copilot integration - Dockerfile + rune.yaml til Yggdrasil
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
/* Lightweight SVG charts — no external dependencies. */
|
||||
|
||||
let uid = 0;
|
||||
|
||||
const BAND_COLORS = {
|
||||
healthy: 'var(--ok)',
|
||||
watch: 'var(--watch)',
|
||||
tight: 'var(--tight)',
|
||||
critical: 'var(--crit)',
|
||||
unknown: 'var(--muted)',
|
||||
};
|
||||
|
||||
function esc(value) {
|
||||
return String(value ?? '').replace(/[&<>"']/g, (character) => ({
|
||||
'&': '&', '<': '<', '>': '>', '"': '"', "'": ''',
|
||||
}[character]));
|
||||
}
|
||||
|
||||
function nearest(points, target) {
|
||||
let best = null;
|
||||
for (const point of points) {
|
||||
if (!best || Math.abs(point.t - target) < Math.abs(best.t - target)) best = point;
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
export function bandColor(band) {
|
||||
return BAND_COLORS[band] || BAND_COLORS.unknown;
|
||||
}
|
||||
|
||||
export function lineChart(container, series, options = {}) {
|
||||
if (!container) return;
|
||||
const active = (series || []).filter((item) => item && item.points && item.points.length);
|
||||
if (!active.length) {
|
||||
container.innerHTML = '<div class="chart-empty">No telemetry in this window yet.</div>';
|
||||
return;
|
||||
}
|
||||
const width = options.width || 920;
|
||||
const height = options.height || 280;
|
||||
const pad = { left: options.padLeft ?? 48, right: 18, top: 16, bottom: 30 };
|
||||
const all = active.flatMap((item) => item.points);
|
||||
let t0 = Math.min(...all.map((point) => point.t));
|
||||
let t1 = Math.max(...all.map((point) => point.t));
|
||||
if (t1 <= t0) t1 = t0 + 3600000;
|
||||
let max = options.max;
|
||||
if (!max) {
|
||||
const peak = Math.max(...all.map((point) => point.v), 1);
|
||||
max = peak * 1.15;
|
||||
}
|
||||
const plotW = width - pad.left - pad.right;
|
||||
const plotH = height - pad.top - pad.bottom;
|
||||
const x = (t) => pad.left + ((t - t0) / (t1 - t0)) * plotW;
|
||||
const y = (v) => pad.top + plotH - (Math.max(0, Math.min(v, max)) / max) * plotH;
|
||||
const span = t1 - t0;
|
||||
const valueFormat = options.valueFormat || ((value) => `${Math.round(value)}`);
|
||||
const timeFormat = (t) => {
|
||||
const date = new Date(t);
|
||||
if (span <= 2 * 86400000) {
|
||||
return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
|
||||
}
|
||||
return date.toLocaleDateString([], { month: 'short', day: 'numeric' });
|
||||
};
|
||||
const tooltipTime = (t) => {
|
||||
const date = new Date(t);
|
||||
if (span <= 2 * 86400000) {
|
||||
return date.toLocaleString([], { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' });
|
||||
}
|
||||
return date.toLocaleDateString([], { weekday: 'short', month: 'short', day: 'numeric' });
|
||||
};
|
||||
|
||||
const parts = [];
|
||||
parts.push(`<svg class="chart-svg" viewBox="0 0 ${width} ${height}" preserveAspectRatio="none" role="img" aria-label="${esc(options.ariaLabel || 'Time series chart')}">`);
|
||||
for (let index = 0; index <= 4; index += 1) {
|
||||
const value = (max / 4) * index;
|
||||
const yy = y(value);
|
||||
parts.push(`<line class="chart-grid" x1="${pad.left}" x2="${width - pad.right}" y1="${yy}" y2="${yy}"></line>`);
|
||||
parts.push(`<text class="chart-axis" x="${pad.left - 9}" y="${yy + 4}" text-anchor="end">${esc(valueFormat(value))}</text>`);
|
||||
}
|
||||
for (let index = 0; index <= 4; index += 1) {
|
||||
const t = t0 + ((t1 - t0) * index) / 4;
|
||||
parts.push(`<text class="chart-axis" x="${x(t)}" y="${height - 8}" text-anchor="${index === 0 ? 'start' : index === 4 ? 'end' : 'middle'}">${esc(timeFormat(t))}</text>`);
|
||||
}
|
||||
|
||||
active.forEach((item) => {
|
||||
if (!item.points.length) return;
|
||||
const points = item.points;
|
||||
const color = item.color || 'var(--accent)';
|
||||
const d = points.map((point, index) => `${index === 0 ? 'M' : 'L'}${x(point.t).toFixed(2)},${y(point.v).toFixed(2)}`).join(' ');
|
||||
if (item.area) {
|
||||
const gradient = `chart-grad-${++uid}`;
|
||||
const baseY = pad.top + plotH;
|
||||
parts.push(`<defs><linearGradient id="${gradient}" x1="0" y1="0" x2="0" y2="1"><stop offset="0%" stop-color="${color}" stop-opacity="0.26"/><stop offset="100%" stop-color="${color}" stop-opacity="0"/></linearGradient></defs>`);
|
||||
parts.push(`<path d="${d} L${x(points[points.length - 1].t).toFixed(2)},${baseY} L${x(points[0].t).toFixed(2)},${baseY} Z" fill="url(#${gradient})"></path>`);
|
||||
}
|
||||
parts.push(`<path d="${d}" fill="none" stroke="${color}" stroke-width="${item.width || 2.2}" stroke-linecap="round" stroke-linejoin="round" ${item.dashed ? 'stroke-dasharray="6 7"' : ''}></path>`);
|
||||
if (item.dot) {
|
||||
const last = points[points.length - 1];
|
||||
parts.push(`<circle cx="${x(last.t)}" cy="${y(last.v)}" r="4" fill="${color}" stroke="var(--surface)" stroke-width="2"></circle>`);
|
||||
}
|
||||
});
|
||||
parts.push(`<line class="chart-guide" x1="0" x2="0" y1="${pad.top}" y2="${pad.top + plotH}" opacity="0"></line>`);
|
||||
parts.push(`<rect class="chart-hit" x="${pad.left}" y="${pad.top}" width="${plotW}" height="${plotH}" fill="transparent"></rect>`);
|
||||
parts.push('</svg>');
|
||||
|
||||
const wrap = document.createElement('div');
|
||||
wrap.className = 'chart-wrap';
|
||||
wrap.innerHTML = parts.join('') + '<div class="chart-tip" hidden></div>';
|
||||
container.replaceChildren(wrap);
|
||||
|
||||
const svg = wrap.querySelector('svg');
|
||||
const tip = wrap.querySelector('.chart-tip');
|
||||
const guide = wrap.querySelector('.chart-guide');
|
||||
svg.addEventListener('pointermove', (event) => {
|
||||
const rect = svg.getBoundingClientRect();
|
||||
const px = ((event.clientX - rect.left) / rect.width) * width;
|
||||
if (px < pad.left || px > width - pad.right) {
|
||||
tip.hidden = true;
|
||||
guide.setAttribute('opacity', '0');
|
||||
return;
|
||||
}
|
||||
const t = t0 + ((px - pad.left) / plotW) * (t1 - t0);
|
||||
const rows = active.map((item) => {
|
||||
const point = nearest(item.points, t);
|
||||
return { label: item.label || item.key || '', color: item.color || 'var(--accent)', point };
|
||||
}).filter((row) => row.point);
|
||||
if (!rows.length) return;
|
||||
const reference = rows[0].point.t;
|
||||
tip.innerHTML = `<div class="tip-time">${esc(tooltipTime(reference))}</div>` + rows.map((row) => (
|
||||
`<div class="tip-row"><span><i style="background:${row.color}"></i>${esc(row.label)}</span><strong>${esc(valueFormat(row.point.v))}</strong></div>`
|
||||
)).join('');
|
||||
tip.hidden = false;
|
||||
const guideX = x(reference);
|
||||
guide.setAttribute('x1', guideX);
|
||||
guide.setAttribute('x2', guideX);
|
||||
guide.setAttribute('opacity', '1');
|
||||
const left = (guideX / width) * 100;
|
||||
tip.style.left = `${Math.max(2, Math.min(76, left))}%`;
|
||||
});
|
||||
svg.addEventListener('pointerleave', () => {
|
||||
tip.hidden = true;
|
||||
guide.setAttribute('opacity', '0');
|
||||
});
|
||||
}
|
||||
|
||||
export function ring(value, options = {}) {
|
||||
const size = options.size || 104;
|
||||
const stroke = options.stroke || 9;
|
||||
const radius = (size - stroke) / 2;
|
||||
const circumference = 2 * Math.PI * radius;
|
||||
const safe = Math.max(0, Math.min(100, Number(value) || 0));
|
||||
const band = options.band || (safe >= 90 ? 'critical' : safe >= 80 ? 'tight' : safe >= 60 ? 'watch' : 'healthy');
|
||||
return `<div class="ring" data-band="${esc(band)}" style="width:${size}px;height:${size}px">
|
||||
<svg viewBox="0 0 ${size} ${size}" aria-hidden="true">
|
||||
<circle class="ring-track" cx="${size / 2}" cy="${size / 2}" r="${radius}" fill="none" stroke-width="${stroke}"/>
|
||||
<circle class="ring-value" cx="${size / 2}" cy="${size / 2}" r="${radius}" fill="none" stroke-width="${stroke}"
|
||||
stroke-linecap="round" stroke-dasharray="${circumference.toFixed(2)}"
|
||||
stroke-dashoffset="${(circumference * (1 - safe / 100)).toFixed(2)}"
|
||||
transform="rotate(-90 ${size / 2} ${size / 2})"/>
|
||||
</svg>
|
||||
<div class="ring-label"><strong>${Math.round(safe)}</strong><span>${esc(options.suffix || '/100')}</span></div>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
export function sparkline(points, color = 'var(--accent)', options = {}) {
|
||||
if (!points || points.length < 2) return '<span class="spark-empty"></span>';
|
||||
const width = options.width || 116;
|
||||
const height = options.height || 34;
|
||||
const pad = 2;
|
||||
let min = Math.min(...points.map((point) => point.v));
|
||||
let max = Math.max(...points.map((point) => point.v));
|
||||
if (max - min < 0.01) { max += 1; min -= 1; }
|
||||
const t0 = points[0].t;
|
||||
const t1 = points[points.length - 1].t || t0 + 1;
|
||||
const x = (t) => pad + ((t - t0) / (t1 - t0 || 1)) * (width - pad * 2);
|
||||
const y = (v) => pad + (1 - (v - min) / (max - min)) * (height - pad * 2);
|
||||
const d = points.map((point, index) => `${index === 0 ? 'M' : 'L'}${x(point.t).toFixed(1)},${y(point.v).toFixed(1)}`).join(' ');
|
||||
const area = `${d} L${x(points[points.length - 1].t).toFixed(1)},${height - pad} L${x(points[0].t).toFixed(1)},${height - pad} Z`;
|
||||
return `<svg class="spark" viewBox="0 0 ${width} ${height}" preserveAspectRatio="none" aria-hidden="true">
|
||||
<path d="${area}" fill="${color}" opacity="0.10"></path>
|
||||
<path d="${d}" fill="none" stroke="${color}" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"></path>
|
||||
</svg>`;
|
||||
}
|
||||
|
||||
export function distributionBars(items, options = {}) {
|
||||
const total = items.reduce((sum, item) => sum + item.value, 0) || 1;
|
||||
return `<div class="dist">${items.map((item) => `
|
||||
<div class="dist-bar" title="${esc(item.label)}: ${item.value}">
|
||||
<span style="width:${(item.value / total) * 100}%;background:${item.color || 'var(--accent)'}"></span>
|
||||
</div>`).join('')}</div>`;
|
||||
}
|
||||
Reference in New Issue
Block a user