前端(任务卡进度条): 10 页接上实时进度,3 页没分母的显式写 null
后端已把心跳里的 current/total 透到 progress/light,但 TaskItemView.progress 是可空字段:
页面少写一行不会报错也不会红,界面上就是安静地没有进度条——11 类客户端任务里 7 类正是这样。
- 新增 src/shared/api/task-progress-live.ts:进度走独立通道,模块级缓存 + 60s 新鲜窗口
为什么不并进任务对象:各页 {...prev.task, ...detail.task} 合并里有 5 处带「变更签名」守卫
(rowsVersion|status|fileStatus|fileReady)。进度每轮都在动,而签名在「正在翻页、还没攒够一批
回传」的时段不变——照那条路走,进度条会一路停在上一次回传的数字上,看着像卡死
- 写入点挂在 light 出口(progress-light.ts)而不是聚合层:4 个页面直接调 getModuleProgressLight
不经过 getPollingProgressBatch,挂聚合层它们永远拿不到;命中缓存也要刷一遍读数,
否则进度会在缓存窗口里被自己的新鲜判定踢掉、凭空消失
- toBatchItems 与 LightPollingItem 补三字段:这里的键集是逐字段挑的,少挑一个就把后端给的数丢掉
- 10 页接 liveProgress:解决风控、跟价、定时匹配、状态查询、提款、巡店删除、商品管理采集等
- Convert/Dedupe/Split 三页写死 progress: null 并说明原因:它们卡列的是完成记录、没有可数的分母。
显式 null 与省略字段在代码里必须长得不一样,否则下一个人无法区分「没得画」和「忘了接」
- 新增 tests/task-progress-view-wiring.test.ts:适配器清单由文件系统给出、总数硬钉 16,
判据同时认 `progress:` 与 ES6 简写 `progress,`(只认冒号把 BrandBrandTab 误报成缺口,
而误报的尺子会被人当噪音绕过去);变异自证三处全 KILLED——抽掉跟价页的接线、
摘掉批次白名单的 progressTotal、去掉分母塌 0 时的旧读数清理,还原后字节哈希一致
- tests/task-progress-polling.test.ts 的整份形状断言同步加三字段(它本来就该在我改白名单时红)
This commit is contained in:
@@ -3,6 +3,7 @@ import { buildJavaUrl } from './url.ts'
|
||||
import { API_ENDPOINTS } from './endpoints.ts'
|
||||
import { getTaskProgressCacheTtlMs, getTaskProgressTimeoutMs } from '../task-progress-config.ts'
|
||||
import { createTaskProgressRequestCache } from '../task-progress-request-cache.ts'
|
||||
import { rememberLiveProgress } from './task-progress-live.ts'
|
||||
import type { TaskProgressBatchVo } from './types/task.ts'
|
||||
|
||||
/**
|
||||
@@ -44,6 +45,13 @@ export interface TaskProgressLightItem {
|
||||
updatedAt?: string | null
|
||||
/** 结果行版本(「最后变更时间毫秒#行数」):未变说明行数据没动过,可跳过重型 batch。 */
|
||||
rowsVersion?: string | null
|
||||
/**
|
||||
* 客户端心跳上报的执行进度(2026-10-10)。后端保证三者同生同灭:
|
||||
* 读不到位置、或分母不是正数时三个都是 null,所以这里的「没有」不等于 0%。
|
||||
*/
|
||||
progressCurrent?: number | null
|
||||
progressTotal?: number | null
|
||||
progressLabel?: string | null
|
||||
}
|
||||
|
||||
export interface GetModuleProgressLightOptions {
|
||||
@@ -85,6 +93,9 @@ export function getModuleProgressLight(
|
||||
const cacheKey = `${path}::${normalizedTaskIds.join(",")}`
|
||||
const cached = progressLightCache.get(cacheKey)
|
||||
if (!options.force && cached !== undefined) {
|
||||
// 命中缓存也要刷新一遍读数:进度按「最后一次被任何一轮 light 看到」计时,
|
||||
// 只在真发请求时刷新会让进度条在缓存窗口里被判成过期而凭空消失。
|
||||
rememberLiveProgress(cached.items)
|
||||
return Promise.resolve(cached)
|
||||
}
|
||||
const inflight = progressLightCache.getInflight(cacheKey)
|
||||
@@ -104,6 +115,7 @@ export function getModuleProgressLight(
|
||||
missingTaskIds: data?.missingTaskIds || [],
|
||||
}
|
||||
progressLightCache.set(cacheKey, normalized)
|
||||
rememberLiveProgress(normalized.items)
|
||||
return normalized
|
||||
})
|
||||
.finally(() => {
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* 心跳上报的实时进度(模块级缓存,2026-10-10)。
|
||||
*
|
||||
* 为什么不直接把 progressCurrent/progressTotal 并进任务对象:各页把 light 结果
|
||||
* `{...prev.task, ...detail.task}` 合并进快照时,有 5 处带「变更签名」守卫
|
||||
* (签名 = rowsVersion|status|fileStatus|fileReady)。进度每轮都在动,而签名在
|
||||
* 「正在翻页、还没攒够一批回传」的时段里不变——照那条路走,进度条会一路停在上一次
|
||||
* 回传时的数字,看着像卡住。签名守卫是为「行数据按需拉取」省流量而设的,不该为它让步。
|
||||
*
|
||||
* 所以进度走这条独立通道:轮询出口写入一次,页面渲染时按 taskId 读。
|
||||
* 读不到(老后端、老客户端没报位置、或超过 FRESH_WINDOW_MS 没刷新)就返回 null,
|
||||
* 由卡片决定**不画**——「没有进度」和「进度 0%」是两件事。
|
||||
*/
|
||||
|
||||
export interface LiveTaskProgress {
|
||||
percent: number
|
||||
stage?: string
|
||||
}
|
||||
|
||||
interface ProgressEntry {
|
||||
current: number
|
||||
total: number
|
||||
label?: string
|
||||
seenAt: number
|
||||
}
|
||||
|
||||
/** 超过这个时长没被任何一轮 light 刷新,就当作已经没有活的进度。 */
|
||||
const FRESH_WINDOW_MS = 60_000
|
||||
/** 缓存上限:正常一页在册任务远小于它,触顶时按最久未刷新淘汰(防无界增长)。 */
|
||||
const MAX_ENTRIES = 800
|
||||
|
||||
const entries = new Map<number, ProgressEntry>()
|
||||
|
||||
function clampPercent(current: number, total: number): number {
|
||||
const percent = Math.floor((current * 100) / total)
|
||||
if (!Number.isFinite(percent)) return 0
|
||||
return Math.min(100, Math.max(0, percent))
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录一轮 light 响应里的进度。
|
||||
*
|
||||
* 后端已保证「读不到位置」时三个字段一起为 null,这里再守一道:分母非正数就不登记,
|
||||
* 免得 0/0 被算成一个 0% 的空轨(客户端归一化在四级 key 全缺时正是发 0/0)。
|
||||
*/
|
||||
export function rememberLiveProgress(lightItems: unknown[] | null | undefined): void {
|
||||
const now = Date.now()
|
||||
for (const raw of lightItems || []) {
|
||||
const item = raw as { taskId?: number; progressTotal?: number | null; progressCurrent?: number | null; progressLabel?: string | null } | null
|
||||
if (!item || typeof item.taskId !== 'number' || item.taskId <= 0) continue
|
||||
const total = typeof item.progressTotal === 'number' ? item.progressTotal : 0
|
||||
if (total <= 0) {
|
||||
entries.delete(item.taskId)
|
||||
continue
|
||||
}
|
||||
const current = typeof item.progressCurrent === 'number' && item.progressCurrent > 0 ? item.progressCurrent : 0
|
||||
const label = typeof item.progressLabel === 'string' && item.progressLabel.trim() ? item.progressLabel.trim() : undefined
|
||||
entries.set(item.taskId, { current, total, label, seenAt: now })
|
||||
}
|
||||
if (entries.size > MAX_ENTRIES) {
|
||||
const stale = Array.from(entries.entries())
|
||||
.sort((left, right) => left[1].seenAt - right[1].seenAt)
|
||||
.slice(0, entries.size - MAX_ENTRIES)
|
||||
for (const [taskId] of stale) entries.delete(taskId)
|
||||
}
|
||||
}
|
||||
|
||||
/** 取某任务的实时进度;没有活的读数时返回 null(调用方不得折成 0%)。 */
|
||||
export function liveProgress(taskId: number | null | undefined, now: number = Date.now()): LiveTaskProgress | null {
|
||||
if (typeof taskId !== 'number' || taskId <= 0) return null
|
||||
const entry = entries.get(taskId)
|
||||
if (!entry) return null
|
||||
if (now - entry.seenAt > FRESH_WINDOW_MS) {
|
||||
entries.delete(taskId)
|
||||
return null
|
||||
}
|
||||
return { percent: clampPercent(entry.current, entry.total), stage: entry.label }
|
||||
}
|
||||
|
||||
/** 任务结束或页面卸载时清掉读数。 */
|
||||
export function forgetLiveProgress(taskId: number): void {
|
||||
entries.delete(taskId)
|
||||
}
|
||||
|
||||
export function resetLiveProgress(): void {
|
||||
entries.clear()
|
||||
}
|
||||
@@ -25,6 +25,9 @@ export interface LightPollingItem {
|
||||
fileError?: string | null
|
||||
fileReady?: boolean | null
|
||||
updatedAt?: string | null
|
||||
progressCurrent?: number | null
|
||||
progressTotal?: number | null
|
||||
progressLabel?: string | null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +36,8 @@ export function toBatchItems(lightItems: unknown[]): LightPollingItem[] {
|
||||
for (const raw of lightItems || []) {
|
||||
const item = raw as { taskId?: number; status?: string | null; statusCode?: string | null
|
||||
fileStatus?: string | null; fileError?: string | null; fileReady?: boolean | null
|
||||
updatedAt?: string | null } | null
|
||||
updatedAt?: string | null; progressCurrent?: number | null; progressTotal?: number | null
|
||||
progressLabel?: string | null } | null
|
||||
if (!item || typeof item.taskId !== 'number' || item.taskId <= 0) {
|
||||
continue
|
||||
}
|
||||
@@ -48,6 +52,9 @@ export function toBatchItems(lightItems: unknown[]): LightPollingItem[] {
|
||||
fileError: item.fileError ?? null,
|
||||
fileReady: item.fileReady ?? null,
|
||||
updatedAt: item.updatedAt ?? null,
|
||||
progressCurrent: item.progressCurrent ?? null,
|
||||
progressTotal: item.progressTotal ?? null,
|
||||
progressLabel: item.progressLabel ?? null,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user