actually fix

This commit is contained in:
Raven Scott 2024-11-30 03:46:49 -05:00
parent d9549454e1
commit 8699a265ae

78
app.js
View File

@ -48,27 +48,30 @@ function startStatsInterval() {
} else { } else {
console.warn('[WARN] No active peer; skipping stats request.'); console.warn('[WARN] No active peer; skipping stats request.');
} }
}, 500); // Poll every 100ms for better reactivity }, 100); // Poll every 100ms for better reactivity
} }
const smoothedStats = {}; // Container-specific smoothing storage const smoothedStats = {}; // Container-specific smoothing storage
function smoothStats(containerId, newStats, smoothingFactor = 0.2) { function smoothStats(containerId, newStats, smoothingFactor = 0.2) {
if (!smoothedStats[containerId]) { if (!smoothedStats[containerId]) {
smoothedStats[containerId] = { cpu: 0, memory: 0 }; smoothedStats[containerId] = { cpu: 0, memory: 0, ip: newStats.ip || 'No IP Assigned' };
} }
smoothedStats[containerId].cpu = smoothedStats[containerId].cpu =
smoothedStats[containerId].cpu * (1 - smoothingFactor) + smoothedStats[containerId].cpu * (1 - smoothingFactor) +
newStats.cpu * smoothingFactor; newStats.cpu * smoothingFactor;
smoothedStats[containerId].memory = smoothedStats[containerId].memory =
smoothedStats[containerId].memory * (1 - smoothingFactor) + smoothedStats[containerId].memory * (1 - smoothingFactor) +
newStats.memory * smoothingFactor; newStats.memory * smoothingFactor;
// Preserve the latest IP address
smoothedStats[containerId].ip = newStats.ip || smoothedStats[containerId].ip;
return smoothedStats[containerId]; return smoothedStats[containerId];
} }
function refreshContainerStats() { function refreshContainerStats() {
console.log('[INFO] Refreshing container stats...'); console.log('[INFO] Refreshing container stats...');
sendCommand('listContainers'); // Request an updated container list sendCommand('listContainers'); // Request an updated container list
@ -297,31 +300,23 @@ function handlePeerData(data, topicId, peer) {
return; return;
} }
// Handle errors in the response
if (response.error) {
console.error(`[ERROR] Server error received: ${response.error}`);
showAlert('danger', response.error);
hideStatusIndicator();
return;
}
// Delegate handling based on the response type // Delegate handling based on the response type
switch (response.type) { switch (response.type) {
case 'stats':
console.log('[INFO] Updating container stats...');
// Ensure IP is included and passed to updateContainerStats
const stats = response.data;
stats.ip = stats.ip || 'No IP Assigned'; // Add a fallback for missing IPs
console.log(`[DEBUG] Passing stats to updateContainerStats: ${JSON.stringify(stats, null, 2)}`);
updateContainerStats(stats);
break;
case 'containers': case 'containers':
console.log('[INFO] Processing container list...'); console.log('[INFO] Processing container list...');
renderContainers(response.data, topicId); // Render containers specific to this topic renderContainers(response.data, topicId); // Render containers specific to this topic
break; break;
case 'stats':
console.log('[INFO] Updating container stats...');
if (!response.data.id) {
console.warn('[WARN] Stats response is missing container ID. Skipping update.');
return;
}
response.data.topicId = topicId; // Attach the topicId to the stats
updateContainerStats(response.data); // Update stats for the specified container
break;
case 'terminalOutput': case 'terminalOutput':
console.log('[INFO] Appending terminal output...'); console.log('[INFO] Appending terminal output...');
appendTerminalOutput(response.data, response.containerId, response.encoding); appendTerminalOutput(response.data, response.containerId, response.encoding);
@ -631,6 +626,10 @@ function renderContainers(containers, topicId) {
const image = container.Image || '-'; const image = container.Image || '-';
const containerId = container.Id; const containerId = container.Id;
const ipAddress = container.ipAddress || 'No IP Assigned'; const ipAddress = container.ipAddress || 'No IP Assigned';
if (ipAddress === 'No IP Assigned') {
console.warn(`[WARN] IP address missing for container ${container.Id}. Retrying...`);
sendCommand('inspectContainer', { id: container.Id });
}
const row = document.createElement('tr'); const row = document.createElement('tr');
row.dataset.containerId = containerId; // Store container ID for reference row.dataset.containerId = containerId; // Store container ID for reference
@ -832,33 +831,36 @@ function addActionListeners(row, container) {
} }
function updateStatsUI(row, stats) {
requestIdleCallback(() => {
row.querySelector('.cpu').textContent = stats.cpu.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';
});
}
function updateContainerStats(stats) { function updateContainerStats(stats) {
if (!stats || !stats.id || typeof stats.cpu === 'undefined' || typeof stats.memory === 'undefined') { if (!stats || !stats.id || typeof stats.cpu === 'undefined' || typeof stats.memory === 'undefined') {
console.error('[ERROR] Invalid stats object:', stats); console.error('[ERROR] Invalid stats object:', stats);
return; return;
} }
console.log(`[DEBUG] Updating stats for container ID: ${stats.id}`); console.log(`[DEBUG] Updating stats for container ID: ${stats.id}`);
const row = containerList.querySelector(`tr[data-container-id="${stats.id}"]`); const row = containerList.querySelector(`tr[data-container-id="${stats.id}"]`);
if (!row) { if (!row) {
console.warn(`[WARN] No matching row for container ID: ${stats.id}`); console.warn(`[WARN] No matching row for container ID: ${stats.id}`);
return; return;
} }
// Ensure the IP address is added or retained from existing row
const existingIpAddress = row.querySelector('.ip-address')?.textContent || 'No IP Assigned';
stats.ip = stats.ip || existingIpAddress;
const smoothed = smoothStats(stats.id, stats); const smoothed = smoothStats(stats.id, stats);
updateStatsUI(row, smoothed); updateStatsUI(row, smoothed);
} }
function updateStatsUI(row, stats) {
console.log("HAHHAHAHAHAHHAHA " + JSON.stringify(stats, null, 2));
requestIdleCallback(() => {
row.querySelector('.cpu').textContent = stats.cpu.toFixed(2) || '0.00';
row.querySelector('.memory').textContent = (stats.memory / (1024 * 1024)).toFixed(2) || '0.00';
row.querySelector('.ip-address').textContent = stats.ip;
});
}
// Function to open the Duplicate Modal with container configurations // Function to open the Duplicate Modal with container configurations