This commit is contained in:
cjw
2026-02-16 16:25:32 +08:00
parent 008f3f24f3
commit 49d6e0376a
5 changed files with 157 additions and 174 deletions
+82 -51
View File
@@ -117,14 +117,56 @@ async def upload_stp(
@router.get("/status/{task_id}") @router.get("/status/{task_id}")
@router.post("/status/{task_id}") @router.post("/status/{task_id}")
async def get_status(task_id: str): async def get_status(task_id: str, db_session: AsyncSession = Depends(get_db_session)):
"""获取任务状态""" """获取任务状态"""
if task_id not in tasks: # 先从内存查找
if task_id in tasks:
task = tasks[task_id]
logger.info(f"返回任务状态(内存): {task_id} - {task['status']}")
return task
# 内存中没有,从数据库查找
from sqlalchemy import select
from models.database import ProcessingTask, STPFile
result = await db_session.execute(
select(ProcessingTask, STPFile)
.join(STPFile, ProcessingTask.stp_file_id == STPFile.id)
.where(ProcessingTask.task_id == task_id)
)
task_record = result.first()
if not task_record:
raise HTTPException(404, "任务不存在") raise HTTPException(404, "任务不存在")
task = tasks[task_id] task, stp_file = task_record
logger.info(f"返回任务状态: {task_id} - {task['status']}") task_data = {
return task "task_id": task.task_id,
"filename": stp_file.original_filename if stp_file else "",
"file_size": stp_file.file_size if stp_file else 0,
"status": task.status,
"progress": task.progress,
"current_step": task.current_step,
"upload_time": task.created_time.isoformat() if task.created_time else "",
"completed_at": task.completed_time.isoformat() if task.completed_time else "",
"error": task.error_message if task.error_message else ""
}
# 尝试获取快照中的 key_info
from models.database import AnalysisResultSnapshot
snapshot_result = await db_session.execute(
select(AnalysisResultSnapshot)
.where(AnalysisResultSnapshot.stp_file_id == stp_file.id)
.order_by(AnalysisResultSnapshot.created_time.desc())
.limit(1)
)
snapshot = snapshot_result.scalar_one_or_none()
if snapshot and snapshot.key_info:
task_data["key_info"] = snapshot.key_info
logger.info(f"返回任务状态(数据库): {task_id} - {task['status']}")
return task_data
@router.get("/debug/tasks") @router.get("/debug/tasks")
@@ -317,60 +359,20 @@ async def result_page(request: Request, task_id: str, db_session: AsyncSession =
from sqlalchemy import select from sqlalchemy import select
from models.database import ProcessingTask, STPFile, GeometryData, MoldCavityData, HTMLFile, AnalysisResultSnapshot from models.database import ProcessingTask, STPFile, GeometryData, MoldCavityData, HTMLFile, AnalysisResultSnapshot
# 先尝试从分析结果快照获取数据(包含完整的 key_info) # 先查询任务记录
result = await db_session.execute( task_result = await db_session.execute(
select(AnalysisResultSnapshot, ProcessingTask, STPFile) select(ProcessingTask, STPFile)
.join(ProcessingTask, AnalysisResultSnapshot.stp_file_id == ProcessingTask.stp_file_id)
.join(STPFile, ProcessingTask.stp_file_id == STPFile.id) .join(STPFile, ProcessingTask.stp_file_id == STPFile.id)
.where(ProcessingTask.task_id == task_id) .where(ProcessingTask.task_id == task_id)
.order_by(AnalysisResultSnapshot.created_time.desc())
.limit(1)
) )
task_record = task_result.first()
snapshot_record = result.first()
# 构建任务详情数据
task_data = {}
if snapshot_record:
# 从快照获取完整数据
snapshot, task, stp_file = snapshot_record
task_data = {
"task_id": task.task_id,
"filename": stp_file.original_filename if stp_file else "",
"file_size": stp_file.file_size if stp_file else 0,
"status": task.status,
"progress": task.progress,
"current_step": task.current_step,
"upload_time": task.created_time.isoformat() if task.created_time else "",
"completed_at": task.completed_time.isoformat() if task.completed_time else "",
"error": task.error_message if task.error_message else ""
}
# 从快照获取完整的数据
task_data["geometry_data"] = snapshot.geometry_data if snapshot.geometry_data else None
task_data["key_info"] = snapshot.key_info if snapshot.key_info else None
task_data["mold_cavity_data"] = snapshot.mold_cavity_data if snapshot.mold_cavity_data else None
logger.info(f"从快照加载任务数据: {task_id}")
else:
# 如果没有快照,从各个表分别获取
result = await db_session.execute(
select(ProcessingTask, STPFile, GeometryData, MoldCavityData, HTMLFile)
.join(STPFile, ProcessingTask.stp_file_id == STPFile.id)
.outerjoin(GeometryData, STPFile.id == GeometryData.stp_file_id)
.outerjoin(MoldCavityData, STPFile.id == MoldCavityData.stp_file_id)
.outerjoin(HTMLFile, STPFile.id == HTMLFile.stp_file_id)
.where(ProcessingTask.task_id == task_id)
)
task_record = result.first()
if not task_record: if not task_record:
raise HTTPException(404, "任务不存在") raise HTTPException(404, "任务不存在")
task, stp_file, geometry_data, mold_cavity_data, html_file = task_record task, stp_file = task_record
# 构建任务详情数据
task_data = { task_data = {
"task_id": task.task_id, "task_id": task.task_id,
"filename": stp_file.original_filename if stp_file else "", "filename": stp_file.original_filename if stp_file else "",
@@ -383,6 +385,35 @@ async def result_page(request: Request, task_id: str, db_session: AsyncSession =
"error": task.error_message if task.error_message else "" "error": task.error_message if task.error_message else ""
} }
# 尝试从分析结果快照获取数据
snapshot_result = await db_session.execute(
select(AnalysisResultSnapshot)
.where(AnalysisResultSnapshot.stp_file_id == stp_file.id)
.order_by(AnalysisResultSnapshot.created_time.desc())
.limit(1)
)
snapshot = snapshot_result.scalar_one_or_none()
if snapshot:
# 从快照获取完整的数据
task_data["geometry_data"] = snapshot.geometry_data if snapshot.geometry_data else None
task_data["key_info"] = snapshot.key_info if snapshot.key_info else None
task_data["mold_cavity_data"] = snapshot.mold_cavity_data if snapshot.mold_cavity_data else None
logger.info(f"从快照加载任务数据: {task_id}")
else:
# 如果没有快照,从各个表分别获取
result = await db_session.execute(
select(GeometryData, MoldCavityData, HTMLFile)
.outerjoin(MoldCavityData, GeometryData.stp_file_id == MoldCavityData.stp_file_id)
.outerjoin(HTMLFile, GeometryData.stp_file_id == HTMLFile.stp_file_id)
.where(GeometryData.stp_file_id == stp_file.id)
)
other_data = result.first()
if other_data:
geometry_data, mold_cavity_data, html_file = other_data
# 如果有几何数据,添加到返回结果 # 如果有几何数据,添加到返回结果
if geometry_data: if geometry_data:
task_data["geometry_data"] = { task_data["geometry_data"] = {
+1 -23
View File
@@ -147,30 +147,8 @@ async function toggleFileRecords(filename) {
} }
async function viewRecordDetails(taskId) { async function viewRecordDetails(taskId) {
// 先检查任务状态,如果数据不完整,提示用户重新分析 // 直接跳转到结果页面,由后端从数据库加载完整数据
try {
const response = await fetch(`/status/${taskId}`);
if (response.ok) {
const task = await response.json();
if (task.status === 'completed' && task.key_info) {
// 数据完整,跳转到结果页面
window.location.href = `/result/${taskId}`; window.location.href = `/result/${taskId}`;
} else {
// 数据不完整,提示用户重新分析
if (confirm('该历史记录的分析数据不完整,是否重新分析?')) {
// 重新分析逻辑
await reanalyzeTask(taskId);
}
}
} else {
// 任务不存在或无法访问,跳转到结果页面尝试显示
window.location.href = `/result/${taskId}`;
}
} catch (error) {
console.error('检查任务状态失败:', error);
// 出错时直接跳转
window.location.href = `/result/${taskId}`;
}
} }
async function reanalyzeTask(taskId) { async function reanalyzeTask(taskId) {
+8 -3
View File
@@ -174,12 +174,18 @@ body {
} }
.key-info-categories { .key-info-categories {
display: grid; display: grid !important;
grid-template-columns: repeat(3, 1fr); grid-template-columns: repeat(3, 1fr) !important;
gap: 20px; gap: 20px;
margin-bottom: 20px; margin-bottom: 20px;
} }
.data-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 10px;
}
.category-section { .category-section {
background: #f8f9fa; background: #f8f9fa;
border-radius: 10px; border-radius: 10px;
@@ -509,4 +515,3 @@ body {
grid-template-columns: 1fr; grid-template-columns: 1fr;
} }
} }
}
+8 -31
View File
@@ -5,7 +5,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>分析结果 - STP文件几何分析工具</title> <title>分析结果 - STP文件几何分析工具</title>
<link rel="stylesheet" href="/static/style.css"> <link rel="stylesheet" href="/static/style.css?v=10">
<style> <style>
.result-container { .result-container {
max-width: 1200px; max-width: 1200px;
@@ -107,14 +107,6 @@
gap: 10px; gap: 10px;
} }
.data-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 8px 0;
border-bottom: 1px solid #f5f5f5;
}
.data-label { .data-label {
font-weight: bold; font-weight: bold;
color: #666; color: #666;
@@ -181,38 +173,23 @@
</div> </div>
<script> <script>
// 从URL获取任务ID // 从模板获取任务数据
const pathParts = window.location.pathname.split('/'); const task = {{ task | tojson | safe }};
const taskId = pathParts[pathParts.length - 1];
// 页面加载完成后获取任务数据 // 页面加载完成后显示任务数据
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function() {
loadTaskData(taskId); if (task && task.task_id) {
});
async function loadTaskData(taskId) {
try {
const response = await fetch(`/status/${taskId}`);
if (!response.ok) {
throw new Error('获取任务数据失败');
}
const task = await response.json();
displayTaskInfo(task); displayTaskInfo(task);
displayResults(task); displayResults(task);
} else {
} catch (error) {
document.getElementById('taskInfo').innerHTML = ` document.getElementById('taskInfo').innerHTML = `
<div class="error-message"> <div class="error-message">
<h3>❌ 加载失败</h3> <h3>❌ 加载失败</h3>
<p>${error.message}</p> <p>无法获取任务数据</p>
<button class="upload-btn" onclick="loadTaskData('${taskId}')">
重试
</button>
</div> </div>
`; `;
} }
} });
function displayTaskInfo(task) { function displayTaskInfo(task) {
const taskInfo = document.getElementById('taskInfo'); const taskInfo = document.getElementById('taskInfo');
+1 -9
View File
@@ -5,7 +5,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>分析结果快照 - STP文件几何分析工具</title> <title>分析结果快照 - STP文件几何分析工具</title>
<link rel="stylesheet" href="/static/style.css"> <link rel="stylesheet" href="/static/style.css?v=10">
<style> <style>
.snapshot-container { .snapshot-container {
max-width: 1200px; max-width: 1200px;
@@ -85,14 +85,6 @@
gap: 10px; gap: 10px;
} }
.data-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 8px 0;
border-bottom: 1px solid #f5f5f5;
}
.data-label { .data-label {
font-weight: bold; font-weight: bold;
color: #666; color: #666;