refator_comm1
This commit is contained in:
@@ -41,19 +41,25 @@ function setBadgeCell(cell, textOrState) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function rebuildTargetSelect(selectEl, currentNode, nodes) {
|
function rebuildTargetSelect(selectEl, currentNode, nodes) {
|
||||||
if (!selectEl) return;
|
if (!selectEl) return [];
|
||||||
const html = nodes.map(n =>
|
const others = (nodes || [])
|
||||||
`<option value="${n}" ${n === currentNode ? 'disabled selected' : ''}>${n}</option>`
|
.map(n => String(n).trim())
|
||||||
).join('');
|
.filter(Boolean)
|
||||||
selectEl.innerHTML = html;
|
.filter(n => n !== String(currentNode || '').trim());
|
||||||
const idx = Array.from(selectEl.options).findIndex(o => o.disabled);
|
selectEl.innerHTML = others.map(n => `<option value="${n}">${n}</option>`).join('');
|
||||||
selectEl.selectedIndex = idx >= 0 ? idx : 0;
|
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 btn = tr?.querySelector('.act-migrate');
|
||||||
|
const targetSel = tr?.querySelector('.target-node');
|
||||||
if (!btn) return;
|
if (!btn) return;
|
||||||
if (isRunning) {
|
const hasTarget = targetSel && targetSel.options && targetSel.options.length > 0;
|
||||||
|
const enable = isRunning && hasTarget;
|
||||||
|
if (enable) {
|
||||||
btn.removeAttribute('disabled');
|
btn.removeAttribute('disabled');
|
||||||
btn.classList.remove('disabled');
|
btn.classList.remove('disabled');
|
||||||
} else {
|
} else {
|
||||||
@@ -62,74 +68,24 @@ function setMigrateDisabled(tr, isRunning) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function stopAllAdminWatches() {
|
function updateActionButtons(tr, isRunning) {
|
||||||
liveSockets.forEach(ws => { try { ws.close(); } catch {} });
|
const bStart = tr?.querySelector('.act-start');
|
||||||
liveSockets.clear();
|
const bStop = tr?.querySelector('.act-stop');
|
||||||
if (slowTimer) clearInterval(slowTimer);
|
const bShutdown = tr?.querySelector('.act-shutdown');
|
||||||
if (fastTimer) clearInterval(fastTimer);
|
if (bStart) {
|
||||||
slowTimer = null;
|
if (isRunning) { bStart.setAttribute('disabled',''); bStart.classList.add('disabled'); }
|
||||||
fastTimer = null;
|
else { bStart.removeAttribute('disabled'); bStart.classList.remove('disabled'); }
|
||||||
activeSids.clear();
|
}
|
||||||
|
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.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))) : []);
|
: (Array.isArray(ns) ? Array.from(new Set(ns.map(n => String(n.name || n.node || n).trim()).filter(Boolean))) : []);
|
||||||
setRows(tbody, []);
|
setRows(tbody, []);
|
||||||
@@ -149,17 +105,19 @@ export async function startAdminWatches() {
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
const htmlRows = rows.map(r => rowHTML([
|
const htmlRows = rows.map(r => rowHTML([
|
||||||
`<input type="checkbox" class="row-check">`,
|
`<input type=\"checkbox\" class=\"row-check\">`,
|
||||||
safe(r.sid),
|
safe(r.sid),
|
||||||
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'),
|
||||||
`<select class="form-select form-select-sm target-node"></select>`,
|
`<div class=\"btn-group\">
|
||||||
`<div class="btn-group">
|
<button class=\"btn btn-sm btn-success act-start\">Start</button>
|
||||||
<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-secondary act-shutdown">Shutdown</button>
|
<button class=\"btn btn-sm btn-outline-danger act-stop\">Stop</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>`
|
</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);
|
setRows(tbody, htmlRows);
|
||||||
@@ -168,7 +126,8 @@ export async function startAdminWatches() {
|
|||||||
Array.from(tbody.querySelectorAll('tr[data-sid]')).forEach(tr => {
|
Array.from(tbody.querySelectorAll('tr[data-sid]')).forEach(tr => {
|
||||||
const nodeCell = tr.children[3];
|
const nodeCell = tr.children[3];
|
||||||
const targetSel = tr.querySelector('.target-node');
|
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 sid = tr.getAttribute('data-sid');
|
||||||
const nameCell = tr.children[2];
|
const nameCell = tr.children[2];
|
||||||
@@ -178,7 +137,7 @@ export async function startAdminWatches() {
|
|||||||
const targetNode = needsTarget ? targetSel?.value : undefined;
|
const targetNode = needsTarget ? targetSel?.value : undefined;
|
||||||
activeSids.add(sid);
|
activeSids.add(sid);
|
||||||
setBadgeCell(tr.children[4], 'working');
|
setBadgeCell(tr.children[4], 'working');
|
||||||
setMigrateDisabled(tr, false);
|
updateMigrateButton(tr, false);
|
||||||
const res = await api.vmAction(sid, kind, targetNode);
|
const res = await api.vmAction(sid, kind, targetNode);
|
||||||
if (res?.ok) {
|
if (res?.ok) {
|
||||||
showToast(`Task ${kind} started for ${safe(nameCell.textContent)}`);
|
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-stop')?.addEventListener('click', () => doAction('stop'));
|
||||||
tr.querySelector('.act-shutdown')?.addEventListener('click', () => doAction('shutdown'));
|
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));
|
tr.querySelector('.act-migrate')?.addEventListener('click', () => doAction('migrate', true));
|
||||||
|
|
||||||
ensureWatchOn();
|
ensureWatchOn();
|
||||||
@@ -221,7 +182,8 @@ export async function startAdminWatches() {
|
|||||||
const newNode = String(rowData.node || '').trim();
|
const newNode = String(rowData.node || '').trim();
|
||||||
if (nodeCell && newNode && nodeCell.textContent.trim() !== newNode) {
|
if (nodeCell && newNode && nodeCell.textContent.trim() !== newNode) {
|
||||||
nodeCell.textContent = 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);
|
flashDot(nameCell);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -232,7 +194,9 @@ export async function startAdminWatches() {
|
|||||||
if (stRaw) {
|
if (stRaw) {
|
||||||
const changed = setBadgeCell(statusCell, stRaw);
|
const changed = setBadgeCell(statusCell, stRaw);
|
||||||
const isRunning = /running|online|started/.test(stRaw);
|
const isRunning = /running|online|started/.test(stRaw);
|
||||||
setMigrateDisabled(tr, isRunning);
|
updateMigrateButton(tr, isRunning);
|
||||||
|
updateActionButtons(tr, isRunning);
|
||||||
|
updateActionButtons(tr, isRunning);
|
||||||
if (changed) flashDot(nameCell);
|
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 stRaw = String((detail.current && (detail.current.status || detail.current.qmpstatus)) || '').toLowerCase();
|
||||||
const changed = setBadgeCell(statusCell, stRaw);
|
const changed = setBadgeCell(statusCell, stRaw);
|
||||||
const isRunning = /running|online|started/.test(stRaw);
|
const isRunning = /running|online|started/.test(stRaw);
|
||||||
setMigrateDisabled(tr, isRunning);
|
updateMigrateButton(tr, isRunning);
|
||||||
|
updateActionButtons(tr, isRunning);
|
||||||
|
updateActionButtons(tr, isRunning);
|
||||||
if (changed) flashDot(nameCell);
|
if (changed) flashDot(nameCell);
|
||||||
|
|
||||||
const newNode = String(detail.node || (detail.meta && detail.meta.node) || '').trim();
|
const newNode = String(detail.node || (detail.meta && detail.meta.node) || '').trim();
|
||||||
|
Reference in New Issue
Block a user