summaryrefslogtreecommitdiff
path: root/web/static/catalog.js
blob: f868a16b1c2753f5eacac4c0a2d0ce9e5c74d23a (plain) (blame)
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
const $ = id => document.getElementById(id);
let activeLog = null;
const element = (tag, text) => { const e = document.createElement(tag); e.textContent = text; return e; };
function url(address, port, protocol = 'http') {
  let host = address;
  if (!host || host === '0.0.0.0' || host === '::') host = location.hostname;
  host = host.replace(/^\[|\]$/g, '');
  return `${protocol}://${host.includes(':') ? `[${host}]` : host}:${port}/`;
}
async function control(name, command) {
  try {
    const r = await fetch(`/_cloud9/sites/${encodeURIComponent(name)}/ctl`, { method:'POST', headers:{'Content-Type':'text/plain'}, body:command });
    if (!r.ok) throw new Error(await r.text());
    $('message').textContent = `${name}: ${command} requested`;
    await refresh();
  } catch (e) { $('message').textContent = e.message; }
}
function button(text, action, disabled = false) { const b = element('button', text); b.disabled = disabled; b.onclick = action; return b; }
function listenerRow(item) {
  const tr = element('tr', '');
  const name = element('td', '');
  if (item.protocol !== 'unknown') {
    const a = element('a', item.service); a.href = url(item.address, item.port, item.protocol); name.append(a);
  } else name.textContent = item.process || 'Unknown';
  tr.append(name, element('td', `${item.address}:${item.port}${item.local ? ' · local only' : ''}`));
  const process = element('td', item.pid ? `PID ${item.pid}` : 'Unavailable'); process.className = 'process'; tr.append(process, element('td', item.evidence));
  return tr;
}
async function refresh() {
  try {
    const r = await fetch('/_cloud9/catalog.json'); if (!r.ok) throw new Error(`Dashboard: ${r.status}`);
    const data = await r.json();
    $('sites').replaceChildren();
    for (const site of data.sites) {
      const card = element('article', ''); const h = element('h2', ''); const a = element('a', site.title); a.href = url('', site.port); h.append(a);
      const s = site.status;
      card.append(h, element('code', site.root), element('p', `${s.phase}${s.watching ? ' · watch on' : ''}${s.queued ? ' · build queued' : ''}${s.exit_code !== null ? ` · exit ${s.exit_code}` : ''}`));
      if (s.finished) card.append(element('small', `Last finished ${new Date(s.finished * 1000).toLocaleString()}`), element('br', ''));
      card.append(button('Build', () => control(site.name, 'build'), !site.build), button(s.watching ? 'Stop watching' : 'Watch', () => control(site.name, s.watching ? 'watch off' : 'watch on'), !site.watch), button('Cancel', () => control(site.name, 'cancel'), !s.pid && !s.queued), button('Log', () => { activeLog = site.name; $('log-title').textContent = site.title; $('logs').showModal(); refreshLog(); }));
      $('sites').append(card);
    }
    const known = data.listeners.filter(x => x.protocol !== 'unknown');
    const unknown = data.listeners.filter(x => x.protocol === 'unknown');
    $('services').replaceChildren(...known.map(listenerRow));
    $('unknown').replaceChildren(...unknown.map(listenerRow));
    $('unknown-title').textContent = `Other listeners (${unknown.length})`;
    if (data.discovery_error) $('message').textContent = data.discovery_error;
  } catch (e) { $('message').textContent = e.message; }
}
async function refreshLog() {
  if (!activeLog) return;
  try { const r = await fetch(`/fs/sites/${encodeURIComponent(activeLog)}/log`); $('log-text').textContent = await r.text(); } catch (e) { $('log-text').textContent = e.message; }
}
$('close-log').onclick = () => { activeLog = null; $('logs').close(); };
$('logs').onclose = () => { activeLog = null; };
refresh(); setInterval(refresh, 5000); setInterval(refreshLog, 1500);