refator_comm1

This commit is contained in:
Mateusz Gruszczyński
2025-10-18 23:11:25 +02:00
parent e73298d9bc
commit cdc13def49

View File

@@ -41,19 +41,25 @@ function setBadgeCell(cell, textOrState) {
}
function rebuildTargetSelect(selectEl, currentNode, nodes) {
if (!selectEl) return;
const html = nodes.map(n =>
`<option value="${n}" ${n === currentNode ? 'disabled selected' : ''}>${n}</option>`
).join('');
selectEl.innerHTML = html;
const idx = Array.from(selectEl.options).findIndex(o => o.disabled);
selectEl.selectedIndex = idx >= 0 ? idx : 0;
if (!selectEl) return [];
const others = (nodes || [])
.map(n => String(n).trim())
.filter(Boolean)
.filter(n => n !== String(currentNode || '').trim());
selectEl.innerHTML = others.map(n => `<option value="${n}">${n}</option>`).join('');
if (selectEl.options.length > 0) {
selectEl.selectedIndex = 0;
}
return others; // return list to decide enable/disable of MIGRATE
}
function setMigrateDisabled(tr, isRunning) {
function updateMigrateButton(tr, isRunning) {
const btn = tr?.querySelector('.act-migrate');
const targetSel = tr?.querySelector('.target-node');
if (!btn) return;
if (isRunning) {
const hasTarget = targetSel && targetSel.options && targetSel.options.length > 0;
const enable = isRunning && hasTarget;
if (enable) {
btn.removeAttribute('disabled');
btn.classList.remove('disabled');
} else {
@@ -62,74 +68,24 @@ function setMigrateDisabled(tr, isRunning) {
}
}
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();
function updateActionButtons(tr, isRunning) {
const bStart = tr?.querySelector('.act-start');
const bStop = tr?.querySelector('.act-stop');
const bShutdown = tr?.querySelector('.act-shutdown');
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 (bShutdown) {
if (isRunning) { bShutdown.removeAttribute('disabled'); bShutdown.classList.remove('disabled'); }
else { bShutdown.setAttribute('disabled',''); bShutdown.classList.add('disabled'); }
}
}
function ensureWatchOn() {
const tbody = document.querySelector('#vm-admin tbody');
if (!tbody) return;
Array.from(tbody.querySelectorAll('tr[data-sid]')).forEach(tr => {
const sid = tr.getAttribute('data-sid');
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');
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);
}
}
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);
}
}
} catch {}
};
} catch {}
});
}
export async function startAdminWatches() {
injectOnceCSS();
const tbody = document.querySelector('#vm-admin tbody');
if (!tbody) return;
const ns = await api.nodesSummary();
const availableNodes = Array.isArray(ns?.nodes)
? Array.from(new Set(ns.nodes.map(n => String(n.name || n.node || n).trim()).filter(Boolean)))
: (Array.isArray(ns) ? Array.from(new Set(ns.map(n => String(n.name || n.node || n).trim()).filter(Boolean))) : []);
setRows(tbody, []);
@@ -149,17 +105,19 @@ export async function startAdminWatches() {
}));
const htmlRows = rows.map(r => rowHTML([
`<input type="checkbox" class="row-check">`,
`<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>`
`<div class=\"btn-group\">
<button class=\"btn btn-sm btn-success act-start\">Start</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>
<button class=\"btn btn-sm btn-warning act-unlock\">Unlock</button>
</div>`,
`<select class=\"form-select form-select-sm target-node\"></select>`,
`<button class=\"btn btn-sm btn-primary w-100 act-migrate\" disabled>MIGRATE</button>`
]));
setRows(tbody, htmlRows);
@@ -168,7 +126,8 @@ export async function startAdminWatches() {
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 _others = rebuildTargetSelect(targetSel, nodeCell?.textContent.trim(), availableNodes);
updateMigrateButton(tr, /running|online|started/i.test(tr.children[4].innerText));
const sid = tr.getAttribute('data-sid');
const nameCell = tr.children[2];
@@ -178,7 +137,7 @@ export async function startAdminWatches() {
const targetNode = needsTarget ? targetSel?.value : undefined;
activeSids.add(sid);
setBadgeCell(tr.children[4], 'working');
setMigrateDisabled(tr, false);
updateMigrateButton(tr, false);
const res = await api.vmAction(sid, kind, targetNode);
if (res?.ok) {
showToast(`Task ${kind} started for ${safe(nameCell.textContent)}`);
@@ -190,8 +149,10 @@ export async function startAdminWatches() {
}
}
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-unlock')?.addEventListener('click', () => doAction('unlock'));
tr.querySelector('.act-migrate')?.addEventListener('click', () => doAction('migrate', true));
ensureWatchOn();
@@ -221,7 +182,8 @@ export async function startAdminWatches() {
const newNode = String(rowData.node || '').trim();
if (nodeCell && newNode && nodeCell.textContent.trim() !== newNode) {
nodeCell.textContent = newNode;
rebuildTargetSelect(targetSel, newNode, nodesNow);
const _others = rebuildTargetSelect(targetSel, newNode, nodesNow);
updateMigrateButton(tr, /running|online|started/i.test(tr.children[4].innerText));
flashDot(nameCell);
}
@@ -232,7 +194,9 @@ export async function startAdminWatches() {
if (stRaw) {
const changed = setBadgeCell(statusCell, stRaw);
const isRunning = /running|online|started/.test(stRaw);
setMigrateDisabled(tr, isRunning);
updateMigrateButton(tr, isRunning);
updateActionButtons(tr, isRunning);
updateActionButtons(tr, isRunning);
if (changed) flashDot(nameCell);
}
}
@@ -259,7 +223,9 @@ export async function startAdminWatches() {
const stRaw = String((detail.current && (detail.current.status || detail.current.qmpstatus)) || '').toLowerCase();
const changed = setBadgeCell(statusCell, stRaw);
const isRunning = /running|online|started/.test(stRaw);
setMigrateDisabled(tr, isRunning);
updateMigrateButton(tr, isRunning);
updateActionButtons(tr, isRunning);
updateActionButtons(tr, isRunning);
if (changed) flashDot(nameCell);
const newNode = String(detail.node || (detail.meta && detail.meta.node) || '').trim();