refator_comm1

This commit is contained in:
Mateusz Gruszczyński
2025-10-18 23:40:59 +02:00
parent eaa9cfbe8c
commit 7a4b73ea93

View File

@@ -7,10 +7,10 @@ let slowTimer = null;
let fastTimer = null; let fastTimer = null;
const activeSids = new Set(); const activeSids = new Set();
// --- Small helpers (no optional chaining) --- // --- tiny safe helpers ---
function qSel(root, sel){ return root ? root.querySelector(sel) : null; } function q(root, sel){ return root ? root.querySelector(sel) : null; }
function text(el){ return (el && el.textContent) ? el.textContent : ''; } function qq(root, sel){ return root ? Array.from(root.querySelectorAll(sel)) : []; }
function val(el){ return el ? el.value : undefined; } function txt(el){ return (el && el.textContent) ? el.textContent : ''; }
function low(x){ return String(x||'').toLowerCase(); } function low(x){ return String(x||'').toLowerCase(); }
function injectOnceCSS() { function injectOnceCSS() {
@@ -29,7 +29,7 @@ function flashDot(cell) {
const dot = document.createElement('span'); const dot = document.createElement('span');
dot.className = 'pulse-dot'; dot.className = 'pulse-dot';
cell.appendChild(dot); cell.appendChild(dot);
setTimeout(() => { if (dot && dot.parentNode) dot.parentNode.removeChild(dot); }, 1500); setTimeout(function(){ if (dot && dot.parentNode) dot.parentNode.removeChild(dot); }, 1500);
} }
function setBadgeCell(cell, textOrState) { function setBadgeCell(cell, textOrState) {
@@ -44,21 +44,33 @@ function setBadgeCell(cell, textOrState) {
return false; 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 [];
}
function rebuildTargetSelect(selectEl, currentNode, nodes) { function rebuildTargetSelect(selectEl, currentNode, nodes) {
if (!selectEl) return []; if (!selectEl) return [];
const current = String(currentNode || '').trim(); const current = String(currentNode || '').trim();
const others = (nodes || []) const all = (nodes || []).map(n => String(n && (n.name || n.node || n)).trim()).filter(Boolean);
.map(n => String(n && (n.name || n.node || n)).trim()) const others = all.filter(n => n !== current);
.filter(Boolean)
.filter(n => n !== current);
selectEl.innerHTML = others.map(n => `<option value="${n}">${n}</option>`).join(''); selectEl.innerHTML = others.map(n => `<option value="${n}">${n}</option>`).join('');
if (selectEl.options.length > 0) selectEl.selectedIndex = 0; // default: pick the "second node" if exists, otherwise first available other
var idx = 0;
if (all.length >= 2) {
const preferred = all[1];
const j = others.indexOf(preferred);
if (j >= 0) idx = j;
}
if (selectEl.options.length > 0) selectEl.selectedIndex = idx;
return others; return others;
} }
function updateMigrateButton(tr, isRunning) { function updateMigrateButton(tr, isRunning) {
const btn = qSel(tr, '.act-migrate'); const btn = q(tr, '.act-migrate');
const targetSel = qSel(tr, '.target-node'); const targetSel = q(tr, '.target-node');
const hasTarget = !!(targetSel && targetSel.options && targetSel.options.length > 0); const hasTarget = !!(targetSel && targetSel.options && targetSel.options.length > 0);
const enable = !!(isRunning && hasTarget); const enable = !!(isRunning && hasTarget);
if (!btn) return; if (!btn) return;
@@ -67,9 +79,9 @@ function updateMigrateButton(tr, isRunning) {
} }
function updateActionButtons(tr, isRunning) { function updateActionButtons(tr, isRunning) {
const bStart = qSel(tr, '.act-start'); const bStart = q(tr, '.act-start');
const bStop = qSel(tr, '.act-stop'); const bStop = q(tr, '.act-stop');
const bShutdown = qSel(tr, '.act-shutdown'); const bShutdown = q(tr, '.act-shutdown');
if (bStart) { if (isRunning) { bStart.setAttribute('disabled',''); bStart.classList.add('disabled'); } else { bStart.removeAttribute('disabled'); bStart.classList.remove('disabled'); } } if (bStart) { if (isRunning) { bStart.setAttribute('disabled',''); bStart.classList.add('disabled'); } else { bStart.removeAttribute('disabled'); bStart.classList.remove('disabled'); } }
if (bStop) { if (isRunning) { bStop.removeAttribute('disabled'); bStop.classList.remove('disabled'); } else { bStop.setAttribute('disabled',''); bStop.classList.add('disabled'); } } 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'); } } if (bShutdown) { if (isRunning) { bShutdown.removeAttribute('disabled'); bShutdown.classList.remove('disabled'); } else { bShutdown.setAttribute('disabled',''); bShutdown.classList.add('disabled'); } }
@@ -86,8 +98,7 @@ export function stopAllAdminWatches() {
function ensureWatchOn() { function ensureWatchOn() {
const tbody = document.querySelector('#vm-admin tbody'); const tbody = document.querySelector('#vm-admin tbody');
if (!tbody) return; if (!tbody) return;
const rows = Array.from(tbody.querySelectorAll('tr[data-sid]')); qq(tbody, 'tr[data-sid]').forEach(function(tr){
rows.forEach(function(tr){
const sid = tr.getAttribute('data-sid'); const sid = tr.getAttribute('data-sid');
if (!sid || liveSockets.has(sid)) return; if (!sid || liveSockets.has(sid)) return;
try { try {
@@ -101,19 +112,19 @@ function ensureWatchOn() {
try { try {
const msg = JSON.parse(ev.data || '{}'); const msg = JSON.parse(ev.data || '{}');
if (!msg || !msg.type) return; if (!msg || !msg.type) return;
const tr = tbody.querySelector('tr[data-sid="' + sid + '"]'); const tr2 = tbody.querySelector('tr[data-sid="' + sid + '"]');
if (!tr) return; if (!tr2) return;
const statusCell = tr.children[4]; const statusCell = tr2.children[4];
const nameCell = tr.children[2]; const nameCell = tr2.children[2];
const nodeCell = tr.children[3]; const nodeCell = tr2.children[3];
const targetSel = qSel(tr, '.target-node'); const targetSel = q(tr2, '.target-node');
if (msg.type === 'status') { if (msg.type === 'status') {
const stRaw = low(msg.status); const stRaw = low(msg.status);
const changed = setBadgeCell(statusCell, stRaw); const changed = setBadgeCell(statusCell, stRaw);
const isRunning = /running|online|started/.test(stRaw); const isRunning = /running|online|started/.test(stRaw);
updateMigrateButton(tr, isRunning); updateMigrateButton(tr2, isRunning);
updateActionButtons(tr, isRunning); updateActionButtons(tr2, isRunning);
if (changed) flashDot(nameCell); if (changed) flashDot(nameCell);
if (stRaw && /running|stopped|shutdown/.test(stRaw)) { if (stRaw && /running|stopped|shutdown/.test(stRaw)) {
setTimeout(function(){ activeSids.delete(sid); }, 3000); setTimeout(function(){ activeSids.delete(sid); }, 3000);
@@ -122,7 +133,7 @@ function ensureWatchOn() {
if (msg.type === 'node' && msg.node) { if (msg.type === 'node' && msg.node) {
const newNode = String(msg.node).trim(); const newNode = String(msg.node).trim();
if (nodeCell && newNode && text(nodeCell).trim() !== newNode) { if (nodeCell && newNode && txt(nodeCell).trim() !== newNode) {
nodeCell.textContent = newNode; nodeCell.textContent = newNode;
rebuildTargetSelect(targetSel, newNode, window.__nodesCache || []); rebuildTargetSelect(targetSel, newNode, window.__nodesCache || []);
flashDot(nameCell); flashDot(nameCell);
@@ -134,14 +145,6 @@ function ensureWatchOn() {
}); });
} }
function dedupe(arr){ return Array.from(new Set(arr)); }
function extractNodeNames(ns){
if (!ns) return [];
if (Array.isArray(ns.nodes)) return dedupe(ns.nodes.map(n => String(n.name || n.node || n).trim()).filter(Boolean));
if (Array.isArray(ns)) return dedupe(ns.map(n => String(n.name || n.node || n).trim()).filter(Boolean));
return [];
}
export async function startAdminWatches() { export async function startAdminWatches() {
injectOnceCSS(); injectOnceCSS();
const tbody = document.querySelector('#vm-admin tbody'); const tbody = document.querySelector('#vm-admin tbody');
@@ -170,29 +173,37 @@ export async function startAdminWatches() {
safe(r.name), safe(r.name),
safe(r.node), safe(r.node),
badge(safe(r.status), /running|online|started/i.test(r.status) ? 'ok' : 'dark'), badge(safe(r.status), /running|online|started/i.test(r.status) ? 'ok' : 'dark'),
`<div class=\"btn-group\"> `<div class="btn-group">
<button class=\"btn btn-sm btn-outline-light act-start\">Start</button> <button type="button" class="btn btn-sm btn-outline-success act-start">Start</button>
<button class=\"btn btn-sm btn-outline-light act-shutdown\">Shutdown</button> <button type="button" class="btn btn-sm btn-outline-warning act-shutdown">Shutdown</button>
<button class=\"btn btn-sm btn-outline-light act-stop\">Stop</button> <button type="button" class="btn btn-sm btn-outline-danger act-stop">Stop</button>
<button class=\"btn btn-sm btn-outline-light 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>`, `<select class="form-select form-select-sm target-node position-relative"></select>`,
`<button class=\"btn btn-sm btn-outline-light w-100 act-migrate\" disabled>MIGRATE</button>` `<button type="button" class="btn btn-sm btn-outline-primary w-100 act-migrate" disabled>MIGRATE</button>`
])); ]));
setRows(tbody, htmlRows); setRows(tbody, htmlRows);
// wire per-row // prepare rows
// (replaced by delegated handler below) 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);
updateMigrateButton(tr, /running|online|started/i.test(tr.children[4].innerText));
});
// delegated events for reliability // delegated events (reliable after refreshes)
tbody.addEventListener('click', async function(ev){ tbody.addEventListener('click', async function(ev){
const t = ev.target; const t = ev.target;
const tr = t && t.closest ? t.closest('tr[data-sid]') : null; const btn = t && t.closest ? t.closest('.act-start,.act-stop,.act-shutdown,.act-unlock,.act-migrate') : null;
if (!btn) return;
const tr = btn.closest ? btn.closest('tr[data-sid]') : null;
if (!tr) return; if (!tr) return;
const sid = tr.getAttribute('data-sid'); const sid = tr.getAttribute('data-sid');
const nameCell = tr.children[2]; const nameCell = tr.children[2];
const targetSel = tr.querySelector('.target-node'); const targetSel = q(tr, '.target-node');
async function doAction(kind, needsTarget) { async function doAction(kind, needsTarget) {
try { try {
const targetNode = needsTarget ? (targetSel ? targetSel.value : undefined) : undefined; const targetNode = needsTarget ? (targetSel ? targetSel.value : undefined) : undefined;
@@ -200,45 +211,44 @@ export async function startAdminWatches() {
setBadgeCell(tr.children[4], 'working'); setBadgeCell(tr.children[4], 'working');
updateMigrateButton(tr, false); updateMigrateButton(tr, false);
const res = await api.vmAction(sid, kind, targetNode); const res = await api.vmAction(sid, kind, targetNode);
if (res && res.ok) { showToast(`Task ${kind} started for ${safe(nameCell.textContent)}`); } if (res && res.ok) { showToast(`Task ${kind} started for ${safe(txt(nameCell))}`); }
else { showToast(`Task ${kind} failed for ${safe(nameCell.textContent)}`, 'danger'); } else { showToast(`Task ${kind} failed for ${safe(txt(nameCell))}`, 'danger'); }
} catch(e) { showToast(`Error: ${e && e.message ? e.message : e}`, 'danger'); } } catch(e) { showToast(`Error: ${e && e.message ? e.message : e}`, 'danger'); }
} }
if (t.matches && t.matches('.act-start')) return doAction('start'); if (btn.classList.contains('act-start')) return doAction('start');
if (t.matches && t.matches('.act-stop')) return doAction('stop'); if (btn.classList.contains('act-stop')) return doAction('stop');
if (t.matches && t.matches('.act-shutdown')) return doAction('shutdown'); if (btn.classList.contains('act-shutdown')) return doAction('shutdown');
if (t.matches && t.matches('.act-unlock')) return doAction('unlock'); if (btn.classList.contains('act-unlock')) return doAction('unlock');
if (t.matches && t.matches('.act-migrate')) return doAction('migrate', true); if (btn.classList.contains('act-migrate')) return doAction('migrate', true);
}); });
window.__nodesCache = availableNodes.slice(); window.__nodesCache = availableNodes.slice();
// slow: full refresh every 30s // slow full refresh
slowTimer = setInterval(async function(){ slowTimer = setInterval(async function(){
try { try {
const latest = await api.listAllVmct(); const latest2 = await api.listAllVmct();
const all = Array.isArray(latest.all) ? latest.all : []; const all2 = Array.isArray(latest2.all) ? latest2.all : [];
const bySid = new Map(all.map(x => [String(x.sid), x])); const bySid = new Map(all2.map(x => [String(x.sid), x]));
const nodesNow = Array.isArray(latest.nodes) ? latest.nodes : (window.__nodesCache || []); const nodesNow2 = Array.isArray(latest2.nodes) ? latest2.nodes : (window.__nodesCache || []);
window.__nodesCache = nodesNow; window.__nodesCache = nodesNow2;
Array.from(tbody.querySelectorAll('tr[data-sid]')).forEach(function(tr){ qq(tbody, 'tr[data-sid]').forEach(function(tr){
const sid = tr.getAttribute('data-sid'); const sid = tr.getAttribute('data-sid');
const rowData = bySid.get(sid); const rowData = bySid.get(sid);
if (!rowData) return; if (!rowData) return;
const nodeCell = tr.children[3]; const nodeCell = tr.children[3];
const statusCell= tr.children[4]; const statusCell= tr.children[4];
const nameCell = tr.children[2]; const nameCell = tr.children[2];
const targetSel = qSel(tr, '.target-node'); const targetSel = q(tr, '.target-node');
const newNode = String(rowData.node || '').trim(); const newNode = String(rowData.node || '').trim();
if (nodeCell && newNode && text(nodeCell).trim() !== newNode) { if (nodeCell && newNode && txt(nodeCell).trim() !== newNode) {
nodeCell.textContent = newNode; nodeCell.textContent = newNode;
rebuildTargetSelect(targetSel, newNode, nodesNow); rebuildTargetSelect(targetSel, newNode, nodesNow2);
flashDot(nameCell); flashDot(nameCell);
} }
// status from slow reconcile — only when not 'working' to avoid overruling WS
const currentTxt = low((statusCell && statusCell.innerText) || ''); const currentTxt = low((statusCell && statusCell.innerText) || '');
if (!/working/.test(currentTxt)) { if (!/working/.test(currentTxt)) {
const stRaw = low(rowData.status || ''); const stRaw = low(rowData.status || '');
@@ -254,7 +264,7 @@ export async function startAdminWatches() {
} catch(e){} } catch(e){}
}, 30000); }, 30000);
// fast: active sids every 10s // fast refresh: active sids
fastTimer = setInterval(async function(){ fastTimer = setInterval(async function(){
try { try {
const sids = Array.from(activeSids); const sids = Array.from(activeSids);
@@ -269,7 +279,7 @@ export async function startAdminWatches() {
const nodeCell = tr.children[3]; const nodeCell = tr.children[3];
const statusCell= tr.children[4]; const statusCell= tr.children[4];
const nameCell = tr.children[2]; const nameCell = tr.children[2];
const targetSel = qSel(tr, '.target-node'); const targetSel = q(tr, '.target-node');
const stRaw = low((detail.current && (detail.current.status || detail.current.qmpstatus)) || ''); const stRaw = low((detail.current && (detail.current.status || detail.current.qmpstatus)) || '');
const changed = setBadgeCell(statusCell, stRaw); const changed = setBadgeCell(statusCell, stRaw);
@@ -280,7 +290,7 @@ export async function startAdminWatches() {
const newNode = String(detail.node || (detail.meta && detail.meta.node) || '').trim(); const newNode = String(detail.node || (detail.meta && detail.meta.node) || '').trim();
if (newNode) { if (newNode) {
if (nodeCell && text(nodeCell).trim() !== newNode) { if (nodeCell && txt(nodeCell).trim() !== newNode) {
nodeCell.textContent = newNode; nodeCell.textContent = newNode;
rebuildTargetSelect(targetSel, newNode, window.__nodesCache || []); rebuildTargetSelect(targetSel, newNode, window.__nodesCache || []);
flashDot(nameCell); flashDot(nameCell);