aboutsummaryrefslogtreecommitdiffstats
path: root/static/modules/stats.js
blob: c76e8899d07c67911a896fccf6f8e46323d8ead4 (plain)
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
// deck-it - stats panel
// 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.

const gib = (kb) => (kb / 1048576).toFixed(1);
const bytes = (b) => (b == null ? "?" : (b / 1073741824).toFixed(1));
const rate = (b) => (b >= 1048576 ? `${(b / 1048576).toFixed(1)}M` : `${Math.round(b / 1024)}k`);
const uptime = (s) => {
  const d = Math.floor(s / 86400);
  const h = Math.floor((s % 86400) / 3600);
  return d ? `${d}d ${h}h` : `${h}h`;
};

function tile(label, value, sub) {
  const d = document.createElement("div");
  d.className = "mtile";
  const l = document.createElement("span");
  l.className = "mlabel";
  l.textContent = label;
  const v = document.createElement("span");
  v.className = "mvalue num";
  v.textContent = value;
  const s = document.createElement("span");
  s.className = "msub";
  s.textContent = sub || "";
  d.append(l, v, s);
  return d;
}

export default {
  title: "System",
  interval: 1,

  mount(el) {
    el.innerHTML = `<div class="stats"></div>`;
  },

  update(st, el) {
    const box = el.querySelector(".stats");
    box.textContent = "";
    const g = st.gpu;
    box.append(
      tile("CPU", `${st.cpu}%`, st.cpuTemp != null ? `${st.cpuTemp}°C` : "—"),
      tile("Memory", gib(st.memUsed), `/ ${gib(st.memTotal)} GiB`),
      tile("GPU", g?.temp != null ? `${g.temp}°` : "—",
           g ? `${bytes(g.memUsed)} / ${bytes(g.memTotal)} GiB` : "no GPU"),
      tile("Network", `↓${rate(st.net.down)}`, `↑${rate(st.net.up)}`),
    );
    const foot = document.createElement("div");
    foot.className = "mfoot";
    foot.textContent = `up ${uptime(st.uptime)}`;
    box.append(foot);
  },
};