Files
crawler-plugin/admin-frontend-vue/src/pages/task-center/task-center-model.ts
T
huangzd1997 06396d537e feat(看板): 客户端任务管理从设备维度改为店铺维度全局在跑看板
原「在线设备」卡按日志上报窗口统计,把 89 台算成在线,与实际在跑的客户端数差一个
量级,且设备维度并不能回答"哪台机器上有任务卡住"。改为:

- 汇总卡:总店铺 / 在跑店铺 / 进行中任务 / 今日完成 / 疑似卡住
- 新增「在跑店铺看板」:当前所有进行中任务按店铺归组(在跑数、卡住数、业务分布、
  最近上报),点店铺行下筛该店铺任务;不落店铺的任务归「不挂店铺」桶
- 卡片与面板同源(同一个 aggregateShops),杜绝口径分裂
- 店铺只认两个真来源:上架结果文件 + 逐店落行的结果文件名;
  biz_task_progress_snapshot.current_scope_key 是实例锁不是店铺,已从口径中剔除
- 设备号退为详情附注,花名册 /devices 与在线判定整条链删除
- 上报口径只留一个 10 分钟窗口(进行中且超 10 分钟无上报 = 疑似卡住)

后端 3487 例全绿;新增反射护栏断言设备维度查询与快照当店铺的来源已彻底移除。
2026-10-03 22:41:29 +08:00

197 lines
7.2 KiB
TypeScript

