Files
short-drama-agent-front/src/features/workflows/WorkflowDiagnosticsDialog.vue
T

178 lines
8.1 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { userFacingText } from '../../lib/user-facing-text'
import ContextNotice from '../../components/ui/ContextNotice.vue'
import { computed, ref, watch } from 'vue'
import { NButton, NCollapse, NCollapseItem, NInput, NSelect, NTab, NTabs } from 'naive-ui'
import { Download, Search } from '@lucide/vue'
import { AppDialog, EmptyState } from '../../components/ui'
import { usePolling } from '../../composables/usePolling'
import { downloadText, formatDate, nodeLabel, workflowLabel } from '../../lib/format'
import { loadWorkflowDiagnostics } from './diagnostics'
import type { Checkpoint } from './types'
/** 完整诊断按需读取,关闭或切项目取消旧查询;不把保存记录误标为执行成功。 */
const props = defineProps<{ projectId: string; checkpoints: Checkpoint[] }>()
const open = defineModel<boolean>('open', { default: false })
const key = computed(() => (open.value ? props.projectId : ''))
/** 诊断弹窗已有刷新按钮,打开后只读取一次。 */
const query = usePolling(key, (id, signal) => (id ? loadWorkflowDiagnostics(id, signal) : Promise.resolve(null)), false)
const tab = ref('timeline')
const workflow = ref('all')
const search = ref('')
const checkpointMap = computed(() => new Map(props.checkpoints.map(item => [item.checkpointId, item])))
const options = computed(() => [
{ label: '全部制作任务', value: 'all' },
...[...new Set(props.checkpoints.map(item => item.workflowName))].map(value => ({
label: workflowLabel(value),
value
}))
])
const groups = computed(() =>
(query.data.value?.groups ?? [])
.map(group => ({
...group,
nodes: group.nodes.filter(
node =>
(workflow.value === 'all' ||
checkpointMap.value.get(node.checkpointId)?.workflowName === workflow.value) &&
`${node.nodeName} ${nodeLabel(node.nodeName)} ${node.checkpointId}`
.toLowerCase()
.includes(search.value.trim().toLowerCase())
)
}))
.filter(group => group.nodes.length)
)
const nodes = computed(() => groups.value.flatMap(group => group.nodes).toSorted((a, b) => a.index - b.index))
watch(
() => props.projectId,
() => {
workflow.value = 'all'
search.value = ''
tab.value = 'timeline'
}
)
/** 只根据 checkpoint 中明确的执行错误显示失败,未知结果仍标记为已保存。 */
function recordLabel(id: string) {
return checkpointMap.value.get(id)?.state.workflowExecution?.status === 'failed' ? '包含失败记录' : '记录已保存'
}
/** 导出原始指标和完整分组,不被视图中的筛选裁剪。 */
function exportReport() {
if (query.data.value)
downloadText(
`project-${props.projectId}-diagnostics.json`,
JSON.stringify({ projectId: props.projectId, ...query.data.value }, null, 2),
'application/json'
)
}
</script>
<template>
<AppDialog v-model:open="open" title="制作任务记录" description="查看任务记录和耗时。" wide>
<ContextNotice type="info" :show-icon="false" class="mt-4 text-xs"
>累计耗时按保存记录统计,成果请到素材页查看。</ContextNotice
>
<ContextNotice v-if="query.error.value" role="alert" type="error" :show-icon="false" class="mt-3"
>{{ query.error.value }},当前保留上次数据。</ContextNotice
>
<ContextNotice
v-for="error in query.data.value?.errors"
:key="error"
role="alert"
type="error"
:show-icon="false"
class="mt-3"
>{{ error }}</ContextNotice
>
<div class="mt-4 flex flex-wrap items-center justify-between gap-3">
<div class="flex flex-wrap items-center gap-4 text-sm">
<template v-if="query.data.value?.metrics">
<span>全项目记录 {{ query.data.value.metrics.nodeCount }}</span>
<span>累计节点耗时 {{ query.data.value.metrics.totalDurationText }}</span>
<span>最近记录重试数 {{ query.data.value.metrics.retryCount }}</span>
</template>
<span v-else class="text-muted">{{ query.loading.value ? '读取诊断中…' : '运行指标暂不可用' }}</span>
</div>
<div class="flex items-center gap-2">
<NButton :disabled="!query.data.value" @click="exportReport"
><template #icon><Download :size="14" /></template>导出诊断</NButton
>
</div>
</div>
<div class="diagnostics-filters mt-5">
<NInput
v-model:value="search"
clearable
placeholder="搜索制作步骤或保存记录"
:input-props="{ 'aria-label': '搜索运行记录' }"
><template #prefix><Search :size="14" /></template
></NInput>
<NSelect v-model:value="workflow" :options="options" aria-label="筛选制作任务" />
<span class="text-xs text-muted">{{ nodes.length }} 条记录</span>
</div>
<NTabs v-model:value="tab" type="line" class="mt-3"
><NTab name="timeline">完整时间线</NTab><NTab name="grouped">按阶段分组</NTab></NTabs
>
<div v-if="tab === 'timeline'" class="diagnostics-records mt-4">
<article v-for="node in nodes" :key="node.checkpointId" class="surface-inset p-4">
<div class="flex flex-wrap items-center justify-between gap-3">
<h3 class="text-sm font-medium">{{ node.index }} · {{ nodeLabel(node.nodeName) }}</h3>
<span
class="text-xs"
:class="recordLabel(node.checkpointId) === '包含失败记录' ? 'text-danger' : 'text-muted'"
>{{ recordLabel(node.checkpointId) }}</span
>
</div>
<p class="mt-2 text-xs text-muted">
{{ workflowLabel(checkpointMap.get(node.checkpointId)?.workflowName) }} ·
{{ userFacingText(node.phase, '其他阶段') }} · {{ node.durationText }} ·
{{ formatDate(node.createdAt) }}
</p>
<p class="mt-2 break-all font-mono text-[11px] text-muted">{{ node.checkpointId }}</p>
</article>
</div>
<NCollapse v-else class="mt-4" :default-expanded-names="groups.map(group => group.phase)"
><NCollapseItem
v-for="group in groups"
:key="group.phase"
:name="group.phase"
:title="`${userFacingText(group.phase, '其他阶段')} · ${group.nodes.length} 条`"
>
<div class="diagnostics-records">
<article v-for="node in group.nodes" :key="node.checkpointId" class="surface-inset p-4">
<p class="text-sm">{{ nodeLabel(node.nodeName) }} · {{ node.durationText }}</p>
<p class="mt-2 text-xs text-muted">
{{ workflowLabel(checkpointMap.get(node.checkpointId)?.workflowName) }} ·
{{ recordLabel(node.checkpointId) }}
</p>
<p class="mt-2 break-all font-mono text-[11px] text-muted">{{ node.checkpointId }}</p>
</article>
</div>
</NCollapseItem></NCollapse
>
<EmptyState
v-if="!query.loading.value && !nodes.length"
title="暂无匹配的运行记录"
description="可清空筛选。仅显示已保存的记录。"
/>
</AppDialog>
</template>
<style>
/* 本组件专属布局与 Naive 内部结构覆盖。 */
@reference "../../styles/styles.css";
.diagnostics-filters {
@apply flex flex-wrap items-center gap-3;
}
.diagnostics-filters > .n-input {
@apply flex-[1_1_240px] min-w-0;
}
.diagnostics-filters > .n-select {
@apply flex-[0_1_180px] min-w-0;
}
.diagnostics-records {
@apply grid gap-3 min-w-0;
}
</style>