Fix Report Engine Progress Bar Error

This commit is contained in:
马一丁
2025-11-21 05:33:05 +08:00
parent bac28b43f4
commit a2c42bda8b
+36 -16
View File
@@ -3641,10 +3641,7 @@
reportTaskId = null; reportTaskId = null;
// 停止可能正在进行的轮询 // 停止可能正在进行的轮询
if (reportPollingInterval) { stopProgressPolling();
clearInterval(reportPollingInterval);
reportPollingInterval = null;
}
// 向所有运行中的应用发送搜索请求(通过刷新iframe传递参数) // 向所有运行中的应用发送搜索请求(通过刷新iframe传递参数)
let totalRunning = 0; let totalRunning = 0;
@@ -5268,10 +5265,11 @@
resetReportLogsForNewTask(taskId, '检测到正在运行的报告任务,日志已重新开始'); resetReportLogsForNewTask(taskId, '检测到正在运行的报告任务,日志已重新开始');
reportTaskId = taskId; reportTaskId = taskId;
reportAutoPreviewLoaded = false; reportAutoPreviewLoaded = false;
startProgressPolling(taskId);
if (window.EventSource) { if (window.EventSource) {
openReportStream(reportTaskId); openReportStream(reportTaskId);
} else { } else {
startProgressPolling(taskId); appendReportStreamLine('浏览器不支持SSE,已切换为轮询模式', 'warn', { badge: 'SSE', force: true });
} }
} else if (statusData.current_task.status === 'completed') { } else if (statusData.current_task.status === 'completed') {
lastCompletedReportTask = statusData.current_task; lastCompletedReportTask = statusData.current_task;
@@ -5346,13 +5344,25 @@
const downloadPdfButton = document.getElementById('downloadPdfButton'); const downloadPdfButton = document.getElementById('downloadPdfButton');
if (!downloadButton || !downloadPdfButton) return; if (!downloadButton || !downloadPdfButton) return;
if (task && task.status === 'completed' && (task.report_file_ready || task.report_file_path)) { const htmlReady = task && task.status === 'completed' && (
task.report_file_ready ||
task.report_file_path ||
task.has_result // 有内容即可允许尝试下载/预览
);
const pdfReady = task && task.status === 'completed' && (
task.ir_file_ready ||
task.ir_file_path ||
task.report_file_ready ||
task.report_file_path
);
if (htmlReady) {
downloadButton.disabled = false; downloadButton.disabled = false;
downloadButton.dataset.taskId = task.task_id; downloadButton.dataset.taskId = task.task_id;
downloadButton.dataset.filename = task.report_file_name || ''; downloadButton.dataset.filename = task.report_file_name || '';
const label = task.report_file_name ? `下载HTML (${task.report_file_name})` : '下载HTML'; const label = task.report_file_name ? `下载HTML (${task.report_file_name})` : '下载HTML';
downloadButton.textContent = label; downloadButton.textContent = label;
downloadPdfButton.disabled = false; downloadPdfButton.disabled = !pdfReady;
downloadPdfButton.dataset.taskId = task.task_id; downloadPdfButton.dataset.taskId = task.task_id;
lastCompletedReportTask = task; lastCompletedReportTask = task;
} else if (!lastCompletedReportTask || (task && task.status !== 'completed')) { } else if (!lastCompletedReportTask || (task && task.status !== 'completed')) {
@@ -5631,10 +5641,13 @@
// 确保从任务开始就能读取日志 // 确保从任务开始就能读取日志
reportLogManager.start(); reportLogManager.start();
// 【兜底】立即启动进度轮询,SSE连上后会自动停止
startProgressPolling(reportTaskId);
if (window.EventSource) { if (window.EventSource) {
openReportStream(reportTaskId); openReportStream(reportTaskId);
} else { } else {
startProgressPolling(data.task_id); appendReportStreamLine('浏览器不支持SSE,已切换为轮询模式', 'warn', { badge: 'SSE', force: true });
} }
} else { } else {
updateTaskProgressStatus(null, 'error', '启动失败: ' + data.error); updateTaskProgressStatus(null, 'error', '启动失败: ' + data.error);
@@ -5664,12 +5677,19 @@
// 【新函数】使用新的日志管理器 // 【新函数】使用新的日志管理器
// 旧的startReportLogRefresh和stopReportLogRefresh已废弃,请使用reportLogManager // 旧的startReportLogRefresh和stopReportLogRefresh已废弃,请使用reportLogManager
// 开始进度轮询 // 开始/停止进度轮询SSE不可用或断开时兜底使用)
function startProgressPolling(taskId) { function stopProgressPolling() {
if (reportPollingInterval) { if (reportPollingInterval) {
clearInterval(reportPollingInterval); clearInterval(reportPollingInterval);
reportPollingInterval = null;
} }
}
function startProgressPolling(taskId) {
if (!taskId) return;
stopProgressPolling();
// 先立即拉取一次,避免长时间停留在5%
checkTaskProgress(taskId);
reportPollingInterval = setInterval(() => { reportPollingInterval = setInterval(() => {
checkTaskProgress(taskId); checkTaskProgress(taskId);
}, 2000); }, 2000);
@@ -5687,7 +5707,7 @@
// reportLogManager会自动处理轮询 // reportLogManager会自动处理轮询
if (data.task.status === 'completed') { if (data.task.status === 'completed') {
clearInterval(reportPollingInterval); stopProgressPolling();
showMessage('报告生成完成!', 'success'); showMessage('报告生成完成!', 'success');
// 自动显示报告 // 自动显示报告
@@ -5699,7 +5719,7 @@
reportTaskId = null; reportTaskId = null;
setGenerateButtonState(false); setGenerateButtonState(false);
} else if (data.task.status === 'error') { } else if (data.task.status === 'error') {
clearInterval(reportPollingInterval); stopProgressPolling();
showMessage('报告生成失败: ' + data.task.error_message, 'error'); showMessage('报告生成失败: ' + data.task.error_message, 'error');
// 重置自动生成标志,允许重新尝试 // 重置自动生成标志,允许重新尝试
@@ -5863,10 +5883,6 @@
startProgressPolling(taskId); startProgressPolling(taskId);
return; return;
} }
if (reportPollingInterval) {
clearInterval(reportPollingInterval);
reportPollingInterval = null;
}
if (reportEventSource && reportEventSource.__taskId === taskId) { if (reportEventSource && reportEventSource.__taskId === taskId) {
if (reportEventSource.readyState !== EventSource.CLOSED) { if (reportEventSource.readyState !== EventSource.CLOSED) {
return; return;
@@ -5913,6 +5929,7 @@
if (reportTaskId) { if (reportTaskId) {
reportLogManager.start(); reportLogManager.start();
} }
startProgressPolling(taskId);
scheduleReportStreamReconnect(taskId); scheduleReportStreamReconnect(taskId);
}; };
@@ -6060,6 +6077,7 @@
break; break;
case 'completed': case 'completed':
appendReportStreamLine(payload.message || '任务完成', 'success'); appendReportStreamLine(payload.message || '任务完成', 'success');
stopProgressPolling();
// 【修复】任务完成前强制刷新最后一次日志,确保所有日志都被读取 // 【修复】任务完成前强制刷新最后一次日志,确保所有日志都被读取
if (reportLogManager && reportLogManager.isRunning) { if (reportLogManager && reportLogManager.isRunning) {
@@ -6084,6 +6102,7 @@
break; break;
case 'cancelled': case 'cancelled':
appendReportStreamLine(payload.message || '任务已取消', 'warn'); appendReportStreamLine(payload.message || '任务已取消', 'warn');
stopProgressPolling();
safeCloseReportStream(); safeCloseReportStream();
updateReportStreamStatus('idle'); updateReportStreamStatus('idle');
reportTaskId = null; reportTaskId = null;
@@ -6091,6 +6110,7 @@
break; break;
case 'error': case 'error':
appendReportStreamLine(payload.message || '任务失败', 'error', { badge: 'ERROR' }); appendReportStreamLine(payload.message || '任务失败', 'error', { badge: 'ERROR' });
stopProgressPolling();
safeCloseReportStream(); safeCloseReportStream();
updateReportStreamStatus('error'); updateReportStreamStatus('error');
reportTaskId = null; reportTaskId = null;