/** 客户端任务管理看板纯逻辑:下拉选项、状态/设备/店铺/进度展示映射、查询参数构建。
* 无框架依赖,可直接被 node 单测导入(对齐 tasks 域 image-video-model 的拆法)。 */
export interface TaskCenterTaskFilter {
username: string
moduleType: string
status: string
/** 店铺名精确匹配(后端按 LOWER(TRIM(...)) 比较),候选来自 /shop-options 与面板下钻。 */
shop: string
startTime: string
endTime: string
}
export interface TaskCenterVariantFilter {
/** 麦象状态码字符串(''=全部 / 0 等待 / 1 处理中 / 2 已完成 / 3 失败)。 */
status: string
}
export type TaskCenterQueryParams = Record<string, string | number>
export function createTaskFilter(): TaskCenterTaskFilter {
return { username: '', moduleType: '', status: '', shop: '', startTime: '', endTime: '' }
}
export function createVariantFilter(): TaskCenterVariantFilter {
return { status: '' }
}
/** 业务下拉:与后端 TaskModuleRegistry 的客户端模块(biz_file_task 12 个 + 品牌检测)对齐。 */
export const MODULE_OPTIONS: Array<{ value: string; label: string }> = [
{ value: '', label: '全部业务' },
{ value: 'PUBLISH', label: '上架' },
{ value: 'DELETE_BRAND', label: '删除ASIN' },
{ value: 'PRODUCT_RISK_RESOLVE', label: '商品风险解决' },
{ value: 'PRICE_TRACK', label: '跟价' },
{ value: 'SHOP_MATCH', label: '定时匹配' },
{ value: 'PATROL_DELETE', label: '巡店删除' },
{ value: 'QUERY_ASIN', label: '查询ASIN' },
{ value: 'WITHDRAW', label: '取款' },
{ value: 'APPEARANCE_PATENT', label: '外观专利检测' },
{ value: 'SIMILAR_ASIN', label: '货源查询' },
{ value: 'COLLECT_DATA', label: '采集数据' },
{ value: 'SHOP_DATA_CRAWL', label: '店铺数据抓取' },
{ value: 'BRAND', label: '品牌检测' },
]
export const TASK_STATUS_OPTIONS: Array<{ value: string; label: string }> = [
{ value: '', label: '全部状态' },
{ value: 'PENDING', label: '待执行' },
{ value: 'RUNNING', label: '执行中' },
{ value: 'SCHEDULED', label: '已排期' },
{ value: 'SUCCESS', label: '已完成' },
{ value: 'FAILED', label: '失败' },
{ value: 'CANCELLED', label: '已取消' },
]
export const VARIANT_STATUS_OPTIONS: Array<{ value: string; label: string }> = [
{ value: '', label: '全部状态' },
{ value: '0', label: '等待执行' },
{ value: '1', label: '处理中' },
{ value: '2', label: '已完成' },
{ value: '3', label: '处理失败' },
]
const TASK_STATUS_LABELS: Record<string, string> = {
PENDING: '待执行',
RUNNING: '执行中',
SCHEDULED: '已排期',
SUCCESS: '已完成',
COMPLETED: '已完成',
FAILED: '失败',
ERROR: '失败',
CANCELLED: '已取消',
CANCELED: '已取消',
}
const VARIANT_STATUS_LABELS: Record<number, string> = {
0: '等待执行',
1: '处理中',
2: '已完成',
3: '处理失败',
}
export type TaskStatusType = 'pending' | 'running' | 'success' | 'failed' | 'cancelled'
export function taskStatusLabel(status: string | null | undefined): string {
const key = (status || '').toUpperCase()
return TASK_STATUS_LABELS[key] || key || '未知'
}
export function taskStatusType(status: string | null | undefined): TaskStatusType {
const key = (status || '').toUpperCase()
if (key === 'SUCCESS' || key === 'COMPLETED') return 'success'
if (key === 'FAILED' || key === 'ERROR') return 'failed'
if (key === 'CANCELLED' || key === 'CANCELED') return 'cancelled'
if (key === 'PENDING') return 'pending'
return 'running'
}
/** 麦象批量任务状态:0 等待 / 1 处理中 / 2 已完成 / 3 失败(不冒充成功)。 */
export function variantStatusLabel(status: number | string | null | undefined): string {
if (status == null || status === '') return '未知'
const code = Number(status)
if (!Number.isFinite(code)) return '未知'
return VARIANT_STATUS_LABELS[code] || '未知'
}
export function variantStatusType(status: number | string | null | undefined): TaskStatusType {
const code = Number(status)
if (code === 2) return 'success'
if (code === 3) return 'failed'
if (code === 0) return 'pending'
return 'running'
}
const DEVICE_ID_DISPLAY_MAX = 18
/** 设备展示名:优先设备名,缺失时截断设备号(任务侧没有主机名)。 */
export function deviceDisplayName(
deviceName: string | null | undefined,
deviceId: string | null | undefined,
): string {
const name = (deviceName || '').trim()
if (name) return name
const id = (deviceId || '').trim()
if (!id) return '未记录'
return id.length > DEVICE_ID_DISPLAY_MAX ? `${id.slice(0, DEVICE_ID_DISPLAY_MAX - 1)}…` : id
}
/** 店铺展示:主店铺名 + 多店铺数量(口径 = 主来源命中数)。 */
export function shopDisplayName(shopName: string | null | undefined, shopCount: number | null | undefined): string {
const name = (shopName || '').trim()
if (!name) return '—'
const count = Number(shopCount || 0)
return count > 1 ? `${name} 等${count}个` : name
}
export function progressText(row: { progressSuccess: number; progressTotal: number }): string {
const total = Number(row.progressTotal || 0)
if (total <= 0) return '—'
return `成功 ${Number(row.progressSuccess || 0)} / 总 ${total}`
}
export function progressPercent(row: { progressSuccess: number; progressTotal: number }): number {
const total = Number(row.progressTotal || 0)
if (total <= 0) return 0
const percent = Math.round((Number(row.progressSuccess || 0) / total) * 100)
return Math.min(100, Math.max(0, percent))
}
/** datetime-local 原生值(YYYY-MM-DDTHH:mm) → 后端 LocalDateTime 兼容的秒级格式。 */
export function toApiTime(value: string | null | undefined): string | undefined {
const text = (value || '').trim().replace(' ', 'T')
const match = /^(\d{4}-\d{2}-\d{2})T(\d{2}):(\d{2})(?::(\d{2}))?$/.exec(text)
if (!match) return undefined
return `${match[1]}T${match[2]}:${match[3]}:${match[4] ?? '00'}`
}
function text(value: unknown): string {
return typeof value === 'string' ? value.trim() : ''
}
/** 任务列表查询参数:空白项不下传(后端按缺省处理)。 */
export function buildTaskQuery(
filter: TaskCenterTaskFilter,
page: number,
pageSize: number,
): TaskCenterQueryParams {
const query: TaskCenterQueryParams = { page, pageSize }
const put = (key: string, value: string | undefined) => {
if (value) query[key] = value
}
put('username', text(filter.username))
put('moduleType', text(filter.moduleType))
put('status', text(filter.status))
put('shop', text(filter.shop))
put('startTime', toApiTime(filter.startTime))
put('endTime', toApiTime(filter.endTime))
return query
}
export function buildVariantQuery(
filter: TaskCenterVariantFilter,
page: number,
pageSize: number,
): TaskCenterQueryParams {
const query: TaskCenterQueryParams = { page, pageSize }
const status = text(filter.status)
if (status !== '') query.status = Number(status)
return query
}
/** 变体采集账号列:平台账号名缺失(历史伪 uid)时回落显示 uid。 */
export function variantAccountText(row: { username: string | null; uid: string | null }): string {
return (row.username || '').trim() || (row.uid || '').trim() || '—'
}