// Statistics page: polls /stats/data and renders Chart.js charts. // Chart.js itself is served from the embedded bundle (/embedded/chart.umd.min.js). (function () { "use strict"; var REFRESH_MS = 5000; var charts = {}; var timer = null; function css(name) { return getComputedStyle(document.documentElement).getPropertyValue(name).trim(); } function series(i) { return css("--series-" + i); } function fmtBytes(n) { if (n < 1024) return n + " B"; if (n < 1048576) return (n / 1024).toFixed(1) + " kB"; return (n / 1048576).toFixed(2) + " MB"; } function fmtUptime(s) { var h = Math.floor(s / 3600), m = Math.floor((s % 3600) / 60); return h ? h + "h " + m + "m" : m ? m + "m " + (s % 60) + "s" : s + "s"; } function hhmmss(t) { var d = new Date(t * 1000); return d.toTimeString().slice(0, 8); } function setText(id, v) { document.getElementById(id).textContent = v; } function draw(id, config) { var canvas = document.getElementById(id); if (charts[id]) { charts[id].data = config.data; charts[id].update("none"); return; } config.options = config.options || {}; config.options.responsive = true; config.options.maintainAspectRatio = false; config.options.animation = false; charts[id] = new Chart(canvas, config); } function grid() { return { color: css("--grid") }; } function scales(extra) { var s = { x: { grid: grid(), ticks: { color: css("--text-secondary"), maxTicksLimit: 6 } }, y: { grid: grid(), beginAtZero: true, ticks: { color: css("--text-secondary") } } }; for (var k in extra) s[k] = Object.assign(s[k] || {}, extra[k]); return s; } function legend() { return { position: "bottom", labels: { color: css("--text-secondary"), boxWidth: 12 } }; } function line(label, data, color, fill) { return { label: label, data: data, borderColor: color, backgroundColor: color + "33", fill: !!fill, borderWidth: 2, pointRadius: 0, pointHoverRadius: 5, tension: 0.25 }; } function bar(label, data, color) { return { label: label, data: data, backgroundColor: color, borderRadius: 4 }; } function palette(n) { var out = []; for (var i = 1; i <= n; i++) out.push(series(((i - 1) % 8) + 1)); return out; } function table(id, head, rows) { var t = document.getElementById(id); var h = "" + head.map(function (c) { return "" + c + ""; }).join("") + ""; rows.forEach(function (r) { h += "" + r.map(function (c) { return "" + String(c).replace(/&/g, "&").replace(/"; }).join("") + ""; }); t.innerHTML = h + (rows.length ? "" : "no data yet") + ""; } function render(d) { var h = d.http, sv = d.services, pts = h.series.points; var labels = pts.map(function (p) { return hhmmss(p.t); }); var errPct = h.totals.requests ? (100 * h.totals.errors / h.totals.requests) : 0; setText("t-uptime", fmtUptime(h.uptime_sec)); setText("t-requests", h.totals.requests); setText("t-errors", h.totals.errors + " (" + errPct.toFixed(1) + "%)"); setText("t-avg", h.totals.avg_ms.toFixed(2) + " ms"); setText("t-p95", h.latency.p95_ms.toFixed(2) + " ms"); setText("t-p99", h.latency.p99_ms.toFixed(2) + " ms"); setText("t-in", fmtBytes(h.totals.bytes_in)); setText("t-out", fmtBytes(h.totals.bytes_out)); setText("t-conn", h.connections.total); setText("t-active", h.connections.active + " (peak " + h.connections.peak + ")"); setText("t-timeouts", h.connections.timeouts + " / " + h.connections.malformed); var left = h.connections.cap ? Math.max(0, h.connections.cap - h.connections.total) : "n/a"; setText("t-left", left); setText("t-updated", "updated " + new Date().toTimeString().slice(0, 8)); draw("c-requests", { type: "line", data: { labels: labels, datasets: [ line("Requests", pts.map(function (p) { return p.requests; }), series(1), true), line("Errors", pts.map(function (p) { return p.errors; }), series(8), false)] }, options: { scales: scales({}), plugins: { legend: legend() }, interaction: { mode: "index", intersect: false } } }); draw("c-latency", { type: "line", data: { labels: labels, datasets: [ line("Avg latency (ms)", pts.map(function (p) { return p.avg_ms; }), series(2), true)] }, options: { scales: scales({}), plugins: { legend: legend() }, interaction: { mode: "index", intersect: false } } }); draw("c-bytes", { type: "line", data: { labels: labels, datasets: [ line("Bytes in", pts.map(function (p) { return p.bytes_in; }), series(3), true), line("Bytes out", pts.map(function (p) { return p.bytes_out; }), series(7), true)] }, options: { scales: scales({}), plugins: { legend: legend() }, interaction: { mode: "index", intersect: false } } }); draw("c-conns", { type: "bar", data: { labels: labels, datasets: [ bar("Connections opened", pts.map(function (p) { return p.connections; }), series(1))] }, options: { scales: scales({}), plugins: { legend: legend() } } }); var codes = Object.keys(h.by_status); draw("c-status", { type: "doughnut", data: { labels: codes, datasets: [{ data: codes.map(function (k) { return h.by_status[k]; }), backgroundColor: codes.map(function (k) { return k < 300 ? series(3) : k < 400 ? series(1) : k < 500 ? series(4) : series(8); }), borderColor: css("--surface-1"), borderWidth: 2 }] }, options: { plugins: { legend: legend() } } }); var methods = Object.keys(h.by_method); draw("c-methods", { type: "doughnut", data: { labels: methods, datasets: [{ data: methods.map(function (k) { return h.by_method[k]; }), backgroundColor: palette(methods.length), borderColor: css("--surface-1"), borderWidth: 2 }] }, options: { plugins: { legend: legend() } } }); draw("c-hist", { type: "bar", data: { labels: h.latency.histogram.map(function (b) { return b.bucket; }), datasets: [bar("Requests", h.latency.histogram.map(function (b) { return b.count; }), series(1))] }, options: { scales: scales({}), plugins: { legend: { display: false } } } }); var top = h.by_route.slice(0, 10); draw("c-routes", { type: "bar", data: { labels: top.map(function (r) { return r.route; }), datasets: [bar("Requests", top.map(function (r) { return r.count; }), series(1)), bar("Errors", top.map(function (r) { return r.errors; }), series(8))] }, options: { indexAxis: "y", scales: scales({}), plugins: { legend: legend() } } }); table("tbl-routes", ["Route", "Count", "Errors", "Avg ms", "Max ms", "Bytes out"], h.by_route.map(function (r) { return [r.route, r.count, r.errors, r.avg_ms.toFixed(2), r.max_ms.toFixed(2), fmtBytes(r.bytes_out)]; })); var st = Object.keys(sv.stores); draw("c-stores", { type: "bar", data: { labels: st, datasets: [ bar("Entries", st.map(function (k) { return sv.stores[k]; }), series(3))] }, options: { indexAxis: "y", scales: scales({}), plugins: { legend: { display: false } } } }); draw("c-cache", { type: "doughnut", data: { labels: ["Hits", "Misses"], datasets: [{ data: [sv.cache.hits, sv.cache.misses], backgroundColor: [series(3), series(4)], borderColor: css("--surface-1"), borderWidth: 2 }] }, options: { plugins: { legend: legend() } } }); var ts = Object.keys(sv.tasks); draw("c-tasks", { type: "doughnut", data: { labels: ts.length ? ts : ["none"], datasets: [{ data: ts.length ? ts.map(function (k) { return sv.tasks[k]; }) : [0], backgroundColor: palette(Math.max(ts.length, 1)), borderColor: css("--surface-1"), borderWidth: 2 }] }, options: { plugins: { legend: legend() } } }); var orm = Object.keys(sv.orm); draw("c-orm", { type: "bar", data: { labels: orm, datasets: [ bar("Tables", orm.map(function (k) { return sv.orm[k].tables; }), series(1)), bar("Rows", orm.map(function (k) { return sv.orm[k].rows; }), series(2))] }, options: { scales: scales({}), plugins: { legend: legend() } } }); table("tbl-workers", ["Worker", "State", "Restarts"], sv.workers.map(function (w) { return [w.name, w.running ? "running" : "stopped", w.restarts]; })); table("tbl-metrics", ["Metric", "Value"], Object.keys(sv.metrics).map(function (k) { return [k, sv.metrics[k]]; })); } function load() { fetch("/stats/data", { cache: "no-store" }).then(function (r) { return r.json(); }) .then(render).catch(function (e) { setText("t-updated", "load failed: " + e); }); } function setAuto(on) { if (timer) { clearInterval(timer); timer = null; } if (on) timer = setInterval(load, REFRESH_MS); } document.getElementById("refresh").addEventListener("click", load); document.getElementById("auto").addEventListener("change", function (e) { setAuto(e.target.checked); }); load(); })();