refator_comm1

This commit is contained in:
Mateusz Gruszczyński
2025-10-18 23:51:21 +02:00
parent 5149ce140c
commit 88f42687bf
2 changed files with 139 additions and 301 deletions

View File

@@ -1,326 +1,155 @@
import { rowHTML, setRows, safe, showToast, badge } from './helpers.js'; import { rowHTML, setRows, safe, showToast, badge } from './helpers.js';
import { api } from './api.js'; import { api } from './api.js';
// --- State --- // helpers
const liveSockets = new Map(); const q = (r, s) => (r || document).querySelector(s);
let slowTimer = null; const qq = (r, s) => Array.from((r || document).querySelectorAll(s));
let fastTimer = null; const low = (x) => String(x ?? '').toLowerCase();
const activeSids = new Set();
// --- tiny safe helpers ---
function q(root, sel){ return root ? root.querySelector(sel) : null; }
function qq(root, sel){ return root ? Array.from(root.querySelectorAll(sel)) : []; }
function txt(el){ return (el && el.textContent) ? el.textContent : ''; }
function low(x){ return String(x||'').toLowerCase(); }
function injectOnceCSS() {
if (document.getElementById('vmadmin-live-css')) return;
const style = document.createElement('style');
style.id = 'vmadmin-live-css';
style.textContent = `
.pulse-dot{display:inline-block;width:8px;height:8px;border-radius:50%;background:#22c55e;margin-left:.5rem;opacity:.25;animation:pulse-vm 1s ease-in-out 4}
@keyframes pulse-vm {0%,100%{opacity:.25;transform:scale(1)}50%{opacity:1;transform:scale(1.25)}}
`;
document.head.appendChild(style);
}
function flashDot(cell) {
if (!cell) return;
const dot = document.createElement('span');
dot.className = 'pulse-dot';
cell.appendChild(dot);
setTimeout(function(){ if (dot && dot.parentNode) dot.parentNode.removeChild(dot); }, 1500);
}
function setBadgeCell(cell, textOrState) {
if (!cell) return false;
let html = '';
const s = low(textOrState);
if (/running|online|started/.test(s)) html = badge('running','ok');
else if (/stopp|shutdown|offline|stopped/.test(s)) html = badge('stopped','dark');
else if (/working|progress|busy/.test(s)) html = badge('working','info');
else html = badge(textOrState || '—','dark');
if (cell.innerHTML !== html) { cell.innerHTML = html; return true; }
return false;
}
function extractNodeNames(ns){
if (!ns) return [];
if (Array.isArray(ns.nodes)) return Array.from(new Set(ns.nodes.map(n => String(n.name || n.node || n).trim()).filter(Boolean)));
if (Array.isArray(ns)) return Array.from(new Set(ns.map(n => String(n.name || n.node || n).trim()).filter(Boolean)));
return [];
}
// --- dropdown: wybierz pierwszy inny node od bieżącego
function rebuildTargetSelect(selectEl, currentNode, nodes) { function rebuildTargetSelect(selectEl, currentNode, nodes) {
if (!selectEl) return []; if (!selectEl) return;
const current = String(currentNode || '').trim(); const cur = String(currentNode || '').trim();
const all = (nodes || []).map(n => String(n && (n.name || n.node || n)).trim()).filter(Boolean); const all = (nodes || []).map(n => String(n.name || n.node || n).trim()).filter(Boolean);
const others = all.filter(n => n && n !== current); const others = all.filter(n => n && n !== cur);
selectEl.innerHTML = others.map(n => `<option value="${n}">${n}</option>`).join(''); selectEl.innerHTML = others.map(n => `<option value="${n}">${n}</option>`).join('');
// wybierz pierwszy sensowny inny node // domyślnie pierwszy sensowny inny node
if (selectEl.options.length > 0) selectEl.selectedIndex = 0; if (selectEl.options.length > 0) selectEl.selectedIndex = 0;
return others;
} }
function updateMigrateButton(tr, rawStatus) { // --- statusy + przyciski
const btn = q(tr, '.act-migrate'); function boolRunning(statusRaw) { return /running|online|started/i.test(statusRaw || ''); }
const targetSel = q(tr, '.target-node'); function setButtonsByStatus(tr, statusRaw) {
if (!btn) return; const running = boolRunning(statusRaw);
const hasTarget = !!(targetSel && targetSel.options && targetSel.options.length > 0); // start tylko gdy nie działa
const s = low(rawStatus || ''); const bStart = tr.querySelector('.act-start');
const busy = /working|progress|busy/.test(s); const bStop = tr.querySelector('.act-stop');
// pozwól offline i online migrate — byle jest docelowy węzeł i nie trwa inna akcja const bShutdown = tr.querySelector('.act-shutdown');
const enable = hasTarget && !busy && s !== '';
if (enable) { btn.removeAttribute('disabled'); btn.classList.remove('disabled'); } if (bStart) bStart.disabled = running;
else { btn.setAttribute('disabled',''); btn.classList.add('disabled'); } if (bStop) bStop.disabled = !running;
if (bShutdown) bShutdown.disabled = !running;
// MIGRATE: aktywny także dla offline (offline migrate)
const bMig = tr.querySelector('.act-migrate');
const sel = tr.querySelector('.target-node');
const hasTarget = !!(sel && sel.value);
// blokujemy tylko gdy nie ma targetu albo trwa „working/progress”
const busy = /working|progress|busy/i.test(statusRaw || '');
if (bMig) bMig.disabled = !hasTarget || busy;
} }
function updateActionButtons(tr, isRunning) { // --- render
const bStart = q(tr, '.act-start'); export async function renderVMAdmin() {
const bStop = q(tr, '.act-stop'); const wrap = document.getElementById('vm-admin');
const bShutdown = q(tr, '.act-shutdown'); if (!wrap) return;
if (bStart) { if (isRunning) { bStart.setAttribute('disabled',''); bStart.classList.add('disabled'); } else { bStart.removeAttribute('disabled'); bStart.classList.remove('disabled'); } } const tbody = wrap.querySelector('tbody');
if (bStop) { if (isRunning) { bStop.removeAttribute('disabled'); bStop.classList.remove('disabled'); } else { bStop.setAttribute('disabled',''); bStop.classList.add('disabled'); } }
if (bShutdown) { if (isRunning) { bShutdown.removeAttribute('disabled'); bShutdown.classList.remove('disabled'); } else { bShutdown.setAttribute('disabled',''); bShutdown.classList.add('disabled'); } }
}
export function stopAllAdminWatches() {
liveSockets.forEach(function(ws){ try { ws.close(); } catch(e){} });
liveSockets.clear();
if (slowTimer) clearInterval(slowTimer);
if (fastTimer) clearInterval(fastTimer);
slowTimer = null; fastTimer = null; activeSids.clear();
}
function ensureWatchOn() {
const tbody = document.querySelector('#vm-admin tbody');
if (!tbody) return;
qq(tbody, 'tr[data-sid]').forEach(function(tr){
const sid = tr.getAttribute('data-sid');
if (!sid || liveSockets.has(sid)) return;
try {
const wsProto = (location.protocol === 'https:') ? 'wss' : 'ws';
const ws = new WebSocket(wsProto + '://' + location.host + '/ws/observe?sid=' + encodeURIComponent(sid));
liveSockets.set(sid, ws);
ws.onopen = function(){};
ws.onclose = function(){ liveSockets.delete(sid); };
ws.onerror = function(){};
ws.onmessage = function(ev){
try {
const msg = JSON.parse(ev.data || '{}');
if (!msg || !msg.type) return;
const tr2 = tbody.querySelector('tr[data-sid="' + sid + '"]');
if (!tr2) return;
const statusCell = tr2.children[4];
const nameCell = tr2.children[2];
const nodeCell = tr2.children[3];
const targetSel = q(tr2, '.target-node');
// dopasowane do serwera: type="vm" i "moved"
if (msg.type === 'vm') {
const cur = msg.current || {};
const stRaw = low(cur.status || cur.qmpstatus || '');
const changed = setBadgeCell(statusCell, stRaw);
const isRunning = /running|online|started/.test(stRaw);
updateMigrateButton(tr2, stRaw);
updateActionButtons(tr2, isRunning);
if (changed) flashDot(nameCell);
if (stRaw && /running|stopped|shutdown/.test(stRaw)) {
setTimeout(function(){ activeSids.delete(sid); }, 3000);
}
}
if (msg.type === 'moved' && msg.new_node) {
const newNode = String(msg.new_node).trim();
if (nodeCell && newNode && txt(nodeCell).trim() !== newNode) {
nodeCell.textContent = newNode;
rebuildTargetSelect(targetSel, newNode, window.__nodesCache || []);
flashDot(nameCell);
}
}
} catch(e){}
};
} catch(e){}
});
}
export async function startAdminWatches() {
injectOnceCSS();
const tbody = document.querySelector('#vm-admin tbody');
if (!tbody) return; if (!tbody) return;
const ns = await api.nodesSummary(); // pobierz listy
const availableNodes = extractNodeNames(ns); const [list, nodesSummary] = await Promise.all([api.listAllVmct(), api.nodesSummary()]);
setRows(tbody, []); const all = Array.isArray(list.all) ? list.all : [];
const nodes = Array.isArray(nodesSummary?.nodes) ? nodesSummary.nodes : [];
try { // zbuduj wiersze (bez checkboxa)
const latest = await api.listAllVmct(); const rows = all.map(vm => {
const all = Array.isArray(latest.all) ? latest.all : []; const status = safe(vm.status || vm.current?.status || vm.current?.qmpstatus || '—');
const nodesNow = Array.isArray(latest.nodes) ? latest.nodes : []; const isRun = boolRunning(status);
window.__nodesCache = nodesNow.slice(); return rowHTML([
safe(vm.sid),
const rows = all.map(vm => ({ safe(vm.name || vm.vmid || vm.sid),
sid: safe(vm.sid), safe(vm.node || '—'),
name: safe(vm.name || vm.vmid || vm.sid), badge(status, isRun ? 'ok' : 'dark'),
node: safe(vm.node || '—'),
status: safe(vm.status || '—')
}));
const htmlRows = rows.map(r => rowHTML([
`<input type="checkbox" class="row-check">`,
safe(r.sid),
safe(r.name),
safe(r.node),
badge(safe(r.status), /running|online|started/i.test(r.status) ? 'ok' : 'dark'),
`<div class="btn-group"> `<div class="btn-group">
<button type="button" class="btn btn-sm btn-outline-success act-start">Start</button> <button type="button" class="btn btn-sm btn-outline-success act-start">Start</button>
<button type="button" class="btn btn-sm btn-outline-warning act-shutdown">Shutdown</button> <button type="button" class="btn btn-sm btn-outline-warning act-shutdown">Shutdown</button>
<button type="button" class="btn btn-sm btn-outline-danger act-stop">Stop</button> <button type="button" class="btn btn-sm btn-outline-danger act-stop">Stop</button>
<button type="button" class="btn btn-sm btn-outline-secondary act-unlock">Unlock</button> <button type="button" class="btn btn-sm btn-outline-secondary act-unlock">Unlock</button>
</div>`, </div>`,
`<select class="form-select form-select-sm target-node position-relative"></select>`, `<div class="d-grid gap-1">
`<button type="button" class="btn btn-sm btn-outline-primary w-100 act-migrate" disabled>MIGRATE</button>` <select class="form-select form-select-sm target-node"></select>
])); <button type="button" class="btn btn-sm btn-outline-primary act-migrate">MIGRATE</button>
</div>`
setRows(tbody, htmlRows); ], `data-sid="${safe(vm.sid)}"`);
// prepare rows
qq(tbody, 'tr[data-sid]').forEach(function(tr){
const nodeCell = tr.children[3];
const targetSel = q(tr, '.target-node');
const currentNode = txt(nodeCell).trim();
rebuildTargetSelect(targetSel, currentNode, availableNodes);
const stRaw = low(tr.children[4].innerText);
const isRunning = /running|online|started/.test(stRaw);
updateMigrateButton(tr, stRaw);
updateActionButtons(tr, isRunning);
}); });
// delegated events (reliable after refreshes) setRows(tbody, rows);
tbody.addEventListener('click', async function(ev){
const t = ev.target; // uzupełnij selecty + stany przycisków
const btn = t && t.closest ? t.closest('.act-start,.act-stop,.act-shutdown,.act-unlock,.act-migrate') : null; qq(tbody, 'tr[data-sid]').forEach(tr => {
const nodeCell = tr.children[2];
const statusCell= tr.children[3];
const sel = tr.querySelector('.target-node');
rebuildTargetSelect(sel, nodeCell?.textContent?.trim(), nodes);
setButtonsByStatus(tr, statusCell?.innerText || '');
});
// --- delegacja CLICK globalnie: łapie też elementy po rerenderach
document.addEventListener('click', async (ev) => {
const btn = ev.target.closest?.('.act-start,.act-stop,.act-shutdown,.act-unlock,.act-migrate');
if (!btn) return; if (!btn) return;
ev.preventDefault(); ev.stopPropagation(); // <— kluczowe, żeby zawsze doszedł POST // zapobiegamy submitom, bąbelkowaniu itp.
const tr = btn.closest ? btn.closest('tr[data-sid]') : null; ev.preventDefault();
ev.stopPropagation();
const tr = btn.closest('tr[data-sid]');
if (!tr) return; if (!tr) return;
const sid = tr.getAttribute('data-sid'); const sid = tr.getAttribute('data-sid');
const nameCell = tr.children[2]; const name = tr.children[1]?.textContent?.trim() || sid;
const statusCell = tr.children[4];
const targetSel = q(tr, '.target-node'); // mapowanie akcji
let action = '';
let target = undefined;
if (btn.classList.contains('act-start')) action = 'start';
if (btn.classList.contains('act-stop')) action = 'stop';
if (btn.classList.contains('act-shutdown')) action = 'shutdown';
if (btn.classList.contains('act-unlock')) action = 'unlock';
if (btn.classList.contains('act-migrate')) {
action = 'migrate';
target = tr.querySelector('.target-node')?.value;
if (!target) {
showToast('Migrate', 'Wybierz docelowy node inny niż bieżący', 'warning');
return;
}
}
async function doAction(kind, needsTarget) {
try { try {
const targetNode = needsTarget ? (targetSel ? targetSel.value : undefined) : undefined; const res = await api.vmAction(sid, action, target);
activeSids.add(sid); if (res?.ok) {
setBadgeCell(statusCell, 'working'); showToast('OK', `${action.toUpperCase()} × ${name}`, 'success');
updateMigrateButton(tr, 'working'); } else {
const res = await api.vmAction(sid, kind, targetNode); showToast('Błąd', res?.error || `Nie udało się: ${action}`, 'danger');
if (res && res.ok) { showToast('OK', `Task ${kind} started for ${safe(txt(nameCell))}`, 'success'); }
else { showToast('Error', `Task ${kind} failed for ${safe(txt(nameCell))}`, 'danger'); }
} catch(e) { showToast('Error', String(e && e.message ? e.message : e), 'danger'); }
} }
if (btn.classList.contains('act-start')) return doAction('start');
if (btn.classList.contains('act-stop')) return doAction('stop');
if (btn.classList.contains('act-shutdown')) return doAction('shutdown');
if (btn.classList.contains('act-unlock')) return doAction('unlock');
if (btn.classList.contains('act-migrate')) return doAction('migrate', true);
});
window.__nodesCache = availableNodes.slice();
// slow full refresh
slowTimer = setInterval(async function(){
try {
const latest2 = await api.listAllVmct();
const all2 = Array.isArray(latest2.all) ? latest2.all : [];
const bySid = new Map(all2.map(x => [String(x.sid), x]));
const nodesNow2 = Array.isArray(latest2.nodes) ? latest2.nodes : (window.__nodesCache || []);
window.__nodesCache = nodesNow2;
qq(tbody, 'tr[data-sid]').forEach(function(tr){
const sid = tr.getAttribute('data-sid');
const rowData = bySid.get(sid);
if (!rowData) return;
const nodeCell = tr.children[3];
const statusCell= tr.children[4];
const nameCell = tr.children[2];
const targetSel = q(tr, '.target-node');
const newNode = String(rowData.node || '').trim();
if (nodeCell && newNode && txt(nodeCell).trim() !== newNode) {
nodeCell.textContent = newNode;
rebuildTargetSelect(targetSel, newNode, nodesNow2);
flashDot(nameCell);
}
const currentTxt = low((statusCell && statusCell.innerText) || '');
if (!/working/.test(currentTxt)) {
const stRaw = low(rowData.status || '');
if (stRaw) {
const changed = setBadgeCell(statusCell, stRaw);
const isRunning = /running|online|started/.test(stRaw);
updateMigrateButton(tr, stRaw);
updateActionButtons(tr, isRunning);
if (changed) flashDot(nameCell);
}
}
});
} catch(e){}
}, 30000);
// fast refresh: active sids
fastTimer = setInterval(async function(){
try {
const sids = Array.from(activeSids);
if (!sids.length) return;
for (let i=0;i<sids.length;i++){
const sid = sids[i];
const detail = await api.vmDetail(sid);
if (!detail || !detail.meta) continue;
const tr = tbody.querySelector('tr[data-sid="' + sid + '"]');
if (!tr) continue;
const nodeCell = tr.children[3];
const statusCell= tr.children[4];
const nameCell = tr.children[2];
const targetSel = q(tr, '.target-node');
const stRaw = low((detail.current && (detail.current.status || detail.current.qmpstatus)) || '');
const changed = setBadgeCell(statusCell, stRaw);
const isRunning = /running|online|started/.test(stRaw);
updateMigrateButton(tr, stRaw);
updateActionButtons(tr, isRunning);
if (changed) flashDot(nameCell);
const newNode = String(detail.node || (detail.meta && detail.meta.node) || '').trim();
if (newNode) {
if (nodeCell && txt(nodeCell).trim() !== newNode) {
nodeCell.textContent = newNode;
rebuildTargetSelect(targetSel, newNode, window.__nodesCache || []);
flashDot(nameCell);
}
}
if (stRaw && /running|stopped|shutdown/.test(stRaw)) {
setTimeout(function(){ activeSids.delete(sid); }, 4000);
}
}
} catch(e){}
}, 10000);
ensureWatchOn();
window.addEventListener('beforeunload', stopAllAdminWatches, { once: true });
} catch (e) { } catch (e) {
showToast('Error', `Failed to load list: ${e && e.message ? e.message : e}`, 'danger'); showToast('Błąd', String(e?.message || e), 'danger');
} }
} }, { capture: true }); // capture: pewność, że nasz handler zadziała przed innymi
// Entry point used by main.js // prosty, lekki refresh co 30s (statusy i node)
export async function renderVMAdmin() { setInterval(async () => {
try { await startAdminWatches(); } try {
catch (e) { const latest = await api.listAllVmct();
showToast('Error', `VM Admin initialization error: ${e && e.message ? e.message : e}`, 'danger'); const bySid = new Map((latest?.all || []).map(x => [String(x.sid), x]));
console.error(e); qq(tbody, 'tr[data-sid]').forEach(tr => {
const sid = tr.getAttribute('data-sid');
const row = bySid.get(sid);
if (!row) return;
const nodeCell = tr.children[2];
const statusCell = tr.children[3];
const sel = tr.querySelector('.target-node');
const newNode = String(row.node || '').trim();
if (newNode && nodeCell?.textContent?.trim() !== newNode) {
nodeCell.textContent = newNode;
rebuildTargetSelect(sel, newNode, nodes);
} }
const st = String(row.status || row.current?.status || row.current?.qmpstatus || '').trim();
if (st && statusCell?.innerText?.trim() !== st) {
statusCell.innerHTML = badge(st, boolRunning(st) ? 'ok' : 'dark');
setButtonsByStatus(tr, st);
}
});
} catch { /* no-op */ }
}, 30000);
} }

View File

@@ -145,27 +145,36 @@ footer.site-footer a:hover {
} }
#vm-admin table { /* --- VM Admin: dropdown „target node” ma być zawsze nad wierszem poniżej --- */
overflow: visible; #vm-admin,
#vm-admin .table-responsive,
#vm-admin table,
#vm-admin tbody,
#vm-admin tr,
#vm-admin td {
overflow: visible !important;
} }
#vm-admin td { #vm-admin td {
vertical-align: middle; position: relative;
z-index: 1;
} }
#vm-admin .target-node { #vm-admin .target-node {
position: relative; position: relative;
z-index: 3; z-index: 1001;
/* ponad sąsiednimi wierszami */
} }
/* --- Toasty: prawy-dolny róg, stabilnie --- */
#toast-container { #toast-container {
position: fixed; position: fixed;
right: max(env(safe-area-inset-right), 1rem); right: max(env(safe-area-inset-right), 1rem);
bottom: max(env(safe-area-inset-bottom), 1rem); bottom: max(env(safe-area-inset-bottom), 1rem);
z-index: 1080; z-index: 2000;
pointer-events: none;
width: min(480px, 96vw); width: min(480px, 96vw);
max-width: min(480px, 96vw); max-width: min(480px, 96vw);
pointer-events: none;
} }
#toast-container .toast { #toast-container .toast {