const DAEMON_URL = 'http://127.0.0.1:12345'; document.addEventListener('DOMContentLoaded', () => { fetchInstances(); document.getElementById('refresh-btn').addEventListener('click', fetchInstances); }); async function fetchInstances() { const tableBody = document.getElementById('instances-table-body'); try { const response = await fetch(`${DAEMON_URL}/instances/list`); if (!response.ok) throw new Error(`Server returned code: ${response.status}`); const instances = await response.json(); renderTable(instances); } catch (error) { console.error('Failed fetching telemetry data:', error); tableBody.innerHTML = `Offline: Could not connect to vs-manager daemon.`; } } function renderTable(instances) { const tableBody = document.getElementById('instances-table-body'); if (!instances || instances.length === 0) { tableBody.innerHTML = `No server configurations registered.`; return; } tableBody.innerHTML = ''; instances.forEach(inst => { const row = document.createElement('tr'); const isRunning = inst.status === 'RUNNING'; row.innerHTML = ` ${inst.name} ${inst.version} ${inst.port} ${inst.status} `; tableBody.appendChild(row); }); document.querySelectorAll('.action-btn').forEach(btn => { btn.addEventListener('click', handleAction); }); } async function handleAction(e) { const button = e.target; const name = button.getAttribute('data-name'); const action = button.getAttribute('data-action'); button.disabled = true; button.innerText = 'Processing...'; try { const response = await fetch(`${DAEMON_URL}/instances/${action}?name=${encodeURIComponent(name)}`, { method: 'POST' }); if (!response.ok) { const errText = await response.text(); alert(`Action Failed: ${errText}`); } } catch (error) { alert(`Network transmission failure: ${error.message}`); } finally { await fetchInstances(); } }