refator_comm1

This commit is contained in:
Mateusz Gruszczyński
2025-10-18 21:28:42 +02:00
parent 3a90a48109
commit e3b3ff235b
2 changed files with 68 additions and 45 deletions

View File

@@ -34,7 +34,8 @@ export async function renderVMAdmin() {
Array.from(tbody.querySelectorAll('tr[data-sid]')).forEach(tr => {
const sid = tr.getAttribute('data-sid');
const colSpan = tr.children.length;
const badgeCell = tr.children[4];
const nodeCell = tr.children[3]; // Node
const badgeCell = tr.children[4]; // Status
// subpanel (log)
let sub = tr.nextElementSibling;
@@ -50,7 +51,11 @@ export async function renderVMAdmin() {
let wsObs = null; // observe websocket
let wsTask = null; // tail websocket (auto z observe)
const closeWS = () => { try { wsObs && wsObs.close(); } catch {} try { wsTask && wsTask.close(); } catch {} wsObs = wsTask = null; };
const closeWS = () => {
try { wsObs && wsObs.close(); } catch {}
try { wsTask && wsTask.close(); } catch {}
wsObs = wsTask = null;
};
const setRowBusy = (busy) => {
const nameCell = tr.children[2];
@@ -100,30 +105,41 @@ export async function renderVMAdmin() {
wsObs.onmessage = (ev) => {
try {
const msg = JSON.parse(ev.data);
if (msg.type === 'vm' && msg.current) {
// aktualizuj badge na podstawie bieżącego statusu
const st = String(msg.current.status || msg.current.qmpstatus || '').toLowerCase();
const ok = /running|online|started/.test(st);
badgeCell.innerHTML = ok ? badge('running','ok') :
(/stopp|shutdown|offline/.test(st) ? badge('stopped','dark') : badge(st||'—','info'));
} else if (msg.type === 'task-start' && msg.upid && msg.node) {
// automatycznie podłącz tail do nowo wykrytego taska
if (badgeCell) {
badgeCell.innerHTML = ok ? badge('running','ok') :
(/stopp|shutdown|offline/.test(st) ? badge('stopped','dark') : badge(st||'—','info'));
}
}
else if (msg.type === 'task-start' && msg.upid && msg.node) {
openTaskWS(msg.upid, msg.node);
} else if (msg.type === 'task' && msg.upid && msg.status) {
// szybkie mrugnięcie statusem
}
else if (msg.type === 'task' && msg.upid && msg.status) {
const stopped = String(msg.status||'').toLowerCase() === 'stopped';
if (stopped && typeof msg.exitstatus !== 'undefined') {
const ok = String(msg.exitstatus||'').toUpperCase() === 'OK';
badgeCell.innerHTML = ok ? badge('running','ok') : badge('error','err');
if (badgeCell) badgeCell.innerHTML = ok ? badge('running','ok') : badge('error','err');
} else {
badgeCell.innerHTML = badge('working','info');
if (badgeCell) badgeCell.innerHTML = badge('working','info');
}
} else if (msg.type === 'done' && msg.upid) {
// koniec zewnętrznego zadania (bez naszego taila)
if (msg.ok) badgeCell.innerHTML = badge('running','ok');
else badgeCell.innerHTML = badge('error','err');
}
else if (msg.type === 'moved' && msg.new_node) {
if (nodeCell) nodeCell.textContent = msg.new_node;
try { document.getElementById('btnRefresh').click(); } catch {}
}
else if (msg.type === 'done' && msg.upid) {
if (typeof msg.ok === 'boolean') {
if (badgeCell) badgeCell.innerHTML = msg.ok ? badge('running','ok') : badge('error','err');
}
}
} catch {}
};
wsObs.onclose = () => { wsObs = null; };
@@ -157,10 +173,8 @@ export async function renderVMAdmin() {
tr.querySelector('.act-shutdown')?.addEventListener('click', () => { toggleSub(true); doAction('shutdown'); });
tr.querySelector('.act-migrate')?.addEventListener('click', () => { toggleSub(true); doAction('migrate', true); });
// Status pokaz/ukryj subpanel (bez WS)
tr.querySelector('.act-status')?.addEventListener('click', () => toggleSub(sub.classList.contains('d-none')));
// NEW: Watch 🔔 włącz/wyłącz broadcast observe
const watchBtn = tr.querySelector('.act-watch');
if (watchBtn) {
watchBtn.addEventListener('click', () => {
@@ -175,6 +189,5 @@ export async function renderVMAdmin() {
}
});
}
});
}