Files
knowledge-governance/web/src/WeKnoraMonitor.tsx
T
hermes cddc4c90d1 fix(monitor): 修复取件死信无法重排与列表 NULL 标志导致的 500
- platform/postgres: listTestPublicationsSQL 的 can_start/can_retry 在 LEFT JOIN
  内容对象为空时算出 NULL,Go 侧 bool 扫描失败使整个列表接口 500,前端三个 lane
  全空(表现为“待入库 0 / 全部任务 0”)。改为 COALESCE(..., false)。
- 新增 POST /api/v1/job-failures/{id}/requeue:取件任务的幂等键绑在版本上,
  失败那次已占住它,重新标注同一文件不会再入队;现在按新轮次 (…:r<n>) 重排、
  复位意图取件状态并把死信标记为 requeued(幂等信封 + 审计字段)。
- web: 死信区对 artifact.fetch 显示「重新入队」;系统设置新增「知识库绑定」分区,
  挂上一直没被渲染的 RoutingBindingsSection,导入目标库可在页面上切换。
- 测试:真实 PostgreSQL 集成测试(NULL 折叠 + 重排轮次/幂等/拒绝)、前端死信按钮
  与设置分区渲染测试。
2026-10-08 12:09:34 +08:00

446 lines
38 KiB
TypeScript
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.
import { useContext, useEffect, useMemo, useState } from 'react'
import { fetchMonitorFiles, fetchMonitorOverview, fetchRunMetrics } from './monitor'
import type { RunMetrics } from './monitor'
import type { KnowledgeProgress, MonitorFilePage, MonitorOverview, MonitorStatus } from './monitor'
import { fetchJobFailures, fetchTestPublications, retryPublication, retryPublicationsBatch, cancelPublicationsBatch, requeueJobFailure, startTestPublications } from './testPublications'
import type { JobFailure, ProcessingState, TestPublication, TestPublicationPage } from './testPublications'
import { fetchActiveUISettings, settingsRefreshDelays } from './uiSettings'
import { WorkspaceContextTabsSetter } from './workspaceContext'
import './weknoraMonitor.css'
const emptyQueue: TestPublicationPage = { items: [], total: 0, limit: 50, offset: 0 }
const gb = (value: number) => `${(value / 1073741824).toFixed(1)}`
const scope = (value: string) => value === 'internal' ? '对内' : value === 'external' ? '对外' : '混合'
const duration = (seconds: number) => seconds <= 0 ? '完成' : seconds < 60 ? `${seconds}秒` : seconds < 3600 ? `${Math.floor(seconds / 60)}分钟` : `${(seconds / 3600).toFixed(1)}小时`
const statusLabels: Record<MonitorStatus, string> = { completed: 'completed', pending: 'pending', processing: 'processing', finalizing: 'finalizing', failed: 'failed', cancelled: 'cancelled' }
const processingLabels: Record<ProcessingState, string> = { planned: '待入库', pending: '等待解析', processing: '解析中', finalizing: '索引与增强', completed: '成功', failed: '失败', stalled: '停滞', cancelled: '已取消' }
const secondsSince = (iso: string) => Math.max(0, Math.round((Date.now() - new Date(iso).getTime()) / 1000))
const adviceFor = (stage: string, code: string) => {
if (stage === '文档解析') return '建议先在 WeKnora 手动解析该文件确认格式,再决定是否重试。'
if (stage === '索引 / 增强') return '多为 MinerU 容量或超时问题,确认服务恢复接单后重试。'
if (stage === 'WeKnora 上传') return '上传失败,确认 WeKnora 可达后重试;重复提交不会创建第二份知识。'
if (stage === '源头取件') return '文件尚未从源头取回:钉钉来源需可导出的登录会话,AList/PP 需直连可用;恢复后系统会自动重试。'
if (stage === 'Hash / 文件校验' || stage === 'OSS') return '文件或暂存校验问题,重新入队会重新拉取来源文件。'
if (stage === '测试知识移除') return '移除失败,请先在测试知识库确认该文档当前状态。'
if (stage === '治理策略') return '按治理策略阻止,请先在来源目录修正对内/对外标注。'
if (code.includes('429')) return '远端容量已满,恢复后可一键重新入队。'
return '确认错误码后可直接重试;尝试次数耗尽将进入死信队列。'
}
// failureHint turns a dead-letter row's machine code into a plain-Chinese
// diagnosis of what actually went wrong and what to do.
const failureHint = (failure: JobFailure) => {
const detail = `${failure.error_code} ${failure.last_error || ''}`
if (detail.includes('not ingestable')) return '钉钉侧该类型不是可下载文档(快捷方式、占位或内部对象),重试也不会成功;建议用“文件名筛选”把它排除,或在钉钉确认它是否为空目录残留。'
if (detail.includes('source_auth_required')) return '来源存储需要网页登录授权(服务凭据未配置),请联系管理员在“系统设置”重新扫码。'
if (detail.includes('invalid_format')) return '取回的内容不是有效的文档/文本格式,可能源头文件本身损坏;可下载原件确认后重试。'
if (detail.includes('source_unavailable')) return '来源服务当时不可达(网络/接口抖动)。任务若已自动重试成功,这里只是历史记录;仍在列表说明源头仍不可达。'
if (failure.error_code === 'test_upload_remote_failed' || detail.includes('test_upload_remote_failed')) return '向测试知识库上传被拒:确认 WeKnora 测试实例可达、目标知识库未停用后,点击重新入队。'
if (detail.includes('empty_document')) return '钉钉文档正文为空(源头就是空文档),解析器拒收空内容;无需重试,在钉钉里补充内容后重新取件即可。'
if (failure.error_code === 'remote_parse_failed') return '文档解析失败:建议先在 WeKnora 手动解析该文件确认格式,再重新入队。'
if (failure.error_code === 'handler_error') return '处理环节报错,展开原始错误定位;确认原因后可重新入队。'
return `错误码 ${failure.error_code}:确认原因后可重新入队。`
}
const pipelineStages = ['源头取件', 'Hash', 'OSS', '文档解析', 'WeKnora 上传', '索引', '完成']
const stageIndex = (label: string) => label === 'Hash / 文件校验' ? 1 : label === 'OSS' ? 2 : label === '文档解析' ? 3 : label === 'WeKnora 上传' ? 4 : label === '索引 / 增强' || label === '测试知识移除' || label === '治理策略' || label === '处理链路' ? 5 : -1
function chainSteps(item: TestPublication) {
const broken = item.processing_state === 'failed' || item.processing_state === 'stalled'
const stopAt = broken ? stageIndex(failureStage(item)) : -1
const activeAt = item.processing_state === 'completed' ? 7 : item.processing_state === 'finalizing' ? 5 : item.processing_state === 'processing' ? 3 : item.processing_state === 'planned' && !item.can_start ? 0 : item.processing_state === 'pending' ? 1 : 0
return pipelineStages.map((stage, index) => {
if (stopAt >= 0 && index === stopAt) return { stage, state: 'bad' as const }
if (stopAt >= 0) return { stage, state: index < stopAt ? 'done' as const : 'todo' as const }
if (index < activeAt) return { stage, state: 'done' as const }
if (index === activeAt && item.processing_state !== 'pending' && item.processing_state !== 'completed' && item.processing_state !== 'planned') return { stage, state: 'active' as const }
if ((item.processing_state === 'pending' || (item.processing_state === 'planned' && !item.can_start)) && index === 0) return { stage, state: item.can_start || item.processing_state === 'pending' ? 'done' as const : 'active' as const }
return { stage, state: 'todo' as const }
})
}
function failureStage(item: TestPublication) {
const code = item.last_error
if (code.includes('artifact') || code.includes('fetch')) return '源头取件'
if (code.includes('hash')) return 'Hash / 文件校验'
if (code.includes('oss')) return 'OSS'
if (code.includes('upload')) return 'WeKnora 上传'
if (code.includes('parse')) return '文档解析'
if (code.includes('stalled') || code.includes('index')) return '索引 / 增强'
if (code.includes('removal')) return '测试知识移除'
if (code.includes('blocked')) return '治理策略'
return item.processing_state === 'failed' ? '处理链路' : '—'
}
interface ViewProps {
overview: MonitorOverview | null
files: MonitorFilePage | null
queue?: TestPublicationPage
plans?: TestPublication[]
attention?: TestPublication[]
loadingMore?: boolean
selectedKB: KnowledgeProgress | null
status: MonitorStatus | ''
queueStatus?: ProcessingState | ''
queueScope?: 'internal' | 'external' | ''
loading: boolean
error: string | null
retryingIDs?: string[]
requeueingIDs?: string[]
cancellingIDs?: string[]
starting?: boolean
startMessage?: string
batchMessage?: string
failures?: JobFailure[]
metrics?: RunMetrics | null
onSelectKB: (kb: KnowledgeProgress) => void
onStatus: (status: MonitorStatus | '') => void
onQueueStatus: (value: ProcessingState | '') => void
onQueueScope: (value: 'internal' | 'external' | '') => void
lane?: string
onLaneChange?: (lane: string) => void
onRetry?: (id: string) => void
onBatchRetry?: (ids: string[]) => void
onBatchCancel?: (ids: string[]) => void
onRequeueFailure?: (id: string) => void
onStart?: (ids: string[]) => void
onLoadMore?: () => void
onRefresh: () => void
}
export function WeKnoraMonitorView({
overview,
files,
queue = emptyQueue,
plans = queue.items,
attention,
selectedKB,
status,
queueStatus = '',
queueScope = '',
lane = 'waiting',
onLaneChange = () => undefined,
loading,
error,
retryingIDs = [],
requeueingIDs = [],
cancellingIDs = [],
starting = false,
loadingMore = false,
startMessage = '',
batchMessage = '',
failures = [],
metrics = null,
onSelectKB,
onStatus,
onQueueStatus = () => undefined,
onQueueScope = () => undefined,
onRetry = () => undefined,
onBatchRetry = () => undefined,
onBatchCancel = () => undefined,
onRequeueFailure = () => undefined,
onStart = () => undefined,
onLoadMore = () => undefined,
onRefresh,
}: ViewProps) {
const [selectedStartIDs, setSelectedStartIDs] = useState<string[]>([])
const [selectedRetryIDs, setSelectedRetryIDs] = useState<string[]>([])
const toggleStartSelection = (id: string) => setSelectedStartIDs((current) => current.includes(id) ? current.filter((value) => value !== id) : [...current, id])
const toggleRetrySelection = (id: string) => setSelectedRetryIDs((current) => current.includes(id) ? current.filter((value) => value !== id) : [...current, id])
const stages: Array<[MonitorStatus, number]> = selectedKB ? [
['completed', selectedKB.completed], ['pending', selectedKB.pending], ['finalizing', selectedKB.finalizing],
['processing', selectedKB.processing], ['failed', selectedKB.failed], ['cancelled', selectedKB.cancelled],
] : []
const orderedQueue = useMemo(() => [...queue.items].sort((left, right) => {
const priority = (item: TestPublication) => item.processing_state === 'failed' || item.processing_state === 'stalled' ? 0 : 1
return priority(left) - priority(right)
}), [queue.items])
// Batch actions must target every failing item the lane shows, not just the
// rows that happen to sit inside the current queue page.
const retryableIDs = (attention ?? queue.items).filter((item) => item.can_retry).map((item) => item.id)
const batchTargets = selectedRetryIDs.length ? retryableIDs.filter((id) => selectedRetryIDs.includes(id)) : retryableIDs
const startablePlans = plans.filter((item) => item.can_start)
const startTargets = selectedStartIDs.length ? startablePlans.filter((item) => selectedStartIDs.includes(item.id)).map((item) => item.id) : startablePlans.map((item) => item.id)
// Failures needing a human must never be hidden by the queue page window:
// prefer the dedicated attention fetch, fall back to the visible page.
const attentionItems = (attention ?? queue.items).filter((item) => item.processing_state === 'failed' || item.processing_state === 'stalled')
const inflightItems = queue.items.filter((item) => item.processing_state === 'processing' || item.processing_state === 'finalizing' || (item.processing_state === 'pending' && !item.can_start))
const doneItems = queue.items.filter((item) => item.processing_state === 'completed')
const plannedItems = queue.items.filter((item) => item.processing_state === 'planned')
const fetchingPlanned = plannedItems.filter((item) => !item.can_start)
const laneCounts: Record<string, number> = {
waiting: startablePlans.length + fetchingPlanned.length,
attention: attentionItems.length + failures.length,
removal: (overview?.knowledge_bases || []).reduce((total, kb) => total + (kb.needs_removal || 0), 0),
inflight: inflightItems.length,
done: doneItems.length,
queue: queue.total,
}
const laneTitles: Record<string, string> = { waiting: '待入库', attention: '需人工处理', removal: '需下架', inflight: '处理中', done: '已完成', queue: '全部任务' }
const actionTask = (item: TestPublication) => <article className="inbox-task" key={item.id}>
<div className="inbox-task-mark">{item.can_start ? <input type="checkbox" aria-label={`选择入库${item.title}`} checked={selectedStartIDs.length === 0 || selectedStartIDs.includes(item.id)} onChange={() => toggleStartSelection(item.id)} /> : item.can_retry ? <input type="checkbox" aria-label={`选择重试${item.title}`} checked={selectedRetryIDs.includes(item.id)} onChange={() => toggleRetrySelection(item.id)} /> : null}</div>
<div>
<h4>{item.title} <span className={`scope-badge ${item.target_scope}`}>{scope(item.target_scope)}</span> {item.change_type === 'added' ? <span className="task-badge add">新增</span> : item.change_type === 'updated' ? <span className="task-badge update">更新</span> : null}</h4>
<div className="why">{item.processing_state === 'failed' || item.processing_state === 'stalled' ? <>{item.processing_state === 'stalled' ? '任务停滞:' : '为什么失败:'}错误码 <code>{item.last_error || '—'}</code> · 第{item.retry_count + 1}次尝试 · 已停留{duration(secondsSince(item.updated_at))} · 失败阶段 {failureStage(item)}</> : <>状态 <code>{processingLabels[item.processing_state]}</code>{item.last_error ? <> · 最近错误 <code>{item.last_error}</code></> : null} · 已停留{duration(secondsSince(item.updated_at))}</>}</div>
<div className="task-chain" aria-label={`处理链${item.title}`}>{chainSteps(item).map((step) => <span key={step.stage} className={`chain-step ${step.state}`}>{step.stage}{step.state === 'done' ? ' ✓' : step.state === 'bad' ? ' ✗' : step.state === 'active' ? ` 中 ${duration(secondsSince(item.updated_at))}` : ''}</span>)}</div>
<div className={item.processing_state === 'failed' || item.processing_state === 'stalled' ? 'errline' : 'tiny muted-line'}>{item.processing_state === 'failed' || item.processing_state === 'stalled' ? adviceFor(failureStage(item), item.last_error) : item.processing_state === 'planned' && !item.can_start ? '正在从源头取回文件,完成后自动进入可处理状态' : item.can_start ? '文件已就绪,等待开始处理' : '无需操作,等待自动处理'} · <code>{item.id}</code></div>
</div>
<div className="inbox-task-actions">{item.can_retry ? <button className="primary" disabled={retryingIDs.includes(item.id)} onClick={() => onRetry(item.id)}>{retryingIDs.includes(item.id) ? '重试中…' : '重试'}</button> : null}{item.can_retry ? <button disabled={cancellingIDs.includes(item.id) || retryingIDs.length > 0} title="不再自动重试;原始记录保留,重新标记入库可再次处理" onClick={() => onBatchCancel([item.id])}>{cancellingIDs.includes(item.id) ? '取消中…' : '不再处理'}</button> : null}</div>
</article>
const emptyLane = (text: string) => <div className="empty-state">{text}</div>
return <main className="page-shell weknora-monitor inbox-layout">
<header className="hero compact"><div><h1>解析监控</h1><p className="subtitle">站在知识库视角看差异:来源与目录里标好的对内/对外文件应不应该在这里,还差哪一步,一目了然。</p></div><span className="monitor-meta">{overview ? `快照 ${new Date(overview.observed_at).toLocaleString('zh-CN', { hour12: false })}` : ''} · 队列 等待{queue.total}条{overview?.pipeline ? ` · 取件中 ${overview.pipeline.fetch_pending}${overview.pipeline.fetch_failed ? ` · 取件失败 ${overview.pipeline.fetch_failed}` : ''}` : ''}</span><button onClick={onRefresh} disabled={loading}>{loading ? '刷新中…' : '刷新'}</button></header>
{error ? <div className="error-banner" role="alert">{error}</div> : null}
{overview?.simulated ? <div className="simulation-banner"><strong>本地模拟数据</strong><span>资源和知识库汇总用于确认页面流程,不代表测试服务器真实数据;任务卡片来自真实治理 API。</span></div> : null}
<div className="inbox-grid">
<nav className="inbox-nav" aria-label="解析监控收件箱">
<h2>知识库差异</h2>
{[['waiting', '待入库', 'blocked'], ['attention', '需人工处理', ''], ['removal', '需下架', ''], ['inflight', '处理中', ''], ['done', '已完成', 'okall']].map(([value, label, tone]) => <button key={value} className={`${lane === value ? 'active' : ''} ${laneCounts[value] ? tone : ''}`} aria-label={`切换到${label}`} onClick={() => onLaneChange(value)}><span>{label}</span><em className="lane-count">{laneCounts[value]}</em></button>)}
<button className={`${lane === 'queue' ? 'active' : ''}`} aria-label="切换到全部任务" onClick={() => onLaneChange('queue')}><span>全部任务</span><em className="lane-count">{queue.total}</em></button>
</nav>
<div className="inbox-work">
{lane === 'waiting' ? <section className="inbox-section" aria-label="待入库">
<div className="section-head"><h2>待入库({startablePlans.length + fetchingPlanned.length})</h2><span className="spacer" /><button className="primary" disabled={starting || !startTargets.length} onClick={() => onStart(startTargets)}>{starting ? '正在生成任务…' : `开始处理(${startTargets.length})`}</button></div>
<p className="tiny">来源目录中标记对内/对外的文件会自动出现在这里;“开始处理”按系统设置的并发上限(当前 {overview?.pipeline?.concurrency ?? 2})创建解析任务,无需再确认。</p>
{startMessage ? <p className="difference-message" role="status">{startMessage}</p> : null}
{plannedItems.length ? plannedItems.map(actionTask) : emptyLane('还没有标记入库的文件。去“来源与目录”把文件或目录标为对内/对外即可。')}
{fetchingPlanned.length ? <p className="tiny">其中 {fetchingPlanned.length} 项仍在从源头取件,取回后自动可处理。</p> : null}
</section> : null}
{lane === 'attention' ? <section className="inbox-section" aria-label="需人工处理">
<div className="section-head"><h2>需要你出手({attentionItems.length})</h2><span className="spacer" /><button disabled={!batchTargets.length || cancellingIDs.length > 0 || retryingIDs.length > 0} title="不再自动重试(如源头就是空文档);原始记录保留,重新标记入库可再次处理" onClick={() => onBatchCancel(batchTargets)}>{cancellingIDs.length ? '取消中…' : `批量取消所选(${batchTargets.length})`}</button><button className="danger" disabled={!batchTargets.length || retryingIDs.length > 0} onClick={() => onBatchRetry(batchTargets)}>{retryingIDs.length ? '重试中…' : `批量重试所选(${batchTargets.length})`}</button></div>
{batchMessage ? <p className="queue-batch-message" role="status">{batchMessage}</p> : null}
{attentionItems.length ? attentionItems.map(actionTask) : emptyLane('没有失败或停滞的任务。')}
<div className="section-head sub"><h3>死信(重试次数用完,系统不再自动处理)</h3><span className="tiny">“源头取件”=从钉钉/AList 把原始文件拉回本地;“上传解析”=把文件送进知识库做解析。死信只是失败任务的登记表,不会改动原始记录;重新入队=再给它一次机会</span></div>
{failures.length ? <ul className="dlq-list">{failures.map((failure) => <li key={failure.id}><span className="monitor-status failed">{failure.error_code}</span><b>{failure.job_type === 'test.upload' ? '上传解析' : failure.job_type === 'artifact.fetch' ? '源头取件' : failure.job_type}</b>{failure.doc_name ? <b>「{failure.doc_name}」</b> : failure.publication_id ? <code>{failure.publication_id}</code> : null}<small>{failure.attempt}/{failure.max_attempts_at_failure} 次 · {new Date(failure.created_at).toLocaleString('zh-CN', { hour12: false })}</small><small>{failureHint(failure)}</small>{failure.job_type === 'artifact.fetch' ? <button disabled={requeueingIDs.includes(failure.id)} onClick={() => onRequeueFailure(failure.id)}>{requeueingIDs.includes(failure.id) ? '重排中…' : '重新入队'}</button> : failure.publication_id ? <button disabled={retryingIDs.includes(failure.publication_id)} onClick={() => onBatchRetry([failure.publication_id as string])}>重新入队</button> : null}</li>)}</ul> : emptyLane('当前没有等待处理的死信记录。')}
</section> : null}
{lane === 'removal' ? <section className="inbox-section" aria-label="需下架">
<div className="section-head"><h2>需下架({laneCounts.removal})</h2><span className="tiny">来源目录里被改为“禁止导入”或撤销标注、但仍在知识库中可检索的文件</span></div>
{(overview?.knowledge_bases || []).some((kb) => kb.needs_removal > 0) ? <ul className="dlq-list">{(overview?.knowledge_bases || []).filter((kb) => kb.needs_removal > 0).map((kb) => <li key={kb.id}><span className="monitor-status failed">需下架</span><b>{kb.name}</b><small>{kb.needs_removal} 份仍在检索中</small></li>)}</ul> : emptyLane('没有标注为禁止但仍留在知识库里的文件。')}
<p className="tiny">下架在“知识清单”里操作(停用检索,可恢复);这里只负责把差异暴露出来。</p>
</section> : null}
{lane === 'inflight' ? <section className="inbox-section" aria-label="处理中">
<div className="section-head"><h2>处理中({inflightItems.length})</h2><span className="tiny">无需操作,仅供查看</span></div>
{inflightItems.length ? inflightItems.map(actionTask) : emptyLane('当前没有正在处理或索引增强的任务。')}
{overview ? <>
<div className="pipeline-strip" aria-label="解析处理链">源头取件 → Hash → OSS → 文档解析 → WeKnora 上传 → 索引 → 完成</div>
<div className="monitor-kb-layout" id="monitor-kb-progress">
<aside className="monitor-kb-list"><h2>知识库解析进度</h2>{overview.knowledge_bases.map((kb) => <button key={kb.id} className={selectedKB?.id === kb.id ? 'active' : ''} onClick={() => onSelectKB(kb)}><span><b>{kb.name}</b><small>{scope(kb.visibility)} · 已导入 {kb.imported}{kb.planned ? ` · 待入库 ${kb.planned}` : ''}</small></span><strong>{kb.completed}完成</strong></button>)}</aside>
<div className="monitor-kb-detail">{selectedKB ? <>
<div className="monitor-kb-heading"><div><h2>{selectedKB.name}</h2><span>{scope(selectedKB.visibility)} · 已导入 {selectedKB.imported}{selectedKB.planned ? ` · 待入库 ${selectedKB.planned}` : ''}</span></div><div><span>对内 {selectedKB.internal_completed} / {selectedKB.internal_imported}</span><span>对外 {selectedKB.external_completed} / {selectedKB.external_imported}</span></div></div>
<div className="monitor-stage-tabs"><button className={status === '' ? 'active' : ''} onClick={() => onStatus('')}>全部 {selectedKB.imported}</button>{stages.filter(([, count]) => count > 0).map(([value, count]) => <button className={status === value ? 'active' : ''} key={value} onClick={() => onStatus(value)}>{statusLabels[value]} {count}</button>)}</div>
<p className="finalizing-note"><b>finalizing:</b>基础解析、切片和向量通常已完成,Wiki、摘要或图谱仍在生成;不能算全部完成。</p>
{files?.sample_only ? <p className="sample-note">当前只展示本地模拟文件样例;真实模式会列出所选状态下的实际文件。</p> : null}
{files?.items.length ? <div className="monitor-file-wrap"><table className="monitor-file-table"><thead><tr><th>文件名</th><th>来源目录</th><th>对内/对外</th><th>当前阶段</th><th>已停留</th><th>说明</th></tr></thead><tbody>{files.items.map((file) => <tr key={`${file.name}-${file.status}`}><td>{file.name}</td><td>{file.directory}</td><td><span className={`scope-badge ${file.visibility}`}>{scope(file.visibility)}</span></td><td><span className={`monitor-status ${file.status}`}>{file.status}</span></td><td>{duration(file.elapsed_seconds)}</td><td>{file.stage_detail}{file.error_code ? <small>{file.error_code}</small> : null}</td></tr>)}</tbody></table></div> : !loading ? <div className="empty-state">当前筛选没有文件。</div> : null}
</> : null}</div>
</div>
</> : null}
</section> : null}
{lane === 'done' ? <section className="inbox-section" aria-label="已完成">
<div className="section-head"><h2>已完成({doneItems.length})</h2><span className="tiny">最近一页数据中的成功任务</span></div>
{doneItems.length ? doneItems.map(actionTask) : emptyLane('当前页没有已完成任务;用全部任务视图翻页查看。')}
</section> : null}
{lane === 'queue' ? <section className="inbox-section" aria-label="全部任务">
<div className="section-head"><h2>全部任务({queue.total})</h2><span className="spacer" /><label>目标知识库<select aria-label="队列目标知识库" value={queueScope} onChange={(event) => onQueueScope(event.target.value as 'internal' | 'external' | '')}><option value="">内/外全部</option><option value="internal">对内知识库</option><option value="external">对外知识库</option></select><select aria-label="队列状态" value={queueStatus} onChange={(event) => onQueueStatus(event.target.value as ProcessingState | '')}><option value="">全部状态</option><option value="planned">待入库</option><option value="pending">等待解析</option><option value="processing">解析中</option><option value="finalizing">索引与增强</option><option value="completed">成功</option><option value="failed">失败</option><option value="stalled">停滞</option><option value="cancelled">已取消</option></select></label></div>
{orderedQueue.length ? orderedQueue.map(actionTask) : !loading ? emptyLane('当前筛选没有解析任务。') : null}
{queue.items.length < queue.total ? <div className="queue-more"><span>已显示 {queue.items.length} / 共 {queue.total}</span><button type="button" disabled={loadingMore} onClick={() => onLoadMore?.()}>{loadingMore ? '加载中…' : '加载更多'}</button></div> : null}
</section> : null}
{metrics ? <footer className="monitor-strip" aria-label="真实运行指标"><div>1小时·成功 <b className={metrics.short_window.failure_rate !== null && metrics.short_window.failure_rate > 5 ? 'bad' : 'good'}>{metrics.short_window.success_rate === null ? '数据不足' : `${metrics.short_window.success_rate}%`}</b></div><div>24小时·成功 <b>{metrics.long_window.success_rate === null ? '数据不足' : `${metrics.long_window.success_rate}%`}</b></div><div>p50 {metrics.short_window.duration_p50_seconds === null ? '—' : duration(Math.round(metrics.short_window.duration_p50_seconds))} · p95 {metrics.short_window.duration_p95_seconds === null ? '—' : duration(Math.round(metrics.short_window.duration_p95_seconds))}</div><div>Worker {metrics.active_workers} · 等待 {metrics.queue.pending} · 运行 {metrics.queue.running}{metrics.queue.failed ? <span className="bad"> · 失败 {metrics.queue.failed}</span> : null}</div>{overview?.health.map((service) => <div key={service.key}>{service.name} <b className={service.readiness === 'ready' ? 'good' : service.readiness === 'blocked' || service.readiness === 'down' ? 'bad' : ''}>{service.readiness === 'ready' ? '可接单' : service.readiness === 'degraded' ? '降级' : service.readiness === 'not_configured' ? '未配置' : service.readiness === 'down' ? '不可达' : '阻断'}</b></div>)}{overview ? <div>CPU {overview.host.cpu_percent.toFixed(0)}% · 内存 {gb(overview.host.memory_used_bytes)}/{gb(overview.host.memory_total_bytes)}GB</div> : null}</footer> : null}
</div>
</div>
</main>
}
export function WeKnoraMonitor() {
const [overview, setOverview] = useState<MonitorOverview | null>(null)
const [selected, setSelected] = useState<KnowledgeProgress | null>(null)
const [files, setFiles] = useState<MonitorFilePage | null>(null)
const [queue, setQueue] = useState<TestPublicationPage>(emptyQueue)
const [plans, setPlans] = useState<TestPublication[]>([])
const [status, setStatus] = useState<MonitorStatus | ''>('')
const [queueStatus, setQueueStatus] = useState<ProcessingState | ''>('')
const [queueScope, setQueueScope] = useState<'internal' | 'external' | ''>('')
const [lane, setLane] = useState<'waiting' | 'attention' | 'removal' | 'inflight' | 'done' | 'queue'>('waiting')
const [loading, setLoading] = useState(true)
const [error, setError] = useState<string | null>(null)
const [documentRevision, setDocumentRevision] = useState(0)
const [resourceRevision, setResourceRevision] = useState(0)
const [documentRefreshMs, setDocumentRefreshMs] = useState(15000)
const [resourceRefreshMs, setResourceRefreshMs] = useState(30000)
const [retryingIDs, setRetryingIDs] = useState<string[]>([])
const [requeueingIDs, setRequeueingIDs] = useState<string[]>([])
const [cancellingIDs, setCancellingIDs] = useState<string[]>([])
const [starting, setStarting] = useState(false)
const [startMessage, setStartMessage] = useState('')
const [batchMessage, setBatchMessage] = useState('')
const [failures, setFailures] = useState<JobFailure[]>([])
const [attention, setAttention] = useState<TestPublication[] | null>(null)
const [loadingMore, setLoadingMore] = useState(false)
const [metrics, setMetrics] = useState<RunMetrics | null>(null)
const setContextTabs = useContext(WorkspaceContextTabsSetter)
useEffect(() => {
const controller = new AbortController()
void fetchActiveUISettings(controller.signal).then((active) => {
const delays = settingsRefreshDelays(active)
if (!controller.signal.aborted) { setDocumentRefreshMs(delays.documentMs); setResourceRefreshMs(delays.resourceMs) }
}).catch((cause) => { if (!controller.signal.aborted) setError(cause instanceof Error ? cause.message : '加载刷新设置失败') })
return () => controller.abort()
}, [])
useEffect(() => {
const controller = new AbortController(); setLoading(true)
void fetchMonitorOverview(controller.signal).then((result) => {
if (controller.signal.aborted) return
setOverview(result)
setSelected((current) => current && result.knowledge_bases.find((kb) => kb.id === current.id) || result.knowledge_bases[0] || null)
setError(null)
}).catch((cause) => { if (!controller.signal.aborted) setError(cause instanceof Error ? cause.message : '加载资源监控失败') }).finally(() => { if (!controller.signal.aborted) setLoading(false) })
return () => controller.abort()
}, [resourceRevision])
useEffect(() => {
const controller = new AbortController(); setLoading(true)
void Promise.all([
fetchTestPublications({ processing_state: queueStatus, target_scope: queueScope, limit: 50, offset: 0 }, controller.signal),
fetchTestPublications({ processing_state: 'planned', limit: 100, offset: 0 }, controller.signal),
fetchTestPublications({ processing_state: ['failed', 'stalled'], limit: 100, offset: 0 }, controller.signal),
]).then(([queuePage, planPage, attentionPage]) => {
if (!controller.signal.aborted) { setQueue(queuePage); setPlans(planPage.items); setAttention(attentionPage.items); setError(null) }
}).catch((cause) => { if (!controller.signal.aborted) setError(cause instanceof Error ? cause.message : '加载解析队列失败') }).finally(() => { if (!controller.signal.aborted) setLoading(false) })
return () => controller.abort()
}, [queueStatus, queueScope, documentRevision])
useEffect(() => {
if (!selected) return
const controller = new AbortController()
setLoading(true)
void fetchMonitorFiles(selected.id, status, 20, 0, controller.signal).then((result) => {
if (!controller.signal.aborted) setFiles(result)
}).catch((cause) => {
if (!controller.signal.aborted) setError(cause instanceof Error ? cause.message : '加载文档阶段失败')
}).finally(() => {
if (!controller.signal.aborted) setLoading(false)
})
return () => controller.abort()
}, [selected, status, documentRevision])
useEffect(() => {
const timer = window.setTimeout(() => setDocumentRevision((value) => value + 1), documentRefreshMs)
return () => window.clearTimeout(timer)
}, [documentRevision, documentRefreshMs])
useEffect(() => {
const timer = window.setTimeout(() => setResourceRevision((value) => value + 1), resourceRefreshMs)
return () => window.clearTimeout(timer)
}, [resourceRevision, resourceRefreshMs])
useEffect(() => {
setContextTabs(selected ? [{ key: selected.id, label: selected.name, onActivate: () => setStatus('') }] : [])
return () => setContextTabs([])
}, [selected, setContextTabs])
useEffect(() => {
const controller = new AbortController()
void fetchJobFailures(controller.signal).then((items) => { if (!controller.signal.aborted) setFailures(items) }).catch(() => undefined)
return () => controller.abort()
}, [documentRevision, resourceRevision])
useEffect(() => {
const controller = new AbortController()
void fetchRunMetrics(controller.signal).then((result) => { if (!controller.signal.aborted) setMetrics(result) }).catch(() => undefined)
return () => controller.abort()
}, [documentRevision, resourceRevision])
const requeueFailure = async (failureID: string) => {
setRequeueingIDs((current) => [...current, failureID])
setBatchMessage('')
try {
const result = await requeueJobFailure(failureID, '源头取件死信重新入队')
setError(null)
setBatchMessage(result.already_queued
? `该取件任务已在队列中(第 ${result.round} 轮)`
: `已重新入队:${result.doc_name || result.version_id.slice(0, 12)}(第 ${result.round} 轮取件),取回后自动进入待入库`)
setDocumentRevision((value) => value + 1)
} catch (cause) {
setError(cause instanceof Error ? cause.message : '重新取件失败')
} finally {
setRequeueingIDs((current) => current.filter((value) => value !== failureID))
}
}
const retryOne = async (id: string) => {
setRetryingIDs((current) => [...current, id])
try {
await retryPublication(id)
setError(null)
setDocumentRevision((value) => value + 1)
} catch (cause) {
setError(cause instanceof Error ? cause.message : '重试失败')
} finally {
setRetryingIDs((current) => current.filter((value) => value !== id))
}
}
const loadMoreQueue = async () => {
if (loadingMore) return
setLoadingMore(true)
try {
const next = await fetchTestPublications({ processing_state: queueStatus, target_scope: queueScope, limit: 50, offset: queue.items.length })
setQueue((current) => {
const seen = new Set(current.items.map((item) => item.id))
return { ...next, offset: 0, items: [...current.items, ...next.items.filter((item) => !seen.has(item.id))] }
})
setError(null)
} catch (cause) {
setError(cause instanceof Error ? cause.message : '加载更多失败')
} finally {
setLoadingMore(false)
}
}
const retryBatch = async (ids: string[]) => {
setRetryingIDs(ids)
setBatchMessage('')
try {
const result = await retryPublicationsBatch(ids, '解析队列批量重试')
setError(null)
const skipped = result.already_running + result.not_retryable
setBatchMessage(`已重新入队 ${result.started} 项${skipped ? `,跳过 ${skipped} 项(处理中或不可重试)` : ''}`)
setDocumentRevision((value) => value + 1)
} catch (cause) {
setError(cause instanceof Error ? cause.message : '批量重试失败')
} finally {
setRetryingIDs([])
}
}
const cancelBatch = async (ids: string[]) => {
setCancellingIDs(ids)
setBatchMessage('')
try {
const result = await cancelPublicationsBatch(ids, '解析队列批量取消(不再重试)')
setError(null)
const skipped = result.already_running + result.not_retryable
setBatchMessage(`已取消 ${result.started} 项,不再自动重试${skipped ? `,跳过 ${skipped} 项(处理中或不可取消)` : ''};原始记录未删除,重新标记入库可再次处理`)
setDocumentRevision((value) => value + 1)
} catch (cause) {
setError(cause instanceof Error ? cause.message : '批量取消失败')
} finally {
setCancellingIDs([])
}
}
const startPlans = async (ids: string[]) => {
setStarting(true)
setStartMessage('')
try {
const result = await startTestPublications(ids, '开始处理')
setStartMessage(`已提交 ${result.selected} 项;新建解析任务 ${result.started} 项;已在队列 ${result.already_started} 项;不可处理 ${result.unavailable} 项(多为源头取件未完成,稍后自动重试)。`)
setError(null)
setDocumentRevision((value) => value + 1)
} catch (cause) {
setStartMessage(cause instanceof Error ? cause.message : '开始处理失败')
} finally {
setStarting(false)
}
}
return <WeKnoraMonitorView overview={overview} files={files} queue={queue} plans={plans} selectedKB={selected} status={status} queueStatus={queueStatus} queueScope={queueScope} loading={loading} error={error} retryingIDs={retryingIDs} requeueingIDs={requeueingIDs} cancellingIDs={cancellingIDs} starting={starting} startMessage={startMessage} batchMessage={batchMessage} failures={failures} metrics={metrics} onSelectKB={(kb) => { setSelected(kb); setStatus(''); setFiles(null) }} onStatus={(value) => { setStatus(value); setFiles(null) }} onQueueStatus={setQueueStatus} onQueueScope={setQueueScope} lane={lane} onLaneChange={(value) => setLane(value as typeof lane)} onRetry={(id) => void retryOne(id)} onBatchRetry={(ids) => void retryBatch(ids)} onBatchCancel={(ids) => void cancelBatch(ids)} onRequeueFailure={(id) => void requeueFailure(id)} onStart={(ids) => void startPlans(ids)} attention={attention ?? undefined} loadingMore={loadingMore} onLoadMore={() => void loadMoreQueue()} onRefresh={() => { setDocumentRevision((value) => value + 1); setResourceRevision((value) => value + 1) }} />
}