diff --git a/src/public/js/ui/dashboard.js b/src/public/js/ui/dashboard.js index a26f932b4..76192e308 100644 --- a/src/public/js/ui/dashboard.js +++ b/src/public/js/ui/dashboard.js @@ -1,11 +1,33 @@ PulseApp.ui = PulseApp.ui || {}; +const FILTER_ALL = 'all'; +const FILTER_VM = 'vm'; +const FILTER_LXC = 'lxc'; + +const GUEST_TYPE_VM = 'VM'; +const GUEST_TYPE_CT = 'CT'; + +const STATUS_RUNNING = 'running'; +const STATUS_STOPPED = 'stopped'; + +const METRIC_CPU = 'cpu'; +const METRIC_MEMORY = 'memory'; +const METRIC_DISK = 'disk'; +const METRIC_DISK_READ = 'diskread'; +const METRIC_DISK_WRITE = 'diskwrite'; +const METRIC_NET_IN = 'netin'; +const METRIC_NET_OUT = 'netout'; + PulseApp.ui.dashboard = (() => { let searchInput = null; let guestMetricDragSnapshot = {}; // To store metrics during slider drag + let tableBodyEl = null; + let statusElementEl = null; function init() { searchInput = document.getElementById('dashboard-search'); + tableBodyEl = document.querySelector('#main-table tbody'); + statusElementEl = document.getElementById('dashboard-status-text'); } function refreshDashboardData() { @@ -212,10 +234,8 @@ PulseApp.ui.dashboard = (() => { } function updateDashboardTable() { - const tableBody = document.querySelector('#main-table tbody'); - const statusElement = document.getElementById('dashboard-status-text'); - if (!tableBody || !statusElement) { - console.error('Dashboard table body or status element not found!'); + if (!tableBodyEl || !statusElementEl) { + console.error('Dashboard table body or status element not found/initialized!'); return; } @@ -229,8 +249,10 @@ PulseApp.ui.dashboard = (() => { const textSearchTerms = searchInput ? searchInput.value.toLowerCase().split(',').map(term => term.trim()).filter(term => term) : []; let filteredData = dashboardData.filter(guest => { - const typeMatch = filterGuestType === 'all' || (filterGuestType === 'vm' && guest.type === 'VM') || (filterGuestType === 'lxc' && guest.type === 'CT'); - const statusMatch = filterStatus === 'all' || guest.status === filterStatus; + const typeMatch = filterGuestType === FILTER_ALL || + (filterGuestType === FILTER_VM && guest.type === GUEST_TYPE_VM) || + (filterGuestType === FILTER_LXC && guest.type === GUEST_TYPE_CT); + const statusMatch = filterStatus === FILTER_ALL || guest.status === filterStatus; const searchMatch = textSearchTerms.length === 0 || textSearchTerms.some(term => (guest.name && guest.name.toLowerCase().includes(term)) || @@ -244,13 +266,13 @@ PulseApp.ui.dashboard = (() => { const state = thresholdState[type]; let guestValue; - if (type === 'cpu') guestValue = guest.cpu * 100; - else if (type === 'memory') guestValue = guest.memory; - else if (type === 'disk') guestValue = guest.disk; - else if (type === 'diskread') guestValue = guest.diskread; - else if (type === 'diskwrite') guestValue = guest.diskwrite; - else if (type === 'netin') guestValue = guest.netin; - else if (type === 'netout') guestValue = guest.netout; + if (type === METRIC_CPU) guestValue = guest.cpu * 100; + else if (type === METRIC_MEMORY) guestValue = guest.memory; + else if (type === METRIC_DISK) guestValue = guest.disk; + else if (type === METRIC_DISK_READ) guestValue = guest.diskread; + else if (type === METRIC_DISK_WRITE) guestValue = guest.diskwrite; + else if (type === METRIC_NET_IN) guestValue = guest.netin; + else if (type === METRIC_NET_OUT) guestValue = guest.netout; else continue; if (state.value > 0) { @@ -284,7 +306,7 @@ PulseApp.ui.dashboard = (() => { nodeGroups[nodeName].push(guest); }); - tableBody.innerHTML = ''; + tableBodyEl.innerHTML = ''; Object.keys(nodeGroups).sort().forEach(nodeName => { visibleNodes.add(nodeName.toLowerCase()); @@ -294,26 +316,26 @@ PulseApp.ui.dashboard = (() => { ${nodeName} `; - tableBody.appendChild(nodeHeaderRow); + tableBodyEl.appendChild(nodeHeaderRow); nodeGroups[nodeName].forEach(guest => { const guestRow = createGuestRow(guest); if (guestRow) { - tableBody.appendChild(guestRow); + tableBodyEl.appendChild(guestRow); visibleCount++; } }); }); } else { - PulseApp.utils.renderTableBody(tableBody, sortedData, createGuestRow, "No matching guests found.", 11); + PulseApp.utils.renderTableBody(tableBodyEl, sortedData, createGuestRow, "No matching guests found.", 11); visibleCount = sortedData.length; sortedData.forEach(guest => visibleNodes.add((guest.node || 'Unknown Node').toLowerCase())); } if (visibleCount === 0) { let filterDescription = []; - if (filterGuestType !== 'all') filterDescription.push(`Type: ${filterGuestType.toUpperCase()}`); - if (filterStatus !== 'all') filterDescription.push(`Status: ${filterStatus}`); + if (filterGuestType !== FILTER_ALL) filterDescription.push(`Type: ${filterGuestType.toUpperCase()}`); + if (filterStatus !== FILTER_ALL) filterDescription.push(`Status: ${filterStatus}`); if (textSearchTerms.length > 0) filterDescription.push(`Search: "${textSearchTerms.join(', ')}"`); const activeThresholds = Object.entries(thresholdState).filter(([_, state]) => state.value > 0); if (activeThresholds.length > 0) { @@ -329,20 +351,20 @@ PulseApp.ui.dashboard = (() => { } message += "."; - tableBody.innerHTML = `