This commit is contained in:
Raven Scott 2024-11-30 02:52:38 -05:00
parent 0e94f047c0
commit 1dc39decf7

45
app.js
View File

@ -21,7 +21,38 @@ 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
function stopStatsInterval() {
if (statsInterval) {
clearInterval(statsInterval);
statsInterval = null;
console.log('[INFO] Stats interval stopped.');
}
}
function startStatsInterval() {
// Clear any existing interval
if (statsInterval) {
clearInterval(statsInterval);
}
// Start a new interval to request stats every second
statsInterval = setInterval(() => {
if (window.activePeer) {
console.log('[INFO] Requesting container stats...');
sendCommand('stats', {}); // Adjust the command if specific arguments are needed
} else {
console.warn('[WARN] No active peer; skipping stats request.');
}
}, 1000); // 1 second interval
}
function refreshContainerStats() {
console.log('[INFO] Refreshing container stats...');
sendCommand('listContainers'); // Request an updated container list
startStatsInterval(); // Restart stats interval
}
function waitForPeerResponse(expectedMessageFragment, timeout = 900000) { function waitForPeerResponse(expectedMessageFragment, timeout = 900000) {
console.log(`[DEBUG] Waiting for peer response with fragment: "${expectedMessageFragment}"`); console.log(`[DEBUG] Waiting for peer response with fragment: "${expectedMessageFragment}"`);
@ -486,6 +517,7 @@ function switchConnection(topicId) {
if (!connection || !connection.peer) { if (!connection || !connection.peer) {
console.error('[ERROR] No connection found or no active peer.'); console.error('[ERROR] No connection found or no active peer.');
showWelcomePage(); showWelcomePage();
stopStatsInterval(); // Stop stats interval if no active peer
return; return;
} }
@ -496,9 +528,14 @@ function switchConnection(topicId) {
resetContainerList(); resetContainerList();
console.log(`[INFO] Switched to connection: ${topicId}`); console.log(`[INFO] Switched to connection: ${topicId}`);
// Start the stats interval
startStatsInterval();
sendCommand('listContainers'); // Request containers for the new connection sendCommand('listContainers'); // Request containers for the new connection
} }
// Attach switchConnection to the global window object // Attach switchConnection to the global window object
window.switchConnection = switchConnection; window.switchConnection = switchConnection;
@ -595,6 +632,9 @@ function addActionListeners(row, container) {
// Refresh the container list to update states // Refresh the container list to update states
sendCommand('listContainers'); sendCommand('listContainers');
// Restart stats interval
startStatsInterval();
} catch (error) { } catch (error) {
console.error('[ERROR] Failed to start container:', error.message); console.error('[ERROR] Failed to start container:', error.message);
showAlert('danger', error.message || 'Failed to start container.'); showAlert('danger', error.message || 'Failed to start container.');
@ -604,6 +644,7 @@ function addActionListeners(row, container) {
} }
}); });
stopBtn.addEventListener('click', async () => { stopBtn.addEventListener('click', async () => {
showStatusIndicator(`Stopping container "${container.Names[0]}"...`); showStatusIndicator(`Stopping container "${container.Names[0]}"...`);
sendCommand('stopContainer', { id: container.Id }); sendCommand('stopContainer', { id: container.Id });
@ -618,6 +659,9 @@ function addActionListeners(row, container) {
// Refresh the container list to update states // Refresh the container list to update states
sendCommand('listContainers'); sendCommand('listContainers');
// Restart stats interval
startStatsInterval();
} catch (error) { } catch (error) {
console.error('[ERROR] Failed to stop container:', error.message); console.error('[ERROR] Failed to stop container:', error.message);
showAlert('danger', error.message || 'Failed to stop container.'); showAlert('danger', error.message || 'Failed to stop container.');
@ -628,6 +672,7 @@ function addActionListeners(row, container) {
}); });
// Restart Button // Restart Button
restartBtn.addEventListener('click', async () => { restartBtn.addEventListener('click', async () => {
showStatusIndicator(`Restarting container "${container.Names[0]}"...`); showStatusIndicator(`Restarting container "${container.Names[0]}"...`);