refator_comm1

This commit is contained in:
Mateusz Gruszczyński
2025-10-18 21:59:24 +02:00
parent a48a10ed35
commit 6bb5e2715f
3 changed files with 69 additions and 80 deletions

View File

@@ -1,10 +1,9 @@
import { badge, rowHTML, setRows, safe, showToast } from './helpers.js'; import { badge, rowHTML, setRows, safe, showToast } from './helpers.js';
import { api } from './api.js'; import { api } from './api.js';
/** Globalny rejestr gniazd: sid -> { obs: WebSocket|null, task: WebSocket|null } */
const liveSockets = new Map(); const liveSockets = new Map();
let reconcileTimer = null;
/** Pomocniczo: zamknij WS-y dla sid */
function closeForSid(sid) { function closeForSid(sid) {
const entry = liveSockets.get(sid); const entry = liveSockets.get(sid);
if (!entry) return; if (!entry) return;
@@ -13,13 +12,17 @@ function closeForSid(sid) {
liveSockets.delete(sid); liveSockets.delete(sid);
} }
/** Exportowany helper do ew. sprzątania z zewnątrz (opcjonalnie) */
export function stopAllAdminWatches() { export function stopAllAdminWatches() {
for (const sid of liveSockets.keys()) closeForSid(sid); if (reconcileTimer) { clearInterval(reconcileTimer); reconcileTimer = null; }
for (const sid of Array.from(liveSockets.keys())) closeForSid(sid);
}
function setBadge(cell, val) {
if (!cell) return;
cell.innerHTML = val;
} }
export async function renderVMAdmin() { export async function renderVMAdmin() {
// zanim przebudujemy tabelę zamknij WS-y (unikamy „sierotek”)
stopAllAdminWatches(); stopAllAdminWatches();
const data = await api.listAllVmct(); const data = await api.listAllVmct();
@@ -44,7 +47,6 @@ export async function renderVMAdmin() {
const tools = `<div class="d-flex align-items-center gap-2"> const tools = `<div class="d-flex align-items-center gap-2">
<button class="btn btn-outline-primary btn-sm act-migrate">Migrate (offline)</button> <button class="btn btn-outline-primary btn-sm act-migrate">Migrate (offline)</button>
<button class="btn btn-outline-secondary btn-sm act-status">Status</button> <button class="btn btn-outline-secondary btn-sm act-status">Status</button>
<button class="btn btn-info btn-sm act-watch" title="Live observe">Watching 🔔</button>
</div>`; </div>`;
return rowHTML([sid, type.toUpperCase(), name, node, st, actions, sel, tools], `data-sid="${sid}"`); return rowHTML([sid, type.toUpperCase(), name, node, st, actions, sel, tools], `data-sid="${sid}"`);
}); });
@@ -54,12 +56,10 @@ export async function renderVMAdmin() {
Array.from(tbody.querySelectorAll('tr[data-sid]')).forEach(tr => { Array.from(tbody.querySelectorAll('tr[data-sid]')).forEach(tr => {
const sid = tr.getAttribute('data-sid'); const sid = tr.getAttribute('data-sid');
const colSpan = tr.children.length; const colSpan = tr.children.length;
const nodeCell = tr.children[3]; // kolumna Node const nodeCell = tr.children[3];
const badgeCell = tr.children[4]; // kolumna Status const badgeCell = tr.children[4];
const targetSel = tr.querySelector('.target-node'); const targetSel = tr.querySelector('.target-node');
const watchBtn = tr.querySelector('.act-watch');
// subpanel (log)
let sub = tr.nextElementSibling; let sub = tr.nextElementSibling;
if (!sub || !sub.classList.contains('mig-row')) { if (!sub || !sub.classList.contains('mig-row')) {
sub = document.createElement('tr'); sub.className = 'mig-row d-none'; sub = document.createElement('tr'); sub.className = 'mig-row d-none';
@@ -88,20 +88,17 @@ export async function renderVMAdmin() {
const html = availableNodes.map(n => const html = availableNodes.map(n =>
`<option value="${n}" ${n === current ? 'disabled selected' : ''}>${n}${n === current ? ' (src)' : ''}</option>`).join(''); `<option value="${n}" ${n === current ? 'disabled selected' : ''}>${n}${n === current ? ' (src)' : ''}</option>`).join('');
targetSel.innerHTML = html; targetSel.innerHTML = html;
// ustaw focus na pierwszy dozwolony, jeśli selected jest disabled
const idx = Array.from(targetSel.options).findIndex(o => !o.disabled); const idx = Array.from(targetSel.options).findIndex(o => !o.disabled);
if (idx >= 0) targetSel.selectedIndex = idx; if (idx >= 0) targetSel.selectedIndex = idx;
}; };
const getTarget = () => targetSel?.value || ''; const getTarget = () => targetSel?.value || '';
// ---- WS task tail (na bieżący UPID) ----
const openTaskWS = (upid, node) => { const openTaskWS = (upid, node) => {
if (!upid) return; if (!upid) return;
toggleSub(true); toggleSub(true);
logPre.textContent = `UPID: ${upid} @ ${node}\n`; logPre.textContent = `UPID: ${upid} @ ${node}\n`;
// zamknij ewentualnie poprzednie tail WS dla tej VM
const entry = liveSockets.get(sid) || {}; const entry = liveSockets.get(sid) || {};
try { entry.task && entry.task.close(); } catch {} try { entry.task && entry.task.close(); } catch {}
const wsTask = new WebSocket(api.wsTaskURL(upid, node)); const wsTask = new WebSocket(api.wsTaskURL(upid, node));
@@ -117,80 +114,55 @@ export async function renderVMAdmin() {
} else if (msg.type === 'status' && msg.status) { } else if (msg.type === 'status' && msg.status) {
const ok = String(msg.status.exitstatus||'').toUpperCase() === 'OK'; const ok = String(msg.status.exitstatus||'').toUpperCase() === 'OK';
const s = String(msg.status.status||'').toLowerCase(); const s = String(msg.status.status||'').toLowerCase();
if (badgeCell) badgeCell.innerHTML = ok ? badge('running','ok') : setBadge(badgeCell, ok ? badge('running','ok') :
(s === 'stopped' ? badge('stopped','dark') : badge('working','info')); (s === 'stopped' ? badge('stopped','dark') : badge('working','info')));
} else if (msg.type === 'done') { } else if (msg.type === 'done') {
const ok = !!msg.ok; const ok = !!msg.ok;
if (badgeCell) badgeCell.innerHTML = ok ? badge('running','ok') : badge('error','err'); setBadge(badgeCell, ok ? badge('running','ok') : badge('error','err'));
setRowBusy(false); setRowBusy(false);
setTimeout(() => toggleSub(false), 1500); setTimeout(() => toggleSub(false), 1500);
try { document.getElementById('btnRefresh').click(); } catch {}
} }
} catch {} } catch {}
}; };
wsTask.onerror = () => {};
wsTask.onclose = () => {
// nie czyścimy entry.task, żeby móc rozróżnić zamknięcie „nasze” vs. serwera
};
}; };
// ---- WS observe (domyślnie ON) ----
const ensureWatchOn = () => { const ensureWatchOn = () => {
// jeśli już obserwujemy nic nie rób
const existing = liveSockets.get(sid); const existing = liveSockets.get(sid);
if (existing && existing.obs && existing.obs.readyState <= 1) return; if (existing && existing.obs && existing.obs.readyState <= 1) return;
// upewnij się, że stare gniazda zamknięte
closeForSid(sid); closeForSid(sid);
const wsObs = new WebSocket(api.wsObserveURL(sid)); const wsObs = new WebSocket(api.wsObserveURL(sid));
liveSockets.set(sid, { obs: wsObs, task: null }); liveSockets.set(sid, { obs: wsObs, task: null });
// wizualnie: włączony
if (watchBtn) {
watchBtn.classList.remove('btn-outline-info');
watchBtn.classList.add('btn-info');
watchBtn.textContent = 'Watching 🔔';
}
wsObs.onmessage = (ev) => { wsObs.onmessage = (ev) => {
try { try {
const msg = JSON.parse(ev.data); const msg = JSON.parse(ev.data);
if (msg.type === 'vm' && msg.current) { if (msg.type === 'vm' && msg.current) {
// live status (działa także dla start/stop/shutdown bez UPID)
const st = String(msg.current.status || msg.current.qmpstatus || '').toLowerCase(); const st = String(msg.current.status || msg.current.qmpstatus || '').toLowerCase();
const ok = /running|online|started/.test(st); const ok = /running|online|started/.test(st);
if (badgeCell) { setBadge(badgeCell, ok ? badge('running','ok') :
badgeCell.innerHTML = ok ? badge('running','ok') : (/stopp|shutdown|offline/.test(st) ? badge('stopped','dark') : badge(st||'—','info')));
(/stopp|shutdown|offline/.test(st) ? badge('stopped','dark') : badge(st||'—','info'));
}
} }
else if (msg.type === 'task-start' && msg.upid && msg.node) { else if (msg.type === 'task-start' && msg.upid && msg.node) {
// jeżeli akcja wystartowała spoza panelu lub bez UPID od API — podepnij log
openTaskWS(msg.upid, msg.node); openTaskWS(msg.upid, msg.node);
} }
else if (msg.type === 'task' && msg.upid && msg.status) { else if (msg.type === 'task' && msg.upid && msg.status) {
const stopped = String(msg.status||'').toLowerCase() === 'stopped'; const stopped = String(msg.status||'').toLowerCase() === 'stopped';
if (stopped && typeof msg.exitstatus !== 'undefined') { if (stopped && typeof msg.exitstatus !== 'undefined') {
const ok = String(msg.exitstatus||'').toUpperCase() === 'OK'; const ok = String(msg.exitstatus||'').toUpperCase() === 'OK';
if (badgeCell) badgeCell.innerHTML = ok ? badge('running','ok') : badge('error','err'); setBadge(badgeCell, ok ? badge('running','ok') : badge('error','err'));
} else { } else {
if (badgeCell) badgeCell.innerHTML = badge('working','info'); setBadge(badgeCell, badge('working','info'));
} }
} }
else if (msg.type === 'moved' && msg.new_node) { else if (msg.type === 'moved' && msg.new_node) {
// VM przeniesiona od razu popraw wiersz
if (nodeCell) nodeCell.textContent = msg.new_node; if (nodeCell) nodeCell.textContent = msg.new_node;
rebuildTargetSelect(msg.new_node); rebuildTargetSelect(msg.new_node);
try { document.getElementById('btnRefresh').click(); } catch {}
} }
else if (msg.type === 'done' && typeof msg.ok === 'boolean') { else if (msg.type === 'done' && typeof msg.ok === 'boolean') {
if (badgeCell) badgeCell.innerHTML = msg.ok ? badge('running','ok') : badge('error','err'); setBadge(badgeCell, msg.ok ? badge('running','ok') : badge('error','err'));
} }
} catch {} } catch {}
@@ -199,16 +171,9 @@ export async function renderVMAdmin() {
wsObs.onclose = () => { wsObs.onclose = () => {
const e = liveSockets.get(sid); const e = liveSockets.get(sid);
if (e && e.obs === wsObs) { if (e && e.obs === wsObs) {
// oznacz jako wyłączone
liveSockets.set(sid, { obs: null, task: e.task || null }); liveSockets.set(sid, { obs: null, task: e.task || null });
if (watchBtn) {
watchBtn.classList.remove('btn-info');
watchBtn.classList.add('btn-outline-info');
watchBtn.textContent = 'Watch 🔔';
}
} }
}; };
wsObs.onerror = () => {};
}; };
const doAction = async (action, withTarget=false) => { const doAction = async (action, withTarget=false) => {
@@ -216,22 +181,21 @@ export async function renderVMAdmin() {
try { try {
const target = withTarget ? getTarget() : undefined; const target = withTarget ? getTarget() : undefined;
// ZAWSZE live zapewnia status/log nawet jeśli API nie odda UPID
ensureWatchOn(); ensureWatchOn();
if (action !== 'unlock') toggleSub(true); // pokaż subpanel logów dla akcji z zadaniami if (action !== 'unlock') {
setBadge(badgeCell, badge('working','info'));
toggleSub(true);
}
const resp = await api.vmAction(sid, action, target); const resp = await api.vmAction(sid, action, target);
if (!resp.ok) throw new Error(resp.error || 'unknown'); if (!resp.ok) throw new Error(resp.error || 'unknown');
if (!resp.upid) { if (!resp.upid) {
// np. unlock albo środowisko nie oddało UPID poczekaj na task-start z observe
logPre.textContent = `Waiting for task… (${action})\n`; logPre.textContent = `Waiting for task… (${action})\n`;
showToast('Info', `${action} zainicjowane`, 'info'); setRowBusy(false);
setRowBusy(false); // spinner wyłącz, status/log dojadą z observe
return; return;
} }
// mamy UPID od razu podłącz tail
openTaskWS(resp.upid, resp.source_node); openTaskWS(resp.upid, resp.source_node);
} catch (e) { } catch (e) {
@@ -241,34 +205,61 @@ export async function renderVMAdmin() {
} }
}; };
// Akcje
tr.querySelector('.act-unlock')?.addEventListener('click', () => doAction('unlock')); tr.querySelector('.act-unlock')?.addEventListener('click', () => doAction('unlock'));
tr.querySelector('.act-start')?.addEventListener('click', () => doAction('start')); tr.querySelector('.act-start')?.addEventListener('click', () => doAction('start'));
tr.querySelector('.act-stop')?.addEventListener('click', () => doAction('stop')); tr.querySelector('.act-stop')?.addEventListener('click', () => doAction('stop'));
tr.querySelector('.act-shutdown')?.addEventListener('click',() => doAction('shutdown')); tr.querySelector('.act-shutdown')?.addEventListener('click',() => doAction('shutdown'));
tr.querySelector('.act-migrate')?.addEventListener('click', () => doAction('migrate', true)); tr.querySelector('.act-migrate')?.addEventListener('click', () => doAction('migrate', true));
// Status pokaż/ukryj subpanel (bez WS)
tr.querySelector('.act-status')?.addEventListener('click', () => toggleSub(sub.classList.contains('d-none'))); tr.querySelector('.act-status')?.addEventListener('click', () => toggleSub(sub.classList.contains('d-none')));
// Watch 🔔 manualny toggle (domyślnie jest ON)
if (watchBtn) {
watchBtn.addEventListener('click', () => {
const e = liveSockets.get(sid);
if (e && e.obs) {
closeForSid(sid);
watchBtn.classList.remove('btn-info'); watchBtn.classList.add('btn-outline-info');
watchBtn.textContent = 'Watch 🔔';
} else {
ensureWatchOn();
}
});
}
// startowo: LIVE bez klikania
ensureWatchOn(); ensureWatchOn();
}); });
// sprzątanie przy zamknięciu karty reconcileTimer = setInterval(async () => {
try {
const latest = await api.listAllVmct();
const all = Array.isArray(latest.all) ? latest.all : [];
const bySid = new Map(all.map(x => [String(x.sid), x]));
Array.from(tbody.querySelectorAll('tr[data-sid]')).forEach(tr => {
const sid = tr.getAttribute('data-sid');
const rowData = bySid.get(sid);
if (!rowData) return;
const nodeCell = tr.children[3];
const badgeCell = tr.children[4];
const targetSel = tr.querySelector('.target-node');
const newNode = String(rowData.node || '').trim();
if (nodeCell && newNode && nodeCell.textContent.trim() !== newNode) {
nodeCell.textContent = newNode;
if (targetSel) {
const options = Array.from(targetSel.options);
options.forEach(o => { o.disabled = (o.value === newNode); o.selected = (o.value === newNode); });
const idx = options.findIndex(o => !o.disabled);
if (idx >= 0) targetSel.selectedIndex = idx;
}
}
const running = /running/i.test(rowData.status || '');
const currentBadge = badgeCell?.innerText?.toLowerCase() || '';
const isWorking = currentBadge.includes('working');
if (badgeCell && !isWorking) {
setBadge(badgeCell, running ? badge('running','ok') : badge(rowData.status || '—','dark'));
}
const existing = liveSockets.get(sid);
if (!(existing && existing.obs && existing.obs.readyState <= 1)) {
const wsObs = new WebSocket(api.wsObserveURL(sid));
liveSockets.set(sid, { obs: wsObs, task: existing?.task || null });
wsObs.onmessage = () => {};
wsObs.onerror = () => {};
}
});
} catch {}
}, 3000);
window.addEventListener('beforeunload', stopAllAdminWatches, { once: true }); window.addEventListener('beforeunload', stopAllAdminWatches, { once: true });
} }

