am-freescan/am-web/src/views/lab/scan-task/ScanExecutionMonitor.vue

228 lines
7.0 KiB
Vue
Raw Normal View History

2026-10-22 16:18:37 +08:00
<template>
<section class="execution-monitor" :aria-busy="loading">
<div class="executed-case">
<span class="monitor-caption">{{ execution?.id ? '本次执行测试用例' : '准备执行测试用例' }}</span>
<strong>{{ identity.caseName }}</strong>
<span>方法号 / 测试用例 ID:<code>{{ identity.testCaseId || '-' }}</code></span>
<span>发布版本:{{ identity.versionNo != null ? `V${identity.versionNo}` : '尚未选择' }}</span>
</div>
<el-alert v-if="!identity.matchesPreparation" type="warning" :closable="false" class="mb-12px"
title="当前查看的是历史执行记录,以下用例和版本以本次执行时的冻结记录为准。" />
<div class="monitor-heading">
<div>
<h3>本次执行进度</h3>
<span class="monitor-caption">{{ execution?.executionNo || '尚未开始执行' }}</span>
</div>
<div class="monitor-actions">
<el-tag v-if="binding">V{{ binding.versionNo }}</el-tag>
<el-button v-if="binding?.cmvrInstId" link type="primary" @click="openFlow"
>流程运行图</el-button
>
<el-button :loading="loading" :disabled="!execution?.id" @click="load(true)"
>刷新日志</el-button
>
</div>
</div>
<el-progress :percentage="percentage" :status="progressStatus" :stroke-width="10" />
<div class="node-summary">
<span>状态:{{ statusLabel(execution?.status || 'PENDING') }}</span>
<span v-if="currentNode">当前节点:{{ currentNode }}</span>
</div>
<el-alert
v-if="execution?.failureReason"
class="mb-12px"
type="error"
:closable="false"
:title="execution.failureReason"
/>
<el-alert v-if="error" class="mb-12px" type="warning" :closable="false" :title="error" />
<h3 class="log-heading">执行日志</h3>
<NativeTaskLog
v-if="execution?.id"
:key="String(execution.id)"
:items="items"
:list="logs"
:task-status="nativeStatus"
:height="height"
item-label="测试用例"
/>
<el-empty v-else description="确认本件装夹并开始后,这里将显示执行进度和节点日志。" />
</section>
</template>
<script setup lang="ts">
import * as ScanApi from '@/api/lab/scan-task'
import type { LabExecutionVO } from '@/api/lab/execution'
import { getInst } from '@/cmvr/common/api/test/log'
import NativeTaskLog from '@/cmvr/aima/views/intelligenceTest/runningTask/task-log.vue'
import { executionIdentity, executionProgress, readableLogs } from './executionPresentation.mjs'
import { statusLabel } from './scanForm.mjs'
const props = withDefaults(
defineProps<{
execution?: Partial<LabExecutionVO>
caseName?: string
testCaseId: string
versionNo?: number
flowData?: any
active?: boolean
height?: number
}>(),
{ active: true, height: 420 }
)
const binding = ref<any>()
const nativeLogs = ref<any[]>([])
const loading = ref(false)
const error = ref('')
const identity = computed(() => executionIdentity(props.execution, binding.value, {
testCaseId: props.testCaseId, caseName: props.caseName, versionNo: props.versionNo, flowData: props.flowData
}))
const logs = computed(() => readableLogs(nativeLogs.value, identity.value.flowData))
const percentage = computed(() => executionProgress(props.execution, logs.value, identity.value.flowData))
const progressStatus = computed(() =>
props.execution?.status === 'SUCCEEDED'
? 'success'
: ['FAILED', 'CANCELLED'].includes(props.execution?.status || '')
? 'exception'
: props.execution?.status === 'PAUSED'
? 'warning'
: undefined
)
const nativeStatusCodes: Record<string, number> = {
SUCCEEDED: 0,
RUNNING: 1,
FAILED: 2,
PAUSED: 3,
CANCELLED: 4
}
const nativeStatus = computed(() => nativeStatusCodes[props.execution?.status || ''] ?? 1)
const items = computed(() => [
{
id: binding.value?.detectionItemId || identity.value.testCaseId,
detectItemId: binding.value?.detectionItemId,
testCaseId: identity.value.testCaseId,
caseName: identity.value.caseName
}
])
const currentNode = computed(() => {
const running = logs.value.filter((log: any) => ['RUNNING', '1'].includes(String(log.status)))
const last = running.at(-1) || logs.value.at(-1)
return last?.nodeName || ''
})
let generation = 0
let pending: { id: string; token: number } | undefined
async function load(force = false) {
const id = props.execution?.id
if (!id || (!force && pending?.id === String(id))) return
const token = ++generation
pending = { id: String(id), token }
loading.value = true
error.value = ''
try {
const result = await ScanApi.getCmvrBinding(id)
if (token !== generation) return
binding.value = result
if (!result?.cmvrInstId) {
error.value = '执行已提交,正在等待原生流程实例。'
return
}
const found: any[] = []
let pageNum = 1
while (true) {
const response: any = await getInst({ instId: result.cmvrInstId, pageNum, pageSize: 100 })
if (token !== generation) return
const rows = response.rows || []
found.push(...rows)
if (!rows.length || found.length >= (response.total ?? found.length)) break
pageNum++
}
nativeLogs.value = found
} catch (failure: any) {
if (token === generation) error.value = failure.message || '日志加载失败,请刷新重试。'
} finally {
if (token === generation) loading.value = false
if (pending?.token === token) pending = undefined
}
}
function openFlow() {
if (!binding.value?.cmvrInstId) return
const query = new URLSearchParams({
itemId: binding.value.detectionItemId,
instId: binding.value.cmvrInstId,
taskId: binding.value.cmvrTaskId || '',
isLog: 'true',
name: identity.value.caseName || '扫描流程'
})
window.open(`/flow?${query}`, '_blank', 'noopener,noreferrer')
}
watch(
() => props.execution?.id,
() => {
generation++
binding.value = undefined
nativeLogs.value = []
loading.value = false
error.value = ''
if (props.active) load(true)
},
{ immediate: true }
)
watch(
() => [props.active, props.execution?.status],
() => {
if (props.active) load(true)
}
)
const timer = setInterval(() => {
if (
props.active &&
!document.hidden &&
['RUNNING', 'QUEUED', 'PREPARING', 'PAUSED'].includes(props.execution?.status || '')
)
load()
}, 2000)
onBeforeUnmount(() => {
generation++
clearInterval(timer)
})
</script>
<style scoped>
.executed-case {
display: grid;
gap: 8px;
padding: 12px 16px;
margin-bottom: 20px;
background: var(--el-fill-color-light);
border-radius: 6px;
overflow-wrap: anywhere;
}
.monitor-heading,
.monitor-actions,
.node-summary {
display: flex;
align-items: center;
gap: 12px;
flex-wrap: wrap;
}
.monitor-heading {
justify-content: space-between;
margin-bottom: 16px;
}
h3 {
margin: 0 0 6px;
font-size: 16px;
}
.monitor-caption,
.node-summary {
color: var(--el-text-color-secondary);
font-size: 13px;
}
.node-summary {
margin: 10px 0 20px;
}
.log-heading {
margin-top: 20px;
}
</style>