refator_comm1

This commit is contained in:
Mateusz Gruszczyński
2025-10-18 22:57:05 +02:00
parent a841a1f9ad
commit 2ffbe9badf
2 changed files with 218 additions and 192 deletions

View File

@@ -17,32 +17,12 @@ function injectOnceCSS() {
document.head.appendChild(style);
}
function closeForSid(sid) {
const entry = liveSockets.get(sid);
if (!entry) return;
try { entry.obs && entry.obs.close(); } catch {}
liveSockets.delete(sid);
}
export function stopAllAdminWatches() {
if (slowTimer) { clearInterval(slowTimer); slowTimer = null; }
if (fastTimer) { clearInterval(fastTimer); fastTimer = null; }
activeSids.clear();
for (const sid of Array.from(liveSockets.keys())) closeForSid(sid);
}
function flashDot(cell) {
if (!cell) return;
const nameWrap = cell.querySelector('.vm-name-wrap') || (() => {
const w = document.createElement('span'); w.className = 'vm-name-wrap';
while (cell.firstChild) w.appendChild(cell.firstChild);
cell.appendChild(w);
return w;
})();
const dot = document.createElement('span');
dot.className = 'pulse-dot';
nameWrap.appendChild(dot);
setTimeout(() => { try { dot.remove(); } catch {} }, 4200);
cell.appendChild(dot);
setTimeout(() => dot.remove(), 1500);
}
function setBadgeCell(cell, textOrState) {
@@ -66,124 +46,149 @@ function rebuildTargetSelect(selectEl, currentNode, nodes) {
`<option value="${n}" ${n === currentNode ? 'disabled selected' : ''}>${n}</option>`
).join('');
selectEl.innerHTML = html;
const idx = Array.from(selectEl.options).findIndex(o => !o.disabled);
if (idx >= 0) selectEl.selectedIndex = idx;
const idx = Array.from(selectEl.options).findIndex(o => o.disabled);
selectEl.selectedIndex = idx >= 0 ? idx : 0;
}
function setMigrateDisabled(tr, disabled) {
const btn = tr.querySelector('.act-migrate');
if (btn) btn.disabled = !!disabled;
function setMigrateDisabled(tr, isRunning) {
const btn = tr?.querySelector('.act-migrate');
if (!btn) return;
if (isRunning) {
btn.removeAttribute('disabled');
btn.classList.remove('disabled');
} else {
btn.setAttribute('disabled', '');
btn.classList.add('disabled');
}
}
export async function renderVMAdmin() {
injectOnceCSS();
stopAllAdminWatches();
export function stopAllAdminWatches() {
liveSockets.forEach(ws => { try { ws.close(); } catch {} });
liveSockets.clear();
if (slowTimer) clearInterval(slowTimer);
if (fastTimer) clearInterval(fastTimer);
slowTimer = null;
fastTimer = null;
activeSids.clear();
}
const data = await api.listAllVmct();
const arr = Array.isArray(data.all) ? data.all : [];
const availableNodes = Array.isArray(data.nodes) ? data.nodes : [];
function ensureWatchOn() {
const tbody = document.querySelector('#vm-admin tbody');
if (!arr.length) { setRows(tbody, [rowHTML(['Brak VM/CT'])]); return; }
const rows = arr.map(x => {
const sid = safe(x.sid), type = safe(x.type), name = safe(x.name), node = safe(x.node);
const nameCell = `<span class="vm-name-wrap">${name}</span>`;
const statusCell = badge('—','dark');
const actions = `
<div class="btn-group btn-group-sm" role="group">
<button class="btn btn-outline-secondary act-unlock">Unlock</button>
<button class="btn btn-outline-success act-start">Start</button>
<button class="btn btn-outline-warning act-shutdown">Shutdown</button>
<button class="btn btn-outline-danger act-stop">Stop</button>
</div>`;
const sel = `<select class="form-select form-select-sm target-node" style="min-width:160px">
${availableNodes.map(n => `<option value="${n}" ${n === x.node ? 'disabled selected' : ''}>${n}</option>`).join('')}
</select>`;
const migrateBtn = `<button class="btn btn-outline-primary btn-sm act-migrate">Migrate (offline)</button>`;
// SID | TYPE | NAME | NODE | STATUS | ACTIONS | TARGET | MIGRATE
return rowHTML([sid, type.toUpperCase(), nameCell, node, statusCell, actions, sel, migrateBtn], `data-sid="${sid}"`);
});
setRows(tbody, rows);
if (!tbody) return;
Array.from(tbody.querySelectorAll('tr[data-sid]')).forEach(tr => {
const sid = tr.getAttribute('data-sid');
const nodeCell = tr.children[3];
const statusCell= tr.children[4];
if (!sid) return;
if (liveSockets.has(sid)) return;
try {
const ws = new WebSocket(`${location.protocol === 'https:' ? 'wss' : 'ws'}://${location.host}/ws/observe?sid=${encodeURIComponent(sid)}`);
liveSockets.set(sid, ws);
ws.onopen = () => {};
ws.onclose = () => { liveSockets.delete(sid); };
ws.onerror = () => {};
ws.onmessage = (ev) => {
try {
const msg = JSON.parse(ev.data || '{}');
if (!msg || !msg.type) return;
const tr = tbody.querySelector(`tr[data-sid="${sid}"]`);
if (!tr) return;
const statusCell = tr.children[4];
const nameCell = tr.children[2];
const nodeCell = tr.children[3];
const targetSel = tr.querySelector('.target-node');
const ensureWatchOn = () => {
const existing = liveSockets.get(sid);
if (existing && existing.obs && existing.obs.readyState <= 1) return;
closeForSid(sid);
const wsObs = new WebSocket(api.wsObserveURL(sid));
liveSockets.set(sid, { obs: wsObs });
wsObs.onmessage = (ev) => {
try {
const msg = JSON.parse(ev.data);
if (msg.type === 'vm' && msg.current) {
const st = String(msg.current.status || msg.current.qmpstatus || '').toLowerCase();
const changed = setBadgeCell(statusCell, st);
const isRunning = /running|online|started/.test(st);
if (msg.type === 'status') {
const stRaw = String(msg.status || '').toLowerCase();
const changed = setBadgeCell(statusCell, stRaw);
const isRunning = /running|online|started/.test(stRaw);
setMigrateDisabled(tr, isRunning);
if (changed) flashDot(nameCell);
if (stRaw && /running|stopped|shutdown/.test(stRaw)) {
setTimeout(() => activeSids.delete(sid), 3000);
}
else if (msg.type === 'task-start') {
setBadgeCell(statusCell, 'working');
activeSids.add(sid);
}
if (msg.type === 'node' && msg.node) {
const newNode = String(msg.node).trim();
if (nodeCell && newNode && nodeCell.textContent.trim() !== newNode) {
nodeCell.textContent = newNode;
rebuildTargetSelect(targetSel, newNode, window.__nodesCache || []);
flashDot(nameCell);
}
else if (msg.type === 'task') {
setBadgeCell(statusCell, 'working');
activeSids.add(sid);
}
else if (msg.type === 'moved' && msg.new_node) {
if (nodeCell && nodeCell.textContent.trim() !== msg.new_node) {
nodeCell.textContent = msg.new_node;
rebuildTargetSelect(targetSel, msg.new_node, window.__nodesCache || []);
flashDot(nameCell);
}
activeSids.add(sid);
}
else if (msg.type === 'done') {
// status końcowy dociągnie kolejny pakiet "vm" lub szybki refresh
setTimeout(() => activeSids.delete(sid), 4000);
flashDot(nameCell);
}
} catch {}
};
} catch {}
});
}
wsObs.onclose = () => {
const e = liveSockets.get(sid);
if (e && e.obs === wsObs) liveSockets.set(sid, { obs: null });
};
};
export async function startAdminWatches() {
injectOnceCSS();
const doAction = async (action, withTarget=false) => {
const tbody = document.querySelector('#vm-admin tbody');
if (!tbody) return;
const availableNodes = await api.listNodes();
setRows(tbody, []);
// inicjalne wypełnienie tabeli
try {
ensureWatchOn();
if (action !== 'unlock') {
setBadgeCell(statusCell, 'working');
activeSids.add(sid);
}
const target = withTarget ? (targetSel?.value || '') : undefined;
const resp = await api.vmAction(sid, action, target);
if (!resp.ok) throw new Error(resp.error || 'unknown');
flashDot(nameCell);
} catch (e) {
showToast('Error', 'ERROR: ' + (e.message || e), 'danger');
activeSids.delete(sid);
}
};
const latest = await api.listAllVmct();
const all = Array.isArray(latest.all) ? latest.all : [];
const nodesNow = Array.isArray(latest.nodes) ? latest.nodes : [];
window.__nodesCache = nodesNow.slice();
const rows = all.map(vm => ({
sid: safe(vm.sid),
name: safe(vm.name || vm.vmid || vm.sid),
node: safe(vm.node || '—'),
status: safe(vm.status || '—')
}));
const html = 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'),
`<select class="form-select form-select-sm target-node"></select>`,
`<div class="btn-group">
<button class="btn btn-sm btn-primary act-migrate" disabled>MIGRATE</button>
<button class="btn btn-sm btn-outline-secondary act-shutdown">Shutdown</button>
<button class="btn btn-sm btn-outline-danger act-stop">Stop</button>
</div>`
])).join('');
setRows(tbody, html);
// podłącz selecty z node'ami i akcje
Array.from(tbody.querySelectorAll('tr[data-sid]')).forEach(tr => {
const nodeCell = tr.children[3];
const targetSel = tr.querySelector('.target-node');
rebuildTargetSelect(targetSel, nodeCell?.textContent.trim(), availableNodes);
const sid = tr.getAttribute('data-sid');
const nameCell = tr.children[2];
async function doAction(kind, needsTarget) {
try {
const targetNode = needsTarget ? targetSel?.value : undefined;
activeSids.add(sid);
setBadgeCell(tr.children[4], 'working');
setMigrateDisabled(tr, false);
const res = await api.vmAction(sid, kind, targetNode);
if (res?.ok) {
showToast(`Zadanie ${kind} wystartowało dla ${safe(nameCell.textContent)}`);
} else {
showToast(`Błąd zadania ${kind} dla ${safe(nameCell.textContent)}`, 'danger');
}
} catch (e) {
showToast(`Błąd: ${e?.message || e}`, 'danger');
}
}
tr.querySelector('.act-unlock')?.addEventListener('click', () => doAction('unlock'));
tr.querySelector('.act-start')?.addEventListener('click', () => doAction('start'));
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));
ensureWatchOn();
@@ -220,7 +225,13 @@ export async function renderVMAdmin() {
// status z wolnego reconcile — tylko gdy brak „working”, żeby nie zagłuszać WS
const currentTxt = (statusCell?.innerText || '').toLowerCase();
if (!/working/.test(currentTxt)) {
// brak statusu w liście — zostaw jak jest, dociągnie WS/fastTimer
const stRaw = String(rowData.status || '').toLowerCase(); // fallback z /cluster/resources
if (stRaw) {
const changed = setBadgeCell(statusCell, stRaw);
const isRunning = /running|online|started/.test(stRaw);
setMigrateDisabled(tr, isRunning);
if (changed) flashDot(nameCell);
}
}
});
} catch {}
@@ -265,4 +276,7 @@ export async function renderVMAdmin() {
}, 10000);
window.addEventListener('beforeunload', stopAllAdminWatches, { once: true });
} catch (e) {
showToast(`Nie udało się załadować listy: ${e?.message || e}`, 'danger');
}
}

View File

@@ -143,3 +143,15 @@ footer.site-footer a:hover {
background-position: 0 0;
}
}
#toast-container {
position: fixed;
right: max(env(safe-area-inset-right), 1rem);
bottom: max(env(safe-area-inset-bottom), 1rem);
z-index: 1080;
pointer-events: none;
}
#toast-container .toast {
pointer-events: auto;
}