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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
// deck-it - the Dashboard: one fixed screen
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// The Dashboard tab: a top band of chips, music and spending always on, a
// row of chips for one to three busy modules, each opening its full slot,
// and the Vikunja tasks due now, only while there are some.
// The page keeps the trend history; the modules' APIs are the existing ones.
import { offline } from "./offline.js";
import { HOLD_MS, buzz } from "./hold.js";
export const HISTORY = 120; // trend samples: two minutes at 1 s
const RUNNING = { k1c: "K1C", sbbuilder: "build", obs: "OBS" }; // the running row's order and chip labels
const G = { cpu: "", mem: "\u{f035b}", gpu: "\u{f08ae}", net: "\u{f04e1}",
wifi: "", charging: "\u{f0084}", tux: "", clock: "",
todo: "\u{f0131}" };
const BATTERY = ["", "", "", "", ""];
// Nerd Font distro logos by os-release ID.
const DISTRO = {
slackware: "", arch: "", debian: "", fedora: "",
ubuntu: "", gentoo: "", opensuse: "", void: "",
};
const SPEND = ["var(--udt-accent)", "var(--udt-blue, #89b4fa)", "var(--udt-green, #a6e3a1)"];
const OTHER = "var(--udt-surface2)";
const esc = (s) => String(s ?? "").replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
const rate = (b) => (b >= 1048576 ? `${(b / 1048576).toFixed(1)}M` : `${Math.round((b ?? 0) / 1024)}k`);
const gib = (b) => (b / 2 ** 30).toFixed(1);
const money = (v) => (v == null ? "—" : `$${Number(v).toFixed(2)}`);
const tok = (n) => (n >= 1e9 ? `${(n / 1e9).toFixed(1)}B` : n >= 1e6 ? `${(n / 1e6).toFixed(1)}M`
: n >= 1e3 ? `${(n / 1e3).toFixed(1)}k` : String(n ?? 0));
const pct = (v) => (v == null ? "—" : `${Math.round(v)}%`);
const short = (m) => String(m ?? "").split("/").pop();
const uptime = (s) => {
const d = Math.floor(s / 86400), h = Math.floor((s % 86400) / 3600);
return d ? `${d}d ${h}h` : h ? `${h}h` : `${Math.floor(s / 60)}m`;
};
export function push(list, v, n = HISTORY) {
list.push(v);
if (list.length > n) list.shift();
return list;
}
// 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 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);
const rest = paid.slice(3).reduce((a, m) => a + m.cost, 0);
return rest > 0 ? [...paid.slice(0, 3), { model: "other", cost: rest }] : paid.slice(0, 3);
}
export function batteryGlyph(b) {
if (b?.status === "charging" || b?.status === "full") return G.charging;
return BATTERY[Math.min(4, Math.max(0, Math.round((b?.level ?? 0) / 25)))];
}
// A line too long for its slot slides to its end and back (deck.css .marq);
// one that fits stays still. Only changed values are written, so the
// animation is not restarted by every poll.
function marquee(el) {
if (!el.getClientRects().length) return; // not shown: nothing to measure
for (const line of el.querySelectorAll(".what, .meta")) {
const over = line.firstElementChild.offsetWidth - line.clientWidth;
line.classList.toggle("marq", over > 0);
const d = `${-Math.max(0, over)}px`;
if (line.style.getPropertyValue("--d") !== d) {
line.style.setProperty("--d", d);
line.style.setProperty("--t", `${2 + Math.max(0, over) / 30}s`); // ~30 px/s plus the pauses
}
}
}
// A todo's due label: "overdue 2d", "today 15:00", or "reminder" when it
// only has a reminder.
export function dueLabel(iso, now = new Date()) {
if (!iso) return "reminder";
const due = new Date(iso);
if (due.toDateString() === now.toDateString() && due >= now)
return `today ${due.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit", hourCycle: "h23" })}`;
const h = (now - due) / 3600000;
return h < 24 ? `overdue ${Math.max(1, Math.round(h))}h` : `overdue ${Math.floor(h / 24)}d`;
}
// Tap, hold, or swipe left to right on one element. A swipe past a third
// of its width fires; a shorter one springs back. Moving cancels the hold.
function gestures(el, { tap, hold, swipe }) {
let x0 = null, dx = 0, timer = null, sliding = false;
const reset = () => {
clearTimeout(timer);
timer = null; x0 = null; sliding = false;
el.classList.remove("holding", "sliding");
};
el.addEventListener("pointerdown", (e) => {
x0 = e.clientX; dx = 0;
el.setPointerCapture(e.pointerId);
el.classList.add("holding");
timer = setTimeout(() => { reset(); buzz(); hold(); }, HOLD_MS);
});
el.addEventListener("pointermove", (e) => {
if (x0 == null) return;
dx = Math.max(0, e.clientX - x0);
if (!sliding && Math.abs(e.clientX - x0) > 12) {
clearTimeout(timer); timer = null; sliding = true;
el.classList.remove("holding"); el.classList.add("sliding");
}
if (sliding) el.style.transform = `translateX(${dx}px)`;
});
el.addEventListener("pointerup", () => {
const wasTap = timer != null, wasSwipe = sliding && dx > el.offsetWidth / 3;
reset();
if (wasSwipe) { el.style.transform = "translateX(110%)"; swipe(); }
else el.style.transform = "";
if (wasTap) tap();
});
el.addEventListener("pointercancel", () => { reset(); el.style.transform = ""; });
}
const SKELETON = `
<svg class="dash-defs" width="0" height="0" aria-hidden="true"><defs>
<linearGradient id="hyline" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="120" y2="0"><stop offset="0" style="stop-color:var(--udt-border-1)"/>
<stop offset=".5" style="stop-color:var(--udt-border-2)"/><stop offset="1" style="stop-color:var(--udt-border-3)"/></linearGradient>
<linearGradient id="hyfill" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="0" y2="100">
<stop offset="0" style="stop-color:var(--udt-border-2);stop-opacity:.45"/>
<stop offset="1" style="stop-color:var(--udt-border-1);stop-opacity:.05"/></linearGradient>
</defs></svg>
<section class="dash-top">
<div class="dash-id"><i class="nf logo"></i><span><b class="os"></b> <span class="codename"></span></span>
<span class="sep">|</span><span><i class="nf">${G.tux}</i><span class="kernel"></span></span>
<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"/><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"/><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="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">
<div class="chip c-ctx"><i class="fill"></i><span class="brand claude ic"></span><span class="v num"></span><span class="k">context</span></div>
<div class="chip c-5h"><i class="fill"></i><span class="brand claude ic"></span><span class="v num"></span><span class="k">5h</span></div>
<div class="chip c-7d"><i class="fill"></i><span class="brand claude ic"></span><span class="v num"></span><span class="k">7d</span></div>
<div class="chip c-bat"><i class="fill"></i><i class="nf ic"></i><span class="v num"></span></div>
<div class="chip c-wifi"><i class="fill"></i><i class="nf ic">${G.wifi}</i><span class="v num"></span><span class="s"></span></div>
</div>
</section>
<section class="dash-mid">
<div class="dash-always"><div class="card dash-music"></div><div class="card dash-spend"></div></div>
<div class="dash-running" hidden></div>
<div class="dash-todo" hidden></div>
</section>`;
export default {
// names: the enabled modules; poll(name, seconds, update) and open(tab)
// come from app.js.
mount(section, names, { poll, open }) {
const on = new Set(names);
const root = document.createElement("div");
root.className = "dash";
root.innerHTML = SKELETON;
section.append(root);
const $ = (s) => root.querySelector(s);
const chip = (sel, v, s, fill, warn) => {
const c = $(sel);
c.querySelector(".v").textContent = v;
if (s != null) c.querySelector(".s").textContent = s;
if (fill != null) c.querySelector(".fill").style.width = `${Math.max(0, Math.min(100, fill))}%`;
c.classList.toggle("hot", Boolean(warn));
};
// A disabled module's parts are left out.
$(".dash-id").hidden = !on.has("stats");
$(".chips.sys").hidden = !on.has("stats");
for (const c of [".c-ctx", ".c-5h", ".c-7d"]) $(c).hidden = !on.has("llm");
$(".dash-spend").hidden = !on.has("llm");
$(".c-bat").hidden = $(".c-wifi").hidden = !on.has("tablet");
$(".dash-music").hidden = !on.has("music");
const hist = { cpu: [], gpu: [], down: [], up: [] };
if (on.has("stats")) poll("stats", 1, (st) => {
$(".logo").textContent = DISTRO[st.os?.id] ?? G.tux;
$(".os").textContent = st.os?.name ?? "";
$(".codename").textContent = st.os?.codename ? `-${st.os.codename}` : "";
$(".codename").hidden = !st.os?.codename;
$(".kernel").textContent = st.os?.kernel ?? "";
$(".up").textContent = uptime(st.uptime);
for (const e of root.querySelectorAll(".upd")) e.hidden = st.updates == null;
$(".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 ?? "";
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);
const g = st.gpu;
$(".c-gpu").hidden = !g;
if (g) {
$(".c-gpu .n").textContent = g.name ?? "";
// Like the CPU chip: usage on a trend, temperature beside it. VRAM
// and fan are on keystone's performance page. Without usage (an
// older kernel) the temperature takes the big value.
if (g.util != null) {
push(hist.gpu, g.util);
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 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)}`);
});
const program = (title, p, brand) => {
const h3 = `<h3>${brand ? `<span class="brand ${brand}"></span>` : ""}${esc(title)}</h3>`;
const model = `<span class="model">${esc(p.model ? short(p.model) : "—")}</span>`;
if (!(p.cost > 0)) { // local or free models: tokens, not dollars
return `<div class="prog inline">${h3}${model}
<span class="tok"><span class="lbl">session</span><b class="num">${tok(p.session_tokens)}</b></span>
<span class="tok"><span class="lbl">total</span><b class="num">${tok(p.tokens)}</b></span></div>`;
}
const parts = spendParts(p.models);
const total = parts.reduce((a, m) => a + m.cost, 0);
const color = (m, i) => (m.model === "other" ? OTHER : SPEND[i]);
return `<div class="prog"><header>${h3}${model}</header>
<div class="nums">
<div><div class="lbl">session</div><div class="big num">${money(p.session_cost)}</div>
<div class="sub">${tok(p.session_tokens)} · ${p.session_calls ?? 0} calls</div></div>
<div><div class="lbl">total</div><div class="big num">${money(p.cost)}</div>
<div class="sub">${tok(p.tokens)} · ${p.calls ?? 0} calls</div></div>
</div>
<div class="stack">${parts.map((m, i) =>
`<i style="width:${(100 * m.cost) / total}%;background:${color(m, i)}"></i>`).join("")}</div>
<div class="legend">${parts.map((m, i) =>
`<span style="--c:${color(m, i)}">${esc(short(m.model))} ${money(m.cost)}</span>`).join("")}</div>
</div>`;
};
if (on.has("llm")) poll("llm", 5, (st) => {
const spend = $(".dash-spend");
if (st.offline) {
offline(spend, { icon: "/logos/claude.svg", title: "Usage dashboard is off",
detail: "Start llm-cost-dashboard and the spending appears here." });
for (const c of [".c-ctx", ".c-5h", ".c-7d"]) chip(c, "—", null, 0);
return;
}
const c = st.claude ?? {};
chip(".c-ctx", pct(c.context_pct), null, c.context_pct, c.context_pct >= 80);
chip(".c-5h", pct(c.five_hour_pct), null, c.five_hour_pct, c.five_hour_pct >= 80);
chip(".c-7d", pct(c.seven_day_pct), null, c.seven_day_pct, c.seven_day_pct >= 80);
spend._offline = null;
spend.innerHTML = program("opencode", st.opencode ?? {}, "opencode") + program("Local Agent", st.assistant ?? {});
});
if (on.has("tablet")) poll("tablet", 10, (t) => {
const b = t.battery ?? {}, w = t.wifi ?? {};
const charging = b.status === "charging" || b.status === "full";
$(".c-bat .ic").textContent = batteryGlyph(b);
chip(".c-bat", b.level != null ? `${b.level}%` : "—", null, b.level, b.level < 20 && !charging);
$(".c-bat").classList.toggle("charging", b.status === "charging");
chip(".c-wifi", w.rssi != null ? `${w.rssi} dBm` : "—", w.band ?? "",
w.rssi == null ? 0 : 2 * (w.rssi + 100)); // -100 dBm empty, -50 full
});
if (on.has("music")) import("./modules/music.js").then(({ default: music, pickGame }) => {
const el = $(".dash-music");
music.mount(el);
// The card shows a running game (PC or Xbox) instead of the music.
const last = { music: {}, games: null, xbox: null };
const draw = () => music.update(last.music, el, pickGame(last.games, last.xbox));
poll("music", music.interval ?? 2, (st) => { last.music = st; draw(); });
if (on.has("games")) poll("games", 5, (st) => { last.games = st; draw(); });
if (on.has("xbox")) poll("xbox", 30, (st) => { last.xbox = st; draw(); });
});
// Vikunja tasks due now: tap opens one on the PC, hold marks it done,
// a swipe right snoozes it 30 min. Redrawn only when the list changes,
// and never under a finger.
const todo = $(".dash-todo");
const drawTodo = (st) => {
const key = JSON.stringify(st);
if (todo._key === key || todo.querySelector(".holding, .sliding")) return;
todo._key = key;
const tasks = st.tasks ?? [];
todo.hidden = !tasks.length && !st.error;
todo.textContent = "";
if (st.error) {
todo.innerHTML = `<div class="td err">Vikunja unreachable</div>`;
todo.firstChild.title = st.error;
return;
}
const act = async (name, id, line) => {
let ok = false;
try {
ok = (await fetch(`/api/todo/${name}`, {
method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ id }) })).ok;
if (ok && name !== "open") drawTodo(await (await fetch("/api/todo")).json());
} catch {}
if (!ok) { // back in place, red for a moment
line.classList.remove("gone");
line.style.transform = "";
line.classList.add("fail");
setTimeout(() => line.classList.remove("fail"), 1500);
}
};
for (const t of tasks.slice(0, 3)) {
const line = document.createElement("div");
line.className = "td";
line.innerHTML = `<i class="nf"></i><span class="col"><span class="tt"></span><span class="due"></span></span>`;
line.querySelector(".nf").textContent = G.todo;
line.querySelector(".tt").textContent = t.title;
const due = dueLabel(t.due);
line.querySelector(".due").textContent = due;
line.classList.toggle("late", due.startsWith("overdue"));
gestures(line, { tap: () => act("open", t.id, line),
hold: () => { line.classList.add("gone"); act("done", t.id, line); },
swipe: () => act("snooze", t.id, line) });
todo.append(line);
}
if (tasks.length > 3) {
const more = document.createElement("div");
more.className = "td more";
more.textContent = `+${tasks.length - 3}`;
todo.append(more);
}
};
if (on.has("todo")) poll("todo", 10, drawTodo);
// The running row: a chip per module, created now in RUNNING order and
// shown while it is busy. A tap opens its full slot, and the module's
// detail(state, el) when it has one, centred over the page; a tap
// outside closes it (closedby="any", Chrome 134+) without reaching what is
// under it, a tap inside opens the module's tab.
const row = $(".dash-running");
for (const name of Object.keys(RUNNING).filter((n) => on.has(n))) {
const chip = document.createElement("div");
chip.className = `chip run-${name}`;
chip.hidden = true;
chip.innerHTML = `<i class="fill"></i><i class="nf ic"></i><span class="v num"></span><span class="k"></span>`;
chip.querySelector(".k").textContent = RUNNING[name];
const pop = document.createElement("dialog");
pop.setAttribute("closedby", "any");
pop.className = "card run-pop";
pop.innerHTML = `<div class="slot slot-${name}"></div><div class="detail"></div>`;
const [slotEl, detailEl] = pop.children;
row.append(chip, pop);
chip.addEventListener("click", () => { pop.showModal(); marquee(slotEl); });
import(`./modules/${name}.js`).then((mod) => {
if (!mod.busy || !mod.slot) return;
const tab = mod.default?.tab;
pop.addEventListener("click", (e) => {
if (tab && !e.target.closest("button")) { pop.close(); open(tab); }
});
// ponytail: the K1C and OBS tabs poll the same API; two polls a second, one cache if it matters.
poll(name, mod.interval ?? mod.default?.interval ?? 2, (st) => {
chip.hidden = !mod.busy(st);
if (chip.hidden) { pop.close(); return; }
mod.slot(st, slotEl);
mod.detail?.(st, detailEl);
// ponytail: the chip mirrors the slot's icon, value and bar, so a
// module draws one slot; a chip(state) export if one ever needs more.
const bar = slotEl.querySelector(".bar");
chip.querySelector(".ic").textContent = slotEl.querySelector(".sic")?.textContent ?? "";
chip.querySelector(".v").textContent = slotEl.querySelector(".pct")?.textContent ?? "";
chip.querySelector(".fill").style.width = bar?.querySelector("i").style.width ?? "";
chip.classList.toggle("busy", !!bar?.classList.contains("busy"));
chip.classList.toggle("rec", !!slotEl.querySelector(".sic.rec"));
row.hidden = !row.querySelector(".chip:not([hidden])");
if (pop.open) marquee(slotEl);
});
});
}
},
};
|