View File

@@ -23,7 +23,6 @@ export const api = {
const proto = (location.protocol === 'https:') ? 'wss' : 'ws'; const proto = (location.protocol === 'https:') ? 'wss' : 'ws';
return `${proto}://${location.host}/ws/task?upid=${encodeURIComponent(upid)}&node=${encodeURIComponent(node)}`; return `${proto}://${location.host}/ws/task?upid=${encodeURIComponent(upid)}&node=${encodeURIComponent(node)}`;
}, },
// NEW:
wsObserveURL: (sid) => { wsObserveURL: (sid) => {
const proto = (location.protocol === 'https:') ? 'wss' : 'ws'; const proto = (location.protocol === 'https:') ? 'wss' : 'ws';
return `${proto}://${location.host}/ws/observe?sid=${encodeURIComponent(sid)}`; return `${proto}://${location.host}/ws/observe?sid=${encodeURIComponent(sid)}`;

View File

@@ -227,8 +227,7 @@
</tr> </tr>
</tbody> </tbody>
</table> </table>
<div class="small text-muted">Akcje: Unlock (qm), Start/Stop/Shutdown, Offline migrate. Postęp widoczny na <div class="small text-muted">Akcje: Unlock (qm), Start/Stop/Shutdown, Offline migrate.</div>
żywo per wiersz.</div>
</div> </div>
</div> </div>
</div> </div>