261 lines
11 KiB
JavaScript
261 lines
11 KiB
JavaScript
// /logs.html entry point: log viewer with three sub-tabs (AGENT, INFRA,
|
|
// SYSTEM). Build log history has moved to /builds.html.
|
|
// AGENT — per-container journald viewer, backed by GET /api/journal/{name}
|
|
// INFRA — infra-container journald viewer (hive-ci, hive-forge, …),
|
|
// same API but full machine journal only (no unit filter)
|
|
// SYSTEM — host service logs, backed by GET /api/journal-host
|
|
//
|
|
// Tab routing via URL hash (#agent, #infra, #system). Default: #agent.
|
|
//
|
|
// URL params `?agent=name` and `?unit=svc` pre-select the agent + unit.
|
|
// When `?agent=` names an infra container the INFRA tab is activated instead.
|
|
// Last-fetched timestamp is shown next to the refresh button on AGENT,
|
|
// INFRA, and SYSTEM tabs so the operator knows how stale the output is.
|
|
|
|
import {
|
|
$, fmtAgeSecs, initServerWarnings,
|
|
} from './common.js';
|
|
import { el } from '@hive/shared/dom.js';
|
|
import '@hive/shared/hive-tab-strip.js';
|
|
|
|
(() => {
|
|
initServerWarnings();
|
|
|
|
// ─── tab routing ──────────────────────────────────────────────────────
|
|
// The shared hash-routed tab strip (@hive/shared/tabs.js) handles the
|
|
// active toggle + aria-selected + pane visibility + lazy fetches via
|
|
// onShow. Constructed in the init block below (after the fetch fns +
|
|
// element refs it depends on exist). `logTabs.active()` replaces the
|
|
// old `activeTab()`.
|
|
let logTabs;
|
|
|
|
// ─── helpers ──────────────────────────────────────────────────────────
|
|
|
|
// Format a last-fetched timestamp: "fetched just now" / "fetched 3m ago".
|
|
function fmtFetchTs(fetchedAt) {
|
|
const ageSecs = Math.floor((Date.now() - fetchedAt) / 1000);
|
|
return 'fetched ' + (ageSecs < 5 ? 'just now' : fmtAgeSecs(ageSecs) + ' ago');
|
|
}
|
|
|
|
// ─── AGENT tab ────────────────────────────────────────────────────────
|
|
|
|
const agentSelect = $('agent-select');
|
|
const agentUnitSelect = $('agent-unit-select');
|
|
const agentRefresh = $('agent-refresh');
|
|
const agentOutput = $('agent-output');
|
|
const agentFetchTs = $('agent-fetch-ts');
|
|
|
|
let agentFetching = false;
|
|
let agentLastFetch = 0;
|
|
async function fetchAgent() {
|
|
if (!agentSelect || !agentOutput) return;
|
|
const name = agentSelect.value;
|
|
if (!name) { agentOutput.textContent = 'select an agent above'; return; }
|
|
if (agentFetching) return;
|
|
agentFetching = true;
|
|
agentOutput.textContent = 'fetching…';
|
|
if (agentFetchTs) agentFetchTs.hidden = true;
|
|
const unit = agentUnitSelect ? agentUnitSelect.value : '';
|
|
const params = new URLSearchParams({ lines: '500' });
|
|
if (unit) params.set('unit', unit);
|
|
try {
|
|
const resp = await fetch('/api/journal/' + encodeURIComponent(name) + '?' + params);
|
|
const text = await resp.text();
|
|
agentOutput.textContent = resp.ok ? (text || '(empty)') : 'error ' + resp.status + '\n' + text;
|
|
agentOutput.scrollTop = agentOutput.scrollHeight;
|
|
if (resp.ok) {
|
|
agentLastFetch = Date.now();
|
|
if (agentFetchTs) {
|
|
agentFetchTs.textContent = fmtFetchTs(agentLastFetch);
|
|
agentFetchTs.hidden = false;
|
|
}
|
|
}
|
|
} catch (err) {
|
|
agentOutput.textContent = 'fetch failed: ' + err;
|
|
} finally {
|
|
agentFetching = false;
|
|
}
|
|
}
|
|
|
|
if (agentSelect) agentSelect.addEventListener('change', fetchAgent);
|
|
if (agentUnitSelect) agentUnitSelect.addEventListener('change', fetchAgent);
|
|
if (agentRefresh) agentRefresh.addEventListener('click', fetchAgent);
|
|
|
|
// ─── INFRA tab ────────────────────────────────────────────────────────
|
|
// Infra containers (hive-ci, hive-forge, hive-gateway, hive-matrix) don't
|
|
// run the per-agent hive daemons, so the unit filter is inapplicable. We
|
|
// always fetch the full machine journal for them.
|
|
|
|
const infraSelect = $('infra-select');
|
|
const infraRefresh = $('infra-refresh');
|
|
const infraOutput = $('infra-output');
|
|
const infraFetchTs = $('infra-fetch-ts');
|
|
|
|
let infraFetching = false;
|
|
let infraLastFetch = 0;
|
|
async function fetchInfra() {
|
|
if (!infraSelect || !infraOutput) return;
|
|
const name = infraSelect.value;
|
|
if (!name) { infraOutput.textContent = 'select a container above'; return; }
|
|
if (infraFetching) return;
|
|
infraFetching = true;
|
|
infraOutput.textContent = 'fetching…';
|
|
if (infraFetchTs) infraFetchTs.hidden = true;
|
|
const params = new URLSearchParams({ lines: '500' });
|
|
try {
|
|
const resp = await fetch('/api/journal/' + encodeURIComponent(name) + '?' + params);
|
|
const text = await resp.text();
|
|
infraOutput.textContent = resp.ok ? (text || '(empty)') : 'error ' + resp.status + '\n' + text;
|
|
infraOutput.scrollTop = infraOutput.scrollHeight;
|
|
if (resp.ok) {
|
|
infraLastFetch = Date.now();
|
|
if (infraFetchTs) {
|
|
infraFetchTs.textContent = fmtFetchTs(infraLastFetch);
|
|
infraFetchTs.hidden = false;
|
|
}
|
|
}
|
|
} catch (err) {
|
|
infraOutput.textContent = 'fetch failed: ' + err;
|
|
} finally {
|
|
infraFetching = false;
|
|
}
|
|
}
|
|
|
|
if (infraSelect) infraSelect.addEventListener('change', fetchInfra);
|
|
if (infraRefresh) infraRefresh.addEventListener('click', fetchInfra);
|
|
|
|
// Fixed allowlist — the four hive infra services never change at
|
|
// runtime, and there's no dashboard API exposing just the name list
|
|
// (the one that used to, `/api/state`'s `infra_containers` field, was
|
|
// start/stop-panel-only and is gone). Mirrors `hive_priv_sock::InfraContainer::ALL`.
|
|
const INFRA_NAMES = ['hive-ci', 'hive-forge', 'hive-gateway', 'hive-matrix'];
|
|
|
|
// ─── container list init ──────────────────────────────────────────────
|
|
// Fetch /api/state once to populate the AGENT selector (agents only);
|
|
// INFRA is a fixed list (see INFRA_NAMES). Also handles the ?agent= /
|
|
// ?unit= deep-link, routing to the INFRA tab when the named container
|
|
// is an infra container.
|
|
async function loadContainerLists() {
|
|
if (infraSelect) {
|
|
infraSelect.replaceChildren();
|
|
infraSelect.append(el('option', { value: '' }, '— select container —'));
|
|
for (const name of INFRA_NAMES) {
|
|
infraSelect.append(el('option', { value: name }, name));
|
|
}
|
|
}
|
|
try {
|
|
const resp = await fetch('/api/state');
|
|
if (!resp.ok) return;
|
|
const state = await resp.json();
|
|
|
|
// Populate AGENT selector (agents only — no infra optgroup).
|
|
if (agentSelect) {
|
|
agentSelect.replaceChildren();
|
|
agentSelect.append(el('option', { value: '' }, '— select agent —'));
|
|
for (const c of (state.containers || [])) {
|
|
agentSelect.append(el('option', { value: c.name }, c.name));
|
|
}
|
|
}
|
|
|
|
// Deep-link: honour ?agent= and ?unit= URL params.
|
|
const urlAgent = new URLSearchParams(location.search).get('agent');
|
|
const urlUnit = new URLSearchParams(location.search).get('unit');
|
|
if (urlAgent) {
|
|
if (INFRA_NAMES.includes(urlAgent)) {
|
|
// Route to INFRA tab.
|
|
logTabs.show('infra');
|
|
if (infraSelect) {
|
|
infraSelect.value = urlAgent;
|
|
fetchInfra();
|
|
}
|
|
} else if (agentSelect) {
|
|
// Route to AGENT tab.
|
|
const found = Array.from(agentSelect.options).some((o) => o.value === urlAgent);
|
|
if (found) {
|
|
agentSelect.value = urlAgent;
|
|
if (urlUnit && agentUnitSelect) {
|
|
const unitFound = Array.from(agentUnitSelect.options)
|
|
.some((o) => o.value === urlUnit);
|
|
if (unitFound) agentUnitSelect.value = urlUnit;
|
|
}
|
|
fetchAgent();
|
|
}
|
|
}
|
|
}
|
|
} catch { /**/ }
|
|
}
|
|
|
|
// ─── SYSTEM tab ───────────────────────────────────────────────────────
|
|
|
|
const systemUnitSelect = $('system-unit-select');
|
|
const systemRefresh = $('system-refresh');
|
|
const systemOutput = $('system-output');
|
|
const systemFetchTs = $('system-fetch-ts');
|
|
|
|
let systemFetching = false;
|
|
let systemLastFetch = 0;
|
|
async function fetchSystem() {
|
|
if (!systemOutput) return;
|
|
if (systemFetching) return;
|
|
systemFetching = true;
|
|
systemOutput.textContent = 'fetching…';
|
|
if (systemFetchTs) systemFetchTs.hidden = true;
|
|
const unit = systemUnitSelect ? systemUnitSelect.value : 'hive-c0re.service';
|
|
const params = new URLSearchParams({ lines: '500' });
|
|
if (unit) params.set('unit', unit);
|
|
try {
|
|
const resp = await fetch('/api/journal-host?' + params);
|
|
const text = await resp.text();
|
|
systemOutput.textContent = resp.ok ? (text || '(empty)') : 'error ' + resp.status + '\n' + text;
|
|
systemOutput.scrollTop = systemOutput.scrollHeight;
|
|
if (resp.ok) {
|
|
systemLastFetch = Date.now();
|
|
if (systemFetchTs) {
|
|
systemFetchTs.textContent = fmtFetchTs(systemLastFetch);
|
|
systemFetchTs.hidden = false;
|
|
}
|
|
}
|
|
} catch (err) {
|
|
systemOutput.textContent = 'fetch failed: ' + err;
|
|
} finally {
|
|
systemFetching = false;
|
|
}
|
|
}
|
|
|
|
if (systemUnitSelect) systemUnitSelect.addEventListener('change', fetchSystem);
|
|
if (systemRefresh) systemRefresh.addEventListener('click', fetchSystem);
|
|
|
|
// ─── init ─────────────────────────────────────────────────────────────
|
|
|
|
// Wire the shared tab strip now that fetchSystem + the element refs it
|
|
// needs are defined. Its initial show() paints the active pane and, if
|
|
// the deep-linked tab is SYSTEM, kicks off the lazy fetch via onShow.
|
|
// Default: AGENT (build logs moved to /builds.html).
|
|
logTabs = document.getElementById('logs-tabbar').configure({
|
|
tabs: [
|
|
{ id: 'agent', label: 'AGENT' },
|
|
{ id: 'infra', label: 'INFRA' },
|
|
{ id: 'system', label: 'SYSTEM' },
|
|
],
|
|
defaultId: 'agent',
|
|
onShow: (id) => {
|
|
if (id === 'system') fetchSystem();
|
|
},
|
|
});
|
|
loadContainerLists();
|
|
|
|
// Tick the last-fetched timestamps every 30s so "fetched 1m ago" stays
|
|
// accurate without a manual refresh.
|
|
setInterval(() => {
|
|
if (agentLastFetch && agentFetchTs && !agentFetchTs.hidden) {
|
|
agentFetchTs.textContent = fmtFetchTs(agentLastFetch);
|
|
}
|
|
if (infraLastFetch && infraFetchTs && !infraFetchTs.hidden) {
|
|
infraFetchTs.textContent = fmtFetchTs(infraLastFetch);
|
|
}
|
|
if (systemLastFetch && systemFetchTs && !systemFetchTs.hidden) {
|
|
systemFetchTs.textContent = fmtFetchTs(systemLastFetch);
|
|
}
|
|
}, 30_000);
|
|
|
|
})();
|