';
return;
}
const keep = preserveIds || new Set();
wrap.innerHTML = workers.map(w => {
const checked = keep.has(w.id) ? ' checked' : '';
return (
''
);
}).join('');
}
function selectedWorkerCheckboxIds() {
return Array.from(document.querySelectorAll('input[name="qc-worker-cb"]:checked')).map(cb => cb.value);
}
let _restored = false;
async function loadWorkers() {
const select = document.getElementById('qc-worker');
let preserveSingle = select ? select.value : '';
let preserveMulti = new Set(selectedWorkerCheckboxIds());
if (!_restored) {
// First load: start from the saved choice rather than the first worker.
_restored = true;
const prefs = loadPrefs();
preserveSingle = prefs.worker || preserveSingle;
if (Array.isArray(prefs.workers)) preserveMulti = new Set(prefs.workers);
}
try {
_workers = await Pulse.api.get('/api/workers') || [];
} catch (e) {
_workers = [];
console.error('[Pulse:quick] failed to load workers', e);
}
renderWorkerSelect(_workers, preserveSingle);
renderWorkerCheckboxes(_workers, preserveMulti);
return _workers;
}
/* -------------------------------------------------------------------
Templates modal
------------------------------------------------------------------- */
function renderTemplates() {
const list = document.getElementById('qc-templates-list');
if (!list) return;
list.innerHTML = TEMPLATES.map((t, i) =>
'
' +
'
' + esc(t.name) + '
' +
'
' + esc(t.cmd) + '
' +
'
' + esc(t.desc) + '
' +
'
'
).join('');
}
function renderHistory() {
const list = document.getElementById('qc-history-list');
if (!list) return;
const history = loadHistory();
if (history.length === 0) {
list.innerHTML = '
'
).join('');
}
/* -------------------------------------------------------------------
Result rendering
------------------------------------------------------------------- */
/* Results show here as they arrive, per worker, instead of only in Executions (#5).
A card per run: "running" until its command_result comes in over the live
connection, or a poll finds it finished if that event was missed. */
const OUTPUT_MAX = 100000; // characters shown per stream
const _runs = new Map(); // execution id -> { card, startedAt, timer }
const _early = new Map(); // results that beat the POST response
function forgetRuns() {
_runs.forEach(r => clearTimeout(r.timer));
_runs.clear();
}
function renderRuns(entries, summary) {
forgetRuns();
const wrap = document.getElementById('qc-result');
if (!wrap) return;
const head = summary ? '
' + esc(summary) + '
' : '';
wrap.innerHTML = head + entries.map(runCard).join('');
entries.forEach((e, i) => {
if (!e.executionId) return;
const run = { card: document.getElementById('qc-run-' + i), startedAt: Date.now(), timer: null };
_runs.set(e.executionId, run);
if (_early.has(e.executionId)) { showResult(e.executionId, _early.get(e.executionId)); return; }
run.timer = setTimeout(() => pollRun(e.executionId, 0), 5000);
});
}
function runCard(e, i) {
const id = e.executionId;
const worker = esc(e.worker);
if (!id) {
return '
';
}
function clip(text) {
const t = String(text || '');
if (t.length <= OUTPUT_MAX) return esc(t);
return esc(t.slice(0, OUTPUT_MAX)) + '\n[' + (t.length - OUTPUT_MAX).toLocaleString() +
' more characters: see the full output in Executions]';
}
function failureLabel(r) {
if (r.timed_out) return 'Timed out';
if (r.signal) return 'Killed (' + r.signal + ')';
if (typeof r.exit_code === 'number') return 'Failed (exit ' + r.exit_code + ')';
return 'Failed';
}
function showResult(executionId, r) {
const run = _runs.get(executionId);
if (!run || !run.card) return;
clearTimeout(run.timer);
_runs.delete(executionId);
_early.delete(executionId);
const status = run.card.querySelector('.qc-run-status');
const ok = Boolean(r.success);
const label = ok ? 'Completed' : failureLabel(r);
status.className = 'lt-status qc-run-status ' + (ok ? 'lt-status-completed' : 'lt-status-failed');
status.textContent = (ok ? '\u2713 ' : '\u2715 ') + label;
const ms = typeof r.duration === 'number' ? r.duration : Date.now() - run.startedAt;
run.card.querySelector('.qc-run-time').textContent = ms < 1000 ? ms + ' ms' : (ms / 1000).toFixed(1) + ' s';
const parts = [];
if (r.stdout) parts.push('
' + clip(r.stdout) + '
');
if (r.stderr) parts.push('
' + clip(r.stderr) + '
');
if (!r.success && r.error && !r.stderr) parts.push('
' + esc(r.error) + '
');
if (!parts.length) parts.push('
No output
');
const body = run.card.querySelector('.qc-run-body');
body.innerHTML = parts.join('');
}
// Fallback for a result event that never arrived (e.g. the live connection dropped).
async function pollRun(executionId, tries) {
const run = _runs.get(executionId);
if (!run) return;
try {
const ex = await Pulse.api.get('/api/executions/' + encodeURIComponent(executionId));
if (ex && ex.status !== 'running') {
const logs = Array.isArray(ex.logs) ? ex.logs : [];
const r = logs.filter(l => l.action === 'command_result').pop();
showResult(executionId, r || { success: ex.status === 'completed', error: 'No result recorded (' + ex.status + ')' });
return;
}
} catch (e) { /* try again */ }
if (tries < 180 && _runs.has(executionId)) run.timer = setTimeout(() => pollRun(executionId, tries + 1), 5000);
}
function onCommandResult(data) {
if (!data || !data.execution_id) return false;
if (_runs.has(data.execution_id)) { showResult(data.execution_id, data); return true; }
// Can arrive before the POST that started it returns: keep it briefly.
_early.set(data.execution_id, data);
if (_early.size > 100) _early.delete(_early.keys().next().value);
return false;
}
function renderExecuting(count) {
forgetRuns();
const wrap = document.getElementById('qc-result');
if (!wrap) return;
wrap.innerHTML = '