better stat tracking

This commit is contained in:
Raven Scott 2024-11-30 03:08:13 -05:00
parent 1dc39decf7
commit b8c6f30d32

65
app.js
View File

@ -21,8 +21,8 @@ window.openTerminals = {};
let activePeer = null; let activePeer = null;
window.activePeer = null; // Expose to other modules window.activePeer = null; // Expose to other modules
hideStatusIndicator(); hideStatusIndicator();
let statsInterval = null; // Global variable to hold the interval let statsInterval = null;
let lastStatsUpdate = Date.now();
function stopStatsInterval() { function stopStatsInterval() {
if (statsInterval) { if (statsInterval) {
clearInterval(statsInterval); clearInterval(statsInterval);
@ -31,21 +31,31 @@ function stopStatsInterval() {
} }
} }
function startStatsInterval() { function startStatsInterval() {
// Clear any existing interval
if (statsInterval) { if (statsInterval) {
clearInterval(statsInterval); clearInterval(statsInterval);
} }
// Start a new interval to request stats every second
statsInterval = setInterval(() => { statsInterval = setInterval(() => {
if (window.activePeer) { if (window.activePeer) {
console.log('[INFO] Requesting container stats...'); const now = Date.now();
sendCommand('stats', {}); // Adjust the command if specific arguments are needed if (now - lastStatsUpdate >= 500) { // Ensure at least 500ms between updates
sendCommand('stats', {}); // Adjust command if necessary
lastStatsUpdate = now;
}
} else { } else {
console.warn('[WARN] No active peer; skipping stats request.'); console.warn('[WARN] No active peer; skipping stats request.');
} }
}, 1000); // 1 second interval }, 100); // Poll every 100ms for better reactivity
}
let smoothedStats = { cpu: 0, memory: 0 };
function smoothStats(newStats, smoothingFactor = 0.2) {
smoothedStats.cpu = smoothedStats.cpu * (1 - smoothingFactor) + newStats.cpu * smoothingFactor;
smoothedStats.memory = smoothedStats.memory * (1 - smoothingFactor) + newStats.memory * smoothingFactor;
return smoothedStats;
} }
function refreshContainerStats() { function refreshContainerStats() {
@ -346,6 +356,7 @@ function addConnection(topicHex) {
e.stopPropagation(); e.stopPropagation();
disconnectConnection(topicId, connectionItem); disconnectConnection(topicId, connectionItem);
}); });
refreshContainerStats();
connectionList.appendChild(connectionItem); connectionList.appendChild(connectionItem);
@ -769,29 +780,33 @@ function addActionListeners(row, container) {
} }
// Function to update container statistics function updateStatsUI(row, stats) {
function updateContainerStats(stats) { requestIdleCallback(() => {
console.log(`[DEBUG] Updating stats for container ID: ${stats.id}, Topic ID: ${stats.topicId}`);
// Ensure stats belong to the active connection
if (!window.activePeer || !connections[stats.topicId] || window.activePeer !== connections[stats.topicId].peer) {
console.warn(`[WARN] Stats received for inactive or unknown connection. Skipping.`);
return;
}
// Find the row for the container by its ID
const row = containerList.querySelector(`tr[data-container-id="${stats.id}"]`);
if (!row) {
console.warn(`[WARN] No matching row for container ID: ${stats.id}. Skipping stats update.`);
return;
}
// Update the container statistics in the UI
row.querySelector('.cpu').textContent = stats.cpu.toFixed(2) || '0.00'; row.querySelector('.cpu').textContent = stats.cpu.toFixed(2) || '0.00';
row.querySelector('.memory').textContent = (stats.memory / (1024 * 1024)).toFixed(2) || '0.00'; row.querySelector('.memory').textContent = (stats.memory / (1024 * 1024)).toFixed(2) || '0.00';
row.querySelector('.ip-address').textContent = stats.ip || 'No IP Assigned'; row.querySelector('.ip-address').textContent = stats.ip || 'No IP Assigned';
});
} }
function updateContainerStats(stats) {
// console.log(`[DEBUG] Updating stats for container ID: ${stats.id}, Topic ID: ${stats.topicId}`);
if (!window.activePeer || !connections[stats.topicId] || window.activePeer !== connections[stats.topicId].peer) {
// console.warn(`[WARN] Stats received for inactive or unknown connection. Skipping.`);
return;
}
const row = containerList.querySelector(`tr[data-container-id="${stats.id}"]`);
if (!row) {
// console.warn(`[WARN] No matching row for container ID: ${stats.id}. Skipping stats update.`);
return;
}
// Use updateStatsUI for cleaner and optimized DOM updates
updateStatsUI(row, stats);
}
// Function to open the Duplicate Modal with container configurations // Function to open the Duplicate Modal with container configurations