Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
359 lines (342 loc) · 17.2 KB
/
Copy pathindex.html
File metadata and controls
359 lines (342 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>GlanceRiff</title>
<style>
:root {
--bg: #101418; --panel: #171c22; --border: #262e38; --text: #d6dde6; --muted: #7d8a99;
--accent: #4fc3f7; --ok: #4caf50; --warn: #f0b429; --crit: #ef5350; --track: #232b35;
--rx: #4fc3f7; --tx: #ce93d8; --rd: #4db6ac; --wr: #ffb74d;
--u-k: #64b5f6; --u-m: #4dd0e1; --u-g: #ba68c8; --u-t: #ff8a65;
}
* { box-sizing: border-box; }
body {
margin: 0; padding: 16px; background: var(--bg); color: var(--text);
font: 13px/1.4 ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace;
}
header { display: flex; flex-wrap: wrap; gap: 4px 20px; align-items: baseline; margin-bottom: 12px; }
header .host { color: var(--accent); font-weight: bold; font-size: 16px; }
header .muted, .muted { color: var(--muted); }
#status.stale { color: var(--crit); }
.grid { display: grid; gap: 12px; }
.gauges { grid-template-columns: repeat(3, 1fr); }
.middle { grid-template-columns: 2fr 3fr; margin-top: 12px; }
.panel { background: var(--panel); border: 1px solid var(--border); border-radius: 6px; padding: 10px 12px; min-width: 0; }
.panel h2 { margin: 0 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.gauge .row { display: flex; justify-content: space-between; margin-bottom: 6px; }
.gauge .pct { font-size: 20px; font-weight: bold; }
.bar { height: 8px; background: var(--track); border-radius: 4px; overflow: hidden; }
.bar > i { display: block; height: 100%; width: 0; transition: width .4s, background-color .4s; }
.cores { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 6px 14px; }
.core { display: grid; grid-template-columns: 2.5em 1fr 4em; gap: 6px; align-items: center; }
.core span:last-child { text-align: right; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 3px 6px; text-align: right; white-space: nowrap; }
th:first-child, td:first-child { text-align: left; }
td.name { text-align: left; overflow: hidden; text-overflow: ellipsis; max-width: 0; width: 100%; }
th { color: var(--muted); font-weight: normal; border-bottom: 1px solid var(--border); }
tbody tr:hover { background: #1d242c; }
#procs th { cursor: pointer; user-select: none; }
#procs th.sorted { color: var(--accent); }
svg.spark { display: block; width: 100%; height: 36px; margin-top: 8px; overflow: visible; }
svg.spark.net { height: 56px; margin: 0 0 8px; }
svg.spark polyline { fill: none; stroke-width: 1.5; vector-effect: non-scaling-stroke; stroke-linejoin: round; }
svg.spark polygon { stroke: none; opacity: .15; }
svg.spark .grid { stroke: var(--border); stroke-width: 1; vector-effect: non-scaling-stroke; }
.net-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.rx { color: var(--rx); } .tx { color: var(--tx); } .rd { color: var(--rd); } .wr { color: var(--wr); }
/* Byte values colored by unit: B, K, M, G, T and up. */
.u0 { color: var(--muted); } .u1 { color: var(--u-k); } .u2 { color: var(--u-m); }
.u3 { color: var(--u-g); } .u4, .u5 { color: var(--u-t); }
.ok { color: var(--ok); } .warn { color: var(--warn); } .crit { color: var(--crit); }
.stack { display: grid; gap: 12px; align-content: start; }
/* First panel takes the height the other column leaves over; later ones keep their own. */
.stack.fill-first { grid-template-rows: 1fr; grid-auto-rows: auto; align-content: stretch; }
#processes { margin-top: 12px; }
.proc-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.proc-head label { cursor: pointer; user-select: none; }
.proc-controls { display: flex; flex-wrap: wrap; gap: 4px 16px; align-items: baseline; justify-content: flex-end; }
#proc-limit {
width: 4.5em; padding: 1px 4px; font: inherit; color: var(--text);
background: var(--track); border: 1px solid var(--border); border-radius: 4px;
}
#proc-limit:focus { outline: none; border-color: var(--accent); }
@media (max-width: 760px) {
.gauges, .middle { grid-template-columns: 1fr; }
.hide-sm { display: none; }
}
</style>
</head>
<body>
<header>
<span class="host" id="hostname">…</span>
<span id="os" class="muted"></span>
<span>up <b id="uptime">–</b></span>
<span>load <b id="load">–</b></span>
<span id="status" class="muted"></span>
</header>
<section class="grid gauges">
<div class="panel gauge" id="g-cpu"><div class="row"><h2>CPU</h2><span class="pct">–</span></div><div class="bar"><i></i></div><div class="muted detail"> </div><svg class="spark" viewBox="0 0 100 30" preserveAspectRatio="none"><line class="grid" x1="0" y1="30" x2="100" y2="30"/><polygon/><polyline/></svg></div>
<div class="panel gauge" id="g-mem"><div class="row"><h2>Memory</h2><span class="pct">–</span></div><div class="bar"><i></i></div><div class="muted detail"> </div><svg class="spark" viewBox="0 0 100 30" preserveAspectRatio="none"><line class="grid" x1="0" y1="30" x2="100" y2="30"/><polygon/><polyline/></svg></div>
<div class="panel gauge" id="g-swap"><div class="row"><h2>Swap</h2><span class="pct">–</span></div><div class="bar"><i></i></div><div class="muted detail"> </div></div>
</section>
<section class="grid middle">
<div class="stack fill-first">
<div class="panel"><h2>Cores</h2><div class="cores" id="cores"></div></div>
<div class="panel">
<div class="net-head"><h2>Disks</h2><span class="muted"><span class="rd">▬ read</span> <span class="wr">▬ write</span> · max <span id="disk-max">–</span></span></div>
<svg class="spark net" id="disk-graph" viewBox="0 0 100 30" preserveAspectRatio="none"><line class="grid" x1="0" y1="30" x2="100" y2="30"/><polyline/><polyline/></svg>
<table><thead><tr><th>Mount</th><th class="hide-sm">FS</th><th>Used</th><th>Total</th><th>Use%</th><th>Read/s</th><th>Write/s</th></tr></thead><tbody id="disks"></tbody></table>
</div>
</div>
<div class="stack fill-first">
<div class="panel">
<div class="net-head"><h2>Network</h2><span class="muted"><span class="rx">▬ rx</span> <span class="tx">▬ tx</span> · max <span id="net-max">–</span></span></div>
<svg class="spark net" id="net-graph" viewBox="0 0 100 30" preserveAspectRatio="none"><line class="grid" x1="0" y1="30" x2="100" y2="30"/><polyline/><polyline/></svg>
<table><thead><tr><th>Interface</th><th>Rx/s</th><th>Tx/s</th><th class="hide-sm">Rx total</th><th class="hide-sm">Tx total</th></tr></thead><tbody id="nets"></tbody></table>
</div>
<div class="panel" id="sensors-panel" hidden><h2>Sensors</h2>
<table><thead><tr><th>Sensor</th><th>Temp</th><th>Max</th><th>Crit</th></tr></thead><tbody id="sensors"></tbody></table>
</div>
</div>
</section>
<section class="panel" id="processes">
<div class="proc-head">
<h2>Processes <span id="proc-count" class="muted"></span></h2>
<div class="proc-controls">
<label class="muted" title="Number of processes to list">
Show <input type="number" id="proc-limit" min="1" max="500" step="1" value="10">
</label>
<label class="muted" title="Off: per core, can exceed 100%. On: share of the whole machine, max 100%.">
<input type="checkbox" id="normalize"> CPU% of total
</label>
</div>
</div>
<table id="procs">
<thead><tr>
<th data-key="pid">PID</th><th data-key="cpu">CPU%</th><th data-key="mem_percent">MEM%</th>
<th data-key="mem">RES</th><th data-key="name" style="text-align:left">Name</th>
</tr></thead>
<tbody></tbody>
</table>
</section>
<script>
const DEFAULT_PROCS = 15, MAX_PROCS = 500;
// Both come from /api/config so the graphs match the server's collection interval.
let refreshMs = 2000, historyCap = 300;
const $ = id => document.getElementById(id);
let sortKey = 'cpu', sortDesc = true, last = null, history = [];
const level = p => p < 50 ? 'ok' : p < 80 ? 'warn' : 'crit';
// Same thresholds as the terminal UI: critical point (or 80 °C), warning from 65 °C.
const tempLevel = (t, crit) => t >= (crit ?? 80) ? 'crit' : t >= 65 ? 'warn' : 'ok';
// I/O wait runs far below CPU load, so it gets its own, lower thresholds.
const ioLevel = p => p < 5 ? 'ok' : p < 15 ? 'warn' : 'crit';
const temp = t => t == null ? '–' : t.toFixed(0) + '°C';
const color = p => `var(--${level(p)})`;
function bytes(n) {
const u = ['B', 'K', 'M', 'G', 'T', 'P']; let i = 0;
while (n >= 1024 && i < u.length - 1) { n /= 1024; i++; }
return i ? n.toFixed(1) + u[i] : n + 'B';
}
// Table cell for a byte value, colored by the unit bytes() picks.
function bytesCell(n, cls = '') {
let unit = 0;
for (let v = n; v >= 1024 && unit < 5; v /= 1024) unit++;
return [bytes(n), `u${unit} ${cls}`.trim()];
}
function uptime(s) {
const d = Math.floor(s / 86400), h = Math.floor(s % 86400 / 3600), m = Math.floor(s % 3600 / 60), sec = s % 60;
const hms = [h, m, sec].map(v => String(v).padStart(2, '0')).join(':');
return d ? `${d}d ${hms}` : hms;
}
// Build a row from cells; every value goes through textContent, never innerHTML.
function row(cells) {
const tr = document.createElement('tr');
for (const c of cells) {
const td = document.createElement('td');
const [text, cls] = Array.isArray(c) ? c : [c];
td.textContent = text;
if (cls) td.className = cls;
tr.appendChild(td);
}
return tr;
}
function gauge(id, pct, detail) {
const el = $(id);
el.querySelector('.pct').textContent = pct.toFixed(1) + '%';
el.querySelector('.pct').className = 'pct ' + level(pct);
const bar = el.querySelector('.bar > i');
bar.style.width = Math.min(pct, 100) + '%';
bar.style.backgroundColor = color(pct);
el.querySelector('.detail').textContent = detail;
}
function renderCores(cores) {
const box = $('cores');
if (box.children.length !== cores.length) {
box.replaceChildren(...cores.map((_, i) => {
const d = document.createElement('div');
d.className = 'core';
d.innerHTML = '<span class="muted"></span><div class="bar"><i></i></div><span></span>';
d.firstChild.textContent = i;
return d;
}));
}
cores.forEach((p, i) => {
const d = box.children[i], bar = d.querySelector('i');
bar.style.width = Math.min(p, 100) + '%';
bar.style.backgroundColor = color(p);
d.lastChild.textContent = p.toFixed(1) + '%';
});
}
// Mirrors HistoryPoint::from_snapshot on the server.
const isLoopback = name => name === 'lo' || /loopback/i.test(name);
function historyPoint(s) {
const nets = s.nets.filter(n => !isLoopback(n.name));
return {
timestamp: s.timestamp, cpu: s.cpu.total, mem: s.mem.percent,
rx_rate: nets.reduce((a, n) => a + n.rx_rate, 0), tx_rate: nets.reduce((a, n) => a + n.tx_rate, 0),
disk_read: s.disk_io.read_rate, disk_write: s.disk_io.write_rate,
};
}
function pushHistory(s) {
if (history.length && history[history.length - 1].timestamp >= s.timestamp) return;
history.push(historyPoint(s));
if (history.length > historyCap) history.splice(0, history.length - historyCap);
}
// Right-aligned: the newest value sits at the right edge, the graph fills in from there.
function linePoints(values, max) {
const step = 100 / Math.max(historyCap - 1, 1), offset = 100 - (values.length - 1) * step;
return values.map((v, i) => `${(offset + i * step).toFixed(2)},${(30 - Math.min(v / max, 1) * 29).toFixed(2)}`).join(' ');
}
function sparkline(svg, values, max, color) {
const pts = linePoints(values, max);
const line = svg.querySelector('polyline'), area = svg.querySelector('polygon');
line.setAttribute('points', pts);
line.style.stroke = color;
if (area && values.length) {
const first = pts.slice(0, pts.indexOf(','));
area.setAttribute('points', `${first},30 ${pts} 100,30`);
area.style.fill = color;
}
}
function renderGraphs() {
if (!history.length) return;
const now = history[history.length - 1];
sparkline($('g-cpu').querySelector('svg'), history.map(p => p.cpu), 100, color(now.cpu));
sparkline($('g-mem').querySelector('svg'), history.map(p => p.mem), 100, color(now.mem));
rateGraph('net-graph', 'net-max', history.map(p => p.rx_rate), history.map(p => p.tx_rate), 'var(--rx)', 'var(--tx)');
rateGraph('disk-graph', 'disk-max', history.map(p => p.disk_read), history.map(p => p.disk_write), 'var(--rd)', 'var(--wr)');
}
// Two byte-rate lines on a shared scale; the peak over the visible history is shown as "max".
function rateGraph(svgId, maxId, a, b, colorA, colorB) {
const max = Math.max(1, ...a, ...b), [lineA, lineB] = $(svgId).querySelectorAll('polyline');
lineA.setAttribute('points', linePoints(a, max));
lineB.setAttribute('points', linePoints(b, max));
lineA.style.stroke = colorA;
lineB.style.stroke = colorB;
$(maxId).textContent = bytes(max) + '/s';
}
function renderProcs() {
if (!last) return;
const dir = sortDesc ? -1 : 1;
const procs = [...last.processes].sort((a, b) => {
const x = a[sortKey], y = b[sortKey];
return (typeof x === 'string' ? x.localeCompare(y, undefined, { sensitivity: 'base' }) : x - y) * dir;
});
const divisor = $('normalize').checked ? Math.max(last.cpu.cores.length, 1) : 1;
const limit = procLimit();
$('procs').tBodies[0].replaceChildren(...procs.slice(0, limit).map(p => {
const cpu = p.cpu / divisor;
return row([p.pid, [cpu.toFixed(1), level(cpu)], p.mem_percent.toFixed(1), bytesCell(p.mem), [p.name, 'name']]);
}));
$('proc-count').textContent = `(${last.processes.length}, top ${Math.min(limit, procs.length)})`;
document.querySelectorAll('#procs th').forEach(th => th.classList.toggle('sorted', th.dataset.key === sortKey));
}
function render(s) {
last = s;
$('hostname').textContent = s.host.hostname;
$('os').textContent = s.host.os;
$('uptime').textContent = uptime(s.host.uptime);
$('load').textContent = s.host.load_avg ? s.host.load_avg.map(v => v.toFixed(2)).join(' ') : 'n/a';
document.title = `${s.host.hostname} · CPU ${s.cpu.total.toFixed(0)}% · GlanceRiff`;
gauge('g-cpu', s.cpu.total, `${s.cpu.cores.length} cores`);
if (s.cpu.iowait != null) {
const io = document.createElement('span');
io.className = ioLevel(s.cpu.iowait);
io.textContent = s.cpu.iowait.toFixed(1) + '%';
$('g-cpu').querySelector('.detail').append(' · iowait ', io);
}
gauge('g-mem', s.mem.percent, `${bytes(s.mem.used)} / ${bytes(s.mem.total)}`);
gauge('g-swap', s.swap.percent, `${bytes(s.swap.used)} / ${bytes(s.swap.total)}`);
renderCores(s.cpu.cores);
$('nets').replaceChildren(...s.nets.map(n => row([n.name, bytesCell(n.rx_rate), bytesCell(n.tx_rate),
bytesCell(n.rx_total, 'hide-sm'), bytesCell(n.tx_total, 'hide-sm')])));
$('sensors-panel').hidden = !s.sensors.length;
$('sensors').replaceChildren(...s.sensors.map(t => row([t.label, [temp(t.temp), tempLevel(t.temp, t.critical)],
temp(t.max), temp(t.critical)])));
$('disks').replaceChildren(...s.disks.map(d => row([d.mount, [d.fs, 'hide-sm'], bytesCell(d.used), bytesCell(d.total),
[d.percent.toFixed(1) + '%', level(d.percent)], bytesCell(d.read_rate), bytesCell(d.write_rate)])));
pushHistory(s);
renderGraphs();
renderProcs();
}
document.querySelectorAll('#procs th').forEach(th => th.addEventListener('click', () => {
const key = th.dataset.key;
if (key === sortKey) sortDesc = !sortDesc;
else { sortKey = key; sortDesc = key !== 'name' && key !== 'pid'; }
renderProcs();
}));
// Whole number within 1..MAX_PROCS; an empty or invalid field falls back to the default.
function procLimit() {
const n = Math.round(Number($('proc-limit').value));
return Number.isFinite(n) && n >= 1 ? Math.min(n, MAX_PROCS) : DEFAULT_PROCS;
}
// Re-render while typing or clicking the arrows; tidy up the field and remember it once done.
$('proc-limit').addEventListener('input', renderProcs);
$('proc-limit').addEventListener('change', e => {
e.target.value = procLimit();
try { localStorage.setItem('proc_limit', e.target.value); } catch {}
renderProcs();
});
try {
const saved = localStorage.getItem('proc_limit');
if (saved !== null) $('proc-limit').value = saved;
} catch {}
// Default comes from --normalize-cpu; a choice made in this browser wins.
$('normalize').addEventListener('change', e => {
try { localStorage.setItem('normalize_cpu', e.target.checked); } catch {}
renderProcs();
});
async function loadConfig() {
let config = {}, saved = null;
try { config = await (await fetch('/api/config')).json(); } catch {}
if (config.interval_ms > 0) refreshMs = config.interval_ms;
if (config.history_len > 0) historyCap = config.history_len;
try { saved = localStorage.getItem('normalize_cpu'); } catch {}
$('normalize').checked = saved !== null ? saved === 'true' : !!config.normalize_cpu;
}
async function tick() {
try {
const res = await fetch('/api/snapshot', { cache: 'no-store' });
if (!res.ok) throw new Error(res.status);
render(await res.json());
$('status').textContent = 'updated ' + new Date().toLocaleTimeString();
$('status').classList.remove('stale');
} catch (e) {
$('status').textContent = 'connection lost, retrying…';
$('status').classList.add('stale');
}
setTimeout(tick, refreshMs);
}
async function loadHistory() {
try {
const points = await (await fetch('/api/history', { cache: 'no-store' })).json();
history = points.slice(-historyCap);
} catch {}
}
loadConfig().then(loadHistory).then(tick);
</script>
</body>
</html>