feat(看板): 变体采集只看在跑,进度列改客户端同源口径

两件事,都是用户当场指出的读数误导:

1. 变体采集页签徽章 106 是麦象历史全量。console 代理一次只接受一个 status,
   故 status 缺省时服务端合并 0(等待)∪1(处理中),按 createTime 倒序后内存切片;
   单状态各拉 200 条,触顶打 warn。前端状态下拉去掉「已完成/处理失败」两个入口。

2. 进度列的「已完成 0 / 总 1」来自 source_file_count(采集/跟价都是单文件任务),
   与客户端进度条不同源。改为按模块分档:COLLECT_DATA 取 result_json 的
   $.totalRows/$.processedRows(与客户端「关键词进度:第 X/N 个」同一对数),
   PRICE_TRACK/SHOP_MATCH 这类客户端本来没有进度条的返回 0,前端 progressTotal<=0
   显示「—」且不再画 0% 进度轨;PUBLISH/DELETE_BRAND/品牌行保持文件数/品牌行数。

护栏:Mapper SQL 渲染 26 例、QueryService 34 例、VariantService 8 例、
ScopeService 7 例;前端 node:test 1653 例。docs/specs/16-client-task-console.md
的「进度口径」一节按新规则重写。
This commit is contained in:
2026-10-04 10:27:09 +08:00
parent 06b6d3fae3
commit e832418f4b
12 changed files with 365 additions and 38 deletions
@@ -94,9 +94,10 @@ public class AdminTaskCenterController {
}
@GetMapping("/variant-tasks")
@Operation(summary = "变体采集列表",
@Operation(summary = "变体采集列表(只看在跑)",
description = "代理麦象 console 批量任务(默认只看 variant-collection):非超管按可见用户 uid 过滤,"
+ "超管看全部;麦象未配置或不可达时返回空页。status 为麦象状态码:0 等待/1 处理中/2 已完成/3 失败。")
+ "超管看全部;麦象未配置或不可达时返回空页。status 为麦象状态码:0 等待/1 处理中/2 已完成/3 失败;"
+ "不传 status = 只看在跑(0∪1 合并,历史不再展示)。")
public ApiResponse<TaskCenterVariantPageVo> variantTasks(HttpServletRequest request,
@RequestParam(required = false) Integer status,
@RequestParam(required = false) String taskType,
@@ -1,5 +1,6 @@
package com.nanri.aiimage.modules.taskcenter.mapper;
import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterCollectStatsRow;
import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterDeviceRow;
import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterShopRow;
import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterShopStatRow;
@@ -327,6 +328,23 @@ public interface TaskCenterQueryMapper {
+ "</script>")
List<TaskCenterSnapshotRow> selectSnapshots(@Param("taskIds") List<Long> taskIds);
/**
* 采集数据任务的关键词级进度(客户端回写的 {@code result_json} 里只抽
* {@code $.totalRows / $.processedRows} 两个数,不把大 JSON 拉回服务层)。
*
* <p>列是原生 JSON 类型,缺键只会得到 NULL(服务层按「无进度」读),不会让整页查询报错。</p>
*/
@Select("<script>"
+ "SELECT t.id AS taskId,"
+ " JSON_UNQUOTE(JSON_EXTRACT(t.result_json, '$.totalRows')) AS totalRows,"
+ " JSON_UNQUOTE(JSON_EXTRACT(t.result_json, '$.processedRows')) AS processedRows"
+ " FROM biz_file_task t"
+ " WHERE t.id IN <foreach collection='taskIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>"
+ " AND UPPER(t.module_type) = 'COLLECT_DATA'"
+ " AND t.result_json IS NOT NULL"
+ "</script>")
List<TaskCenterCollectStatsRow> selectCollectDataKeywordStats(@Param("taskIds") List<Long> taskIds);
/** 详情:按任务号取单行(只认 12 个客户端模块,其余模块不在看板范围)。 */
@Select("SELECT t.id AS id, t.task_no AS taskNo, UPPER(t.module_type) AS moduleType,"
+ " " + STATUS_T + " AS status, t.user_id AS userId, t.device_id AS deviceId,"
@@ -0,0 +1,17 @@
package com.nanri.aiimage.modules.taskcenter.model.dto;
import lombok.Data;
/**
* 采集数据任务的关键词级进度:从 {@code biz_file_task.result_json} 抽出的两个数,
* 与客户端进度条同源(collectStage 等其余字段看板不读)。
*
* <p>JSON_UNQUOTE 的产物是字符串,脏值(非数字/缺失)由服务层按无进度处理。</p>
*/
@Data
public class TaskCenterCollectStatsRow {
private Long taskId;
private String totalRows;
private String processedRows;
}
@@ -2,6 +2,7 @@ package com.nanri.aiimage.modules.taskcenter.service;
import com.nanri.aiimage.common.module.TaskModuleRegistry;
import com.nanri.aiimage.modules.taskcenter.mapper.TaskCenterQueryMapper;
import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterCollectStatsRow;
import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterDeviceRow;
import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterShopRow;
import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterShopStatRow;
@@ -259,9 +260,11 @@ public class TaskCenterQueryService {
: toUserNames(safe(queryMapper.selectUsernames(userIds)));
Map<String, TaskCenterSnapshotRow> snapshots = row.getId() == null ? Map.of()
: toSnapshots(safe(queryMapper.selectSnapshots(List.of(row.getId()))));
Map<Long, TaskCenterCollectStatsRow> collectStats = row.getId() == null ? Map.of()
: selectCollectStats(List.of(row), snapshots);
TaskCenterTaskDetailVo vo = fillRow(new TaskCenterTaskDetailVo(), row, usernames, deviceNames,
snapshots, collectShops(bizIds), LocalDateTime.now());
snapshots, collectStats, collectShops(bizIds), LocalDateTime.now());
vo.setShops(shopStats(row));
vo.setErrorMessage(row.getErrorMessage());
return vo;
@@ -311,12 +314,14 @@ public class TaskCenterQueryService {
: toUserNames(safe(queryMapper.selectUsernames(userIds)));
Map<String, TaskCenterSnapshotRow> snapshots = rowIds.isEmpty() ? Map.of()
: toSnapshots(safe(queryMapper.selectSnapshots(rowIds)));
Map<Long, TaskCenterCollectStatsRow> collectStats = selectCollectStats(rows, snapshots);
Map<Long, List<String>> shops = collectShops(bizIds);
LocalDateTime now = LocalDateTime.now();
List<TaskCenterTaskRowVo> items = new ArrayList<>(rows.size());
for (TaskCenterTaskRow row : rows) {
items.add(fillRow(new TaskCenterTaskRowVo(), row, usernames, Map.of(), snapshots, shops, now));
items.add(fillRow(new TaskCenterTaskRowVo(), row, usernames, Map.of(), snapshots,
collectStats, shops, now));
}
return items;
}
@@ -398,6 +403,7 @@ public class TaskCenterQueryService {
Map<Long, String> usernames,
Map<String, String> deviceNames,
Map<String, TaskCenterSnapshotRow> snapshots,
Map<Long, TaskCenterCollectStatsRow> collectStats,
Map<Long, List<String>> shops,
LocalDateTime now) {
vo.setId(row.getId());
@@ -423,9 +429,7 @@ public class TaskCenterQueryService {
vo.setCurrentScope(snapshot.getCurrentScopeKey());
vo.setMessage(snapshot.getMessage());
} else {
vo.setProgressTotal(nullToZero(row.getTotalCount()));
vo.setProgressSuccess(nullToZero(row.getSuccessCount()));
vo.setProgressFailed(nullToZero(row.getFailedCount()));
fillFallbackProgress(vo, row, collectStats.get(row.getId()));
}
// 品牌检测按关键字执行、不落店铺;品牌行的 current_scope_key 是品牌名,不能当店铺用。
@@ -439,6 +443,70 @@ public class TaskCenterQueryService {
return vo;
}
/**
* 无快照时的进度回退,口径 = 客户端画不画进度条(用户 2026-10-04 定案):
* <ul>
* <li>采集数据:客户端进度条是关键词进度(result_json 的 processedRows/totalRows),看板用同一份数;</li>
* <li>上架 / 删除ASIN / 品牌行:客户端进度条的单位本就是文件数 / 品牌行数,沿用行内计数;</li>
* <li>其余(跟价、定时匹配等):客户端没有进度条,进度置 0,前端画「—」——
* 旧的「输入文件数」回退让单文件任务恒显示 0/1,是误导。</li>
* </ul>
*/
private static void fillFallbackProgress(TaskCenterTaskRowVo vo, TaskCenterTaskRow row,
TaskCenterCollectStatsRow collectStat) {
String module = row.getModuleType();
if (row.isBrand() || "PUBLISH".equals(module) || "DELETE_BRAND".equals(module)) {
vo.setProgressTotal(nullToZero(row.getTotalCount()));
vo.setProgressSuccess(nullToZero(row.getSuccessCount()));
vo.setProgressFailed(nullToZero(row.getFailedCount()));
return;
}
if ("COLLECT_DATA".equals(module)) {
long total = parseStatNumber(collectStat == null ? null : collectStat.getTotalRows());
long processed = parseStatNumber(collectStat == null ? null : collectStat.getProcessedRows());
vo.setProgressTotal(Math.max(0, total));
// 已完成 = 已处理关键词数(与客户端「关键词进度:第 X/N 个」同数)
vo.setProgressSuccess(total <= 0 ? 0 : Math.min(Math.max(0, processed), total));
vo.setProgressFailed(0);
return;
}
vo.setProgressTotal(0);
vo.setProgressSuccess(0);
vo.setProgressFailed(0);
}
/** JSON_UNQUOTE 的产物是字符串:非数字/缺失一律按无进度(0),不猜。 */
private static long parseStatNumber(String value) {
if (value == null || value.isBlank()) {
return 0;
}
try {
return Long.parseLong(value.trim());
} catch (NumberFormatException ex) {
return 0;
}
}
/** 采集数据且无快照的行才需要关键词进度;页内没有这种行就不发查询。 */
private Map<Long, TaskCenterCollectStatsRow> selectCollectStats(List<TaskCenterTaskRow> rows,
Map<String, TaskCenterSnapshotRow> snapshots) {
List<Long> ids = rows.stream()
.filter(row -> !row.isBrand() && "COLLECT_DATA".equals(row.getModuleType()))
.filter(row -> row.getId() != null
&& !snapshots.containsKey(snapshotKey(row.getId(), row.getModuleType())))
.map(TaskCenterTaskRow::getId).distinct().toList();
if (ids.isEmpty()) {
return Map.of();
}
Map<Long, TaskCenterCollectStatsRow> stats = new HashMap<>();
for (TaskCenterCollectStatsRow row : safe(queryMapper.selectCollectDataKeywordStats(ids))) {
if (row.getTaskId() != null) {
stats.put(row.getTaskId(), row);
}
}
return stats;
}
private static boolean isStuck(TaskCenterTaskRow row, LocalDateTime now) {
if (row.getStatus() == null || !ACTIVE_STATUSES.contains(row.getStatus())) {
return false;
@@ -12,6 +12,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
@@ -24,6 +25,9 @@ import java.util.stream.Collectors;
*
* <p>可见范围靠 uid 过滤实现:非超管把可见用户号拼成逗号分隔的 uid 参数(麦象侧拆分后 IN),
* 超管不传 uid(看全部)。麦象未配置或不可达时返回空页并记日志,页面不因外部依赖炸掉。</p>
*
* <p>不传 status 时只看在跑(等待 0 ∪ 处理中 1):麦象一次只能筛一个状态,
* 服务层各拉一页合并;历史终态不再作为页签默认口径(徽章说的必须是「在跑几条」)。</p>
*/
@Service
@Slf4j
@@ -33,6 +37,12 @@ public class TaskCenterVariantService {
/** 页签默认只看变体采集这一类批量任务;传 taskType 可覆盖。 */
public static final String VARIANT_TASK_TYPE = "variant-collection";
/** 麦象状态码:0 等待 / 1 处理中,两者合起来才是「在跑」。 */
private static final int STATUS_WAITING = 0;
private static final int STATUS_PROCESSING = 1;
/** 在跑任务正常在几十量级;触顶时 total 仍按麦象回值,但翻页尾部会缺行(记 warn)。 */
private static final int ACTIVE_FETCH_SIZE = 200;
private static final long DEFAULT_PAGE_SIZE = 20;
private static final long MAX_PAGE_SIZE = 100;
@@ -60,13 +70,43 @@ public class TaskCenterVariantService {
String uidsCsv = scope.allUsers() ? null
: scope.visibleUserIds().stream().sorted().map(String::valueOf).collect(Collectors.joining(","));
BatchTaskPage maixiang = consoleClient.batchTasksPage(uidsCsv, status, type,
(int) normalizedPage, (int) normalizedSize);
vo.setTotal(maixiang.total());
vo.setItems(assemble(maixiang.items()));
int size = (int) normalizedSize;
if (status != null) {
BatchTaskPage maixiang = consoleClient.batchTasksPage(uidsCsv, status, type,
(int) normalizedPage, size);
vo.setTotal(maixiang.total());
vo.setItems(assemble(maixiang.items()));
return vo;
}
// 不传状态 = 只看在跑(用户 2026-10-04 定案:历史的不看了)。
// 麦象一次只能筛一个状态,等待(0) 与处理中(1) 各拉一页后在内存合并分页。
BatchTaskPage waiting = consoleClient.batchTasksPage(uidsCsv, STATUS_WAITING, type, 1, ACTIVE_FETCH_SIZE);
BatchTaskPage processing = consoleClient.batchTasksPage(uidsCsv, STATUS_PROCESSING, type, 1, ACTIVE_FETCH_SIZE);
List<BatchTaskItem> active = new ArrayList<>(waiting.items());
active.addAll(processing.items());
active.sort(Comparator.comparing(BatchTaskItem::createTime,
Comparator.nullsLast(Comparator.reverseOrder())));
// total 用麦象回值(-1 哨兵或缺字段时回落为已拉到的行数),保证徽章说的是「在跑」而不是历史全量。
long total = effectiveTotal(waiting) + effectiveTotal(processing);
vo.setTotal(total);
if (total > active.size()) {
log.warn("[task-center] 变体采集在跑数 {} 超过单状态拉取上限 {},翻页尾部会缺行",
total, ACTIVE_FETCH_SIZE);
}
long from = (normalizedPage - 1) * normalizedSize;
if (from >= active.size()) {
return vo;
}
long to = Math.min(from + normalizedSize, active.size());
vo.setItems(assemble(active.subList((int) from, (int) to)));
return vo;
}
private static long effectiveTotal(BatchTaskPage page) {
return Math.max(page.total(), page.items().size());
}
/** uid → 账号名映射只对数字 uid 生效;麦象任务可能带非数字伪 uid(如 collect-27475)。 */
private List<TaskCenterVariantTaskVo> assemble(List<BatchTaskItem> items) {
Set<Long> userIds = new LinkedHashSet<>();