aboutsummaryrefslogtreecommitdiffstats
path: root/static/modules/sbbuilder.js
blob: 6131bc08c7d0f01219df81703ec42e8c15ab2ea3 (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
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
// deck-it - the build VM's running slot
// 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 build VM has no tab: on the Dashboard it is a running chip while a job
// is active, opening to its slot and detail, and nothing otherwise.

export const interval = 10;   // each poll is an ssh round trip

const ACTIVE = ["running", "busy", "starting"];
const active = (st) => (st.jobs ?? []).filter((j) => ACTIVE.includes(j.status));

export const busy = (st) => !st.offline && active(st).length > 0;

export function slot(st, el) {
  if (!el.firstChild) {
    el.innerHTML = `<i class="nf sic"></i>
      <span class="what"><span></span></span><span class="pct num"></span>
      <span class="meta"><span></span></span>
      <div class="bar busy"><i></i></div>`;
  }
  // A running build carries sbb-remote's progress: the package on line 1, done
  // of total (dependencies included) on the right, op, its ETA, the estimated
  // end and failures on line 2, and a real bar. Every slot width works the
  // same: the lines slide when they do not fit.
  const jobs = active(st);
  const p = jobs[0]?.progress;
  const load = st.stats?.load;
  const hm = (t) => new Date(t * 1000).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
  el.querySelector(".what > span").textContent =
    p?.current?.split("/").pop() || `Build VM · ${jobs[0]?.kind || "job"}`;
  el.querySelector(".pct").textContent =
    p?.total ? `${p.done}/${p.total}` : jobs.length > 1 ? `${jobs.length} jobs` : "";
  el.querySelector(".meta > span").textContent = [
    p ? p.op : jobs[0]?.status,
    p?.eta && `ETA ${p.eta}`,
    p?.end && `ends ~${hm(p.end)}`,
    p?.failed && `${p.failed} failed`,
    load && `load ${load[0].toFixed(2)}`,
  ].filter(Boolean).join(" · ");
  const bar = el.querySelector(".bar");
  bar.classList.toggle("busy", !p?.total);
  bar.firstChild.style.width = p?.total ? `${Math.min(100, (100 * p.done) / p.total)}%` : "";
}

// Each package of a job's plan with where it stands: its result once done
// (progress.results while running, sbb-remote 0.13.1; results once ended),
// "building" for the one in progress, "waiting" for the rest.
export function planRows(job) {
  const results = job?.progress?.results ?? job?.results ?? {};
  return (job?.plan ?? []).map((name) => ({
    name,
    status: results[name] ?? (name === job.progress?.current ? "building" : "waiting"),
  }));
}

// The dashboard's own cpu and memory glyphs, and nf-md-harddisk.
const VM_ICONS = { cpu: "\u{f2db}", mem: "\u{f035b}", disk: "\u{f02ca}" };
const gib = (b) => `${(b / 2 ** 30).toFixed(1)} GiB`;
const since = (t) => {
  const m = Math.max(0, Math.round((Date.now() / 1000 - t) / 60));
  return m < 60 ? `${m} min` : `${Math.floor(m / 60)} h ${m % 60} min`;
};

// The opened chip's extra lines: the command and its start, the VM's CPU,
// memory and disk as meters, and every package of the plan. The plan is redrawn only when a
// package moves on.
export function detail(st, el) {
  const job = active(st)[0];
  if (!el.firstChild) {
    el.innerHTML = `<div class="dl cmd"><span class="k">job</span><span class="v"></span></div>
      <div class="vm">${Object.entries(VM_ICONS).map(([k, ic]) =>
        `<div class="chip m-${k}" title="${k}"><i class="fill"></i><i class="nf ic">${ic}</i><span class="v num"></span><span class="s"></span></div>`).join("")}</div>
      <ol class="plan"></ol>`;
  }
  const hm = (t) => new Date(t * 1000).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
  el.querySelector(".cmd .v").textContent = job ? [
    [job.kind, ...(job.args ?? [])].join(" "),
    job.start && `started ${hm(job.start)}, ${since(job.start)} ago`,
  ].filter(Boolean).join(" · ") : "";
  const s = st.stats ?? {};
  const meter = (k, share, v, sub) => {
    const c = el.querySelector(`.m-${k}`);
    c.querySelector(".fill").style.width = `${Math.min(100, Math.max(0, share * 100))}%`;
    c.querySelector(".v").textContent = v;
    c.querySelector(".s").textContent = sub;
  };
  meter("cpu", (s.cpu ?? 0) / 100, s.cpu == null ? "–" : `${Math.round(s.cpu)}%`,
        s.load ? `load ${s.load.map((x) => x.toFixed(1)).join(" ")}` : "");
  const used = s.mem ? s.mem.total - s.mem.available : 0;
  meter("mem", s.mem ? used / s.mem.total : 0, s.mem ? gib(used) : "–", s.mem ? `of ${gib(s.mem.total)}` : "");
  meter("disk", s.disk ? 1 - s.disk.free / s.disk.total : 0, s.disk ? `${gib(s.disk.free)}` : "–", s.disk ? "free" : "");
  const rows = planRows(job);
  const key = JSON.stringify(rows);
  const plan = el.querySelector(".plan");
  if (plan._key === key) return;
  plan._key = key;
  plan.hidden = rows.length === 0;
  plan.replaceChildren(...rows.map((r) => {
    const li = document.createElement("li");
    li.className = r.status;
    li.innerHTML = `<span class="pn"></span><span class="ps"></span>`;
    li.firstChild.textContent = r.name;
    li.lastChild.textContent = r.status;
    return li;
  }));
}