Files
crawler-plugin/frontend-vue/src/pages/brand/components/BrandCollectDataTab.vue
T
huangzd1997 89a69d651e fix(采集数据): 提交即启动+历史「等待中」单可重启——33462 挂 20 小时的入口缺失
线上 33462(uid=1245)建单后 status 一直 PENDING、updated_at 与 created_at 逐字相同、
receivedRows=0。取证:当天 15:46:06 的 parse 请求确实到达服务端,同批相邻的 33463/33464
都在几分钟内被 activate,唯独 33462 全天零次;ONLINE 通道的 PENDING 服务端没有任何兜底
(StaleTaskRepairService 明确跳过 PENDING、finalizeStaleTasks 只扫 RUNNING、
TaskResumeService 只处理 FAILED),而把这一单翻成 RUNNING 的唯一动作是前端发 activate,
它当时绑死在「本次会话刚 parse 的 lastTaskId」上——用户点完提交不点启动,界面就再无入口。

- submitCollect 成功后自动走启动链(两步并一步)
- 历史行对 PENDING 单出「启动任务」,filters 取服务端从 request_json 回传的那份,
  不取界面当前值,否则「重新启动」会悄悄改掉这一单的采集范围
- 三条启动路收敛为单一出口 useCollectDataDispatch(共用前置校验与入队失败回滚)
- 缺桌面壳不再静默 return;回滚失败并进同一句报错,消掉搬文件带来的新增静默 catch

判据 tests/collect-data-提交即启动.test.ts(4 格,反向验过:改回旧形状 3 红)
组件 861→842 行(可读棘轮按对象点名只许减,未动基线)
2026-11-06 12:27:18 +08:00

