aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--static/dashboard.js38
-rw-r--r--static/deck.css7
-rw-r--r--tests/test_dashboard.mjs7
3 files changed, 36 insertions, 16 deletions
diff --git a/static/dashboard.js b/static/dashboard.js
index adf690e..c6eab3c 100644
--- a/static/dashboard.js
+++ b/static/dashboard.js
@@ -53,15 +53,27 @@ export function push(list, v, n = HISTORY) {
return list;
}
-// A filled area over a 120x100 viewBox, the newest sample at the right edge;
-// the largest value reaches 12% from the top.
-export function area(samples, max, n = HISTORY) {
- if (!samples.length) return "";
+// The trend over a 120x100 viewBox, the newest sample at the right edge; the
+// largest value reaches 12% from the top.
+export function line(samples, max, n = HISTORY) {
const x = (i) => ((n - samples.length + i) * 120) / (n - 1);
const y = (v) => 100 - Math.min(1, v / (max || 1)) * 88;
- return `M${x(0)},100 ` + samples.map((v, i) => `L${x(i)},${y(v)}`).join(" ") + " L120,100 Z";
+ return samples.map((v, i) => `${i ? "L" : "M"}${x(i)},${y(v)}`).join(" ");
+}
+
+// The area under line(), closed along the bottom edge. It is filled only:
+// stroking it would also draw the bottom and side edges.
+export function area(samples, max, n = HISTORY) {
+ const l = line(samples, max, n);
+ return l && `M${l.slice(1, l.indexOf(","))},100 L${l.slice(1)} L120,100 Z`;
}
+// A graph's two paths: the filled area, then the stroked line over it.
+const graph = ([fill, stroke], samples, max) => {
+ fill.setAttribute("d", area(samples, max));
+ stroke.setAttribute("d", line(samples, max));
+};
+
// The top 3 paid models (llm.py sorts them by cost), then the rest as "other".
export function spendParts(models) {
const paid = (models ?? []).filter((m) => m.cost > 0);
@@ -114,12 +126,12 @@ const SKELETON = `
<span class="sep">|</span><span><i class="nf">${G.clock}</i><span class="up"></span></span>
<span class="sep upd">|</span><span class="upd n-upd"></span></div>
<div class="chips sys">
- <div class="chip c-cpu"><svg viewBox="0 0 120 100" preserveAspectRatio="none"><path class="hy"/></svg>
+ <div class="chip c-cpu"><svg viewBox="0 0 120 100" preserveAspectRatio="none"><path class="hy"/><path class="hy ln"/></svg>
<i class="nf ic">${G.cpu}</i><span class="col"><span class="n"></span><span class="v num"></span></span><span class="s"></span></div>
<div class="chip c-mem"><i class="fill"></i><i class="nf ic">${G.mem}</i><span class="v num"></span><span class="s"></span></div>
- <div class="chip c-gpu"><svg viewBox="0 0 120 100" preserveAspectRatio="none"><path class="hy"/></svg>
+ <div class="chip c-gpu"><svg viewBox="0 0 120 100" preserveAspectRatio="none"><path class="hy"/><path class="hy ln"/></svg>
<i class="nf ic">${G.gpu}</i><span class="col"><span class="n"></span><span class="v num"></span></span><span class="s"></span></div>
- <div class="chip c-net"><svg viewBox="0 0 120 100" preserveAspectRatio="none"><path class="hy"/><path class="up"/></svg>
+ <div class="chip c-net"><svg viewBox="0 0 120 100" preserveAspectRatio="none"><path class="hy"/><path class="hy ln"/><path class="up"/><path class="up ln"/></svg>
<i class="nf ic">${G.net}</i><span class="v num"></span></div>
</div>
<div class="chips aux">
@@ -174,7 +186,7 @@ export default {
$(".n-upd").textContent = `${st.updates} update${st.updates === 1 ? "" : "s"} available`;
push(hist.cpu, st.cpu); push(hist.down, st.net.down); push(hist.up, st.net.up);
$(".c-cpu .n").textContent = st.cpuName ?? "";
- $(".c-cpu path").setAttribute("d", area(hist.cpu, Math.max(25, ...hist.cpu)));
+ graph($(".c-cpu").querySelectorAll("path"), hist.cpu, Math.max(25, ...hist.cpu));
chip(".c-cpu", `${st.cpu}%`, st.cpuTemp != null ? `${st.cpuTemp}°C` : "", null, st.cpuTemp >= 85);
chip(".c-mem", gib(st.memUsed * 1024), `/ ${Math.round(st.memTotal / 1048576)} GiB`,
(100 * st.memUsed) / st.memTotal);
@@ -187,15 +199,15 @@ export default {
// older kernel) the temperature takes the big value.
if (g.util != null) {
push(hist.gpu, g.util);
- $(".c-gpu path").setAttribute("d", area(hist.gpu, Math.max(25, ...hist.gpu)));
+ graph($(".c-gpu").querySelectorAll("path"), hist.gpu, Math.max(25, ...hist.gpu));
}
const t = g.temp != null ? `${g.temp}°C` : "—";
chip(".c-gpu", g.util != null ? `${g.util}%` : t, g.util != null ? t : "", null, g.temp >= 85);
}
const max = Math.max(1, ...hist.down, ...hist.up);
- const [down, up] = $(".c-net").querySelectorAll("path");
- down.setAttribute("d", area(hist.down, max));
- up.setAttribute("d", area(hist.up, max));
+ const net = [...$(".c-net").querySelectorAll("path")];
+ graph(net.slice(0, 2), hist.down, max);
+ graph(net.slice(2), hist.up, max);
chip(".c-net", `↓${rate(st.net.down)} ↑${rate(st.net.up)}`);
});
diff --git a/static/deck.css b/static/deck.css
index 74cf147..b6d2202 100644
--- a/static/deck.css
+++ b/static/deck.css
@@ -672,8 +672,11 @@ select {
.dash .chip svg, .dash .chip .fill { position: absolute; inset: 0; pointer-events: none; }
.dash .chip svg { width: 100%; height: 100%; }
.dash .chip svg path { stroke-width: 1.5; vector-effect: non-scaling-stroke; }
-.dash .chip svg path.hy { fill: url(#hyfill); stroke: url(#hyline); }
-.dash .chip svg path.up { fill: color-mix(in srgb, var(--udt-text) 8%, transparent); stroke: color-mix(in srgb, var(--udt-text) 30%, transparent); }
+.dash .chip svg path.hy { fill: url(#hyfill); }
+.dash .chip svg path.up { fill: color-mix(in srgb, var(--udt-text) 8%, transparent); }
+.dash .chip svg path.ln { fill: none; }
+.dash .chip svg path.hy.ln { stroke: url(#hyline); }
+.dash .chip svg path.up.ln { stroke: color-mix(in srgb, var(--udt-text) 30%, transparent); }
.dash .chip .fill { right: auto; width: 0; background: color-mix(in srgb, var(--udt-accent) 20%, transparent); transition: width .4s; }
/* Charging: streaks rain right to left into the fill's edge. The strip sits past
the fill and the chip's overflow clips it; each layer moves a whole tile per
diff --git a/tests/test_dashboard.mjs b/tests/test_dashboard.mjs
index 2fc19eb..dac4e5b 100644
--- a/tests/test_dashboard.mjs
+++ b/tests/test_dashboard.mjs
@@ -15,7 +15,7 @@
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import assert from "node:assert/strict";
-import { area, push, spendParts, batteryGlyph, HISTORY } from "../static/dashboard.js";
+import { area, line, push, spendParts, batteryGlyph, HISTORY } from "../static/dashboard.js";
import { busy as printing } from "../static/modules/k1c.js";
import { busy as building } from "../static/modules/sbbuilder.js";
import { busy as live } from "../static/modules/obs.js";
@@ -32,6 +32,11 @@ const d = area([0, 10], 10);
assert.match(d, /^M[\d.]+,100 /);
assert.match(d, / L120,100 Z$/);
assert.ok(d.includes(",12"), "the max sample sits at 12% from the top");
+// line: the same samples, open, so no bottom or side edge is stroked
+assert.equal(line([], 10), "");
+const l = line([0, 10], 10);
+assert.match(l, /^M[\d.]+,100 L120,12$/);
+assert.ok(d.includes(l.slice(1)), "area follows line");
// spendParts: top 3 paid models, then "other" with the rest
const models = [5, 4, 3, 2, 1, 0].map((c, i) => ({ model: `m${i}`, cost: c }));