109 lines
3.1 KiB
JavaScript
109 lines
3.1 KiB
JavaScript
/**
|
|
* Progress Page JavaScript
|
|
* Handles Server-Sent Events (SSE) for real-time QC progress updates
|
|
*/
|
|
|
|
// Get DOM elements
|
|
const progressBar = document.getElementById('progressBar');
|
|
const progressText = document.getElementById('progressText');
|
|
|
|
// Get total checks count from template
|
|
const totalChecks = window.totalChecks || 0;
|
|
let completedChecks = 0;
|
|
|
|
// Create EventSource for Server-Sent Events
|
|
const eventSource = new EventSource('/progress/stream');
|
|
|
|
// Handle incoming progress events
|
|
eventSource.onmessage = function(event) {
|
|
const data = JSON.parse(event.data);
|
|
|
|
if (data.check_id === 'system') {
|
|
handleSystemEvent(data);
|
|
} else {
|
|
handleCheckEvent(data);
|
|
}
|
|
};
|
|
|
|
// Handle SSE errors
|
|
eventSource.onerror = function(error) {
|
|
console.error('SSE Error:', error);
|
|
|
|
// Close the connection
|
|
eventSource.close();
|
|
|
|
// Show error message
|
|
alert('Lost connection to server. Please refresh the page.');
|
|
};
|
|
|
|
/**
|
|
* Handle check-specific progress events
|
|
* @param {Object} data - Event data {check_id, status, message}
|
|
*/
|
|
function handleCheckEvent(data) {
|
|
const checkElement = document.getElementById(`check_${data.check_id}`);
|
|
if (!checkElement) {
|
|
console.warn(`Check element not found: check_${data.check_id}`);
|
|
return;
|
|
}
|
|
|
|
const statusElement = checkElement.querySelector('.check-status');
|
|
const messageElement = checkElement.querySelector('.check-message');
|
|
|
|
// Update status class
|
|
statusElement.className = 'check-status';
|
|
statusElement.classList.add(`status-${data.status}`);
|
|
|
|
// Update message
|
|
messageElement.textContent = data.message;
|
|
|
|
// Update progress if completed
|
|
if (['passed', 'error', 'failed', 'skipped'].includes(data.status)) {
|
|
completedChecks++;
|
|
updateProgressBar();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handle system-level events
|
|
* @param {Object} data - Event data {check_id: 'system', status, message}
|
|
*/
|
|
function handleSystemEvent(data) {
|
|
if (data.status === 'started') {
|
|
console.log('QC checks started:', data.message);
|
|
} else if (data.status === 'generating_report') {
|
|
progressText.textContent = 'Generating report...';
|
|
} else if (data.status === 'completed') {
|
|
// Store report path in session storage
|
|
sessionStorage.setItem('report_path', data.message);
|
|
|
|
// Close SSE connection
|
|
eventSource.close();
|
|
|
|
// Redirect to results page
|
|
window.location.href = '/results';
|
|
} else if (data.status === 'error') {
|
|
// Close SSE connection
|
|
eventSource.close();
|
|
|
|
// Show error message
|
|
alert(`Error running QC checks:\n\n${data.message}`);
|
|
|
|
// Redirect back to upload page
|
|
setTimeout(() => {
|
|
window.location.href = '/';
|
|
}, 2000);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update progress bar and text
|
|
*/
|
|
function updateProgressBar() {
|
|
const percentage = (completedChecks / totalChecks) * 100;
|
|
|
|
progressBar.style.width = `${percentage}%`;
|
|
progressBar.textContent = `${Math.round(percentage)}%`;
|
|
|
|
progressText.textContent = `${completedChecks} of ${totalChecks} checks completed`;
|
|
}
|