feat: update frontend polling interval and add timeout warning

This commit is contained in:
yilv committed 2026-04-04 11:10:16 +08:00
1 parent ff983a8d66
commit 83b4e50bc8
1 file changed
+51 -25
+51 -25
View File
@@ -1,7 +1,7 @@
<template>
<div>
<h1 class="page-title">{{ $t('admin.dataEntries') }}</h1>
<!-- 工具栏 -->
<div class="toolbar mobile-toolbar">
<el-button @click="fetchItems">{{ $t('common.refresh') }}</el-button>
@@ -17,7 +17,7 @@
<span class="hint">{{ selectedIds.length }} 个已选择</span>
</div>
</template>
<div class="table-responsive">
<el-table :data="items" style="width: 100%" @selection-change="onSelectionChange">
<el-table-column type="selection" width="55" />
@@ -97,7 +97,10 @@
<el-input-number v-model="inferenceForm.stride" :min="100" :max="2000" :step="100" />
</el-form-item>
<el-form-item :label="$t('admin.batchSize')">
<el-input-number v-model="inferenceForm.batch_size" :min="1" :max="256" :step="1" />
<el-input-number v-model="inferenceForm.batch_size" :min="1" :max="1" :step="1" disabled />
<div style="font-size: 12px; color: #909399; margin-top: 4px">
2H4G 服务器优化:batch_size 固定为 1,降低内存占用
</div>
</el-form-item>
<el-form-item :label="$t('admin.confidenceThreshold')">
<el-slider v-model="inferenceForm.confidence_threshold" :min="0" :max="1" :step="0.05" />
@@ -120,6 +123,16 @@
<!-- 推理进度对话框 -->
<el-dialog v-model="progressVisible" title="推理进度" width="700px" :close-on-click-modal="false">
<div v-if="currentTask">
<!-- 添加超时警告 -->
<el-alert
v-if="isTaskTimeout"
title="任务长时间无响应,可能已异常终止,系统正在自动恢复..."
type="warning"
:closable="false"
show-icon
style="margin-bottom: 20px"
/>
<el-alert
:title="getStatusText(currentTask.status)"
:type="getStatusType(currentTask.status)"
@@ -155,7 +168,7 @@
<div v-if="currentTask.status === 'completed' && currentTask.results" style="margin-top: 20px">
<el-divider>推理结果</el-divider>
<el-table :data="currentTask.results" style="width: 100%" max-height="300">
<el-table-column prop="patient_id" label="患者ID" width="150" />
<el-table-column prop="patient_id" label="患者 ID" width="150" />
<el-table-column prop="predicted_class" label="预测类别" width="100">
<template #default="scope">
<el-tag :type="scope.row.predicted_class > 0 ? 'danger' : 'success'">
@@ -187,7 +200,7 @@
<!-- 推理任务列表对话框 -->
<el-dialog v-model="taskListVisible" title="推理任务列表" width="900px">
<el-table :data="taskList" style="width: 100%" v-loading="taskListLoading">
<el-table-column prop="task_id" label="任务ID" width="280" show-overflow-tooltip />
<el-table-column prop="task_id" label="任务 ID" width="280" show-overflow-tooltip />
<el-table-column prop="status" label="状态" width="120">
<template #default="scope">
<el-tag :type="getStatusType(scope.row.status)">
@@ -265,7 +278,7 @@
</template>
<script setup>
import { onMounted, ref, onUnmounted } from 'vue'
import { onMounted, ref, onUnmounted, computed } from 'vue'
import { ElMessage } from 'element-plus'
import api from '../../api'
@@ -286,7 +299,7 @@ const inferenceForm = ref({
model_path: '',
signal_len: 1000,
stride: 1000,
batch_size: 64,
batch_size: 1,
confidence_threshold: 0.75,
num_classes: 2
})
@@ -309,6 +322,18 @@ const taskTotal = ref(0)
let progressTimer = null
let runningTasks = ref([]) // 运行中的任务列表
let syncTimer = null // 进度同步定时器
// 添加计算属性检测超时
const isTaskTimeout = computed(() => {
if (!currentTask.value || currentTask.value.status !== 'running') return false
const lastHeartbeat = new Date(currentTask.value.heartbeat || currentTask.value.created_at)
const now = new Date()
const diffMinutes = (now - lastHeartbeat) / 1000 / 60
return diffMinutes > 5 // 超过 5 分钟无心跳显示警告
})
// 获取患者文件夹列表
const fetchItems = async () => {
try {
@@ -320,7 +345,7 @@ const fetchItems = async () => {
// 定时获取运行中的任务
await fetchRunningTasks()
// 启动进度同步定时器
startSyncTimer()
} catch (error) {
@@ -340,21 +365,21 @@ const fetchRunningTasks = async () => {
}
}
// 启动进度同步定时器(每 3 秒同步一次)
// 启动进度同步定时器(每 5 秒同步一次)
const startSyncTimer = () => {
stopSyncTimer()
syncTimer = setInterval(async () => {
// 检查是否有运行中的任务
const hasRunningTask = runningTasks.value.some(t => t.status === 'running')
if (hasRunningTask) {
await fetchRunningTasks()
} else {
// 没有运行中的任务,停止同步
stopSyncTimer()
}
}, 3000) // 每 3 秒同步一次
}, 5000) // 每 5 秒同步一次
}
// 停止进度同步定时器
@@ -443,9 +468,9 @@ const confirmInference = async () => {
submitting.value = true
try {
const res = await api.post('/admin/inference-data/run', inferenceForm.value)
ElMessage.success(`推理任务已提交,任务ID: ${res.task_id}`)
ElMessage.success(`推理任务已提交,任务 ID: ${res.task_id}`)
inferenceVisible.value = false
// 显示进度对话框
currentTask.value = {
task_id: res.task_id,
@@ -461,7 +486,7 @@ const confirmInference = async () => {
error: null
}
progressVisible.value = true
// 开始轮询进度
startProgressPolling(res.task_id)
} catch (error) {
@@ -478,18 +503,20 @@ const confirmInference = async () => {
// 开始轮询进度
const startProgressPolling = (taskId) => {
stopProgressPolling()
progressTimer = setInterval(async () => {
try {
const res = await api.get(`/admin/inference-data/tasks/${taskId}`)
currentTask.value = res.data
// 如果任务完成或失败,停止轮询和同步
if (res.status === 'completed' || res.status === 'failed') {
stopProgressPolling()
stopSyncTimer()
if (res.status === 'completed') {
ElMessage.success('推理任务完成')
} else if (res.status === 'failed') {
ElMessage.error('推理任务失败:' + (res.error || '未知错误'))
}
// 更新患者列表
setTimeout(fetchItems, 1000)
@@ -500,7 +527,7 @@ const startProgressPolling = (taskId) => {
} catch (error) {
console.error('获取任务进度失败', error)
}
}, 2000) // 每秒轮询一次
}, 5000) // 每 5 秒轮询一次
}
// 停止轮询进度
@@ -518,7 +545,7 @@ const resetInference = () => {
model_path: availableModels.value[0]?.path || '',
signal_len: 1000,
stride: 1000,
batch_size: 64,
batch_size: 1,
confidence_threshold: 0.75,
num_classes: 2
}
@@ -647,11 +674,11 @@ const formatTime = (timeStr) => {
// 导出推理结果
const exportInferenceResults = () => {
if (!inferenceResults.value.length) return
const headers = ['PatientID', 'Predicted_Class', 'Confidence', 'Strict_Pred', 'Strict_Status',
'Hard_Pred', 'Hard_Conf', 'Filtered_Rate', 'Valid_Final',
const headers = ['PatientID', 'Predicted_Class', 'Confidence', 'Strict_Pred', 'Strict_Status',
'Hard_Pred', 'Hard_Conf', 'Filtered_Rate', 'Valid_Final',
'Prob_Mean_0', 'Prob_Mean_1', 'Prob_Max_0', 'Prob_Max_1']
const rows = inferenceResults.value.map(r => [
r.patient_id,
r.predicted_class,
@@ -667,7 +694,7 @@ const exportInferenceResults = () => {
r.prob_maxs?.[0] || 0,
r.prob_maxs?.[1] || 0
])
const csv = [headers.join(','), ...rows.map(row => row.join(','))].join('\n')
const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' })
const link = document.createElement('a')
@@ -763,4 +790,3 @@ onUnmounted(() => {
}
}
</style>