aboutsummaryrefslogtreecommitdiffstats
path: root/static/modules/stats.js
blob: a119c6ce8112d02e813e4f8921f5bdccb8ec21ea (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
const gb = (kb) => (kb / 1048576).toFixed(1);
const rate = (b) => (b >= 1048576 ? `${(b / 1048576).toFixed(1)} MB/s` : `${Math.round(b / 1024)} KB/s`);
const uptime = (s) => {
  const d = Math.floor(s / 86400);
  const h = Math.floor((s % 86400) / 3600);
  const m = Math.floor((s % 3600) / 60);
  return d ? `${d}d ${h}h` : `${h}h ${m}m`;
};
const gib = (b) => (b / 1073741824).toFixed(1);
const gpu = (g) => {
  if (!g) return "—";
  if (g.asleep) return `${g.name} (asleep)`;
  const parts = [g.name];
  if (g.util != null) parts.push(`${g.util}%`);
  if (g.temp != null) parts.push(`${g.temp}°C`);
  if (g.fan != null) parts.push(`${g.fan} rpm`);
  if (g.memUsed != null) {
    parts.push(g.memTotal ? `${gib(g.memUsed)} / ${gib(g.memTotal)} GiB` : `${gib(g.memUsed)} GiB`);
  }
  return parts.join(" · ");
};

function row(label, value) {
  const div = document.createElement("div");
  div.className = "row";
  const name = document.createElement("span");
  name.className = "muted";
  name.textContent = label;
  const val = document.createElement("span");
  val.className = "num";
  val.textContent = value;
  div.append(name, val);
  return div;
}

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

  mount(el) {
    const box = document.createElement("div");
    box.className = "stats";
    el.append(box);
  },

  update(st, el) {
    const box = el.querySelector(".stats");
    box.textContent = "";
    box.append(
      row("CPU", `${st.cpu}%${st.cpuTemp != null ? ` · ${st.cpuTemp}°C` : ""}`),
      row("Memory", `${gb(st.memUsed)} / ${gb(st.memTotal)} GiB`),
      row("GPU", gpu(st.gpu)),
      row("Network", `↓ ${rate(st.net.down)}  ↑ ${rate(st.net.up)}`),
      row("Uptime", uptime(st.uptime)),
    );
  },
};