843 lines
34 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.
<template>
<div class="page-shell module-page">
<AmazonToolPageShell tool-id="collect">
<div class="main-content">
<aside class="left-panel left-panel--with-template">
<div class="section-title">上传文件</div>
<FileUploadZone
hint="选择需要采集的 Excel 文件或文件夹(默认根目录:采集前数据.xlsx),系统将按下方筛选条件自动采集数据。"
:names="selectedFileNames"
:max-display="8"
@select-files="selectFiles"
@select-folder="selectFolder"
/>
<div class="section-title">筛选条件</div>
<div class="filter-zone">
<div class="filter-row">
<label class="filter-label">金额区间</label>
<div class="range-inputs">
<input
v-model.number="filters.minAmount"
type="number"
step="0.01"
min="0"
class="filter-input"
placeholder="最低"
/>
<span class="range-separator">至</span>
<input
v-model.number="filters.maxAmount"
type="number"
step="0.01"
min="0"
class="filter-input"
placeholder="最高"
/>
</div>
</div>
<div class="filter-checks">
<label class="filter-check-row">
<input type="checkbox" class="filter-check-input" v-model="filters.rank" />
<span class="filter-check-text">排名</span>
</label>
<label class="filter-check-row">
<input type="checkbox" class="filter-check-input" v-model="filters.fba" />
<span class="filter-check-text">FBA</span>
</label>
<label class="filter-check-row">
<input type="checkbox" class="filter-check-input" v-model="filters.fbm" />
<span class="filter-check-text">FBM</span>
</label>
</div>
</div>
<div class="section-title country-section-title">
<span>采集国家</span>
<span v-if="countryPrefSaving" class="country-pref-saving">保存中…</span>
</div>
<p class="hint country-pref-hint">
每次任务只采集一个国家,选择后会自动保存,下次进入默认使用该国家。
</p>
<div class="country-pref-radios">
<label
v-for="row in COUNTRY_OPTIONS"
:key="row.code"
class="country-check-row"
:class="{ selected: selectedCountryCode === row.code }"
>
<input
type="radio"
name="collect-data-country"
class="country-check-input"
:checked="selectedCountryCode === row.code"
@change="selectCountry(row.code)"
/>
<span class="country-check-text">{{ row.label }}({{ row.code }})</span>
</label>
</div>
<div class="run-row">
<button
type="button"
class="btn-run"
:disabled="submitting || !uploadedFiles.length"
@click="submitCollect"
>
{{ submitting ? '提交中...' : '提交采集任务' }}
</button>
<button
type="button"
class="btn-run btn-queue"
:disabled="pushing || !lastTaskId"
@click="pushToPythonQueue"
>
{{ pushing ? '启动中...' : '重新启动' }}
</button>
</div>
<p class="loading-msg">提交后自动识别 Excel 内容并直接开始采集,进度和结果在右侧查看。若自动启动失败(例如不在桌面客户端内),单子保持「等待中」,可在右侧历史列表点「启动任务」。</p>
<div v-if="queuePushResult" class="queue-debug-card">
<div class="section-title queue-debug-title">启动结果</div>
<div class="queue-debug-line">{{ queuePushResult }}</div>
<pre v-if="queuePayloadText" class="queue-debug-payload">{{ queuePayloadText }}</pre>
</div>
<ModuleTemplateDownload module-code="collect-data" filename="数据采集 文档格式.xlsx" />
</aside>
<section class="right-panel">
<TaskCenterPanel
:on-batch-delete="batchDeleteHistory"
:on-refresh="refreshBoard"
title="采集数据"
:cards="collectCards"
:current-items="currentTaskViews"
:history-items="historyTaskViews"
current-title="当前任务"
current-empty-text="暂无当前任务"
history-empty-text="暂无历史记录"
>
<template #item-actions="{ item }">
<button v-if="canRetryTask(itemSource(item))" type="button" class="btn-retry"
@click="retryTaskRecord(itemSource(item))">重试</button>
<button type="button" class="btn-delete" @click="deleteTaskRecord(itemSource(item))">删除</button>
</template>
<template #history-item-actions="{ item }">
<button v-if="canStartTask(itemSource(item))" type="button" class="btn-run"
@click="startPendingTask(itemSource(item))">启动任务</button>
<button v-if="canDownload(itemSource(item))" type="button" class="download"
@click="downloadResult(itemSource(item))">下载</button>
<button v-if="canRetryTask(itemSource(item))" type="button" class="btn-retry"
@click="retryTaskRecord(itemSource(item))">重试</button>
<button v-if="itemSource(item).resultId" type="button" class="btn-delete"
@click="deleteTaskRecord(itemSource(item))">删除</button>
</template>
</TaskCenterPanel>
</section>
</div>
</AmazonToolPageShell>
</div>
</template>
<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, reactive, ref, shallowRef, triggerRef } from 'vue'
import { userNotice } from '@/shared/utils/user-notice'
import { ElMessage } from 'element-plus'
import { retryClientTask } from '@/shared/api/task-retry'
import AmazonToolPageShell from '@/pages/amazon/components/AmazonToolPageShell.vue';
import FileUploadZone from '@/shared/components/FileUploadZone.vue'
import ModuleTemplateDownload from '@/shared/components/ModuleTemplateDownload.vue'
import TaskCenterPanel from '@/shared/components/tasks/TaskCenterPanel.vue'
import type { TaskItemView, TaskStatCard } from '@/shared/components/tasks/types'
import { useExcelFileUpload } from '@/shared/composables/useExcelFileUpload'
import { useTaskProgressLoop } from '@/shared/composables/useTaskProgressLoop'
import { createCategorizedTimers } from '@/shared/utils/categorized-timers'
import { saveUrlWithProgress } from '@/shared/utils/download-progress'
import { checkParseResult } from '@/shared/dispatch-guard'
import { useCollectDataDispatch } from '@/shared/composables/useCollectDataDispatch'
import { passGuard } from '@/shared/dispatch-guard-ui'
import {
parseCollectData,
getCollectDataDashboard,
getCollectDataHistory,
getCollectDataCountryPreference,
putCollectDataCountryPreference,
getCollectDataTaskProgressBatch,
deleteCollectDataTask,
deleteCollectDataHistory,
type CollectDataDashboardVo,
type CollectDataHistoryItem,
type CollectDataParseVo,
type CollectDataTaskDetailVo,
} from '@/shared/api/java-modules'
import { getPollingProgressBatch } from '@/shared/api/task-progress-polling.ts'
import { formatDateTime } from '@/shared/utils/datetime'
import { taskStatusClass, taskStatusText } from '@/shared/utils/task-status-text'
import { terminalKeywordShortfall } from '@/shared/utils/task-progress-readout'
import { runBatchDelete } from '@/shared/utils/batch-delete'
import { confirmTaskDeletion } from '@/shared/task-delete-confirm'
const COUNTRY_OPTIONS = [
{ code: 'DE', label: '德国' },
{ code: 'UK', label: '英国' },
{ code: 'FR', label: '法国' },
{ code: 'IT', label: '意大利' },
{ code: 'ES', label: '西班牙' },
] as const
const submitting = ref(false)
const pushing = ref(false)
const {
files: uploadedFiles,
selectedPaths: selectedFileNames,
selectFiles,
selectFolder,
} = useExcelFileUpload({
requireUploadCapability: true,
pickFilesUnavailableWarning: '当前环境不支持文件选择或上传',
folderUnavailableWarning: '当前环境不支持文件夹选择,请在本地客户端中打开',
errorFallbackMessage: (action) => (action === 'files' ? '文件选择失败' : '选择失败'),
onUploaded: () => {
lastTaskId.value = null
lastParseVo.value = null
},
onFailed: () => {
lastTaskId.value = null
lastParseVo.value = null
},
})
const filters = reactive<{
minAmount: number | null
maxAmount: number | null
rank: boolean
fba: boolean
fbm: boolean
}>({
minAmount: null,
maxAmount: null,
rank: true,
fba: false,
fbm: true,
})
const selectedCountryCode = ref<string>('DE')
const selectedCountryCodes = computed(() => selectedCountryCode.value ? [selectedCountryCode.value] : [])
/** 防止接口慢回包覆盖用户已经做出的修改 */
const countryPrefUserTouched = ref(false)
const countryPrefSaving = ref(false)
const timers = createCategorizedTimers('collect-data-tab')
let countryPrefSaveTimer: number | null = null
const lastTaskId = ref<number | null>(null)
const lastParseVo = ref<CollectDataParseVo | null>(null)
const queuePushResult = ref('')
const queuePayloadText = ref('')
const dashboard = ref<CollectDataDashboardVo>({
pendingTaskCount: 0,
processedTaskCount: 0,
successTaskCount: 0,
failedTaskCount: 0,
})
const currentItems = ref<CollectDataHistoryItem[]>([])
const historyItems = ref<CollectDataHistoryItem[]>([])
/**
* 轮询过程中收到的任务最新快照,按 taskId 缓存,用于历史返回前的占位展示
* (审查 C3:整份快照含行明细、体积大,改 shallowRef,写入一律整份替换并 triggerRef)
*/
const taskSnapshots = shallowRef<Record<number, CollectDataTaskDetailVo>>({})
const POLLING_STORAGE_KEY = 'brand:collect-data:polling-task-ids'
/** 轮询可比对的轻量字段(/tasks/progress/light 白名单里本模块快照有的字段),只有它们变化才重建快照对象 */
function taskProgressSignatureOf(detail: CollectDataTaskDetailVo) {
const task = detail.task
if (!task) return ''
return [task.status ?? '', task.updatedAt ?? ''].join('|')
}
/**
* currentItems 的 taskId → 下标索引。
* 轮询每轮都要按 taskId 定位行,此前每次都对整份列表 map 一遍;有索引后只改命中的那一行。
* 列表被整体替换(loadHistory)时必须重建;使用处还会做一次 taskId 校验兜底。
*/
let currentItemsIndex = new Map<number, number>()
function rebuildCurrentItemsIndex() {
const next = new Map<number, number>()
currentItems.value.forEach((item, index) => {
const taskId = Number(item.taskId)
if (Number.isFinite(taskId) && taskId > 0) next.set(taskId, index)
})
currentItemsIndex = next
}
/** 原地更新 currentItems 里指定任务的那一行(找不到就什么都不做) */
function patchCurrentItem(
taskId: number,
latestItem: CollectDataHistoryItem | undefined,
status: string | undefined,
) {
let index = currentItemsIndex.get(taskId)
if (index === undefined || Number(currentItems.value[index]?.taskId) !== taskId) {
rebuildCurrentItemsIndex()
index = currentItemsIndex.get(taskId)
}
if (index === undefined) return
const current = currentItems.value[index]
if (!current) return
currentItems.value[index] = {
...current,
...(latestItem || {}),
taskStatus: status ?? latestItem?.taskStatus ?? current.taskStatus,
}
}
const progressLoop = useTaskProgressLoop<CollectDataTaskDetailVo>({
scope: 'collect-data-tab',
storageKey: POLLING_STORAGE_KEY,
// F5:主轮询只读 status/fileReady 等轻量白名单字段,走 light 端点减小响应体;
// 轻量端点异常/空结果时适配器自动回退重型 batch(否则会漏刷新结果)
fetchProgress: (ids) => getPollingProgressBatch('collectData', ids, {
fallback: () => getCollectDataTaskProgressBatch(ids),
}),
extractTaskId: (detail) => detail.task?.id ?? null,
extractStatus: (detail) => detail.task?.status ?? '',
onUpdate: (taskId, detail) => {
const prev = taskSnapshots.value[taskId]
const merged: CollectDataTaskDetailVo = prev
? { ...prev, task: { ...(prev.task || {}), ...(detail.task || {}) }, items: detail.items ?? prev.items }
: detail
// 只有轮询字段(或行明细)真的变了才展开快照对象:整份快照含行明细,无变化时重建纯属浪费
if (!prev || merged.items !== prev.items || taskProgressSignatureOf(prev) !== taskProgressSignatureOf(merged)) {
taskSnapshots.value = { ...taskSnapshots.value, [taskId]: merged }
triggerRef(taskSnapshots)
}
patchCurrentItem(taskId, detail.items?.[0], detail.task?.status)
},
onTerminal: async (taskId) => {
if (taskSnapshots.value[taskId]) {
const next = { ...taskSnapshots.value }
delete next[taskId]
taskSnapshots.value = next
triggerRef(taskSnapshots)
}
await Promise.all([loadDashboard(), loadHistory()])
},
})
function selectCountry(code: string) {
if (!COUNTRY_OPTIONS.some((row) => row.code === code)) return
if (selectedCountryCode.value === code) return
countryPrefUserTouched.value = true
selectedCountryCode.value = code
scheduleSaveCountryPref()
}
function scheduleSaveCountryPref() {
if (countryPrefSaveTimer != null) {
timers.clearTimer('country-pref-save', countryPrefSaveTimer)
}
countryPrefSaveTimer = timers.setTimeout('country-pref-save', () => {
countryPrefSaveTimer = null
void persistCountryPref()
}, 450)
}
async function persistCountryPref() {
if (!selectedCountryCode.value) return
countryPrefSaving.value = true
try {
await putCollectDataCountryPreference([selectedCountryCode.value])
} catch (error) {
ElMessage.error(error instanceof Error ? error.message : '保存国家顺序失败')
} finally {
countryPrefSaving.value = false
}
}
async function loadCountryPreference() {
try {
const vo = await getCollectDataCountryPreference()
if (countryPrefUserTouched.value) return
const codes = vo?.country_codes
if (Array.isArray(codes) && codes.length) {
selectedCountryCode.value = String(codes[0] || 'DE')
}
} catch {
/* 接口失败时保留本地默认 */
}
}
function buildFiltersPayload() {
const minAmount = filters.minAmount === null || filters.minAmount === undefined || Number.isNaN(filters.minAmount)
? null
: filters.minAmount
const maxAmount = filters.maxAmount === null || filters.maxAmount === undefined || Number.isNaN(filters.maxAmount)
? null
: filters.maxAmount
return {
amount: maxAmount ?? minAmount,
minAmount,
maxAmount,
min_amount: minAmount,
max_amount: maxAmount,
rank: !!filters.rank,
fba: !!filters.fba,
fbm: !!filters.fbm,
countryCodes: [...selectedCountryCodes.value],
countryCode: selectedCountryCode.value,
country_code: selectedCountryCode.value,
}
}
async function submitCollect() {
if (!uploadedFiles.value.length) {
ElMessage.warning('请先选择 Excel 文件')
return
}
if (!selectedCountryCode.value) {
ElMessage.warning('请选择采集国家')
return
}
submitting.value = true
try {
const vo = await parseCollectData({
files: uploadedFiles.value.map((f) => ({
fileKey: f.fileKey,
originalFilename: f.originalFilename,
relativePath: f.relativePath,
})),
task_type: 'collect-data',
filters: buildFiltersPayload(),
})
lastParseVo.value = vo
lastTaskId.value = vo.taskId ?? null
// 0 行落库的任务推给 Python 后拉不到任何明细,会一直停在执行中
const guard = checkParseResult(vo, { requiredColumnsHint: 'ASIN / 国家' })
if (!(await passGuard(guard))) {
lastParseVo.value = null
lastTaskId.value = null
await Promise.all([loadDashboard(), loadHistory()])
return
}
ElMessage.success(`提交成功:任务 ${vo.taskNo || vo.taskId},识别 ${vo.acceptedRows ?? 0} 行有效数据`)
// 提交即启动:这里曾只做到「建单落库」就结束,启动是第二个按钮。用户点完提交就离开,
// 单子会永远停在等待中——ONLINE 通道服务端没有任何 PENDING 兜底会把它推起来(线上 33462 挂 20 小时)。
if (lastTaskId.value) {
await dispatchCollectDataTask(lastTaskId.value, {
taskNo: vo.taskNo, totalRows: vo.acceptedRows ?? 0, pageSize: vo.pageSize,
filters: buildFiltersPayload(),
})
}
await Promise.all([loadDashboard(), loadHistory()])
} catch (error) {
ElMessage.error(error instanceof Error ? error.message : '提交失败')
} finally {
submitting.value = false
}
}
// 启动链本体在 useCollectDataDispatch(提交即启动、当前单重试、历史「等待中」单重启三条路共用同一出口)
const { dispatch: dispatchCollectDataTask, isStarting } = useCollectDataDispatch({
notify: (kind, text) => ElMessage[kind](text),
onBusyChange: (busy) => { pushing.value = busy },
onDispatched: (payload, queueSize) => {
queuePushResult.value = `已启动任务(排队中的任务数:${queueSize})`
queuePayloadText.value = JSON.stringify(payload, null, 2)
},
onStarted: (taskId) => progressLoop.add(taskId),
})
async function pushToPythonQueue() {
if (!lastTaskId.value) {
ElMessage.warning('请先提交采集任务')
return
}
const vo = lastParseVo.value
if (await dispatchCollectDataTask(lastTaskId.value, {
taskNo: vo?.taskNo, totalRows: vo?.acceptedRows ?? 0, pageSize: vo?.pageSize,
filters: buildFiltersPayload(),
})) await Promise.all([loadDashboard(), loadHistory()])
}
// filters 取服务端从 request_json 回传的那份、不取界面当前值,否则「重新启动」会悄悄改掉这一单的采集范围;
// pageSize 历史 VO 里没有(它是 parse 的返回字段),走出厂 50:它只决定采集端每次拉多少行明细,不改采集范围。
async function startPendingTask(item: CollectDataHistoryItem) {
if (!item.taskId) return
if (await dispatchCollectDataTask(item.taskId, {
taskNo: item.taskNo, totalRows: item.totalRows ?? 0, filters: item.filters,
})) await Promise.all([loadDashboard(), loadHistory()])
}
function canStartTask(item: CollectDataHistoryItem | null | undefined): boolean {
return Boolean(item?.taskId) && normalizeTaskStatus(item as CollectDataHistoryItem) === 'PENDING'
&& !isStarting(item?.taskId as number)
}
/**
* 失败任务重试:在**原任务**上复位续跑(不新建任务),与外观专利同口径。
* 只有 FAILED 才出按钮——成功/进行中的任务重试没有语义。
*/
function canRetryTask(item: CollectDataHistoryItem | null | undefined): boolean {
return Boolean(item?.taskId) && normalizeTaskStatus(item as CollectDataHistoryItem) === 'FAILED'
}
async function retryTaskRecord(item: CollectDataHistoryItem | null | undefined) {
const taskId = item?.taskId
if (!taskId) return
try {
await retryClientTask(taskId)
ElMessage.success('已提交重试:在这条任务上继续跑(不新建任务)')
await refreshBoard()
} catch (error) {
ElMessage.error(error instanceof Error ? error.message : '重试失败')
}
}
function normalizeTaskStatus(item: CollectDataHistoryItem) {
return (item.taskStatus || '').toUpperCase()
}
function statusText(item: CollectDataHistoryItem) {
const status = normalizeTaskStatus(item)
if (status === 'RUNNING' || status === 'PENDING') return taskStatusText(status)
if (status === 'SUCCESS' || item.success) return taskStatusText('SUCCESS')
return status === 'FAILED' ? taskStatusText('FAILED') : '等待中'
}
function statusClass(item: CollectDataHistoryItem) {
const status = normalizeTaskStatus(item)
if (status === 'RUNNING' || status === 'FAILED') return taskStatusClass(status)
if (status === 'SUCCESS' || item.success) return taskStatusClass('SUCCESS')
// 其余(含 PENDING/CANCELLED)保持 'pending'
return taskStatusClass(status, { classMap: { CANCELLED: 'pending' } })
}
function taskFilterSummary(item: CollectDataHistoryItem) {
const status = normalizeTaskStatus(item)
if (status !== 'SUCCESS' && !item.success) return ''
const dedupeFiltered = Number(item.dedupeFilteredCount) || 0
const invalidFiltered = Number(item.invalidFilteredCount) || 0
const brandRejected = Number(item.brandRejectedCount) || 0
const finalRowCount = Number(item.finalRowCount ?? item.rowCount) || 0
return `去重 ${dedupeFiltered} · 无效品牌 ${invalidFiltered} · 品牌拒绝 ${brandRejected} · 保留 ${finalRowCount}`
}
function taskProgressPercent(item: CollectDataHistoryItem) {
const total = Number(item.totalRows)
const processed = Number(item.processedRows)
if (Number.isFinite(total) && total > 0 && Number.isFinite(processed) && processed >= 0) {
return Math.max(0, Math.min(100, Math.round((processed / total) * 100)))
}
const value = Number(item.progressPercent)
if (Number.isFinite(value)) return Math.max(0, Math.min(100, Math.round(value)))
const status = normalizeTaskStatus(item)
return status === 'SUCCESS' || item.success ? 100 : 0
}
function taskStageProgress(item: CollectDataHistoryItem) {
if (item.collectStage === 'search') {
return `搜索页面:${item.searchCurrentPage ?? 0}/${item.searchTotalPages ?? '-'} 页`
}
if (item.collectStage === 'detail') {
return `商品详情采集:${item.detailProcessedAsins ?? 0}/${item.detailTotalAsins ?? 0} 个商品`
}
return ''
}
function taskKeywordProgress(item: CollectDataHistoryItem) {
const total = Number(item.totalRows)
if (!Number.isFinite(total) || total <= 0) return ''
const completed = Math.max(0, Number(item.processedRows) || 0)
return `关键词进度:第 ${Math.min(total, completed + 1)}/${total} 个`
}
function canDownload(item: CollectDataHistoryItem) {
const status = normalizeTaskStatus(item)
// 失败任务只要已生成结果文件也允许下载:失败时后端按已收到的分片组装部分结果,
// 已采集的数据不应随任务失败一起不可用(taskId 28599 事故)。
return Boolean(item.downloadUrl && (item.success || status === 'SUCCESS' || status === 'FAILED'))
}
async function downloadResult(item: CollectDataHistoryItem) {
if (!item.downloadUrl) {
ElMessage.warning('当前结果没有下载地址')
return
}
const filename = item.resultFilename || `${item.sourceFilename || 'collect-data-result'}.xlsx`
const downloadId = `collect-data:${item.resultId || item.taskId || Date.now()}`
const result = await saveUrlWithProgress(item.downloadUrl, filename, downloadId)
if (result.success) {
ElMessage.success(`已保存: ${result.path || filename}`)
} else if (result.error && result.error !== '用户取消') {
ElMessage.error(userNotice(result.error))
}
}
/** 手动刷新:强拉历史 + 统计 + 当前轮询任务进度(轮询意外中断后的自救出口) */
async function refreshBoard() {
await Promise.all([loadDashboard(), loadHistory(), progressLoop.refreshOnce()])
}
async function loadDashboard() {
try {
dashboard.value = await getCollectDataDashboard()
} catch (error) {
/* 忽略:dashboard 失败不打断主流程 */
}
}
async function loadHistory() {
try {
const vo = await getCollectDataHistory(50)
const items = vo.items || []
const currentTasks = items.filter((it) => {
const s = (it.taskStatus || '').toUpperCase()
return s === 'PENDING' || s === 'RUNNING'
})
const running = currentTasks.filter((it) => (it.taskStatus || '').toUpperCase() === 'RUNNING')
historyItems.value = items.filter((it) => {
const s = (it.taskStatus || '').toUpperCase()
return s === 'SUCCESS' || s === 'FAILED'
})
// 仅轮询已入 Python 队列的 RUNNING 任务;PENDING 单由提交即启动接手,启动失败时在历史行出「启动任务」。
const seenTaskIds = new Set<number>()
for (const item of running) {
if (item.taskId) {
seenTaskIds.add(item.taskId)
progressLoop.add(item.taskId)
}
}
for (const taskId of [...progressLoop.taskIds.value]) {
const onScreen = running.some((it) => it.taskId === taskId)
if (!onScreen) {
progressLoop.remove(taskId)
}
}
// 合并:历史里 RUNNING/PENDING 的条目 + 轮询缓存里有快照但尚未在历史返回中的占位
const seen = new Set<number>(currentTasks.map((it) => it.taskId).filter((id): id is number => typeof id === 'number'))
const merged: CollectDataHistoryItem[] = [...currentTasks]
for (const taskId of progressLoop.taskIds.value) {
if (seen.has(taskId)) continue
const detail = taskSnapshots.value[taskId]
const placeholder = detail?.items?.[0]
if (placeholder) {
merged.push(placeholder)
} else if (detail?.task) {
merged.push({
taskId: detail.task.id,
taskNo: detail.task.taskNo,
taskStatus: detail.task.status,
startedAt: detail.task.createdAt,
finishedAt: detail.task.finishedAt,
sourceFilename: '采集数据',
} as CollectDataHistoryItem)
}
}
currentItems.value = merged
// 列表整体替换后重建 taskId 索引,轮询就能原地更新命中的那一行
rebuildCurrentItemsIndex()
} catch {
/* 忽略:历史接口失败时保持空列表 */
}
}
async function deleteTaskRecord(item: CollectDataHistoryItem) {
if (!(await confirmTaskDeletion(item))) return
try {
if (item.resultId) {
await deleteCollectDataHistory(item.resultId)
} else if (item.taskId) {
await deleteCollectDataTask(item.taskId)
} else {
return
}
if (item.taskId) {
progressLoop.remove(item.taskId)
}
ElMessage.success('已删除')
await Promise.all([loadDashboard(), loadHistory()])
} catch (error) {
ElMessage.error(error instanceof Error ? error.message : '删除失败')
}
}
const collectCards = computed<TaskStatCard[]>(() => [
{ label: '运行中任务', value: dashboard.value.pendingTaskCount },
{ label: '已结束任务', value: dashboard.value.processedTaskCount },
{ label: '成功任务', value: dashboard.value.successTaskCount },
{ label: '失败任务', value: dashboard.value.failedTaskCount },
])
const currentTaskViews = computed<TaskItemView[]>(() => currentItems.value.map(toCollectTaskView))
const historyTaskViews = computed<TaskItemView[]>(() => historyItems.value.map(toCollectTaskView))
function itemSource(item: TaskItemView): CollectDataHistoryItem {
return item.source as CollectDataHistoryItem
}
function isCollectTaskActive(item: CollectDataHistoryItem) {
const status = normalizeTaskStatus(item)
return status !== 'SUCCESS' && status !== 'FAILED' && !item.success
}
function toCollectTaskView(item: CollectDataHistoryItem): TaskItemView {
const isActive = isCollectTaskActive(item)
const keywordProgress = taskKeywordProgress(item)
const stageProgress = taskStageProgress(item)
const shortfall = terminalKeywordShortfall(item)
// 品牌检测查不了的行:既不进结果、也没被判无效品牌,过去在界面上一个出口都没有
// (task 32573 回传 600 行、其中 173 行这么丢了,用户只看到"保留 1")。
// 这里只陈述事实与原因原文;该做什么的完整指引走通知,避免在 TS 里复刻一份
// BrandCheckFailureNotice 的归因映射而与之漂移。
const brandQueryFailedLine = item.brandQueryFailedCount
? `品牌检测未出结论:${item.brandQueryFailedCount} 行`
+ `${item.brandQueryFailedReason ? `(${item.brandQueryFailedReason})` : ''}`
: ''
const percent = taskProgressPercent(item)
const finishedAt = item.finishedAt ? formatDateTime(item.finishedAt) : isActive ? '进行中' : '-'
const extraLines: string[] = isActive
? [
...(item.rowCount != null ? [`行数:${item.rowCount}`] : []),
`总数据去重过滤:${item.dedupeFilteredCount ?? 0}`,
`无效品牌过滤:${item.invalidFilteredCount ?? 0}`,
...(brandQueryFailedLine ? [brandQueryFailedLine] : []),
...(keywordProgress ? [keywordProgress] : []),
...(stageProgress ? [stageProgress] : []),
]
: [
`总数据去重过滤:${item.dedupeFilteredCount ?? 0}`,
`无效品牌过滤:${item.invalidFilteredCount ?? 0}`,
...(brandQueryFailedLine ? [brandQueryFailedLine] : []),
...(item.resultFilename ? [item.resultFilename] : []),
...(taskFilterSummary(item) ? [`筛选统计:${taskFilterSummary(item)}`] : []),
// 终态却没跑完全部关键词:把缺口写在卡片上。此前只有那根 25% 的进度条在说话,
// 而卡片同时写着「已完成」,读起来像是进度条坏了,实际是它在报任务提前结束。
...(shortfall ? [shortfall] : []),
...(item.error ? [`错误:${userNotice(item.error)}`] : []),
]
return {
key: `collect-${item.taskId ?? item.resultId ?? item.sourceFilename}`,
title: item.sourceFilename || '采集数据',
taskId: item.taskId ?? '-',
startedAt: formatDateTime(item.startedAt || item.createdAt),
finishedAt,
statusText: statusText(item),
statusClass: statusClass(item),
extraLines,
progress: isActive || percent > 0
? { percent, stage: '关键词进度' }
: null,
source: item,
}
}
onMounted(() => {
void loadCountryPreference()
void loadDashboard()
void loadHistory()
})
onBeforeUnmount(() => {
if (countryPrefSaveTimer != null) {
timers.clearTimer('country-pref-save', countryPrefSaveTimer)
countryPrefSaveTimer = null
}
timers.clearScope()
})
/**
* 历史记录批量删除:复用单条删除逻辑(删除后原函数各自刷新列表)
*/
async function batchDeleteHistory(views: TaskItemView[]) {
const { total, failed } = await runBatchDelete(views, (view) => deleteTaskRecord(itemSource(view)))
if (!total) return
if (failed > 0) {
ElMessage.warning(`删除完成,${failed} 条失败`)
} else {
ElMessage.success(`已删除 ${total} 条历史记录`)
}
}
</script>
<style scoped>
.module-page { min-height: 100vh; background: #242424; }
.main-content { display: flex; height: calc(100vh - 56px); min-height: calc(100vh - 56px); }
.left-panel { width: 400px; background: #242424; padding: 20px; overflow-y: auto; border-right: 1px solid #2e3a52; }
.right-panel { flex: 1; min-width: 0; background: #242424; display: flex; flex-direction: column; }
.section-title { font-size: 13px; color: #a0acbe; margin: 12px 0 10px; }
.hint, .loading-msg, .files, .muted { color: #5e6878; font-size: 12px; line-height: 1.5; }
.btns, .run-row { display: flex; gap: 10px; flex-wrap: wrap; }
.opt-btn, .btn-run, .btn-delete, .download { border: none; cursor: pointer; border-radius: 7px; }
.opt-btn { padding: 8px 14px; color: #c8d2e2; background: #242424; border: 1px solid #3e4a62; }
.btn-run { padding: 10px 18px; color: #f5f8fc; background: #3498db; font-weight: 600; }
.btn-run.btn-queue { background: #27ae60; }
.btn-run:disabled { opacity: .55; cursor: not-allowed; }
.filter-zone {
border: 1px solid #2e3a52;
border-radius: 8px;
padding: 12px;
background: #242424;
margin-bottom: 14px;
display: flex;
flex-direction: column;
gap: 10px;
}
.filter-row {
display: flex;
align-items: center;
gap: 10px;
}
.filter-label {
width: 110px;
color: #a0acbe;
font-size: 12px;
}
.filter-input {
flex: 1;
min-width: 0;
padding: 6px 10px;
background: #242424;
border: 1px solid #3e4a62;
border-radius: 6px;
color: #c8d2e2;
font-size: 13px;
outline: none;
}
.filter-input:focus { border-color: #3498db; }
.range-inputs { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; }
.range-separator { color: #5e6878; font-size: 12px; white-space: nowrap; }
.filter-checks { display: flex; gap: 16px; }
.filter-check-row { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; user-select: none; color: #c8d2e2; font-size: 12px; }
.filter-check-input { width: 16px; height: 16px; accent-color: #409eff; cursor: pointer; }
.country-pref-hint { margin-top: -4px; }
.country-section-title { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.country-pref-saving { color: #3498db; font-size: 11px; }
.country-pref-radios { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin-bottom: 14px; }
.country-check-row { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; font-size: 12px; color: #c8d2e2; padding: 8px 10px; border: 1px solid #333; border-radius: 8px; background: #242424; }
.country-check-row.selected { border-color: #409eff; background: rgba(64, 158, 255, .12); color: #e6f2ff; }
.country-check-input { width: 16px; height: 16px; accent-color: #409eff; cursor: pointer; flex-shrink: 0; }
.country-check-text { line-height: 1.3; }
.queue-debug-card { margin-top: 14px; border: 1px solid #2e3a52; border-radius: 8px; padding: 10px 12px; background: #1f1f1f; }
.queue-debug-title { margin-top: 0; }
.queue-debug-line { color: #9ad; font-size: 12px; }
.queue-debug-payload { margin-top: 8px; padding: 8px 10px; background: #151a25; border-radius: 6px; color: #a0acbe; font-size: 11px; overflow: auto; max-height: 200px; }
.download { padding: 6px 10px; color: #c8d2e2; background: rgba(52, 152, 219, .18); }
.btn-delete { padding: 6px 10px; color: #ff8f8f; background: rgba(231, 76, 60, .12); }
@media (max-width: 1100px) {
.main-content { flex-direction: column; height: auto; }
.left-panel { width: 100%; border-right: none; border-bottom: 1px solid #2e3a52; }
}
</style>