refactor: 第一批低风险收敛——任务缓存服务泛型化 + 前端死代码清理与状态映射统一

This commit is contained in:
2026-10-01 14:05:30 +08:00
parent f05960659f
commit 4b29a150f3
74 changed files with 873 additions and 3566 deletions
@@ -1,23 +1,23 @@
package com.nanri.aiimage.modules.appearancepatent.service; package com.nanri.aiimage.modules.appearancepatent.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto; import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
import lombok.RequiredArgsConstructor; import com.nanri.aiimage.modules.task.service.AbstractPendingRowTaskCacheService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.Duration; import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/**
* 外观专利模块任务缓存服务:待处理行队列 + 心跳复用通用基类
* {@link AbstractPendingRowTaskCacheService};本模块额外的行级两态标记
* (处理中 / 已完成)与轮次计数留在本类。
*/
@Service @Service
@RequiredArgsConstructor
@Slf4j @Slf4j
public class AppearancePatentTaskCacheService { public class AppearancePatentTaskCacheService extends AbstractPendingRowTaskCacheService<AppearancePatentResultRowDto> {
private static final long TTL_HOURS = 24;
/** /**
* 处理中标记 TTL。取 15 分钟:合批后单批实测约 30–120 秒(合批前 10 行要 220 秒), * 处理中标记 TTL。取 15 分钟:合批后单批实测约 30–120 秒(合批前 10 行要 220 秒),
@@ -26,117 +26,16 @@ public class AppearancePatentTaskCacheService {
*/ */
private static final Duration IN_FLIGHT_TTL = Duration.ofMinutes(15); private static final Duration IN_FLIGHT_TTL = Duration.ofMinutes(15);
private final StringRedisTemplate stringRedisTemplate; private static final long TTL_HOURS = 24;
private final ObjectMapper objectMapper;
public void appendPendingRow(Long taskId, String scopeHash, Integer chunkIndex, AppearancePatentResultRowDto row) { public AppearancePatentTaskCacheService(StringRedisTemplate stringRedisTemplate,
if (taskId == null || taskId <= 0 || scopeHash == null || scopeHash.isBlank() || chunkIndex == null || row == null) { ObjectMapper objectMapper) {
return; super("appearance-patent", AppearancePatentResultRowDto.class, stringRedisTemplate, objectMapper);
}
try {
stringRedisTemplate.opsForList().rightPush(
pendingRowsKey(taskId),
objectMapper.writeValueAsString(new PendingRow(scopeHash, chunkIndex, row))
);
stringRedisTemplate.expire(pendingRowsKey(taskId), Duration.ofHours(TTL_HOURS));
touchTaskHeartbeat(taskId);
} catch (Exception ignored) {
}
} }
public long pendingRowCount(Long taskId) { @Override
Long size; protected void deleteExtraTaskCacheKeys(Long taskId) {
try { stringRedisTemplate.delete(processedRowsKey(taskId));
size = stringRedisTemplate.opsForList().size(pendingRowsKey(taskId));
} catch (Exception ex) {
log.warn("[appearance-patent-cache] pending row count degraded taskId={} msg={}", taskId, ex.getMessage());
return 0L;
}
return size == null ? 0L : size;
}
public List<PendingRow> drainPendingRows(Long taskId, int limit) {
if (taskId == null || taskId <= 0 || limit <= 0) {
return List.of();
}
String key = pendingRowsKey(taskId);
List<String> values;
try {
values = stringRedisTemplate.opsForList().range(key, 0, limit - 1L);
} catch (Exception ex) {
log.warn("[appearance-patent-cache] drain range degraded taskId={} msg={}", taskId, ex.getMessage());
return List.of();
}
if (values == null || values.isEmpty()) {
return List.of();
}
try {
stringRedisTemplate.opsForList().trim(key, values.size(), -1);
} catch (Exception ex) {
log.warn("[appearance-patent-cache] drain trim degraded taskId={} msg={}", taskId, ex.getMessage());
return List.of();
}
List<PendingRow> rows = new ArrayList<>();
for (String value : values) {
if (value == null || value.isBlank()) {
continue;
}
try {
rows.add(objectMapper.readValue(value, PendingRow.class));
} catch (Exception ignored) {
}
}
touchTaskHeartbeat(taskId);
return rows;
}
public void touchTaskHeartbeat(Long taskId) {
if (taskId == null || taskId <= 0) {
return;
}
try {
stringRedisTemplate.opsForValue().set(
heartbeatKey(taskId),
String.valueOf(Instant.now().toEpochMilli()),
Duration.ofHours(TTL_HOURS)
);
} catch (Exception ex) {
log.warn("[appearance-patent-cache] touch heartbeat degraded taskId={} msg={}", taskId, ex.getMessage());
}
}
public long getTaskHeartbeatMillis(Long taskId) {
if (taskId == null || taskId <= 0) {
return 0L;
}
String raw;
try {
raw = stringRedisTemplate.opsForValue().get(heartbeatKey(taskId));
} catch (Exception ex) {
log.warn("[appearance-patent-cache] get heartbeat degraded taskId={} msg={}", taskId, ex.getMessage());
return 0L;
}
if (raw == null || raw.isBlank()) {
return 0L;
}
try {
return Long.parseLong(raw);
} catch (NumberFormatException ignored) {
return 0L;
}
}
public void deleteTaskCache(Long taskId) {
if (taskId == null || taskId <= 0) {
return;
}
try {
stringRedisTemplate.delete(pendingRowsKey(taskId));
stringRedisTemplate.delete(heartbeatKey(taskId));
stringRedisTemplate.delete(processedRowsKey(taskId));
} catch (Exception ex) {
log.warn("[appearance-patent-cache] delete degraded taskId={} msg={}", taskId, ex.getMessage());
}
} }
/** /**
@@ -277,15 +176,4 @@ public class AppearancePatentTaskCacheService {
private String processedRowKey(Long taskId, String rowKey) { private String processedRowKey(Long taskId, String rowKey) {
return processedRowsKey(taskId) + ":" + rowKey; return processedRowsKey(taskId) + ":" + rowKey;
} }
private String pendingRowsKey(Long taskId) {
return "appearance-patent:task:pending-rows:" + taskId;
}
private String heartbeatKey(Long taskId) {
return "appearance-patent:task:heartbeat:" + taskId;
}
public record PendingRow(String scopeHash, Integer chunkIndex, AppearancePatentResultRowDto row) {
}
} }
@@ -14,9 +14,9 @@ import com.nanri.aiimage.modules.patroldelete.service.PatrolDeleteResolveService
import com.nanri.aiimage.modules.patroldelete.service.PatrolDeleteTaskService; import com.nanri.aiimage.modules.patroldelete.service.PatrolDeleteTaskService;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest;
import com.nanri.aiimage.common.model.vo.ProductRiskCandidateVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCandidateVo;
import com.nanri.aiimage.common.model.vo.ProductRiskDashboardVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskDashboardVo;
import com.nanri.aiimage.common.model.vo.ProductRiskMatchShopsVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskMatchShopsVo;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn; import io.swagger.v3.oas.annotations.enums.ParameterIn;
@@ -10,9 +10,9 @@ import com.nanri.aiimage.modules.patroldelete.model.entity.PatrolDeleteShopCandi
import com.nanri.aiimage.modules.patroldelete.model.vo.PatrolDeleteConditionVo; import com.nanri.aiimage.modules.patroldelete.model.vo.PatrolDeleteConditionVo;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest;
import com.nanri.aiimage.common.model.vo.ProductRiskCandidateVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCandidateVo;
import com.nanri.aiimage.common.model.vo.ProductRiskMatchShopsVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskMatchShopsVo;
import com.nanri.aiimage.common.model.vo.ProductRiskShopQueueItemVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskShopQueueItemVo;
import com.nanri.aiimage.modules.ziniao.model.vo.ZiniaoShopMatchResultVo; import com.nanri.aiimage.modules.ziniao.model.vo.ZiniaoShopMatchResultVo;
import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopIndexService; import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopIndexService;
import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopSwitchService; import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopSwitchService;
@@ -1,216 +1,25 @@
package com.nanri.aiimage.modules.patroldelete.service; package com.nanri.aiimage.modules.patroldelete.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.modules.patroldelete.model.dto.PatrolDeleteShopPayloadDto; import com.nanri.aiimage.modules.patroldelete.model.dto.PatrolDeleteShopPayloadDto;
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity; import com.nanri.aiimage.modules.task.service.AbstractModuleTaskCacheService;
import com.nanri.aiimage.modules.task.service.TaskScopePayloadStorageService; import com.nanri.aiimage.modules.task.service.TaskScopePayloadStorageService;
import com.nanri.aiimage.modules.task.util.TaskEntityLocalCache; import com.nanri.aiimage.modules.task.util.TaskEntityLocalCache;
import lombok.RequiredArgsConstructor; import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.Duration; /**
import java.time.Instant; * 巡检删除模块任务缓存服务:通用实现见 {@link AbstractModuleTaskCacheService},
import java.util.ArrayList; * 本类只声明模块差异(moduleType / Redis key 前缀 / 载荷类型)。
import java.util.LinkedHashMap; */
import java.util.List;
import java.util.Map;
@Service @Service
@RequiredArgsConstructor public class PatrolDeleteTaskCacheService extends AbstractModuleTaskCacheService<PatrolDeleteShopPayloadDto> {
@Slf4j
public class PatrolDeleteTaskCacheService {
private static final String MODULE_TYPE = "PATROL_DELETE"; public PatrolDeleteTaskCacheService(StringRedisTemplate stringRedisTemplate,
private static final long PAYLOAD_TTL_HOURS = 24; ObjectMapper objectMapper,
private final StringRedisTemplate stringRedisTemplate; TaskScopePayloadStorageService taskScopePayloadStorageService,
private final ObjectMapper objectMapper; TaskEntityLocalCache taskEntityLocalCache) {
private final TaskScopePayloadStorageService taskScopePayloadStorageService; super("PATROL_DELETE", "patrol-delete", PatrolDeleteShopPayloadDto.class,
private final TaskEntityLocalCache taskEntityLocalCache; stringRedisTemplate, objectMapper, taskScopePayloadStorageService, taskEntityLocalCache);
public PatrolDeleteShopPayloadDto getShopMergedPayload(Long taskId, String shopKey) {
return taskScopePayloadStorageService.getScopePayload(taskId, MODULE_TYPE, shopKey, PatrolDeleteShopPayloadDto.class);
}
public void saveShopMergedPayload(Long taskId, String shopKey, PatrolDeleteShopPayloadDto payload) {
if (taskId == null || taskId <= 0 || shopKey == null || shopKey.isBlank() || payload == null) {
return;
}
taskScopePayloadStorageService.saveScopePayload(taskId, MODULE_TYPE, shopKey, payload);
touchTaskHeartbeat(taskId);
}
public void removeShopMergedPayload(Long taskId, String shopKey) {
taskScopePayloadStorageService.removeScopePayload(taskId, MODULE_TYPE, shopKey);
}
public Map<String, PatrolDeleteShopPayloadDto> getAllShopMergedPayload(Long taskId) {
return taskScopePayloadStorageService.getAllScopePayload(taskId, MODULE_TYPE, PatrolDeleteShopPayloadDto.class);
}
public boolean hasAnyShopMergedPayload(Long taskId) {
return taskScopePayloadStorageService.hasAnyScopePayload(taskId, MODULE_TYPE);
}
public long countShopMergedPayload(Long taskId) {
return taskScopePayloadStorageService.countScopePayload(taskId, MODULE_TYPE);
}
public long getTaskHeartbeatMillis(Long taskId) {
if (taskId == null || taskId <= 0) {
return 0L;
}
String raw;
try {
raw = stringRedisTemplate.opsForValue().get(buildTaskHeartbeatKey(taskId));
} catch (Exception ex) {
log.warn("[patrol-delete-cache] get heartbeat degraded taskId={} msg={}", taskId, ex.getMessage());
return 0L;
}
if (raw == null || raw.isBlank()) {
return 0L;
}
try {
return Long.parseLong(raw);
} catch (NumberFormatException ignored) {
return 0L;
}
}
public Map<Long, Long> getTaskHeartbeatMillisBatch(List<Long> taskIds) {
Map<Long, Long> result = new LinkedHashMap<>();
if (taskIds == null || taskIds.isEmpty()) {
return result;
}
List<Long> normalized = taskIds.stream()
.filter(id -> id != null && id > 0)
.distinct()
.toList();
if (normalized.isEmpty()) {
return result;
}
List<String> keys = normalized.stream().map(this::buildTaskHeartbeatKey).toList();
List<String> values;
try {
values = stringRedisTemplate.opsForValue().multiGet(keys);
} catch (Exception ex) {
log.warn("[patrol-delete-cache] batch get heartbeat degraded taskIds={} msg={}", normalized, ex.getMessage());
return result;
}
for (int i = 0; i < normalized.size(); i++) {
String raw = values != null && i < values.size() ? values.get(i) : null;
if (raw == null || raw.isBlank()) {
result.put(normalized.get(i), 0L);
continue;
}
try {
result.put(normalized.get(i), Long.parseLong(raw));
} catch (NumberFormatException ignored) {
result.put(normalized.get(i), 0L);
}
}
return result;
}
public void touchTaskHeartbeat(Long taskId) {
try {
stringRedisTemplate.opsForValue().set(
buildTaskHeartbeatKey(taskId),
String.valueOf(Instant.now().toEpochMilli()),
Duration.ofHours(PAYLOAD_TTL_HOURS));
} catch (Exception ex) {
log.warn("[patrol-delete-cache] touch heartbeat degraded taskId={} msg={}", taskId, ex.getMessage());
}
}
public void deleteTaskCache(Long taskId) {
evictTaskCacheOnly(taskId);
taskScopePayloadStorageService.deleteTaskScopePayloads(taskId, MODULE_TYPE);
}
public void evictTaskCacheOnly(Long taskId) {
if (taskId == null || taskId <= 0) {
return;
}
taskEntityLocalCache.evict(taskId);
try {
stringRedisTemplate.delete(buildTaskHeartbeatKey(taskId));
stringRedisTemplate.delete(buildTaskEntityKey(taskId));
} catch (Exception ex) {
log.warn("[patrol-delete-cache] delete cache degraded taskId={} msg={}", taskId, ex.getMessage());
}
}
public void saveTaskCache(FileTaskEntity task) {
if (task == null || task.getId() == null) {
return;
}
taskEntityLocalCache.put(task.getId(), task);
try {
stringRedisTemplate.opsForValue().set(
buildTaskEntityKey(task.getId()),
objectMapper.writeValueAsString(task),
Duration.ofHours(PAYLOAD_TTL_HOURS)
);
} catch (Exception ignored) {
}
}
public Map<Long, FileTaskEntity> getTaskCacheBatch(java.util.List<Long> taskIds) {
Map<Long, FileTaskEntity> result = new LinkedHashMap<>();
if (taskIds == null || taskIds.isEmpty()) {
return result;
}
java.util.List<Long> normalized = taskIds.stream()
.filter(id -> id != null && id > 0)
.distinct()
.toList();
if (normalized.isEmpty()) {
return result;
}
long now = System.currentTimeMillis();
java.util.List<Long> missingIds = new ArrayList<>();
for (Long taskId : normalized) {
TaskEntityLocalCache.Entry cached = taskEntityLocalCache.get(taskId, now);
if (cached != null) {
result.put(taskId, cached.task());
} else {
missingIds.add(taskId);
}
}
if (missingIds.isEmpty()) {
return result;
}
java.util.List<String> keys = missingIds.stream().map(this::buildTaskEntityKey).toList();
java.util.List<String> values;
try {
values = stringRedisTemplate.opsForValue().multiGet(keys);
} catch (Exception ex) {
log.warn("[patrol-delete-cache] batch get task cache degraded taskIds={} msg={}", missingIds, ex.getMessage());
return result;
}
for (int i = 0; i < missingIds.size(); i++) {
Long taskId = missingIds.get(i);
String val = values != null && i < values.size() ? values.get(i) : null;
if (val == null || val.isBlank()) {
continue;
}
try {
FileTaskEntity task = objectMapper.readValue(val, FileTaskEntity.class);
result.put(taskId, task);
taskEntityLocalCache.put(taskId, task);
} catch (Exception ignored) {
}
}
return result;
}
private String buildTaskHeartbeatKey(Long taskId) {
return "patrol-delete:task:heartbeat:" + taskId;
}
private String buildTaskEntityKey(Long taskId) {
return "patrol-delete:task:entity:" + taskId;
} }
} }
@@ -22,7 +22,7 @@ import com.nanri.aiimage.modules.patroldelete.model.vo.PatrolDeleteTaskBatchVo;
import com.nanri.aiimage.modules.patroldelete.model.vo.PatrolDeleteCreateTaskVo; import com.nanri.aiimage.modules.patroldelete.model.vo.PatrolDeleteCreateTaskVo;
import com.nanri.aiimage.modules.patroldelete.model.vo.PatrolDeleteHistoryVo; import com.nanri.aiimage.modules.patroldelete.model.vo.PatrolDeleteHistoryVo;
import com.nanri.aiimage.modules.patroldelete.model.vo.PatrolDeleteResultItemVo; import com.nanri.aiimage.modules.patroldelete.model.vo.PatrolDeleteResultItemVo;
import com.nanri.aiimage.common.model.vo.ProductRiskDashboardVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskDashboardVo;
import com.nanri.aiimage.modules.task.mapper.FileResultMapper; import com.nanri.aiimage.modules.task.mapper.FileResultMapper;
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper; import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
import com.nanri.aiimage.modules.task.model.entity.FileResultEntity; import com.nanri.aiimage.modules.task.model.entity.FileResultEntity;
@@ -1,211 +1,25 @@
package com.nanri.aiimage.modules.pricetrack.service; package com.nanri.aiimage.modules.pricetrack.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.modules.pricetrack.model.dto.PriceTrackSubmitResultRequest; import com.nanri.aiimage.modules.pricetrack.model.dto.PriceTrackSubmitResultRequest;
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity; import com.nanri.aiimage.modules.task.service.AbstractModuleTaskCacheService;
import com.nanri.aiimage.modules.task.service.TaskScopePayloadStorageService; import com.nanri.aiimage.modules.task.service.TaskScopePayloadStorageService;
import com.nanri.aiimage.modules.task.util.TaskEntityLocalCache; import com.nanri.aiimage.modules.task.util.TaskEntityLocalCache;
import lombok.RequiredArgsConstructor; import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.Duration; /**
import java.time.Instant; * 跟价模块任务缓存服务:通用实现见 {@link AbstractModuleTaskCacheService},
import java.util.ArrayList; * 本类只声明模块差异(moduleType / Redis key 前缀 / 载荷类型)。
import java.util.LinkedHashMap; */
import java.util.List;
import java.util.Map;
@Service @Service
@RequiredArgsConstructor public class PriceTrackTaskCacheService extends AbstractModuleTaskCacheService<PriceTrackSubmitResultRequest.ShopResult> {
@Slf4j
public class PriceTrackTaskCacheService {
private static final String MODULE_TYPE = "PRICE_TRACK"; public PriceTrackTaskCacheService(StringRedisTemplate stringRedisTemplate,
private static final long HEARTBEAT_TTL_HOURS = 24; ObjectMapper objectMapper,
private final StringRedisTemplate stringRedisTemplate; TaskScopePayloadStorageService taskScopePayloadStorageService,
private final ObjectMapper objectMapper; TaskEntityLocalCache taskEntityLocalCache) {
private final TaskScopePayloadStorageService taskScopePayloadStorageService; super("PRICE_TRACK", "price-track", PriceTrackSubmitResultRequest.ShopResult.class,
private final TaskEntityLocalCache taskEntityLocalCache; stringRedisTemplate, objectMapper, taskScopePayloadStorageService, taskEntityLocalCache);
public void touchTaskHeartbeat(Long taskId) {
if (taskId == null || taskId <= 0) {
return;
}
try {
stringRedisTemplate.opsForValue().set(
buildTaskHeartbeatKey(taskId),
String.valueOf(Instant.now().toEpochMilli()),
Duration.ofHours(HEARTBEAT_TTL_HOURS));
} catch (Exception ex) {
log.warn("[price-track-cache] touch heartbeat degraded taskId={} msg={}", taskId, ex.getMessage());
}
}
public long getTaskHeartbeatMillis(Long taskId) {
if (taskId == null || taskId <= 0) {
return 0L;
}
String raw;
try {
raw = stringRedisTemplate.opsForValue().get(buildTaskHeartbeatKey(taskId));
} catch (Exception ex) {
log.warn("[price-track-cache] get heartbeat degraded taskId={} msg={}", taskId, ex.getMessage());
return 0L;
}
if (raw == null || raw.isBlank()) {
return 0L;
}
try {
return Long.parseLong(raw);
} catch (NumberFormatException ignored) {
return 0L;
}
}
public Map<Long, Long> getTaskHeartbeatMillisBatch(List<Long> taskIds) {
Map<Long, Long> result = new LinkedHashMap<>();
if (taskIds == null || taskIds.isEmpty()) {
return result;
}
List<Long> normalized = taskIds.stream()
.filter(id -> id != null && id > 0)
.distinct()
.toList();
if (normalized.isEmpty()) {
return result;
}
List<String> keys = normalized.stream().map(this::buildTaskHeartbeatKey).toList();
List<String> values;
try {
values = stringRedisTemplate.opsForValue().multiGet(keys);
} catch (Exception ex) {
log.warn("[price-track-cache] batch get heartbeat degraded taskIds={} msg={}", normalized, ex.getMessage());
return result;
}
for (int i = 0; i < normalized.size(); i++) {
String raw = values != null && i < values.size() ? values.get(i) : null;
if (raw == null || raw.isBlank()) {
result.put(normalized.get(i), 0L);
continue;
}
try {
result.put(normalized.get(i), Long.parseLong(raw));
} catch (NumberFormatException ignored) {
result.put(normalized.get(i), 0L);
}
}
return result;
}
public PriceTrackSubmitResultRequest.ShopResult getShopMergedPayload(Long taskId, String shopKey) {
return taskScopePayloadStorageService.getScopePayload(taskId, MODULE_TYPE, shopKey, PriceTrackSubmitResultRequest.ShopResult.class);
}
public void saveShopMergedPayload(Long taskId, String shopKey, PriceTrackSubmitResultRequest.ShopResult payload) {
if (taskId == null || taskId <= 0 || shopKey == null || shopKey.isBlank() || payload == null) {
return;
}
taskScopePayloadStorageService.saveScopePayload(taskId, MODULE_TYPE, shopKey, payload);
touchTaskHeartbeat(taskId);
}
public void removeShopMergedPayload(Long taskId, String shopKey) {
taskScopePayloadStorageService.removeScopePayload(taskId, MODULE_TYPE, shopKey);
}
public Map<String, PriceTrackSubmitResultRequest.ShopResult> getAllShopMergedPayload(Long taskId) {
return taskScopePayloadStorageService.getAllScopePayload(taskId, MODULE_TYPE, PriceTrackSubmitResultRequest.ShopResult.class);
}
public boolean hasAnyShopMergedPayload(Long taskId) {
return taskScopePayloadStorageService.hasAnyScopePayload(taskId, MODULE_TYPE);
}
public void deleteTaskCache(Long taskId) {
if (taskId == null || taskId <= 0) {
return;
}
taskEntityLocalCache.evict(taskId);
try {
stringRedisTemplate.delete(buildTaskHeartbeatKey(taskId));
stringRedisTemplate.delete(buildTaskEntityKey(taskId));
} catch (Exception ex) {
log.warn("[price-track-cache] delete cache degraded taskId={} msg={}", taskId, ex.getMessage());
}
taskScopePayloadStorageService.deleteTaskScopePayloads(taskId, MODULE_TYPE);
}
public void saveTaskCache(FileTaskEntity task) {
if (task == null || task.getId() == null) {
return;
}
taskEntityLocalCache.put(task.getId(), task);
try {
stringRedisTemplate.opsForValue().set(
buildTaskEntityKey(task.getId()),
objectMapper.writeValueAsString(task),
Duration.ofHours(HEARTBEAT_TTL_HOURS)
);
} catch (Exception ignored) {
}
}
public Map<Long, FileTaskEntity> getTaskCacheBatch(java.util.List<Long> taskIds) {
Map<Long, FileTaskEntity> result = new LinkedHashMap<>();
if (taskIds == null || taskIds.isEmpty()) {
return result;
}
java.util.List<Long> normalized = taskIds.stream()
.filter(id -> id != null && id > 0)
.distinct()
.toList();
if (normalized.isEmpty()) {
return result;
}
long now = System.currentTimeMillis();
java.util.List<Long> missingIds = new ArrayList<>();
for (Long taskId : normalized) {
TaskEntityLocalCache.Entry cached = taskEntityLocalCache.get(taskId, now);
if (cached != null) {
result.put(taskId, cached.task());
} else {
missingIds.add(taskId);
}
}
if (missingIds.isEmpty()) {
return result;
}
java.util.List<String> keys = missingIds.stream().map(this::buildTaskEntityKey).toList();
java.util.List<String> values;
try {
values = stringRedisTemplate.opsForValue().multiGet(keys);
} catch (Exception ex) {
log.warn("[price-track-cache] batch get task cache degraded taskIds={} msg={}", missingIds, ex.getMessage());
return result;
}
for (int i = 0; i < missingIds.size(); i++) {
Long taskId = missingIds.get(i);
String val = values != null && i < values.size() ? values.get(i) : null;
if (val == null || val.isBlank()) {
continue;
}
try {
FileTaskEntity task = objectMapper.readValue(val, FileTaskEntity.class);
result.put(taskId, task);
taskEntityLocalCache.put(taskId, task);
} catch (Exception ignored) {
}
}
return result;
}
private String buildTaskHeartbeatKey(Long taskId) {
return "price-track:task:heartbeat:" + taskId;
}
private String buildTaskEntityKey(Long taskId) {
return "price-track:task:entity:" + taskId;
} }
} }
@@ -9,14 +9,14 @@ import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCreateTaskRequ
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskSubmitResultRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskSubmitResultRequest;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskTaskBatchRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskTaskBatchRequest;
import com.nanri.aiimage.common.model.vo.ProductRiskCandidateVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCandidateVo;
import com.nanri.aiimage.common.model.vo.ProductRiskCountryPreferenceVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCountryPreferenceVo;
import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCreateTaskVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCreateTaskVo;
import com.nanri.aiimage.common.model.vo.ProductRiskDashboardVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskDashboardVo;
import com.nanri.aiimage.common.model.vo.ProductRiskHistoryVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskHistoryVo;
import com.nanri.aiimage.common.model.vo.ProductRiskMatchShopsVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskMatchShopsVo;
import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskPendingDeleteVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskPendingDeleteVo;
import com.nanri.aiimage.common.model.vo.ProductRiskTaskBatchVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskTaskBatchVo;
import com.nanri.aiimage.modules.productrisk.service.ProductRiskResolveService; import com.nanri.aiimage.modules.productrisk.service.ProductRiskResolveService;
import com.nanri.aiimage.modules.productrisk.service.ProductRiskTaskService; import com.nanri.aiimage.modules.productrisk.service.ProductRiskTaskService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
@@ -1,7 +1,7 @@
package com.nanri.aiimage.modules.productrisk.model.dto; package com.nanri.aiimage.modules.productrisk.model.dto;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.nanri.aiimage.common.model.vo.ProductRiskShopQueueItemVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskShopQueueItemVo;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotEmpty;
@@ -1,4 +1,4 @@
package com.nanri.aiimage.common.model.vo; package com.nanri.aiimage.modules.productrisk.model.vo;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
@@ -1,4 +1,4 @@
package com.nanri.aiimage.common.model.vo; package com.nanri.aiimage.modules.productrisk.model.vo;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
@@ -5,7 +5,7 @@ import lombok.Data;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.nanri.aiimage.common.model.vo.ProductRiskResultItemVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskResultItemVo;
@Data @Data
@Schema(description = "创建任务响应:含任务 id 与各店初始快照(含 resultId),用于推队列与前端展示") @Schema(description = "创建任务响应:含任务 id 与各店初始快照(含 resultId),用于推队列与前端展示")
@@ -1,4 +1,4 @@
package com.nanri.aiimage.common.model.vo; package com.nanri.aiimage.modules.productrisk.model.vo;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
@@ -1,4 +1,4 @@
package com.nanri.aiimage.common.model.vo; package com.nanri.aiimage.modules.productrisk.model.vo;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;
@@ -1,4 +1,4 @@
package com.nanri.aiimage.common.model.vo; package com.nanri.aiimage.modules.productrisk.model.vo;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;
@@ -1,4 +1,4 @@
package com.nanri.aiimage.common.model.vo; package com.nanri.aiimage.modules.productrisk.model.vo;
import com.fasterxml.jackson.annotation.JsonAlias; import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
@@ -1,4 +1,4 @@
package com.nanri.aiimage.common.model.vo; package com.nanri.aiimage.modules.productrisk.model.vo;
import com.fasterxml.jackson.annotation.JsonAlias; import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
@@ -1,4 +1,4 @@
package com.nanri.aiimage.common.model.vo; package com.nanri.aiimage.modules.productrisk.model.vo;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;
@@ -1,4 +1,4 @@
package com.nanri.aiimage.common.model.vo; package com.nanri.aiimage.modules.productrisk.model.vo;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;
@@ -1,4 +1,4 @@
package com.nanri.aiimage.common.model.vo; package com.nanri.aiimage.modules.productrisk.model.vo;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.nanri.aiimage.modules.shopmatch.model.vo.ShopMatchTaskStageVo; import com.nanri.aiimage.modules.shopmatch.model.vo.ShopMatchTaskStageVo;
@@ -12,10 +12,10 @@ import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequ
import com.nanri.aiimage.modules.productrisk.model.entity.ProductRiskCountryPrefEntity; import com.nanri.aiimage.modules.productrisk.model.entity.ProductRiskCountryPrefEntity;
import com.nanri.aiimage.modules.productrisk.model.entity.ProductRiskShopCandidateEntity; import com.nanri.aiimage.modules.productrisk.model.entity.ProductRiskShopCandidateEntity;
import com.nanri.aiimage.modules.productrisk.model.enums.ProductRiskCountryCode; import com.nanri.aiimage.modules.productrisk.model.enums.ProductRiskCountryCode;
import com.nanri.aiimage.common.model.vo.ProductRiskCandidateVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCandidateVo;
import com.nanri.aiimage.common.model.vo.ProductRiskCountryPreferenceVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCountryPreferenceVo;
import com.nanri.aiimage.common.model.vo.ProductRiskMatchShopsVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskMatchShopsVo;
import com.nanri.aiimage.common.model.vo.ProductRiskShopQueueItemVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskShopQueueItemVo;
import com.nanri.aiimage.modules.ziniao.model.vo.ZiniaoShopMatchResultVo; import com.nanri.aiimage.modules.ziniao.model.vo.ZiniaoShopMatchResultVo;
import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopIndexService; import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopIndexService;
import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopSwitchService; import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopSwitchService;
@@ -1,212 +1,25 @@
package com.nanri.aiimage.modules.productrisk.service; package com.nanri.aiimage.modules.productrisk.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskShopPayloadDto; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskShopPayloadDto;
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity; import com.nanri.aiimage.modules.task.service.AbstractModuleTaskCacheService;
import com.nanri.aiimage.modules.task.service.TaskScopePayloadStorageService; import com.nanri.aiimage.modules.task.service.TaskScopePayloadStorageService;
import com.nanri.aiimage.modules.task.util.TaskEntityLocalCache; import com.nanri.aiimage.modules.task.util.TaskEntityLocalCache;
import lombok.RequiredArgsConstructor; import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.Duration; /**
import java.time.Instant; * 品牌风险模块任务缓存服务:通用实现见 {@link AbstractModuleTaskCacheService},
import java.util.ArrayList; * 本类只声明模块差异(moduleType / Redis key 前缀 / 载荷类型)。
import java.util.LinkedHashMap; */
import java.util.List;
import java.util.Map;
@Service @Service
@RequiredArgsConstructor public class ProductRiskTaskCacheService extends AbstractModuleTaskCacheService<ProductRiskShopPayloadDto> {
@Slf4j
public class ProductRiskTaskCacheService {
private static final String MODULE_TYPE = "PRODUCT_RISK_RESOLVE"; public ProductRiskTaskCacheService(StringRedisTemplate stringRedisTemplate,
private static final long PAYLOAD_TTL_HOURS = 24; ObjectMapper objectMapper,
private final StringRedisTemplate stringRedisTemplate; TaskScopePayloadStorageService taskScopePayloadStorageService,
private final ObjectMapper objectMapper; TaskEntityLocalCache taskEntityLocalCache) {
private final TaskScopePayloadStorageService taskScopePayloadStorageService; super("PRODUCT_RISK_RESOLVE", "product-risk", ProductRiskShopPayloadDto.class,
private final TaskEntityLocalCache taskEntityLocalCache; stringRedisTemplate, objectMapper, taskScopePayloadStorageService, taskEntityLocalCache);
public ProductRiskShopPayloadDto getShopMergedPayload(Long taskId, String shopKey) {
return taskScopePayloadStorageService.getScopePayload(taskId, MODULE_TYPE, shopKey, ProductRiskShopPayloadDto.class);
}
public void saveShopMergedPayload(Long taskId, String shopKey, ProductRiskShopPayloadDto payload) {
if (taskId == null || taskId <= 0 || shopKey == null || shopKey.isBlank() || payload == null) {
return;
}
taskScopePayloadStorageService.saveScopePayload(taskId, MODULE_TYPE, shopKey, payload);
touchTaskHeartbeat(taskId);
}
public void removeShopMergedPayload(Long taskId, String shopKey) {
taskScopePayloadStorageService.removeScopePayload(taskId, MODULE_TYPE, shopKey);
}
public Map<String, ProductRiskShopPayloadDto> getAllShopMergedPayload(Long taskId) {
return taskScopePayloadStorageService.getAllScopePayload(taskId, MODULE_TYPE, ProductRiskShopPayloadDto.class);
}
public boolean hasAnyShopMergedPayload(Long taskId) {
return taskScopePayloadStorageService.hasAnyScopePayload(taskId, MODULE_TYPE);
}
public long countShopMergedPayload(Long taskId) {
return taskScopePayloadStorageService.countScopePayload(taskId, MODULE_TYPE);
}
public long getTaskHeartbeatMillis(Long taskId) {
if (taskId == null || taskId <= 0) {
return 0L;
}
String raw;
try {
raw = stringRedisTemplate.opsForValue().get(buildTaskHeartbeatKey(taskId));
} catch (Exception ex) {
log.warn("[product-risk-cache] get heartbeat degraded taskId={} msg={}", taskId, ex.getMessage());
return 0L;
}
if (raw == null || raw.isBlank()) {
return 0L;
}
try {
return Long.parseLong(raw);
} catch (NumberFormatException ignored) {
return 0L;
}
}
public Map<Long, Long> getTaskHeartbeatMillisBatch(List<Long> taskIds) {
Map<Long, Long> result = new LinkedHashMap<>();
if (taskIds == null || taskIds.isEmpty()) {
return result;
}
List<Long> normalized = taskIds.stream()
.filter(id -> id != null && id > 0)
.distinct()
.toList();
if (normalized.isEmpty()) {
return result;
}
List<String> keys = normalized.stream().map(this::buildTaskHeartbeatKey).toList();
List<String> values;
try {
values = stringRedisTemplate.opsForValue().multiGet(keys);
} catch (Exception ex) {
log.warn("[product-risk-cache] batch get heartbeat degraded taskIds={} msg={}", normalized, ex.getMessage());
return result;
}
for (int i = 0; i < normalized.size(); i++) {
String raw = values != null && i < values.size() ? values.get(i) : null;
if (raw == null || raw.isBlank()) {
result.put(normalized.get(i), 0L);
continue;
}
try {
result.put(normalized.get(i), Long.parseLong(raw));
} catch (NumberFormatException ignored) {
result.put(normalized.get(i), 0L);
}
}
return result;
}
public void deleteTaskCache(Long taskId) {
if (taskId == null || taskId <= 0) {
return;
}
taskEntityLocalCache.evict(taskId);
try {
stringRedisTemplate.delete(buildTaskHeartbeatKey(taskId));
stringRedisTemplate.delete(buildTaskEntityKey(taskId));
} catch (Exception ex) {
log.warn("[product-risk-cache] delete cache degraded taskId={} msg={}", taskId, ex.getMessage());
}
taskScopePayloadStorageService.deleteTaskScopePayloads(taskId, MODULE_TYPE);
}
public void saveTaskCache(FileTaskEntity task) {
if (task == null || task.getId() == null) {
return;
}
taskEntityLocalCache.put(task.getId(), task);
try {
stringRedisTemplate.opsForValue().set(
buildTaskEntityKey(task.getId()),
objectMapper.writeValueAsString(task),
Duration.ofHours(PAYLOAD_TTL_HOURS)
);
} catch (Exception ignored) {
}
}
public Map<Long, FileTaskEntity> getTaskCacheBatch(java.util.List<Long> taskIds) {
Map<Long, FileTaskEntity> result = new LinkedHashMap<>();
if (taskIds == null || taskIds.isEmpty()) {
return result;
}
java.util.List<Long> normalized = taskIds.stream()
.filter(id -> id != null && id > 0)
.distinct()
.toList();
if (normalized.isEmpty()) {
return result;
}
long now = System.currentTimeMillis();
java.util.List<Long> missingIds = new ArrayList<>();
for (Long taskId : normalized) {
TaskEntityLocalCache.Entry cached = taskEntityLocalCache.get(taskId, now);
if (cached != null) {
result.put(taskId, cached.task());
} else {
missingIds.add(taskId);
}
}
if (missingIds.isEmpty()) {
return result;
}
java.util.List<String> keys = missingIds.stream().map(this::buildTaskEntityKey).toList();
java.util.List<String> values;
try {
values = stringRedisTemplate.opsForValue().multiGet(keys);
} catch (Exception ex) {
log.warn("[product-risk-cache] batch get task cache degraded taskIds={} msg={}", missingIds, ex.getMessage());
return result;
}
for (int i = 0; i < missingIds.size(); i++) {
Long taskId = missingIds.get(i);
String val = values != null && i < values.size() ? values.get(i) : null;
if (val == null || val.isBlank()) {
continue;
}
try {
FileTaskEntity task = objectMapper.readValue(val, FileTaskEntity.class);
result.put(taskId, task);
taskEntityLocalCache.put(taskId, task);
} catch (Exception ignored) {
}
}
return result;
}
public void touchTaskHeartbeat(Long taskId) {
try {
stringRedisTemplate.opsForValue().set(
buildTaskHeartbeatKey(taskId),
String.valueOf(Instant.now().toEpochMilli()),
Duration.ofHours(PAYLOAD_TTL_HOURS));
} catch (Exception ex) {
log.warn("[product-risk-cache] touch heartbeat degraded taskId={} msg={}", taskId, ex.getMessage());
}
}
private String buildTaskHeartbeatKey(Long taskId) {
return "product-risk:task:heartbeat:" + taskId;
}
private String buildTaskEntityKey(Long taskId) {
return "product-risk:task:entity:" + taskId;
} }
} }
@@ -14,14 +14,14 @@ import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskShopPayloadDto
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskSubmitResultRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskSubmitResultRequest;
import com.nanri.aiimage.modules.productrisk.model.entity.ProductRiskShopCandidateEntity; import com.nanri.aiimage.modules.productrisk.model.entity.ProductRiskShopCandidateEntity;
import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCreateTaskVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCreateTaskVo;
import com.nanri.aiimage.common.model.vo.ProductRiskDashboardVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskDashboardVo;
import com.nanri.aiimage.common.model.vo.ProductRiskHistoryVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskHistoryVo;
import com.nanri.aiimage.common.model.vo.ProductRiskResultItemVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskResultItemVo;
import com.nanri.aiimage.common.model.vo.ProductRiskTaskBatchVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskTaskBatchVo;
import com.nanri.aiimage.common.model.vo.ProductRiskTaskDetailVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskTaskDetailVo;
import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskPendingDeleteVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskPendingDeleteVo;
import com.nanri.aiimage.common.model.vo.ProductRiskTaskItemVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskTaskItemVo;
import com.nanri.aiimage.common.model.vo.ProductRiskShopQueueItemVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskShopQueueItemVo;
import com.nanri.aiimage.modules.task.mapper.FileResultMapper; import com.nanri.aiimage.modules.task.mapper.FileResultMapper;
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper; import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
import com.nanri.aiimage.modules.task.model.entity.FileResultEntity; import com.nanri.aiimage.modules.task.model.entity.FileResultEntity;
@@ -4,9 +4,9 @@ import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.common.security.AdminAuthSupport; import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest;
import com.nanri.aiimage.common.model.vo.ProductRiskCandidateVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCandidateVo;
import com.nanri.aiimage.common.model.vo.ProductRiskDashboardVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskDashboardVo;
import com.nanri.aiimage.common.model.vo.ProductRiskMatchShopsVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskMatchShopsVo;
import com.nanri.aiimage.modules.queryasin.model.dto.QueryAsinCreateTaskRequest; import com.nanri.aiimage.modules.queryasin.model.dto.QueryAsinCreateTaskRequest;
import com.nanri.aiimage.modules.queryasin.model.dto.QueryAsinSubmitResultRequest; import com.nanri.aiimage.modules.queryasin.model.dto.QueryAsinSubmitResultRequest;
import com.nanri.aiimage.modules.queryasin.model.dto.QueryAsinTaskBatchRequest; import com.nanri.aiimage.modules.queryasin.model.dto.QueryAsinTaskBatchRequest;
@@ -4,9 +4,9 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nanri.aiimage.common.exception.BusinessException; import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest;
import com.nanri.aiimage.common.model.vo.ProductRiskCandidateVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCandidateVo;
import com.nanri.aiimage.common.model.vo.ProductRiskMatchShopsVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskMatchShopsVo;
import com.nanri.aiimage.common.model.vo.ProductRiskShopQueueItemVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskShopQueueItemVo;
import com.nanri.aiimage.modules.queryasin.mapper.QueryAsinShopCandidateMapper; import com.nanri.aiimage.modules.queryasin.mapper.QueryAsinShopCandidateMapper;
import com.nanri.aiimage.modules.queryasin.model.dto.QueryAsinCountryAsinsDto; import com.nanri.aiimage.modules.queryasin.model.dto.QueryAsinCountryAsinsDto;
import com.nanri.aiimage.modules.queryasin.model.entity.QueryAsinShopCandidateEntity; import com.nanri.aiimage.modules.queryasin.model.entity.QueryAsinShopCandidateEntity;
@@ -1,213 +1,25 @@
package com.nanri.aiimage.modules.queryasin.service; package com.nanri.aiimage.modules.queryasin.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.modules.queryasin.model.dto.QueryAsinShopPayloadDto; import com.nanri.aiimage.modules.queryasin.model.dto.QueryAsinShopPayloadDto;
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity; import com.nanri.aiimage.modules.task.service.AbstractModuleTaskCacheService;
import com.nanri.aiimage.modules.task.service.TaskScopePayloadStorageService; import com.nanri.aiimage.modules.task.service.TaskScopePayloadStorageService;
import com.nanri.aiimage.modules.task.util.TaskEntityLocalCache; import com.nanri.aiimage.modules.task.util.TaskEntityLocalCache;
import lombok.RequiredArgsConstructor; import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.Duration; /**
import java.time.Instant; * 查询ASIN 模块任务缓存服务:通用实现见 {@link AbstractModuleTaskCacheService},
import java.util.ArrayList; * 本类只声明模块差异(moduleType / Redis key 前缀 / 载荷类型)。
import java.util.LinkedHashMap; */
import java.util.List;
import java.util.Map;
@Service @Service
@RequiredArgsConstructor public class QueryAsinTaskCacheService extends AbstractModuleTaskCacheService<QueryAsinShopPayloadDto> {
@Slf4j
public class QueryAsinTaskCacheService {
private static final String MODULE_TYPE = "QUERY_ASIN"; public QueryAsinTaskCacheService(StringRedisTemplate stringRedisTemplate,
private static final long PAYLOAD_TTL_HOURS = 24; ObjectMapper objectMapper,
private final StringRedisTemplate stringRedisTemplate; TaskScopePayloadStorageService taskScopePayloadStorageService,
private final ObjectMapper objectMapper; TaskEntityLocalCache taskEntityLocalCache) {
private final TaskScopePayloadStorageService taskScopePayloadStorageService; super("QUERY_ASIN", "query-asin", QueryAsinShopPayloadDto.class,
private final TaskEntityLocalCache taskEntityLocalCache; stringRedisTemplate, objectMapper, taskScopePayloadStorageService, taskEntityLocalCache);
public QueryAsinShopPayloadDto getShopMergedPayload(Long taskId, String shopKey) {
return taskScopePayloadStorageService.getScopePayload(taskId, MODULE_TYPE, shopKey, QueryAsinShopPayloadDto.class);
}
public void saveShopMergedPayload(Long taskId, String shopKey, QueryAsinShopPayloadDto payload) {
if (taskId == null || taskId <= 0 || shopKey == null || shopKey.isBlank() || payload == null) {
return;
}
taskScopePayloadStorageService.saveScopePayload(taskId, MODULE_TYPE, shopKey, payload);
touchTaskHeartbeat(taskId);
}
public void removeShopMergedPayload(Long taskId, String shopKey) {
taskScopePayloadStorageService.removeScopePayload(taskId, MODULE_TYPE, shopKey);
}
public Map<String, QueryAsinShopPayloadDto> getAllShopMergedPayload(Long taskId) {
return taskScopePayloadStorageService.getAllScopePayload(taskId, MODULE_TYPE, QueryAsinShopPayloadDto.class);
}
public boolean hasAnyShopMergedPayload(Long taskId) {
return taskScopePayloadStorageService.hasAnyScopePayload(taskId, MODULE_TYPE);
}
public long countShopMergedPayload(Long taskId) {
return taskScopePayloadStorageService.countScopePayload(taskId, MODULE_TYPE);
}
public long getTaskHeartbeatMillis(Long taskId) {
if (taskId == null || taskId <= 0) {
return 0L;
}
String raw;
try {
raw = stringRedisTemplate.opsForValue().get(buildTaskHeartbeatKey(taskId));
} catch (Exception ex) {
log.warn("[query-asin-cache] get heartbeat degraded taskId={} msg={}", taskId, ex.getMessage());
return 0L;
}
if (raw == null || raw.isBlank()) {
return 0L;
}
try {
return Long.parseLong(raw);
} catch (NumberFormatException ignored) {
return 0L;
}
}
public Map<Long, Long> getTaskHeartbeatMillisBatch(List<Long> taskIds) {
Map<Long, Long> result = new LinkedHashMap<>();
if (taskIds == null || taskIds.isEmpty()) {
return result;
}
List<Long> normalized = taskIds.stream()
.filter(id -> id != null && id > 0)
.distinct()
.toList();
if (normalized.isEmpty()) {
return result;
}
List<String> keys = normalized.stream().map(this::buildTaskHeartbeatKey).toList();
List<String> values;
try {
values = stringRedisTemplate.opsForValue().multiGet(keys);
} catch (Exception ex) {
log.warn("[query-asin-cache] batch get heartbeat degraded taskIds={} msg={}", normalized, ex.getMessage());
return result;
}
for (int i = 0; i < normalized.size(); i++) {
String raw = values != null && i < values.size() ? values.get(i) : null;
if (raw == null || raw.isBlank()) {
result.put(normalized.get(i), 0L);
continue;
}
try {
result.put(normalized.get(i), Long.parseLong(raw));
} catch (NumberFormatException ignored) {
result.put(normalized.get(i), 0L);
}
}
return result;
}
public void touchTaskHeartbeat(Long taskId) {
try {
stringRedisTemplate.opsForValue().set(
buildTaskHeartbeatKey(taskId),
String.valueOf(Instant.now().toEpochMilli()),
Duration.ofHours(PAYLOAD_TTL_HOURS));
} catch (Exception ex) {
log.warn("[query-asin-cache] touch heartbeat degraded taskId={} msg={}", taskId, ex.getMessage());
}
}
public void deleteTaskCache(Long taskId) {
if (taskId == null || taskId <= 0) {
return;
}
taskEntityLocalCache.evict(taskId);
try {
stringRedisTemplate.delete(buildTaskHeartbeatKey(taskId));
stringRedisTemplate.delete(buildTaskEntityKey(taskId));
} catch (Exception ex) {
log.warn("[query-asin-cache] delete cache degraded taskId={} msg={}", taskId, ex.getMessage());
}
taskScopePayloadStorageService.deleteTaskScopePayloads(taskId, MODULE_TYPE);
}
public void saveTaskCache(FileTaskEntity task) {
if (task == null || task.getId() == null) {
return;
}
taskEntityLocalCache.put(task.getId(), task);
try {
stringRedisTemplate.opsForValue().set(
buildTaskEntityKey(task.getId()),
objectMapper.writeValueAsString(task),
Duration.ofHours(PAYLOAD_TTL_HOURS)
);
} catch (Exception ignored) {
}
}
public Map<Long, FileTaskEntity> getTaskCacheBatch(java.util.List<Long> taskIds) {
Map<Long, FileTaskEntity> result = new LinkedHashMap<>();
if (taskIds == null || taskIds.isEmpty()) {
return result;
}
java.util.List<Long> normalized = taskIds.stream()
.filter(id -> id != null && id > 0)
.distinct()
.toList();
if (normalized.isEmpty()) {
return result;
}
long now = System.currentTimeMillis();
java.util.List<Long> missingIds = new ArrayList<>();
for (Long taskId : normalized) {
TaskEntityLocalCache.Entry cached = taskEntityLocalCache.get(taskId, now);
if (cached != null) {
result.put(taskId, cached.task());
} else {
missingIds.add(taskId);
}
}
if (missingIds.isEmpty()) {
return result;
}
java.util.List<String> keys = missingIds.stream().map(this::buildTaskEntityKey).toList();
java.util.List<String> values;
try {
values = stringRedisTemplate.opsForValue().multiGet(keys);
} catch (Exception ex) {
log.warn("[query-asin-cache] batch get task cache degraded taskIds={} msg={}", missingIds, ex.getMessage());
return result;
}
for (int i = 0; i < missingIds.size(); i++) {
Long taskId = missingIds.get(i);
String val = values != null && i < values.size() ? values.get(i) : null;
if (val == null || val.isBlank()) {
continue;
}
try {
FileTaskEntity task = objectMapper.readValue(val, FileTaskEntity.class);
result.put(taskId, task);
taskEntityLocalCache.put(taskId, task);
} catch (Exception ignored) {
}
}
return result;
}
private String buildTaskHeartbeatKey(Long taskId) {
return "query-asin:task:heartbeat:" + taskId;
}
private String buildTaskEntityKey(Long taskId) {
return "query-asin:task:entity:" + taskId;
} }
} }
@@ -22,7 +22,7 @@ import com.nanri.aiimage.modules.queryasin.model.vo.QueryAsinTaskBatchVo;
import com.nanri.aiimage.modules.queryasin.model.vo.QueryAsinCreateTaskVo; import com.nanri.aiimage.modules.queryasin.model.vo.QueryAsinCreateTaskVo;
import com.nanri.aiimage.modules.queryasin.model.vo.QueryAsinHistoryVo; import com.nanri.aiimage.modules.queryasin.model.vo.QueryAsinHistoryVo;
import com.nanri.aiimage.modules.queryasin.model.vo.QueryAsinResultItemVo; import com.nanri.aiimage.modules.queryasin.model.vo.QueryAsinResultItemVo;
import com.nanri.aiimage.common.model.vo.ProductRiskDashboardVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskDashboardVo;
import com.nanri.aiimage.modules.task.mapper.FileResultMapper; import com.nanri.aiimage.modules.task.mapper.FileResultMapper;
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper; import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
import com.nanri.aiimage.modules.task.model.entity.FileResultEntity; import com.nanri.aiimage.modules.task.model.entity.FileResultEntity;
@@ -5,10 +5,10 @@ import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCountryPreferenceSaveRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCountryPreferenceSaveRequest;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest;
import com.nanri.aiimage.common.model.vo.ProductRiskCandidateVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCandidateVo;
import com.nanri.aiimage.common.model.vo.ProductRiskCountryPreferenceVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCountryPreferenceVo;
import com.nanri.aiimage.common.model.vo.ProductRiskDashboardVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskDashboardVo;
import com.nanri.aiimage.common.model.vo.ProductRiskMatchShopsVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskMatchShopsVo;
import com.nanri.aiimage.modules.shopdatacrawl.model.dto.ShopDataCrawlCreateTaskRequest; import com.nanri.aiimage.modules.shopdatacrawl.model.dto.ShopDataCrawlCreateTaskRequest;
import com.nanri.aiimage.modules.shopdatacrawl.model.dto.ShopDataCrawlSubmitResultRequest; import com.nanri.aiimage.modules.shopdatacrawl.model.dto.ShopDataCrawlSubmitResultRequest;
import com.nanri.aiimage.modules.shopdatacrawl.model.dto.ShopDataCrawlTaskBatchRequest; import com.nanri.aiimage.modules.shopdatacrawl.model.dto.ShopDataCrawlTaskBatchRequest;
@@ -12,10 +12,10 @@ import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequ
import com.nanri.aiimage.modules.shopdatacrawl.model.entity.ShopDataCrawlCountryPrefEntity; import com.nanri.aiimage.modules.shopdatacrawl.model.entity.ShopDataCrawlCountryPrefEntity;
import com.nanri.aiimage.modules.shopdatacrawl.model.entity.ShopDataCrawlShopCandidateEntity; import com.nanri.aiimage.modules.shopdatacrawl.model.entity.ShopDataCrawlShopCandidateEntity;
import com.nanri.aiimage.modules.productrisk.model.enums.ProductRiskCountryCode; import com.nanri.aiimage.modules.productrisk.model.enums.ProductRiskCountryCode;
import com.nanri.aiimage.common.model.vo.ProductRiskCandidateVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCandidateVo;
import com.nanri.aiimage.common.model.vo.ProductRiskCountryPreferenceVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCountryPreferenceVo;
import com.nanri.aiimage.common.model.vo.ProductRiskMatchShopsVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskMatchShopsVo;
import com.nanri.aiimage.common.model.vo.ProductRiskShopQueueItemVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskShopQueueItemVo;
import com.nanri.aiimage.modules.shopkey.service.ShopManageService; import com.nanri.aiimage.modules.shopkey.service.ShopManageService;
import com.nanri.aiimage.modules.ziniao.model.vo.ZiniaoShopMatchResultVo; import com.nanri.aiimage.modules.ziniao.model.vo.ZiniaoShopMatchResultVo;
import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopIndexService; import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopIndexService;
@@ -1,215 +1,35 @@
package com.nanri.aiimage.modules.shopdatacrawl.service; package com.nanri.aiimage.modules.shopdatacrawl.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.config.TaskPressureProperties; import com.nanri.aiimage.config.TaskPressureProperties;
import com.nanri.aiimage.modules.shopdatacrawl.model.dto.ShopDataCrawlShopPayloadDto; import com.nanri.aiimage.modules.shopdatacrawl.model.dto.ShopDataCrawlShopPayloadDto;
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity; import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
import com.nanri.aiimage.modules.task.service.AbstractModuleTaskCacheService;
import com.nanri.aiimage.modules.task.service.TaskScopePayloadStorageService; import com.nanri.aiimage.modules.task.service.TaskScopePayloadStorageService;
import com.nanri.aiimage.modules.task.util.TaskEntityLocalCache; import com.nanri.aiimage.modules.task.util.TaskEntityLocalCache;
import lombok.RequiredArgsConstructor; import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
/**
* 店铺数据采集模块任务缓存服务:通用实现见 {@link AbstractModuleTaskCacheService},
* 本类只声明模块差异(moduleType / Redis key 前缀 / 载荷类型)与本地缓存监控入口。
*/
@Service @Service
@RequiredArgsConstructor public class ShopDataCrawlTaskCacheService extends AbstractModuleTaskCacheService<ShopDataCrawlShopPayloadDto> {
@Slf4j
public class ShopDataCrawlTaskCacheService {
private static final String MODULE_TYPE = "SHOP_DATA_CRAWL";
private static final long PAYLOAD_TTL_HOURS = 24;
private final StringRedisTemplate stringRedisTemplate;
private final ObjectMapper objectMapper;
private final TaskPressureProperties taskPressureProperties; private final TaskPressureProperties taskPressureProperties;
private final TaskScopePayloadStorageService taskScopePayloadStorageService;
private final TaskEntityLocalCache taskEntityLocalCache;
public ShopDataCrawlShopPayloadDto getShopMergedPayload(Long taskId, String shopKey) { public ShopDataCrawlTaskCacheService(StringRedisTemplate stringRedisTemplate,
return taskScopePayloadStorageService.getScopePayload(taskId, MODULE_TYPE, shopKey, ShopDataCrawlShopPayloadDto.class); ObjectMapper objectMapper,
} TaskPressureProperties taskPressureProperties,
TaskScopePayloadStorageService taskScopePayloadStorageService,
public void saveShopMergedPayload(Long taskId, String shopKey, ShopDataCrawlShopPayloadDto payload) { TaskEntityLocalCache taskEntityLocalCache) {
if (taskId == null || taskId <= 0 || shopKey == null || shopKey.isBlank() || payload == null) { super("SHOP_DATA_CRAWL", "shop-data-crawl", ShopDataCrawlShopPayloadDto.class,
return; stringRedisTemplate, objectMapper, taskScopePayloadStorageService, taskEntityLocalCache);
} this.taskPressureProperties = taskPressureProperties;
taskScopePayloadStorageService.saveScopePayload(taskId, MODULE_TYPE, shopKey, payload);
touchTaskHeartbeat(taskId);
}
public void removeShopMergedPayload(Long taskId, String shopKey) {
taskScopePayloadStorageService.removeScopePayload(taskId, MODULE_TYPE, shopKey);
}
public Map<String, ShopDataCrawlShopPayloadDto> getAllShopMergedPayload(Long taskId) {
return taskScopePayloadStorageService.getAllScopePayload(taskId, MODULE_TYPE, ShopDataCrawlShopPayloadDto.class);
}
public boolean hasAnyShopMergedPayload(Long taskId) {
return taskScopePayloadStorageService.hasAnyScopePayload(taskId, MODULE_TYPE);
}
public long countShopMergedPayload(Long taskId) {
return taskScopePayloadStorageService.countScopePayload(taskId, MODULE_TYPE);
}
public long getTaskHeartbeatMillis(Long taskId) {
if (taskId == null || taskId <= 0) {
return 0L;
}
String raw;
try {
raw = stringRedisTemplate.opsForValue().get(buildTaskHeartbeatKey(taskId));
} catch (Exception ex) {
log.warn("[shop-data-crawl-cache] get heartbeat degraded taskId={} msg={}", taskId, ex.getMessage());
return 0L;
}
if (raw == null || raw.isBlank()) {
return 0L;
}
try {
return Long.parseLong(raw);
} catch (NumberFormatException ignored) {
return 0L;
}
}
public Map<Long, Long> getTaskHeartbeatMillisBatch(List<Long> taskIds) {
Map<Long, Long> result = new LinkedHashMap<>();
if (taskIds == null || taskIds.isEmpty()) {
return result;
}
List<Long> normalized = taskIds.stream()
.filter(id -> id != null && id > 0)
.distinct()
.toList();
if (normalized.isEmpty()) {
return result;
}
List<String> keys = normalized.stream().map(this::buildTaskHeartbeatKey).toList();
List<String> values;
try {
values = stringRedisTemplate.opsForValue().multiGet(keys);
} catch (Exception ex) {
log.warn("[shop-data-crawl-cache] batch get heartbeat degraded taskIds={} msg={}", normalized, ex.getMessage());
return result;
}
for (int i = 0; i < normalized.size(); i++) {
String raw = values != null && i < values.size() ? values.get(i) : null;
if (raw == null || raw.isBlank()) {
result.put(normalized.get(i), 0L);
continue;
}
try {
result.put(normalized.get(i), Long.parseLong(raw));
} catch (NumberFormatException ignored) {
result.put(normalized.get(i), 0L);
}
}
return result;
}
public void touchTaskHeartbeat(Long taskId) {
try {
stringRedisTemplate.opsForValue().set(
buildTaskHeartbeatKey(taskId),
String.valueOf(Instant.now().toEpochMilli()),
Duration.ofHours(PAYLOAD_TTL_HOURS));
} catch (Exception ex) {
log.warn("[shop-data-crawl-cache] touch heartbeat degraded taskId={} msg={}", taskId, ex.getMessage());
}
}
public void deleteTaskCache(Long taskId) {
if (taskId == null || taskId <= 0) {
return;
}
taskEntityLocalCache.evict(taskId);
try {
stringRedisTemplate.delete(buildTaskHeartbeatKey(taskId));
stringRedisTemplate.delete(buildTaskEntityKey(taskId));
} catch (Exception ex) {
log.warn("[shop-data-crawl-cache] delete cache degraded taskId={} msg={}", taskId, ex.getMessage());
}
taskScopePayloadStorageService.deleteTaskScopePayloads(taskId, MODULE_TYPE);
}
public void saveTaskCache(FileTaskEntity task) {
if (task == null || task.getId() == null) {
return;
}
taskEntityLocalCache.put(task.getId(), task);
try {
stringRedisTemplate.opsForValue().set(
buildTaskEntityKey(task.getId()),
objectMapper.writeValueAsString(task),
Duration.ofHours(PAYLOAD_TTL_HOURS)
);
} catch (Exception ignored) {
}
}
public Map<Long, FileTaskEntity> getTaskCacheBatch(java.util.List<Long> taskIds) {
Map<Long, FileTaskEntity> result = new LinkedHashMap<>();
if (taskIds == null || taskIds.isEmpty()) {
return result;
}
java.util.List<Long> normalized = taskIds.stream()
.filter(id -> id != null && id > 0)
.distinct()
.toList();
if (normalized.isEmpty()) {
return result;
}
long now = System.currentTimeMillis();
java.util.List<Long> missingIds = new ArrayList<>();
for (Long taskId : normalized) {
TaskEntityLocalCache.Entry cached = taskEntityLocalCache.get(taskId, now);
if (cached != null) {
result.put(taskId, cached.task());
} else {
missingIds.add(taskId);
}
}
if (missingIds.isEmpty()) {
return result;
}
java.util.List<String> keys = missingIds.stream().map(this::buildTaskEntityKey).toList();
java.util.List<String> values;
try {
values = stringRedisTemplate.opsForValue().multiGet(keys);
} catch (Exception ex) {
log.warn("[shop-data-crawl-cache] batch get task cache degraded taskIds={} msg={}", missingIds, ex.getMessage());
return result;
}
for (int i = 0; i < missingIds.size(); i++) {
Long taskId = missingIds.get(i);
String val = values != null && i < values.size() ? values.get(i) : null;
if (val == null || val.isBlank()) {
continue;
}
try {
FileTaskEntity task = objectMapper.readValue(val, FileTaskEntity.class);
result.put(taskId, task);
taskEntityLocalCache.put(taskId, task);
} catch (Exception ignored) {
}
}
return result;
}
private String buildTaskHeartbeatKey(Long taskId) {
return "shop-data-crawl:task:heartbeat:" + taskId;
}
private String buildTaskEntityKey(Long taskId) {
return "shop-data-crawl:task:entity:" + taskId;
} }
/** 本地缓存当前条目数(测试与监控用)。 */ /** 本地缓存当前条目数(测试与监控用)。 */
@@ -217,4 +37,3 @@ public class ShopDataCrawlTaskCacheService {
return taskEntityLocalCache.size(); return taskEntityLocalCache.size();
} }
} }
@@ -29,7 +29,7 @@ import com.nanri.aiimage.modules.shopdatacrawl.model.vo.ShopDataCrawlCreateTaskV
import com.nanri.aiimage.modules.shopdatacrawl.model.vo.ShopDataCrawlHistoryVo; import com.nanri.aiimage.modules.shopdatacrawl.model.vo.ShopDataCrawlHistoryVo;
import com.nanri.aiimage.modules.shopdatacrawl.model.vo.ShopDataCrawlResultItemVo; import com.nanri.aiimage.modules.shopdatacrawl.model.vo.ShopDataCrawlResultItemVo;
import com.nanri.aiimage.modules.shopdatacrawl.spi.DuplicateCheckRefreshPort; import com.nanri.aiimage.modules.shopdatacrawl.spi.DuplicateCheckRefreshPort;
import com.nanri.aiimage.common.model.vo.ProductRiskDashboardVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskDashboardVo;
import com.nanri.aiimage.modules.task.mapper.FileResultMapper; import com.nanri.aiimage.modules.task.mapper.FileResultMapper;
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper; import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
import com.nanri.aiimage.modules.task.mapper.TaskChunkMapper; import com.nanri.aiimage.modules.task.mapper.TaskChunkMapper;
@@ -6,12 +6,12 @@ import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRe
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCountryPreferenceSaveRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCountryPreferenceSaveRequest;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskTaskBatchRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskTaskBatchRequest;
import com.nanri.aiimage.common.model.vo.ProductRiskCandidateVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCandidateVo;
import com.nanri.aiimage.common.model.vo.ProductRiskCountryPreferenceVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCountryPreferenceVo;
import com.nanri.aiimage.common.model.vo.ProductRiskDashboardVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskDashboardVo;
import com.nanri.aiimage.common.model.vo.ProductRiskHistoryVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskHistoryVo;
import com.nanri.aiimage.common.model.vo.ProductRiskMatchShopsVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskMatchShopsVo;
import com.nanri.aiimage.common.model.vo.ProductRiskTaskBatchVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskTaskBatchVo;
import com.nanri.aiimage.modules.shopmatch.model.dto.ShopMatchCreateTaskRequest; import com.nanri.aiimage.modules.shopmatch.model.dto.ShopMatchCreateTaskRequest;
import com.nanri.aiimage.modules.shopmatch.model.dto.ShopMatchStageCompleteRequest; import com.nanri.aiimage.modules.shopmatch.model.dto.ShopMatchStageCompleteRequest;
import com.nanri.aiimage.modules.shopmatch.model.dto.ShopMatchSubmitResultRequest; import com.nanri.aiimage.modules.shopmatch.model.dto.ShopMatchSubmitResultRequest;
@@ -8,10 +8,10 @@ import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRe
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCountryPreferenceSaveRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCountryPreferenceSaveRequest;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest;
import com.nanri.aiimage.modules.productrisk.model.enums.ProductRiskCountryCode; import com.nanri.aiimage.modules.productrisk.model.enums.ProductRiskCountryCode;
import com.nanri.aiimage.common.model.vo.ProductRiskCandidateVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCandidateVo;
import com.nanri.aiimage.common.model.vo.ProductRiskCountryPreferenceVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCountryPreferenceVo;
import com.nanri.aiimage.common.model.vo.ProductRiskMatchShopsVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskMatchShopsVo;
import com.nanri.aiimage.common.model.vo.ProductRiskShopQueueItemVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskShopQueueItemVo;
import com.nanri.aiimage.modules.shopmatch.mapper.ShopMatchCountryPrefMapper; import com.nanri.aiimage.modules.shopmatch.mapper.ShopMatchCountryPrefMapper;
import com.nanri.aiimage.modules.shopmatch.mapper.ShopMatchShopCandidateMapper; import com.nanri.aiimage.modules.shopmatch.mapper.ShopMatchShopCandidateMapper;
import com.nanri.aiimage.modules.shopmatch.model.entity.ShopMatchCountryPrefEntity; import com.nanri.aiimage.modules.shopmatch.model.entity.ShopMatchCountryPrefEntity;
@@ -1,12 +1,12 @@
package com.nanri.aiimage.modules.shopmatch.service; package com.nanri.aiimage.modules.shopmatch.service;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.config.TaskPressureProperties;
import com.nanri.aiimage.modules.shopmatch.model.dto.ShopMatchShopPayloadDto; import com.nanri.aiimage.modules.shopmatch.model.dto.ShopMatchShopPayloadDto;
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity; import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
import com.nanri.aiimage.modules.task.service.AbstractModuleTaskCacheService;
import com.nanri.aiimage.modules.task.service.TaskScopePayloadStorageService; import com.nanri.aiimage.modules.task.service.TaskScopePayloadStorageService;
import com.nanri.aiimage.modules.task.util.TaskEntityLocalCache; import com.nanri.aiimage.modules.task.util.TaskEntityLocalCache;
import com.nanri.aiimage.config.TaskPressureProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -16,130 +16,41 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardOpenOption; import java.nio.file.StandardOpenOption;
import java.time.Duration; import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
/**
* 店铺匹配模块任务缓存服务:心跳与店铺维度载荷复用通用基类
* {@link AbstractModuleTaskCacheService};任务实体不走 Redis,而是落本地临时文件
* (重启后仍可续跑),因此覆写 saveTaskCache / getTaskCacheBatch / deleteTaskCache。
*/
@Service @Service
@RequiredArgsConstructor
@Slf4j @Slf4j
public class ShopMatchTaskCacheService { public class ShopMatchTaskCacheService extends AbstractModuleTaskCacheService<ShopMatchShopPayloadDto> {
private static final String MODULE_TYPE = "SHOP_MATCH";
private static final long PAYLOAD_TTL_HOURS = 24; private static final long PAYLOAD_TTL_HOURS = 24;
private final StringRedisTemplate stringRedisTemplate;
private final ObjectMapper objectMapper;
private final TaskPressureProperties taskPressureProperties; private final TaskPressureProperties taskPressureProperties;
private final TaskScopePayloadStorageService taskScopePayloadStorageService;
private final TaskEntityLocalCache taskEntityLocalCache;
public void touchTaskHeartbeat(Long taskId) { public ShopMatchTaskCacheService(StringRedisTemplate stringRedisTemplate,
if (taskId == null || taskId <= 0) { ObjectMapper objectMapper,
return; TaskPressureProperties taskPressureProperties,
} TaskScopePayloadStorageService taskScopePayloadStorageService,
try { TaskEntityLocalCache taskEntityLocalCache) {
stringRedisTemplate.opsForValue().set( super("SHOP_MATCH", "shop-match", ShopMatchShopPayloadDto.class,
buildTaskHeartbeatKey(taskId), stringRedisTemplate, objectMapper, taskScopePayloadStorageService, taskEntityLocalCache);
String.valueOf(Instant.now().toEpochMilli()), this.taskPressureProperties = taskPressureProperties;
Duration.ofHours(PAYLOAD_TTL_HOURS));
} catch (Exception ex) {
log.warn("[shop-match-cache] touch heartbeat degraded taskId={} msg={}", taskId, ex.getMessage());
}
}
public long getTaskHeartbeatMillis(Long taskId) {
if (taskId == null || taskId <= 0) {
return 0L;
}
String raw;
try {
raw = stringRedisTemplate.opsForValue().get(buildTaskHeartbeatKey(taskId));
} catch (Exception ex) {
log.warn("[shop-match-cache] get heartbeat degraded taskId={} msg={}", taskId, ex.getMessage());
return 0L;
}
if (raw == null || raw.isBlank()) {
return 0L;
}
try {
return Long.parseLong(raw);
} catch (NumberFormatException ignored) {
return 0L;
}
}
public Map<Long, Long> getTaskHeartbeatMillisBatch(List<Long> taskIds) {
Map<Long, Long> result = new LinkedHashMap<>();
if (taskIds == null || taskIds.isEmpty()) {
return result;
}
List<Long> normalized = taskIds.stream()
.filter(id -> id != null && id > 0)
.distinct()
.toList();
if (normalized.isEmpty()) {
return result;
}
List<String> keys = normalized.stream().map(this::buildTaskHeartbeatKey).toList();
List<String> values;
try {
values = stringRedisTemplate.opsForValue().multiGet(keys);
} catch (Exception ex) {
log.warn("[shop-match-cache] batch get heartbeat degraded taskIds={} msg={}", normalized, ex.getMessage());
return result;
}
for (int i = 0; i < normalized.size(); i++) {
String raw = values != null && i < values.size() ? values.get(i) : null;
if (raw == null || raw.isBlank()) {
result.put(normalized.get(i), 0L);
continue;
}
try {
result.put(normalized.get(i), Long.parseLong(raw));
} catch (NumberFormatException ignored) {
result.put(normalized.get(i), 0L);
}
}
return result;
}
public ShopMatchShopPayloadDto getShopMergedPayload(Long taskId, String shopKey) {
return taskScopePayloadStorageService.getScopePayload(taskId, MODULE_TYPE, shopKey, ShopMatchShopPayloadDto.class);
}
public void saveShopMergedPayload(Long taskId, String shopKey, ShopMatchShopPayloadDto payload) {
if (taskId == null || taskId <= 0 || shopKey == null || shopKey.isBlank() || payload == null) {
return;
}
taskScopePayloadStorageService.saveScopePayload(taskId, MODULE_TYPE, shopKey, payload);
touchTaskHeartbeat(taskId);
}
public void removeShopMergedPayload(Long taskId, String shopKey) {
taskScopePayloadStorageService.removeScopePayload(taskId, MODULE_TYPE, shopKey);
}
public Map<String, ShopMatchShopPayloadDto> getAllShopMergedPayload(Long taskId) {
return taskScopePayloadStorageService.getAllScopePayload(taskId, MODULE_TYPE, ShopMatchShopPayloadDto.class);
}
public boolean hasAnyShopMergedPayload(Long taskId) {
return taskScopePayloadStorageService.hasAnyScopePayload(taskId, MODULE_TYPE);
} }
@Override
public void deleteTaskCache(Long taskId) { public void deleteTaskCache(Long taskId) {
if (taskId == null || taskId <= 0) { if (taskId == null || taskId <= 0) {
return; return;
} }
taskEntityLocalCache.evict(taskId); evictTaskCacheOnly(taskId);
try { taskScopePayloadStorageService.deleteTaskScopePayloads(taskId, moduleType());
stringRedisTemplate.delete(buildTaskHeartbeatKey(taskId));
} catch (Exception ex) {
log.warn("[shop-match-cache] delete heartbeat degraded taskId={} msg={}", taskId, ex.getMessage());
}
taskScopePayloadStorageService.deleteTaskScopePayloads(taskId, MODULE_TYPE);
try { try {
Path taskDir = buildTaskDir(taskId); Path taskDir = buildTaskDir(taskId);
if (!Files.exists(taskDir)) { if (!Files.exists(taskDir)) {
@@ -165,6 +76,7 @@ public class ShopMatchTaskCacheService {
} }
} }
@Override
public void saveTaskCache(FileTaskEntity task) { public void saveTaskCache(FileTaskEntity task) {
if (task == null || task.getId() == null) { if (task == null || task.getId() == null) {
return; return;
@@ -183,7 +95,8 @@ public class ShopMatchTaskCacheService {
} }
} }
public Map<Long, FileTaskEntity> getTaskCacheBatch(java.util.List<Long> taskIds) { @Override
public Map<Long, FileTaskEntity> getTaskCacheBatch(List<Long> taskIds) {
Map<Long, FileTaskEntity> result = new LinkedHashMap<>(); Map<Long, FileTaskEntity> result = new LinkedHashMap<>();
if (taskIds == null || taskIds.isEmpty()) { if (taskIds == null || taskIds.isEmpty()) {
return result; return result;
@@ -240,10 +153,6 @@ public class ShopMatchTaskCacheService {
return buildTaskDir(taskId).resolve("_task-entity.json"); return buildTaskDir(taskId).resolve("_task-entity.json");
} }
private String buildTaskHeartbeatKey(Long taskId) {
return "shop-match:task:heartbeat:" + taskId;
}
/** /**
* 文件缓存新鲜度判断:超过 {@code localTaskEntityFileCacheMillis} 视为过期。 * 文件缓存新鲜度判断:超过 {@code localTaskEntityFileCacheMillis} 视为过期。
* 通过文件 mtime 判断,避免在 finalize 与 poll 线程的竞态下把陈旧的 RUNNING 写回后被永久信任。 * 通过文件 mtime 判断,避免在 finalize 与 poll 线程的竞态下把陈旧的 RUNNING 写回后被永久信任。
@@ -10,13 +10,13 @@ import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.config.TaskPressureProperties; import com.nanri.aiimage.config.TaskPressureProperties;
import com.nanri.aiimage.modules.file.service.oss.OssStorageService; import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
import com.nanri.aiimage.modules.productrisk.model.enums.ProductRiskCountryCode; import com.nanri.aiimage.modules.productrisk.model.enums.ProductRiskCountryCode;
import com.nanri.aiimage.common.model.vo.ProductRiskDashboardVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskDashboardVo;
import com.nanri.aiimage.common.model.vo.ProductRiskHistoryVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskHistoryVo;
import com.nanri.aiimage.common.model.vo.ProductRiskResultItemVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskResultItemVo;
import com.nanri.aiimage.common.model.vo.ProductRiskShopQueueItemVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskShopQueueItemVo;
import com.nanri.aiimage.common.model.vo.ProductRiskTaskBatchVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskTaskBatchVo;
import com.nanri.aiimage.common.model.vo.ProductRiskTaskDetailVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskTaskDetailVo;
import com.nanri.aiimage.common.model.vo.ProductRiskTaskItemVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskTaskItemVo;
import com.nanri.aiimage.modules.pricetrack.model.vo.SkipPriceAsinPageVo; import com.nanri.aiimage.modules.pricetrack.model.vo.SkipPriceAsinPageVo;
import com.nanri.aiimage.modules.queryasin.model.dto.QueryAsinCountryAsinsDto; import com.nanri.aiimage.modules.queryasin.model.dto.QueryAsinCountryAsinsDto;
import com.nanri.aiimage.modules.shopmatch.mapper.ShopMatchShopCandidateMapper; import com.nanri.aiimage.modules.shopmatch.mapper.ShopMatchShopCandidateMapper;
@@ -1,144 +1,20 @@
package com.nanri.aiimage.modules.similarasin.service; package com.nanri.aiimage.modules.similarasin.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.modules.similarasin.model.dto.SimilarAsinResultRowDto; import com.nanri.aiimage.modules.similarasin.model.dto.SimilarAsinResultRowDto;
import lombok.RequiredArgsConstructor; import com.nanri.aiimage.modules.task.service.AbstractPendingRowTaskCacheService;
import lombok.extern.slf4j.Slf4j; import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.Duration; /**
import java.time.Instant; * 相似ASIN 模块任务缓存服务:待处理行队列 + 心跳,通用实现见
import java.util.ArrayList; * {@link AbstractPendingRowTaskCacheService},本类只声明模块差异。
import java.util.List; */
@Service @Service
@RequiredArgsConstructor public class SimilarAsinTaskCacheService extends AbstractPendingRowTaskCacheService<SimilarAsinResultRowDto> {
@Slf4j
public class SimilarAsinTaskCacheService {
private static final long TTL_HOURS = 24; public SimilarAsinTaskCacheService(StringRedisTemplate stringRedisTemplate,
ObjectMapper objectMapper) {
private final StringRedisTemplate stringRedisTemplate; super("similar-asin", SimilarAsinResultRowDto.class, stringRedisTemplate, objectMapper);
private final ObjectMapper objectMapper;
public void appendPendingRow(Long taskId, String scopeHash, Integer chunkIndex, SimilarAsinResultRowDto row) {
if (taskId == null || taskId <= 0 || scopeHash == null || scopeHash.isBlank() || chunkIndex == null || row == null) {
return;
}
try {
stringRedisTemplate.opsForList().rightPush(
pendingRowsKey(taskId),
objectMapper.writeValueAsString(new PendingRow(scopeHash, chunkIndex, row))
);
stringRedisTemplate.expire(pendingRowsKey(taskId), Duration.ofHours(TTL_HOURS));
touchTaskHeartbeat(taskId);
} catch (Exception ignored) {
}
}
public long pendingRowCount(Long taskId) {
Long size;
try {
size = stringRedisTemplate.opsForList().size(pendingRowsKey(taskId));
} catch (Exception ex) {
log.warn("[similar-asin-cache] pending row count degraded taskId={} msg={}", taskId, ex.getMessage());
return 0L;
}
return size == null ? 0L : size;
}
public List<PendingRow> drainPendingRows(Long taskId, int limit) {
if (taskId == null || taskId <= 0 || limit <= 0) {
return List.of();
}
String key = pendingRowsKey(taskId);
List<String> values;
try {
values = stringRedisTemplate.opsForList().range(key, 0, limit - 1L);
} catch (Exception ex) {
log.warn("[similar-asin-cache] drain range degraded taskId={} msg={}", taskId, ex.getMessage());
return List.of();
}
if (values == null || values.isEmpty()) {
return List.of();
}
try {
stringRedisTemplate.opsForList().trim(key, values.size(), -1);
} catch (Exception ex) {
log.warn("[similar-asin-cache] drain trim degraded taskId={} msg={}", taskId, ex.getMessage());
return List.of();
}
List<PendingRow> rows = new ArrayList<>();
for (String value : values) {
if (value == null || value.isBlank()) {
continue;
}
try {
rows.add(objectMapper.readValue(value, PendingRow.class));
} catch (Exception ignored) {
}
}
touchTaskHeartbeat(taskId);
return rows;
}
public void touchTaskHeartbeat(Long taskId) {
if (taskId == null || taskId <= 0) {
return;
}
try {
stringRedisTemplate.opsForValue().set(
heartbeatKey(taskId),
String.valueOf(Instant.now().toEpochMilli()),
Duration.ofHours(TTL_HOURS)
);
} catch (Exception ex) {
log.warn("[similar-asin-cache] touch heartbeat degraded taskId={} msg={}", taskId, ex.getMessage());
}
}
public long getTaskHeartbeatMillis(Long taskId) {
if (taskId == null || taskId <= 0) {
return 0L;
}
String raw;
try {
raw = stringRedisTemplate.opsForValue().get(heartbeatKey(taskId));
} catch (Exception ex) {
log.warn("[similar-asin-cache] get heartbeat degraded taskId={} msg={}", taskId, ex.getMessage());
return 0L;
}
if (raw == null || raw.isBlank()) {
return 0L;
}
try {
return Long.parseLong(raw);
} catch (NumberFormatException ignored) {
return 0L;
}
}
public void deleteTaskCache(Long taskId) {
if (taskId == null || taskId <= 0) {
return;
}
try {
stringRedisTemplate.delete(pendingRowsKey(taskId));
stringRedisTemplate.delete(heartbeatKey(taskId));
} catch (Exception ex) {
log.warn("[similar-asin-cache] delete degraded taskId={} msg={}", taskId, ex.getMessage());
}
}
private String pendingRowsKey(Long taskId) {
return "similar-asin:task:pending-rows:" + taskId;
}
private String heartbeatKey(Long taskId) {
return "similar-asin:task:heartbeat:" + taskId;
}
public record PendingRow(String scopeHash, Integer chunkIndex, SimilarAsinResultRowDto row) {
} }
} }
@@ -0,0 +1,282 @@
package com.nanri.aiimage.modules.task.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
import com.nanri.aiimage.modules.task.util.TaskEntityLocalCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* 业务模块「任务缓存服务」泛型基类:心跳键 + 任务实体缓存(本地 LRU + Redis)+ 店铺维度载荷存取。
*
* <p><b>新增任务模块请继承本类,不要复制粘贴再改前缀。</b>各模块此前是逐行复制的
* XxxTaskCacheService(约 200 行 × N 份),除以下三点外完全一致,现统一收敛到这里:
* <ul>
* <li>{@code moduleType}:店铺维度载荷在 task_scope 存储里的隔离键;</li>
* <li>{@code keyPrefix}:Redis 键前缀(心跳 {@code <keyPrefix>:task:heartbeat:*}、
* 实体 {@code <keyPrefix>:task:entity:*})与降级日志标签 {@code [<keyPrefix>-cache]};</li>
* <li>{@code payloadType}:店铺合并载荷的具体类型。必须传 {@code Class<T>},
* 不能用 {@code (Class<T>) T.class} 之类的擦除技巧,否则 JSON 反序列化会得到 LinkedHashMap。</li>
* </ul>
*
* <p>子类只需提供构造器把三个差异常量传给 {@code super};模块独有的缓存形态
* (如 ShopMatch 的文件实体缓存、PatrolDelete 的分段清理)在子类覆写对应方法即可。
* 公共实现统一为「taskId 非法直接返回」的守卫版:此前个别模块的 touch/delete 缺守卫,
* 但全量调用方传入的都是已校验的任务 id,且被跳过的底层操作本身也有 null 守卫,行为等价。
*
* @param <T> 该模块店铺维度合并载荷的类型(taskScopePayloadStorageService 的反序列化目标)
*/
@Slf4j
public abstract class AbstractModuleTaskCacheService<T> {
/** 心跳与任务实体缓存的 Redis TTL(与历史各模块实现一致的 24 小时)。 */
private static final long PAYLOAD_TTL_HOURS = 24;
private final String moduleType;
private final String keyPrefix;
private final Class<T> payloadType;
private final String logTag;
protected final StringRedisTemplate stringRedisTemplate;
protected final ObjectMapper objectMapper;
protected final TaskScopePayloadStorageService taskScopePayloadStorageService;
protected final TaskEntityLocalCache taskEntityLocalCache;
protected AbstractModuleTaskCacheService(String moduleType,
String keyPrefix,
Class<T> payloadType,
StringRedisTemplate stringRedisTemplate,
ObjectMapper objectMapper,
TaskScopePayloadStorageService taskScopePayloadStorageService,
TaskEntityLocalCache taskEntityLocalCache) {
this.moduleType = moduleType;
this.keyPrefix = keyPrefix;
this.payloadType = payloadType;
this.logTag = keyPrefix + "-cache";
this.stringRedisTemplate = stringRedisTemplate;
this.objectMapper = objectMapper;
this.taskScopePayloadStorageService = taskScopePayloadStorageService;
this.taskEntityLocalCache = taskEntityLocalCache;
}
// ------------------------------------------------------------------
// 心跳
// ------------------------------------------------------------------
public void touchTaskHeartbeat(Long taskId) {
if (taskId == null || taskId <= 0) {
return;
}
try {
stringRedisTemplate.opsForValue().set(
buildTaskHeartbeatKey(taskId),
String.valueOf(Instant.now().toEpochMilli()),
Duration.ofHours(PAYLOAD_TTL_HOURS));
} catch (Exception ex) {
log.warn("[{}] touch heartbeat degraded taskId={} msg={}", logTag, taskId, ex.getMessage());
}
}
public long getTaskHeartbeatMillis(Long taskId) {
if (taskId == null || taskId <= 0) {
return 0L;
}
String raw;
try {
raw = stringRedisTemplate.opsForValue().get(buildTaskHeartbeatKey(taskId));
} catch (Exception ex) {
log.warn("[{}] get heartbeat degraded taskId={} msg={}", logTag, taskId, ex.getMessage());
return 0L;
}
if (raw == null || raw.isBlank()) {
return 0L;
}
try {
return Long.parseLong(raw);
} catch (NumberFormatException ignored) {
return 0L;
}
}
public Map<Long, Long> getTaskHeartbeatMillisBatch(List<Long> taskIds) {
Map<Long, Long> result = new LinkedHashMap<>();
if (taskIds == null || taskIds.isEmpty()) {
return result;
}
List<Long> normalized = taskIds.stream()
.filter(id -> id != null && id > 0)
.distinct()
.toList();
if (normalized.isEmpty()) {
return result;
}
List<String> keys = normalized.stream().map(this::buildTaskHeartbeatKey).toList();
List<String> values;
try {
values = stringRedisTemplate.opsForValue().multiGet(keys);
} catch (Exception ex) {
log.warn("[{}] batch get heartbeat degraded taskIds={} msg={}", logTag, normalized, ex.getMessage());
return result;
}
for (int i = 0; i < normalized.size(); i++) {
String raw = values != null && i < values.size() ? values.get(i) : null;
if (raw == null || raw.isBlank()) {
result.put(normalized.get(i), 0L);
continue;
}
try {
result.put(normalized.get(i), Long.parseLong(raw));
} catch (NumberFormatException ignored) {
result.put(normalized.get(i), 0L);
}
}
return result;
}
// ------------------------------------------------------------------
// 店铺维度合并载荷
// ------------------------------------------------------------------
public T getShopMergedPayload(Long taskId, String shopKey) {
return taskScopePayloadStorageService.getScopePayload(taskId, moduleType, shopKey, payloadType);
}
public void saveShopMergedPayload(Long taskId, String shopKey, T payload) {
if (taskId == null || taskId <= 0 || shopKey == null || shopKey.isBlank() || payload == null) {
return;
}
taskScopePayloadStorageService.saveScopePayload(taskId, moduleType, shopKey, payload);
touchTaskHeartbeat(taskId);
}
public void removeShopMergedPayload(Long taskId, String shopKey) {
taskScopePayloadStorageService.removeScopePayload(taskId, moduleType, shopKey);
}
public Map<String, T> getAllShopMergedPayload(Long taskId) {
return taskScopePayloadStorageService.getAllScopePayload(taskId, moduleType, payloadType);
}
public boolean hasAnyShopMergedPayload(Long taskId) {
return taskScopePayloadStorageService.hasAnyScopePayload(taskId, moduleType);
}
public long countShopMergedPayload(Long taskId) {
return taskScopePayloadStorageService.countScopePayload(taskId, moduleType);
}
// ------------------------------------------------------------------
// 任务实体缓存(本地 LRU + Redis)
// ------------------------------------------------------------------
/**
* 只清理心跳/实体缓存,不动店铺维度载荷。删除整任务用 {@link #deleteTaskCache(Long)}。
*/
public void evictTaskCacheOnly(Long taskId) {
if (taskId == null || taskId <= 0) {
return;
}
taskEntityLocalCache.evict(taskId);
try {
stringRedisTemplate.delete(buildTaskHeartbeatKey(taskId));
stringRedisTemplate.delete(buildTaskEntityKey(taskId));
} catch (Exception ex) {
log.warn("[{}] delete cache degraded taskId={} msg={}", logTag, taskId, ex.getMessage());
}
}
public void deleteTaskCache(Long taskId) {
if (taskId == null || taskId <= 0) {
return;
}
evictTaskCacheOnly(taskId);
taskScopePayloadStorageService.deleteTaskScopePayloads(taskId, moduleType);
}
public void saveTaskCache(FileTaskEntity task) {
if (task == null || task.getId() == null) {
return;
}
taskEntityLocalCache.put(task.getId(), task);
try {
stringRedisTemplate.opsForValue().set(
buildTaskEntityKey(task.getId()),
objectMapper.writeValueAsString(task),
Duration.ofHours(PAYLOAD_TTL_HOURS)
);
} catch (Exception ignored) {
}
}
public Map<Long, FileTaskEntity> getTaskCacheBatch(List<Long> taskIds) {
Map<Long, FileTaskEntity> result = new LinkedHashMap<>();
if (taskIds == null || taskIds.isEmpty()) {
return result;
}
List<Long> normalized = taskIds.stream()
.filter(id -> id != null && id > 0)
.distinct()
.toList();
if (normalized.isEmpty()) {
return result;
}
long now = System.currentTimeMillis();
List<Long> missingIds = new ArrayList<>();
for (Long taskId : normalized) {
TaskEntityLocalCache.Entry cached = taskEntityLocalCache.get(taskId, now);
if (cached != null) {
result.put(taskId, cached.task());
} else {
missingIds.add(taskId);
}
}
if (missingIds.isEmpty()) {
return result;
}
List<String> keys = missingIds.stream().map(this::buildTaskEntityKey).toList();
List<String> values;
try {
values = stringRedisTemplate.opsForValue().multiGet(keys);
} catch (Exception ex) {
log.warn("[{}] batch get task cache degraded taskIds={} msg={}", logTag, missingIds, ex.getMessage());
return result;
}
for (int i = 0; i < missingIds.size(); i++) {
Long taskId = missingIds.get(i);
String val = values != null && i < values.size() ? values.get(i) : null;
if (val == null || val.isBlank()) {
continue;
}
try {
FileTaskEntity task = objectMapper.readValue(val, FileTaskEntity.class);
result.put(taskId, task);
taskEntityLocalCache.put(taskId, task);
} catch (Exception ignored) {
}
}
return result;
}
// ------------------------------------------------------------------
// 键构造(子类如需自定义键形可覆写)
// ------------------------------------------------------------------
protected String moduleType() {
return moduleType;
}
protected String buildTaskHeartbeatKey(Long taskId) {
return keyPrefix + ":task:heartbeat:" + taskId;
}
protected String buildTaskEntityKey(Long taskId) {
return keyPrefix + ":task:entity:" + taskId;
}
}
@@ -0,0 +1,182 @@
package com.nanri.aiimage.modules.task.service;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
/**
* 「待处理行」型任务缓存服务泛型基类:Redis list 暂存待 LLM 检测的行 + 心跳键。
*
* <p><b>新增同类任务模块请继承本类,不要复制粘贴再改前缀。</b>此类形态与
* {@link AbstractModuleTaskCacheService}(心跳 + 实体缓存 + 店铺维度载荷)不同:
* 它没有任务实体缓存,也没有按店铺聚合的载荷,只有按任务组织的「待处理行」队列
* (此前 AppearancePatentTaskCacheService / SimilarAsinTaskCacheService 逐行复制)。
* 模块级差异只有三点:
* <ul>
* <li>{@code keyPrefix}:Redis 键前缀({@code <keyPrefix>:task:pending-rows:*}、
* {@code <keyPrefix>:task:heartbeat:*})与降级日志标签 {@code [<keyPrefix>-cache]};</li>
* <li>{@code rowType}:行 DTO 的具体类型。必须传 {@code Class<R>}——Redis list 里每条
* JSON 反序列化目标形如 {@code PendingRow<R>},泛型擦除后必须显式给出行类型,
* 否则 row 字段会退化成 LinkedHashMap;</li>
* <li>任务级附加键:子类覆写 {@link #deleteExtraTaskCacheKeys(Long)} 清理自己的行级标记键。</li>
* </ul>
*
* @param <R> 待处理行 DTO 类型
*/
@Slf4j
public abstract class AbstractPendingRowTaskCacheService<R> {
/** Redis list 中一条待处理行:所属店铺、来源分块、行内容。 */
public record PendingRow<ROW>(String scopeHash, Integer chunkIndex, ROW row) {
}
/** pending-rows list、心跳与完成标记的 Redis TTL(与历史实现一致的 24 小时)。 */
private static final long TTL_HOURS = 24;
protected final StringRedisTemplate stringRedisTemplate;
protected final ObjectMapper objectMapper;
private final String keyPrefix;
private final String logTag;
private final JavaType pendingRowJavaType;
protected AbstractPendingRowTaskCacheService(String keyPrefix,
Class<R> rowType,
StringRedisTemplate stringRedisTemplate,
ObjectMapper objectMapper) {
this.keyPrefix = keyPrefix;
this.logTag = keyPrefix + "-cache";
this.pendingRowJavaType = objectMapper.getTypeFactory()
.constructParametricType(PendingRow.class, rowType);
this.stringRedisTemplate = stringRedisTemplate;
this.objectMapper = objectMapper;
}
public void appendPendingRow(Long taskId, String scopeHash, Integer chunkIndex, R row) {
if (taskId == null || taskId <= 0 || scopeHash == null || scopeHash.isBlank() || chunkIndex == null || row == null) {
return;
}
try {
stringRedisTemplate.opsForList().rightPush(
pendingRowsKey(taskId),
objectMapper.writeValueAsString(new PendingRow<>(scopeHash, chunkIndex, row))
);
stringRedisTemplate.expire(pendingRowsKey(taskId), Duration.ofHours(TTL_HOURS));
touchTaskHeartbeat(taskId);
} catch (Exception ignored) {
}
}
public long pendingRowCount(Long taskId) {
Long size;
try {
size = stringRedisTemplate.opsForList().size(pendingRowsKey(taskId));
} catch (Exception ex) {
log.warn("[{}] pending row count degraded taskId={} msg={}", logTag, taskId, ex.getMessage());
return 0L;
}
return size == null ? 0L : size;
}
public List<PendingRow<R>> drainPendingRows(Long taskId, int limit) {
if (taskId == null || taskId <= 0 || limit <= 0) {
return List.of();
}
String key = pendingRowsKey(taskId);
List<String> values;
try {
values = stringRedisTemplate.opsForList().range(key, 0, limit - 1L);
} catch (Exception ex) {
log.warn("[{}] drain range degraded taskId={} msg={}", logTag, taskId, ex.getMessage());
return List.of();
}
if (values == null || values.isEmpty()) {
return List.of();
}
try {
stringRedisTemplate.opsForList().trim(key, values.size(), -1);
} catch (Exception ex) {
log.warn("[{}] drain trim degraded taskId={} msg={}", logTag, taskId, ex.getMessage());
return List.of();
}
List<PendingRow<R>> rows = new ArrayList<>();
for (String value : values) {
if (value == null || value.isBlank()) {
continue;
}
try {
rows.add(objectMapper.readValue(value, pendingRowJavaType));
} catch (Exception ignored) {
}
}
touchTaskHeartbeat(taskId);
return rows;
}
public void touchTaskHeartbeat(Long taskId) {
if (taskId == null || taskId <= 0) {
return;
}
try {
stringRedisTemplate.opsForValue().set(
heartbeatKey(taskId),
String.valueOf(Instant.now().toEpochMilli()),
Duration.ofHours(TTL_HOURS)
);
} catch (Exception ex) {
log.warn("[{}] touch heartbeat degraded taskId={} msg={}", logTag, taskId, ex.getMessage());
}
}
public long getTaskHeartbeatMillis(Long taskId) {
if (taskId == null || taskId <= 0) {
return 0L;
}
String raw;
try {
raw = stringRedisTemplate.opsForValue().get(heartbeatKey(taskId));
} catch (Exception ex) {
log.warn("[{}] get heartbeat degraded taskId={} msg={}", logTag, taskId, ex.getMessage());
return 0L;
}
if (raw == null || raw.isBlank()) {
return 0L;
}
try {
return Long.parseLong(raw);
} catch (NumberFormatException ignored) {
return 0L;
}
}
public void deleteTaskCache(Long taskId) {
if (taskId == null || taskId <= 0) {
return;
}
try {
stringRedisTemplate.delete(pendingRowsKey(taskId));
stringRedisTemplate.delete(heartbeatKey(taskId));
deleteExtraTaskCacheKeys(taskId);
} catch (Exception ex) {
log.warn("[{}] delete degraded taskId={} msg={}", logTag, taskId, ex.getMessage());
}
}
/** 任务级附加键清理钩子(如外观专利的行级完成标记),默认无。 */
protected void deleteExtraTaskCacheKeys(Long taskId) {
}
protected String pendingRowsKey(Long taskId) {
return keyPrefix + ":task:pending-rows:" + taskId;
}
protected String heartbeatKey(Long taskId) {
return keyPrefix + ":task:heartbeat:" + taskId;
}
}
@@ -4,9 +4,9 @@ import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.common.security.AdminAuthSupport; import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest;
import com.nanri.aiimage.common.model.vo.ProductRiskCandidateVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCandidateVo;
import com.nanri.aiimage.common.model.vo.ProductRiskDashboardVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskDashboardVo;
import com.nanri.aiimage.common.model.vo.ProductRiskMatchShopsVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskMatchShopsVo;
import com.nanri.aiimage.modules.withdraw.model.dto.WithdrawCandidateClearRequest; import com.nanri.aiimage.modules.withdraw.model.dto.WithdrawCandidateClearRequest;
import com.nanri.aiimage.modules.withdraw.model.dto.WithdrawCreateTaskRequest; import com.nanri.aiimage.modules.withdraw.model.dto.WithdrawCreateTaskRequest;
import com.nanri.aiimage.modules.withdraw.model.dto.WithdrawSubmitResultRequest; import com.nanri.aiimage.modules.withdraw.model.dto.WithdrawSubmitResultRequest;
@@ -4,9 +4,9 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nanri.aiimage.common.exception.BusinessException; import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest;
import com.nanri.aiimage.common.model.vo.ProductRiskCandidateVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCandidateVo;
import com.nanri.aiimage.common.model.vo.ProductRiskMatchShopsVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskMatchShopsVo;
import com.nanri.aiimage.common.model.vo.ProductRiskShopQueueItemVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskShopQueueItemVo;
import com.nanri.aiimage.modules.withdraw.mapper.WithdrawShopCandidateMapper; import com.nanri.aiimage.modules.withdraw.mapper.WithdrawShopCandidateMapper;
import com.nanri.aiimage.modules.withdraw.model.entity.WithdrawShopCandidateEntity; import com.nanri.aiimage.modules.withdraw.model.entity.WithdrawShopCandidateEntity;
import com.nanri.aiimage.modules.ziniao.model.vo.ZiniaoShopMatchResultVo; import com.nanri.aiimage.modules.ziniao.model.vo.ZiniaoShopMatchResultVo;
@@ -1,177 +1,25 @@
package com.nanri.aiimage.modules.withdraw.service; package com.nanri.aiimage.modules.withdraw.service;
import com.fasterxml.jackson.databind.ObjectMapper; import com.nanri.aiimage.modules.task.service.AbstractModuleTaskCacheService;
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
import com.nanri.aiimage.modules.task.service.TaskScopePayloadStorageService; import com.nanri.aiimage.modules.task.service.TaskScopePayloadStorageService;
import com.nanri.aiimage.modules.task.util.TaskEntityLocalCache; import com.nanri.aiimage.modules.task.util.TaskEntityLocalCache;
import com.nanri.aiimage.modules.withdraw.model.dto.WithdrawShopPayloadDto; import com.nanri.aiimage.modules.withdraw.model.dto.WithdrawShopPayloadDto;
import lombok.RequiredArgsConstructor; import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.Duration; /**
import java.time.Instant; * 提现模块任务缓存服务:通用实现见 {@link AbstractModuleTaskCacheService},
import java.util.ArrayList; * 本类只声明模块差异(moduleType / Redis key 前缀 / 载荷类型)。
import java.util.LinkedHashMap; */
import java.util.List;
import java.util.Map;
@Service @Service
@RequiredArgsConstructor public class WithdrawTaskCacheService extends AbstractModuleTaskCacheService<WithdrawShopPayloadDto> {
@Slf4j
public class WithdrawTaskCacheService {
private static final String MODULE_TYPE = "WITHDRAW"; public WithdrawTaskCacheService(StringRedisTemplate stringRedisTemplate,
private static final long PAYLOAD_TTL_HOURS = 24; ObjectMapper objectMapper,
TaskScopePayloadStorageService taskScopePayloadStorageService,
private final StringRedisTemplate stringRedisTemplate; TaskEntityLocalCache taskEntityLocalCache) {
private final ObjectMapper objectMapper; super("WITHDRAW", "withdraw", WithdrawShopPayloadDto.class,
private final TaskScopePayloadStorageService taskScopePayloadStorageService; stringRedisTemplate, objectMapper, taskScopePayloadStorageService, taskEntityLocalCache);
private final TaskEntityLocalCache taskEntityLocalCache;
public WithdrawShopPayloadDto getShopMergedPayload(Long taskId, String shopKey) {
return taskScopePayloadStorageService.getScopePayload(taskId, MODULE_TYPE, shopKey, WithdrawShopPayloadDto.class);
}
public void saveShopMergedPayload(Long taskId, String shopKey, WithdrawShopPayloadDto payload) {
if (taskId == null || taskId <= 0 || shopKey == null || shopKey.isBlank() || payload == null) {
return;
}
taskScopePayloadStorageService.saveScopePayload(taskId, MODULE_TYPE, shopKey, payload);
touchTaskHeartbeat(taskId);
}
public void removeShopMergedPayload(Long taskId, String shopKey) {
taskScopePayloadStorageService.removeScopePayload(taskId, MODULE_TYPE, shopKey);
}
public Map<String, WithdrawShopPayloadDto> getAllShopMergedPayload(Long taskId) {
return taskScopePayloadStorageService.getAllScopePayload(taskId, MODULE_TYPE, WithdrawShopPayloadDto.class);
}
public boolean hasAnyShopMergedPayload(Long taskId) {
return taskScopePayloadStorageService.hasAnyScopePayload(taskId, MODULE_TYPE);
}
public Map<Long, Long> getTaskHeartbeatMillisBatch(List<Long> taskIds) {
Map<Long, Long> result = new LinkedHashMap<>();
if (taskIds == null || taskIds.isEmpty()) {
return result;
}
List<Long> normalized = taskIds.stream().filter(id -> id != null && id > 0).distinct().toList();
if (normalized.isEmpty()) {
return result;
}
List<String> keys = normalized.stream().map(this::buildTaskHeartbeatKey).toList();
List<String> values;
try {
values = stringRedisTemplate.opsForValue().multiGet(keys);
} catch (Exception ex) {
log.warn("[withdraw-cache] batch get heartbeat degraded taskIds={} msg={}", normalized, ex.getMessage());
return result;
}
for (int i = 0; i < normalized.size(); i++) {
String raw = values != null && i < values.size() ? values.get(i) : null;
try {
result.put(normalized.get(i), raw == null || raw.isBlank() ? 0L : Long.parseLong(raw));
} catch (NumberFormatException ignored) {
result.put(normalized.get(i), 0L);
}
}
return result;
}
public void touchTaskHeartbeat(Long taskId) {
if (taskId == null || taskId <= 0) {
return;
}
try {
stringRedisTemplate.opsForValue().set(
buildTaskHeartbeatKey(taskId),
String.valueOf(Instant.now().toEpochMilli()),
Duration.ofHours(PAYLOAD_TTL_HOURS));
} catch (Exception ex) {
log.warn("[withdraw-cache] touch heartbeat degraded taskId={} msg={}", taskId, ex.getMessage());
}
}
public void deleteTaskCache(Long taskId) {
if (taskId == null || taskId <= 0) {
return;
}
taskEntityLocalCache.evict(taskId);
try {
stringRedisTemplate.delete(buildTaskHeartbeatKey(taskId));
stringRedisTemplate.delete(buildTaskEntityKey(taskId));
} catch (Exception ex) {
log.warn("[withdraw-cache] delete cache degraded taskId={} msg={}", taskId, ex.getMessage());
}
taskScopePayloadStorageService.deleteTaskScopePayloads(taskId, MODULE_TYPE);
}
public void saveTaskCache(FileTaskEntity task) {
if (task == null || task.getId() == null) {
return;
}
taskEntityLocalCache.put(task.getId(), task);
try {
stringRedisTemplate.opsForValue().set(
buildTaskEntityKey(task.getId()),
objectMapper.writeValueAsString(task),
Duration.ofHours(PAYLOAD_TTL_HOURS));
} catch (Exception ignored) {
}
}
public Map<Long, FileTaskEntity> getTaskCacheBatch(List<Long> taskIds) {
Map<Long, FileTaskEntity> result = new LinkedHashMap<>();
if (taskIds == null || taskIds.isEmpty()) {
return result;
}
List<Long> normalized = taskIds.stream().filter(id -> id != null && id > 0).distinct().toList();
long now = System.currentTimeMillis();
List<Long> missingIds = new ArrayList<>();
for (Long taskId : normalized) {
TaskEntityLocalCache.Entry cached = taskEntityLocalCache.get(taskId, now);
if (cached != null) {
result.put(taskId, cached.task());
} else {
missingIds.add(taskId);
}
}
if (missingIds.isEmpty()) {
return result;
}
List<String> keys = missingIds.stream().map(this::buildTaskEntityKey).toList();
List<String> values;
try {
values = stringRedisTemplate.opsForValue().multiGet(keys);
} catch (Exception ex) {
log.warn("[withdraw-cache] batch get task cache degraded taskIds={} msg={}", missingIds, ex.getMessage());
return result;
}
for (int i = 0; i < missingIds.size(); i++) {
Long taskId = missingIds.get(i);
String raw = values != null && i < values.size() ? values.get(i) : null;
if (raw == null || raw.isBlank()) {
continue;
}
try {
FileTaskEntity task = objectMapper.readValue(raw, FileTaskEntity.class);
result.put(taskId, task);
taskEntityLocalCache.put(taskId, task);
} catch (Exception ignored) {
}
}
return result;
}
private String buildTaskHeartbeatKey(Long taskId) {
return "withdraw:task:heartbeat:" + taskId;
}
private String buildTaskEntityKey(Long taskId) {
return "withdraw:task:entity:" + taskId;
} }
} }
@@ -11,7 +11,7 @@ import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.security.AdminAuthSupport; import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.config.TaskPressureProperties; import com.nanri.aiimage.config.TaskPressureProperties;
import com.nanri.aiimage.modules.file.service.oss.OssStorageService; import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
import com.nanri.aiimage.common.model.vo.ProductRiskDashboardVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskDashboardVo;
import com.nanri.aiimage.modules.task.mapper.FileResultMapper; import com.nanri.aiimage.modules.task.mapper.FileResultMapper;
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper; import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
import com.nanri.aiimage.modules.task.model.entity.FileResultEntity; import com.nanri.aiimage.modules.task.model.entity.FileResultEntity;
@@ -4,8 +4,8 @@ import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.nanri.aiimage.common.exception.BusinessException; import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.model.vo.ProductRiskCandidateVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCandidateVo;
import com.nanri.aiimage.common.model.vo.ProductRiskMatchShopsVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskMatchShopsVo;
import com.nanri.aiimage.modules.patroldelete.mapper.PatrolDeleteConditionMapper; import com.nanri.aiimage.modules.patroldelete.mapper.PatrolDeleteConditionMapper;
import com.nanri.aiimage.modules.patroldelete.mapper.PatrolDeleteShopCandidateMapper; import com.nanri.aiimage.modules.patroldelete.mapper.PatrolDeleteShopCandidateMapper;
import com.nanri.aiimage.modules.patroldelete.model.entity.PatrolDeleteShopCandidateEntity; import com.nanri.aiimage.modules.patroldelete.model.entity.PatrolDeleteShopCandidateEntity;
@@ -4,8 +4,8 @@ import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.nanri.aiimage.common.exception.BusinessException; import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.model.vo.ProductRiskCandidateVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCandidateVo;
import com.nanri.aiimage.common.model.vo.ProductRiskMatchShopsVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskMatchShopsVo;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest;
import com.nanri.aiimage.modules.queryasin.mapper.QueryAsinShopCandidateMapper; import com.nanri.aiimage.modules.queryasin.mapper.QueryAsinShopCandidateMapper;
@@ -5,9 +5,9 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.exception.BusinessException; import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.model.vo.ProductRiskCandidateVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCandidateVo;
import com.nanri.aiimage.common.model.vo.ProductRiskCountryPreferenceVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCountryPreferenceVo;
import com.nanri.aiimage.common.model.vo.ProductRiskMatchShopsVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskMatchShopsVo;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCountryPreferenceSaveRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCountryPreferenceSaveRequest;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest;
@@ -4,8 +4,8 @@ import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.nanri.aiimage.common.exception.BusinessException; import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.model.vo.ProductRiskCandidateVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCandidateVo;
import com.nanri.aiimage.common.model.vo.ProductRiskMatchShopsVo; import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskMatchShopsVo;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest; import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest;
import com.nanri.aiimage.modules.withdraw.mapper.WithdrawShopCandidateMapper; import com.nanri.aiimage.modules.withdraw.mapper.WithdrawShopCandidateMapper;
@@ -106,6 +106,7 @@ import {
} from '@/shared/dispatch-guard' } from '@/shared/dispatch-guard'
import { passGuard } from '@/shared/dispatch-guard-ui' import { passGuard } from '@/shared/dispatch-guard-ui'
import { formatDateTime } from '@/shared/utils/datetime' import { formatDateTime } from '@/shared/utils/datetime'
import { taskStatusClass, taskStatusText } from '@/shared/utils/task-status-text'
import { uploadPathsToJava } from '@/shared/utils/upload-to-java' import { uploadPathsToJava } from '@/shared/utils/upload-to-java'
import { runBatchDelete } from '@/shared/utils/batch-delete' import { runBatchDelete } from '@/shared/utils/batch-delete'
import { getModuleProgressLight } from '@/shared/api/progress-light.ts' import { getModuleProgressLight } from '@/shared/api/progress-light.ts'
@@ -720,24 +721,24 @@ function hasMeaningfulProgressValue(value: unknown) {
function statusText(item: AppearancePatentHistoryItem) { function statusText(item: AppearancePatentHistoryItem) {
const status = normalizeTaskStatus(item) const status = normalizeTaskStatus(item)
if (status === 'RUNNING') return '执行中' if (status === 'RUNNING') return taskStatusText(status)
if (status === 'PENDING') return '已解析,等待后台执行' if (status === 'PENDING') return taskStatusText(status, { text: { PENDING: '已解析,等待后台执行' } })
// 已判死(含心跳超时)的任务始终显示失败:后台检测可能仍在跑、文件生成后下载按钮会自动出现, // 已判死(含心跳超时)的任务始终显示失败:后台检测可能仍在跑、文件生成后下载按钮会自动出现,
// 但状态文案不能是"结果生成中",否则用户会以为任务还在执行(2026-09-28 29473/29479) // 但状态文案不能是"结果生成中",否则用户会以为任务还在执行(2026-09-28 29473/29479)
if (status === 'FAILED') return isResultBuildFailed(item) ? '结果生成失败' : '失败' if (status === 'FAILED') return isResultBuildFailed(item) ? '结果生成失败' : taskStatusText('FAILED')
if (isResultPreparing(item)) return '结果生成中' if (isResultPreparing(item)) return '结果生成中'
if (isResultBuildFailed(item)) return '结果生成失败' if (isResultBuildFailed(item)) return '结果生成失败'
if (hasDownloadableResult(item)) return '已完成' if (hasDownloadableResult(item)) return taskStatusText('SUCCESS')
if (status === 'SUCCESS' || item.success) return '已完成' if (status === 'SUCCESS' || item.success) return taskStatusText('SUCCESS')
return '等待中' return '等待中'
} }
function statusClass(item: AppearancePatentHistoryItem) { function statusClass(item: AppearancePatentHistoryItem) {
const status = normalizeTaskStatus(item) const status = normalizeTaskStatus(item)
if (status === 'FAILED' || isResultBuildFailed(item)) return 'failed' if (status === 'FAILED' || isResultBuildFailed(item)) return taskStatusClass('FAILED')
if (status === 'RUNNING' || isResultPreparing(item)) return 'running' if (status === 'RUNNING' || isResultPreparing(item)) return taskStatusClass('RUNNING')
if (hasDownloadableResult(item)) return 'success' if (hasDownloadableResult(item)) return taskStatusClass('SUCCESS')
if (status === 'SUCCESS' || item.success) return 'success' if (status === 'SUCCESS' || item.success) return taskStatusClass('SUCCESS')
return 'pending' return 'pending'
} }
@@ -126,6 +126,7 @@ import { createCategorizedTimers } from '@/shared/utils/categorized-timers'
import type { UploadFileVo } from '@/shared/api/upload.ts' import type { UploadFileVo } from '@/shared/api/upload.ts'
import type { UploadedFileRef } from '@/shared/api/types/upload.ts' import type { UploadedFileRef } from '@/shared/api/types/upload.ts'
import { formatDateTime } from '@/shared/utils/datetime' import { formatDateTime } from '@/shared/utils/datetime'
import { taskStatusClass, taskStatusText } from '@/shared/utils/task-status-text'
import { uploadPathsToJava } from '@/shared/utils/upload-to-java' import { uploadPathsToJava } from '@/shared/utils/upload-to-java'
import { runBatchDelete } from '@/shared/utils/batch-delete' import { runBatchDelete } from '@/shared/utils/batch-delete'
@@ -270,23 +271,30 @@ function errorMessage(item: BrandTaskItem) {
} }
function statusText(status?: BrandTaskItem['status']) { function statusText(status?: BrandTaskItem['status']) {
const value = (status || '').toLowerCase() // 与原实现一致:小写映射 + 未知状态原样展示(完全替换默认表)
const map: Record<string, string> = { return taskStatusText(status, {
pending: '排队中', replace: true,
running: '执行中', text: {
success: '已完成', PENDING: '排队中',
failed: '失败', RUNNING: '执行中',
cancelled: '已取消', SUCCESS: '已完成',
} FAILED: '失败',
return map[value] || String(status ?? '未知') CANCELLED: '已取消',
},
textFallback: String(status ?? '未知'),
})
} }
function statusClass(status?: BrandTaskItem['status']) { function statusClass(status?: BrandTaskItem['status']) {
const value = (status || '').toLowerCase() return taskStatusClass(status, {
if (value === 'running') return 'running' replace: true,
if (value === 'success') return 'success' classMap: {
if (value === 'failed') return 'failed' RUNNING: 'running',
return 'pending' SUCCESS: 'success',
FAILED: 'failed',
},
classFallback: 'pending',
})
} }
// ---- 右侧任务面板统一视图(TaskCenterPanel)---- // ---- 右侧任务面板统一视图(TaskCenterPanel)----
@@ -181,6 +181,7 @@ import {
} from '@/shared/api/java-modules' } from '@/shared/api/java-modules'
import { getPollingProgressBatch } from '@/shared/api/task-progress-polling.ts' import { getPollingProgressBatch } from '@/shared/api/task-progress-polling.ts'
import { formatDateTime } from '@/shared/utils/datetime' import { formatDateTime } from '@/shared/utils/datetime'
import { taskStatusClass, taskStatusText } from '@/shared/utils/task-status-text'
import { uploadPathsToJava } from '@/shared/utils/upload-to-java' import { uploadPathsToJava } from '@/shared/utils/upload-to-java'
import { runBatchDelete } from '@/shared/utils/batch-delete' import { runBatchDelete } from '@/shared/utils/batch-delete'
import { confirmTaskDeletion } from '@/shared/task-delete-confirm' import { confirmTaskDeletion } from '@/shared/task-delete-confirm'
@@ -540,19 +541,17 @@ function normalizeTaskStatus(item: CollectDataHistoryItem) {
function statusText(item: CollectDataHistoryItem) { function statusText(item: CollectDataHistoryItem) {
const status = normalizeTaskStatus(item) const status = normalizeTaskStatus(item)
if (status === 'RUNNING') return '执行中' if (status === 'RUNNING' || status === 'PENDING') return taskStatusText(status)
if (status === 'PENDING') return '等待中' if (status === 'SUCCESS' || item.success) return taskStatusText('SUCCESS')
if (status === 'SUCCESS' || item.success) return '已完成' return status === 'FAILED' ? taskStatusText('FAILED') : '等待中'
if (status === 'FAILED') return '失败'
return '等待中'
} }
function statusClass(item: CollectDataHistoryItem) { function statusClass(item: CollectDataHistoryItem) {
const status = normalizeTaskStatus(item) const status = normalizeTaskStatus(item)
if (status === 'RUNNING') return 'running' if (status === 'RUNNING' || status === 'FAILED') return taskStatusClass(status)
if (status === 'FAILED') return 'failed' if (status === 'SUCCESS' || item.success) return taskStatusClass('SUCCESS')
if (status === 'SUCCESS' || item.success) return 'success' // 其余(含 PENDING/CANCELLED)保持 'pending'
return 'pending' return taskStatusClass(status, { classMap: { CANCELLED: 'pending' } })
} }
function taskFilterSummary(item: CollectDataHistoryItem) { function taskFilterSummary(item: CollectDataHistoryItem) {
@@ -289,6 +289,7 @@ import {
sanitizeCountryCodes, sanitizeCountryCodes,
} from "@/shared/country-options"; } from "@/shared/country-options";
import { formatDateTime } from '@/shared/utils/datetime' import { formatDateTime } from '@/shared/utils/datetime'
import { RUNNING_UNTIL_DONE_STATUS_OPTIONS, taskStatusClass, taskStatusText } from '@/shared/utils/task-status-text'
import { useHistoryPolling } from '@/shared/composables/useHistoryPolling' import { useHistoryPolling } from '@/shared/composables/useHistoryPolling'
import { runBatchDelete } from '@/shared/utils/batch-delete' import { runBatchDelete } from '@/shared/utils/batch-delete'
import { isRecordMissingError } from '@/shared/utils/task-queue-state.ts' import { isRecordMissingError } from '@/shared/utils/task-queue-state.ts'
@@ -645,17 +646,11 @@ function isTaskTerminal(status?: string) {
} }
function statusText(status?: string) { function statusText(status?: string) {
if (status === "SUCCESS" || status === "COMPLETED") return "已完成"; return taskStatusText(status, RUNNING_UNTIL_DONE_STATUS_OPTIONS);
if (status === "FAILED") return "失败";
return "执行中";
} }
function statusClass(status?: string) { function statusClass(status?: string) {
return status === "SUCCESS" || status === "COMPLETED" return taskStatusClass(status, RUNNING_UNTIL_DONE_STATUS_OPTIONS);
? "success"
: status === "FAILED"
? "failed"
: "running";
} }
function canDownload(item: PatrolDeleteHistoryItem) { function canDownload(item: PatrolDeleteHistoryItem) {
@@ -267,6 +267,7 @@ import {
} from '@/shared/api/java-modules' } from '@/shared/api/java-modules'
import { getPollingProgressBatch } from '@/shared/api/task-progress-polling.ts' import { getPollingProgressBatch } from '@/shared/api/task-progress-polling.ts'
import { formatDateTime } from '@/shared/utils/datetime' import { formatDateTime } from '@/shared/utils/datetime'
import { taskStatusClass, taskStatusText } from '@/shared/utils/task-status-text'
import { uploadPathsToJava } from '@/shared/utils/upload-to-java' import { uploadPathsToJava } from '@/shared/utils/upload-to-java'
import { runBatchDelete } from '@/shared/utils/batch-delete' import { runBatchDelete } from '@/shared/utils/batch-delete'
import { isRecordMissingError } from '@/shared/utils/task-queue-state.ts' import { isRecordMissingError } from '@/shared/utils/task-queue-state.ts'
@@ -1643,18 +1644,15 @@ function resolvedTaskStatus(item: PriceTrackHistoryItem) {
function statusText(item: PriceTrackHistoryItem) { function statusText(item: PriceTrackHistoryItem) {
const st = resolvedTaskStatus(item) const st = resolvedTaskStatus(item)
if (st === 'SUCCESS') return '已完成' if (st === 'SUCCESS' || st === 'FAILED' || st === 'RUNNING') return taskStatusText(st)
if (st === 'FAILED') return '失败' return item.success ? taskStatusText('SUCCESS') : '等待中'
if (st === 'RUNNING') return '执行中'
if (item.success) return '已完成'
return '等待中'
} }
function statusClass(item: PriceTrackHistoryItem) { function statusClass(item: PriceTrackHistoryItem) {
const st = resolvedTaskStatus(item) const st = resolvedTaskStatus(item)
if (st === 'SUCCESS' || item.success) return 'success' if (st === 'SUCCESS' || item.success) return taskStatusClass('SUCCESS')
if (st === 'FAILED') return 'failed' // 其余(FAILED/未知状态)保持原实现:failed / running
return 'running' return taskStatusClass(st, { classMap: { CANCELLED: 'running' }, classFallback: 'running' })
} }
function canDownload(item: PriceTrackHistoryItem) { function canDownload(item: PriceTrackHistoryItem) {
@@ -183,6 +183,7 @@ import { passGuard } from '@/shared/dispatch-guard-ui'
import ZiniaoVersionSetting from '@/shared/components/ZiniaoVersionSetting.vue' import ZiniaoVersionSetting from '@/shared/components/ZiniaoVersionSetting.vue'
import { useZiniaoVersion } from '@/shared/utils/ziniao-version' import { useZiniaoVersion } from '@/shared/utils/ziniao-version'
import { formatDateTime } from '@/shared/utils/datetime' import { formatDateTime } from '@/shared/utils/datetime'
import { taskStatusClass, taskStatusText } from '@/shared/utils/task-status-text'
import { runBatchDelete } from '@/shared/utils/batch-delete' import { runBatchDelete } from '@/shared/utils/batch-delete'
import { isRecordMissingError } from '@/shared/utils/task-queue-state.ts' import { isRecordMissingError } from '@/shared/utils/task-queue-state.ts'
import { useTablePaging } from '@/shared/composables/useTablePaging.ts' import { useTablePaging } from '@/shared/composables/useTablePaging.ts'
@@ -1024,18 +1025,15 @@ function resolvedTaskStatus(item: ProductRiskHistoryItem) {
function statusText(item: ProductRiskHistoryItem) { function statusText(item: ProductRiskHistoryItem) {
const st = resolvedTaskStatus(item) const st = resolvedTaskStatus(item)
if (st === 'SUCCESS') return '已完成' if (st === 'SUCCESS' || st === 'FAILED' || st === 'RUNNING') return taskStatusText(st)
if (st === 'FAILED') return '失败' return item.success ? taskStatusText('SUCCESS') : '等待中'
if (st === 'RUNNING') return '执行中'
if (item.success) return '已完成'
return '等待中'
} }
function statusClass(item: ProductRiskHistoryItem) { function statusClass(item: ProductRiskHistoryItem) {
const st = resolvedTaskStatus(item) const st = resolvedTaskStatus(item)
if (st === 'SUCCESS' || item.success) return 'success' if (st === 'SUCCESS' || item.success) return taskStatusClass('SUCCESS')
if (st === 'FAILED') return 'failed' // 其余(FAILED/未知状态)保持原实现:failed / running
return 'running' return taskStatusClass(st, { classMap: { CANCELLED: 'running' }, classFallback: 'running' })
} }
function canDownload(item: ProductRiskHistoryItem) { function canDownload(item: ProductRiskHistoryItem) {
@@ -211,6 +211,7 @@ import { useTaskProgressLoop } from '@/shared/composables/useTaskProgressLoop'
import { getTaskPollIntervalMs } from '@/shared/task-progress-config' import { getTaskPollIntervalMs } from '@/shared/task-progress-config'
import { createCategorizedTimers } from '@/shared/utils/categorized-timers' import { createCategorizedTimers } from '@/shared/utils/categorized-timers'
import { saveUrlWithProgress } from '@/shared/utils/download-progress' import { saveUrlWithProgress } from '@/shared/utils/download-progress'
import { taskStatusClass, taskStatusText } from '@/shared/utils/task-status-text'
import { import {
EXCEL_EXTENSIONS, EXCEL_EXTENSIONS,
checkParseResult, checkParseResult,
@@ -1048,20 +1049,11 @@ function reconcileStoredQueue() {
} }
function statusText(status?: string | null) { function statusText(status?: string | null) {
const value = normalizeStatus(status) return taskStatusText(normalizeStatus(status))
if (value === 'SUCCESS' || value === 'COMPLETED') return '已完成'
if (value === 'FAILED') return '失败'
if (value === 'CANCELLED') return '已取消'
if (value === 'RUNNING') return '执行中'
return '等待中'
} }
function statusClass(status?: string | null) { function statusClass(status?: string | null) {
const value = normalizeStatus(status) return taskStatusClass(normalizeStatus(status))
if (value === 'SUCCESS' || value === 'COMPLETED') return 'success'
if (value === 'FAILED' || value === 'CANCELLED') return 'failed'
if (value === 'RUNNING') return 'running'
return 'pending'
} }
function matchText(file: PublishFileItem) { function matchText(file: PublishFileItem) {
@@ -231,6 +231,7 @@ import { passGuard } from "@/shared/dispatch-guard-ui";
import ZiniaoVersionSetting from "@/shared/components/ZiniaoVersionSetting.vue"; import ZiniaoVersionSetting from "@/shared/components/ZiniaoVersionSetting.vue";
import { useZiniaoVersion } from "@/shared/utils/ziniao-version"; import { useZiniaoVersion } from "@/shared/utils/ziniao-version";
import { formatDateTime } from '@/shared/utils/datetime' import { formatDateTime } from '@/shared/utils/datetime'
import { RUNNING_UNTIL_DONE_STATUS_OPTIONS, taskStatusClass, taskStatusText } from '@/shared/utils/task-status-text'
import { useHistoryPolling } from '@/shared/composables/useHistoryPolling' import { useHistoryPolling } from '@/shared/composables/useHistoryPolling'
import { runBatchDelete } from '@/shared/utils/batch-delete' import { runBatchDelete } from '@/shared/utils/batch-delete'
import { isRecordMissingError, parseTaskStartTimes, removeTaskStartTime, upsertTaskStartTime } from '@/shared/utils/task-queue-state.ts' import { isRecordMissingError, parseTaskStartTimes, removeTaskStartTime, upsertTaskStartTime } from '@/shared/utils/task-queue-state.ts'
@@ -518,17 +519,11 @@ function isTaskTerminal(status?: string) {
} }
function statusText(status?: string) { function statusText(status?: string) {
if (status === "SUCCESS" || status === "COMPLETED") return "已完成"; return taskStatusText(status, RUNNING_UNTIL_DONE_STATUS_OPTIONS);
if (status === "FAILED") return "失败";
return "执行中";
} }
function statusClass(status?: string) { function statusClass(status?: string) {
return status === "SUCCESS" || status === "COMPLETED" return taskStatusClass(status, RUNNING_UNTIL_DONE_STATUS_OPTIONS);
? "success"
: status === "FAILED"
? "failed"
: "running";
} }
function canDownload(item: QueryAsinHistoryItem) { function canDownload(item: QueryAsinHistoryItem) {
@@ -140,6 +140,7 @@ import { getPywebviewApi } from '@/shared/bridges/pywebview'
import { getTaskPollIntervalMs } from '@/shared/task-progress-config' import { getTaskPollIntervalMs } from '@/shared/task-progress-config'
import { createCategorizedTimers } from '@/shared/utils/categorized-timers' import { createCategorizedTimers } from '@/shared/utils/categorized-timers'
import { saveUrlWithProgress } from '@/shared/utils/download-progress' import { saveUrlWithProgress } from '@/shared/utils/download-progress'
import { taskStatusClass, taskStatusText } from '@/shared/utils/task-status-text'
import { checkQueuePayload } from '@/shared/dispatch-guard' import { checkQueuePayload } from '@/shared/dispatch-guard'
import { passGuard } from '@/shared/dispatch-guard-ui' import { passGuard } from '@/shared/dispatch-guard-ui'
import { mergeHistoryItems } from '@/shared/merge-history-items' import { mergeHistoryItems } from '@/shared/merge-history-items'
@@ -275,12 +276,11 @@ function isCountrySelectionLocked(code: string) { return orderedCountryCodes.val
function isTerminal(status?: string) { return status === 'SUCCESS' || status === 'FAILED' || status === 'COMPLETED' } function isTerminal(status?: string) { return status === 'SUCCESS' || status === 'FAILED' || status === 'COMPLETED' }
function statusText(item: ShopDataCrawlHistoryItem) { function statusText(item: ShopDataCrawlHistoryItem) {
const status = item.taskStatus const status = item.taskStatus
if (status === 'SUCCESS' || status === 'COMPLETED') return '已完成' if (status === 'SUCCESS' || status === 'COMPLETED' || status === 'FAILED') return taskStatusText(status)
if (status === 'FAILED') return '失败'
if (item.success === true) return '结果文件生成中' if (item.success === true) return '结果文件生成中'
return '处理中' return '处理中'
} }
function statusClass(status?: string) { return status === 'SUCCESS' || status === 'COMPLETED' ? 'success' : status === 'FAILED' ? 'failed' : 'running' } function statusClass(status?: string) { return taskStatusClass(status, { classMap: { CANCELLED: 'running' }, classFallback: 'running' }) }
function canDownload(item: ShopDataCrawlHistoryItem) { return Boolean(item.resultId && (item.fileReady || item.downloadUrl)) } function canDownload(item: ShopDataCrawlHistoryItem) { return Boolean(item.resultId && (item.fileReady || item.downloadUrl)) }
function formatDateTime(value?: string) { if (!value) return '-'; const date = new Date(value); return Number.isNaN(date.getTime()) ? value : date.toLocaleString('zh-CN', { hour12: false }) } function formatDateTime(value?: string) { if (!value) return '-'; const date = new Date(value); return Number.isNaN(date.getTime()) ? value : date.toLocaleString('zh-CN', { hour12: false }) }
function formatMatchStatus(status?: string) { return ({ MATCHED: '已匹配', PENDING: '待匹配', CONFLICT: '需确认', INDEX_STALE: '匹配已过期' } as Record<string, string>)[status || ''] || status || '-' } function formatMatchStatus(status?: string) { return ({ MATCHED: '已匹配', PENDING: '待匹配', CONFLICT: '需确认', INDEX_STALE: '匹配已过期' } as Record<string, string>)[status || ''] || status || '-' }
@@ -129,6 +129,7 @@ import { getPywebviewApi } from '@/shared/bridges/pywebview'
import { getTaskPollIntervalMs } from '@/shared/task-progress-config' import { getTaskPollIntervalMs } from '@/shared/task-progress-config'
import { createCategorizedTimers } from '@/shared/utils/categorized-timers' import { createCategorizedTimers } from '@/shared/utils/categorized-timers'
import { saveUrlWithProgress } from '@/shared/utils/download-progress' import { saveUrlWithProgress } from '@/shared/utils/download-progress'
import { taskStatusClass, taskStatusText } from '@/shared/utils/task-status-text'
import { useZiniaoVersion } from '@/shared/utils/ziniao-version' import { useZiniaoVersion } from '@/shared/utils/ziniao-version'
import { runBatchDelete } from '@/shared/utils/batch-delete' import { runBatchDelete } from '@/shared/utils/batch-delete'
import { useTablePaging } from '@/shared/composables/useTablePaging.ts' import { useTablePaging } from '@/shared/composables/useTablePaging.ts'
@@ -531,8 +532,8 @@ function resolvedTaskStatus(item: ShopMatchHistoryItem) {
function taskSnapshotOf(item: ShopMatchHistoryItem) { const taskId = normalizeTaskId(item.taskId); return taskId ? taskSnapshots.value[taskId] : undefined } function taskSnapshotOf(item: ShopMatchHistoryItem) { const taskId = normalizeTaskId(item.taskId); return taskId ? taskSnapshots.value[taskId] : undefined }
function currentTaskStageText(item: ShopMatchHistoryItem) { const snapshot = taskSnapshotOf(item); const task = snapshot?.task; const stages = task?.scheduleStages || []; if (!stages.length) return ''; const total = stages.length; const activeIndex = typeof task?.activeStageIndex === 'number' ? task.activeStageIndex : undefined; const currentIndex = typeof task?.currentStageIndex === 'number' ? task.currentStageIndex : undefined; if (typeof activeIndex === 'number') return `执行进度: 第 ${activeIndex + 1}/${total} 次执行中`; if (typeof currentIndex === 'number') return `执行进度: 等待第 ${currentIndex + 1}/${total} 次`; return `执行进度: 共 ${total} 次`} function currentTaskStageText(item: ShopMatchHistoryItem) { const snapshot = taskSnapshotOf(item); const task = snapshot?.task; const stages = task?.scheduleStages || []; if (!stages.length) return ''; const total = stages.length; const activeIndex = typeof task?.activeStageIndex === 'number' ? task.activeStageIndex : undefined; const currentIndex = typeof task?.currentStageIndex === 'number' ? task.currentStageIndex : undefined; if (typeof activeIndex === 'number') return `执行进度: 第 ${activeIndex + 1}/${total} 次执行中`; if (typeof currentIndex === 'number') return `执行进度: 等待第 ${currentIndex + 1}/${total} 次`; return `执行进度: 共 ${total} 次`}
function nextScheduledDisplay(item: ShopMatchHistoryItem) { const snapshot = taskSnapshotOf(item); const task = snapshot?.task; const stages = task?.scheduleStages || []; if (typeof task?.currentStageIndex === 'number') { const stage = stages.find((entry) => entry.stageIndex === task.currentStageIndex); if (stage?.scheduledAt) return formatMonthDayTime(stage.scheduledAt) } if (item.scheduledAt) return formatMonthDayTime(item.scheduledAt); return '' } function nextScheduledDisplay(item: ShopMatchHistoryItem) { const snapshot = taskSnapshotOf(item); const task = snapshot?.task; const stages = task?.scheduleStages || []; if (typeof task?.currentStageIndex === 'number') { const stage = stages.find((entry) => entry.stageIndex === task.currentStageIndex); if (stage?.scheduledAt) return formatMonthDayTime(stage.scheduledAt) } if (item.scheduledAt) return formatMonthDayTime(item.scheduledAt); return '' }
function statusText(item: ShopMatchHistoryItem) { const status = resolvedTaskStatus(item); if (status === 'SCHEDULED') return '待执行'; if (status === 'RUNNING') return '执行中'; if (status === 'SUCCESS' || status === 'COMPLETED') return '已完成'; if (status === 'FAILED') return '失败'; return item.success ? '已完成' : '未知' } function statusText(item: ShopMatchHistoryItem) { const status = resolvedTaskStatus(item); if (['SCHEDULED', 'RUNNING', 'SUCCESS', 'COMPLETED', 'FAILED'].includes(status)) return taskStatusText(status); return item.success ? taskStatusText('SUCCESS') : '未知' }
function statusClass(item: ShopMatchHistoryItem) { const status = resolvedTaskStatus(item); return status === 'SUCCESS' || status === 'COMPLETED' ? 'success' : status === 'FAILED' ? 'failed' : 'running' } function statusClass(item: ShopMatchHistoryItem) { const status = resolvedTaskStatus(item); return taskStatusClass(status, { classMap: { SCHEDULED: 'running', CANCELLED: 'running' }, classFallback: 'running' }) }
// 失败任务也可能有已跑出来的部分结果文件(后端按已收分片组装),有文件就允许下载 // 失败任务也可能有已跑出来的部分结果文件(后端按已收分片组装),有文件就允许下载
function canDownload(item: ShopMatchHistoryItem) { const status = resolvedTaskStatus(item); return !!item.resultId && (!!item.fileReady || !!item.downloadUrl) && (status === 'SUCCESS' || status === 'COMPLETED' || status === 'FAILED') } function canDownload(item: ShopMatchHistoryItem) { const status = resolvedTaskStatus(item); return !!item.resultId && (!!item.fileReady || !!item.downloadUrl) && (status === 'SUCCESS' || status === 'COMPLETED' || status === 'FAILED') }
async function downloadResult(item: ShopMatchHistoryItem) { if (!item.resultId) return; const url = getShopMatchResultDownloadUrl(item.resultId); const filename = item.outputFilename || `${item.shopName || 'result'}.xlsx`; const result = await saveUrlWithProgress(url, filename, `shop-match:${item.resultId}`); if (result.success) ElMessage.success(`已保存: ${result.path || filename}`); else if (result.error && result.error !== '用户取消') ElMessage.error(result.error) } async function downloadResult(item: ShopMatchHistoryItem) { if (!item.resultId) return; const url = getShopMatchResultDownloadUrl(item.resultId); const filename = item.outputFilename || `${item.shopName || 'result'}.xlsx`; const result = await saveUrlWithProgress(url, filename, `shop-match:${item.resultId}`); if (result.success) ElMessage.success(`已保存: ${result.path || filename}`); else if (result.error && result.error !== '用户取消') ElMessage.error(result.error) }
@@ -151,6 +151,7 @@ import {
} from '@/shared/dispatch-guard' } from '@/shared/dispatch-guard'
import { passGuard } from '@/shared/dispatch-guard-ui' import { passGuard } from '@/shared/dispatch-guard-ui'
import { formatDateTime } from '@/shared/utils/datetime' import { formatDateTime } from '@/shared/utils/datetime'
import { taskStatusClass, taskStatusText } from '@/shared/utils/task-status-text'
import { uploadPathsToJava } from '@/shared/utils/upload-to-java' import { uploadPathsToJava } from '@/shared/utils/upload-to-java'
import { runBatchDelete } from '@/shared/utils/batch-delete' import { runBatchDelete } from '@/shared/utils/batch-delete'
import { confirmTaskDeletion } from '@/shared/task-delete-confirm' import { confirmTaskDeletion } from '@/shared/task-delete-confirm'
@@ -871,20 +872,19 @@ function hasMeaningfulProgressValue(value: unknown) {
function statusText(item: SimilarAsinHistoryItem) { function statusText(item: SimilarAsinHistoryItem) {
const status = normalizeTaskStatus(item) const status = normalizeTaskStatus(item)
if (status === 'RUNNING') return '执行中' if (status === 'RUNNING') return taskStatusText(status)
if (status === 'PENDING') return '已解析待启动' if (status === 'PENDING') return taskStatusText(status, { text: { PENDING: '已解析待启动' } })
if (isResultPreparing(item)) return '结果生成中' if (isResultPreparing(item)) return '结果生成中'
if (isResultBuildFailed(item)) return '结果生成失败' if (isResultBuildFailed(item)) return '结果生成失败'
if (status === 'SUCCESS' || item.success) return '已完成' if (status === 'SUCCESS' || item.success) return taskStatusText('SUCCESS')
if (status === 'FAILED') return '失败' return status === 'FAILED' ? taskStatusText('FAILED') : '等待中'
return '等待中'
} }
function statusClass(item: SimilarAsinHistoryItem) { function statusClass(item: SimilarAsinHistoryItem) {
const status = normalizeTaskStatus(item) const status = normalizeTaskStatus(item)
if (status === 'RUNNING' || isResultPreparing(item)) return 'running' if (status === 'RUNNING' || isResultPreparing(item)) return taskStatusClass('RUNNING')
if (isResultBuildFailed(item) || status === 'FAILED') return 'failed' if (isResultBuildFailed(item) || status === 'FAILED') return taskStatusClass('FAILED')
if (status === 'SUCCESS' || item.success) return 'success' if (status === 'SUCCESS' || item.success) return taskStatusClass('SUCCESS')
return 'pending' return 'pending'
} }
@@ -167,6 +167,7 @@ import {
type VariantTaskListItem, type VariantTaskListItem,
} from '@/shared/bridges/pywebview' } from '@/shared/bridges/pywebview'
import { saveUrlWithProgress } from '@/shared/utils/download-progress' import { saveUrlWithProgress } from '@/shared/utils/download-progress'
import { taskStatusClass, taskStatusText, type TaskStatusOptions } from '@/shared/utils/task-status-text'
/** 本地任务记录:桥接返回的列表项 + 前端维护字段 */ /** 本地任务记录:桥接返回的列表项 + 前端维护字段 */
type VariantEntry = VariantTaskListItem & { type VariantEntry = VariantTaskListItem & {
@@ -222,12 +223,32 @@ function taskMeta(item: VariantEntry) {
return item.meta || {} return item.meta || {}
} }
// 数字状态(0-3) + 字符串状态的混合映射,与统一默认表不一致,整体走 overrides
const VARIANT_STATUS_OVERRIDES: TaskStatusOptions = {
text: {
'': '排队中',
'0': '排队中',
'1': '执行中',
'2': '已完成',
'3': '失败',
PENDING: '排队中',
PROCESSING: '执行中',
DONE: '已完成',
ERROR: '失败',
STOPPED: '已取消',
},
classMap: {
'1': 'running',
'2': 'success',
'3': 'failed',
DONE: 'success',
ERROR: 'failed',
CANCELLED: 'pending',
},
}
function statusClass(status?: number | string) { function statusClass(status?: number | string) {
const numeric = Number(status) return taskStatusClass(String(status ?? ''), VARIANT_STATUS_OVERRIDES)
if (numeric === 1 || /running|processing/i.test(String(status))) return 'running'
if (numeric === 2 || /success|completed|done/i.test(String(status))) return 'success'
if (numeric === 3 || /failed|error/i.test(String(status))) return 'failed'
return 'pending'
} }
let autoTimer: number | null = null let autoTimer: number | null = null
@@ -247,14 +268,11 @@ function baseName(path: string) {
} }
function statusText(status?: number | string) { function statusText(status?: number | string) {
const numeric = Number(status) if (status === undefined || status === null) return '未知'
if (numeric === 0 || String(status).toLowerCase() === 'pending' || String(status).toLowerCase() === 'queued') return '排队中' return taskStatusText(String(status), {
if (numeric === 1 || String(status).toLowerCase() === 'running' || String(status).toLowerCase() === 'processing') return '执行中' ...VARIANT_STATUS_OVERRIDES,
if (numeric === 2 || ['success', 'completed', 'done'].includes(String(status).toLowerCase())) return '已完成' textFallback: String(status),
if (numeric === 3 || ['failed', 'error'].includes(String(status).toLowerCase())) return '失败' })
if (['cancelled', 'stopped'].includes(String(status).toLowerCase())) return '已取消'
if (status === 0 || status === 1 || status === 2 || status === 3) return statusText(String(status))
return String(status ?? '未知')
} }
function isBusy(status?: number | string) { function isBusy(status?: number | string) {
@@ -271,6 +271,7 @@ import { passGuard } from "@/shared/dispatch-guard-ui";
import ZiniaoVersionSetting from "@/shared/components/ZiniaoVersionSetting.vue"; import ZiniaoVersionSetting from "@/shared/components/ZiniaoVersionSetting.vue";
import { useZiniaoVersion } from "@/shared/utils/ziniao-version"; import { useZiniaoVersion } from "@/shared/utils/ziniao-version";
import { formatDateTime } from '@/shared/utils/datetime' import { formatDateTime } from '@/shared/utils/datetime'
import { RUNNING_UNTIL_DONE_STATUS_OPTIONS, taskStatusClass, taskStatusText } from '@/shared/utils/task-status-text'
import { useHistoryPolling } from '@/shared/composables/useHistoryPolling' import { useHistoryPolling } from '@/shared/composables/useHistoryPolling'
import { runBatchDelete } from '@/shared/utils/batch-delete' import { runBatchDelete } from '@/shared/utils/batch-delete'
import { isRecordMissingError, parseTaskStartTimes, removeTaskStartTime, upsertTaskStartTime } from '@/shared/utils/task-queue-state.ts' import { isRecordMissingError, parseTaskStartTimes, removeTaskStartTime, upsertTaskStartTime } from '@/shared/utils/task-queue-state.ts'
@@ -658,17 +659,11 @@ function isTaskTerminal(status?: string) {
} }
function statusText(status?: string) { function statusText(status?: string) {
if (status === "SUCCESS" || status === "COMPLETED") return "已完成"; return taskStatusText(status, RUNNING_UNTIL_DONE_STATUS_OPTIONS);
if (status === "FAILED") return "失败";
return "执行中";
} }
function statusClass(status?: string) { function statusClass(status?: string) {
return status === "SUCCESS" || status === "COMPLETED" return taskStatusClass(status, RUNNING_UNTIL_DONE_STATUS_OPTIONS);
? "success"
: status === "FAILED"
? "failed"
: "running";
} }
function canDownload(item: WithdrawTaskGroupItem) { function canDownload(item: WithdrawTaskGroupItem) {
@@ -1,70 +0,0 @@
/**
* API 高频响应 schema 注册表(Task 27)。
*
* 为 task/result 核心响应与各业务模块注册字段 schema,供
* createApiFieldCompatChecker 在契约测试与联调前做字段漂移检查。
* 模块级 schema 目前复用 task+result 组合,后续某模块有独特字段时
* 在其定义内覆盖/追加即可,无需改动检查器。
*/
import { createApiFieldCompatChecker, type ApiFieldSpec } from './api-field-compat.ts'
export interface ApiFieldSchemaDef {
/** schema 版本号,变更时递增 */
version: number
/** 字段名 → 规格 */
schema: Record<string, ApiFieldSpec>
}
/** task 核心响应字段 */
export const taskCoreSchema: Record<string, ApiFieldSpec> = {
taskId: { type: 'number', required: true },
status: { type: 'string', required: true },
progress: { type: 'number' },
message: { type: 'string' },
resultId: { type: 'number' },
}
/** 结果文件阶段字段(与 ResultFilePhase 对应,全部可选) */
export const resultFileSchema: Record<string, ApiFieldSpec> = {
fileStatus: { type: 'string' },
fileReady: { type: 'boolean' },
fileProgress: { type: 'number' },
fileProgressLabel: { type: 'string' },
fileProgressStage: { type: 'string' },
downloadUrl: { type: 'string' },
freshDownloadUrl: { type: 'string' },
}
/** task + result 组合 schema */
export const taskWithResultSchema: Record<string, ApiFieldSpec> = {
...taskCoreSchema,
...resultFileSchema,
}
const BUSINESS_MODULES = [
'dedupe', 'split', 'convert', 'publish', 'similar-asin', 'appearance-patent',
'delete-brand', 'price-track', 'shop-match', 'query-asin', 'withdraw',
'collect-data', 'image-video',
] as const
export const apiFieldSchemas: Record<string, ApiFieldSchemaDef> = {
task: { version: 1, schema: taskCoreSchema },
result: { version: 1, schema: resultFileSchema },
'task-result': { version: 1, schema: taskWithResultSchema },
}
for (const module of BUSINESS_MODULES) {
apiFieldSchemas[module] = { version: 1, schema: taskWithResultSchema }
}
export function getApiFieldSchema(name: string): ApiFieldSchemaDef | null {
return apiFieldSchemas[name] ?? null
}
export function createModuleFieldChecker(module: string) {
const def = getApiFieldSchema(module)
if (!def) {
throw new Error(`未注册的模块: ${module}`)
}
return createApiFieldCompatChecker({ schema: def.schema })
}
@@ -1,196 +0,0 @@
/**
* 构建拆包规划器(Task 91)。
*
* 为多页应用(MPA)生成 manualChunks 配置,按页面拆分公共业务 chunk:
* - vendorRules:全局 vendor 规则(Element Plus、Vue 运行时等),所有入口共享;
* - perEntryRules:按入口隔离的页面私有 chunk(匹配需同时给出 entryName,
* 未匹配入口的模块不会被误拆到其他页面 chunk);
* - 同名 chunk 的规则自动合并去重;未匹配模块返回 undefined,交由 Vite
* 默认拆包逻辑处理,不强制拆包。
*
* 有界配置:maxRules 限制 vendor 规则数量(超限拒绝计数,规则不生效);
* maxEntries 限制登记的入口数量(超出部分连同其 per-entry 规则一并跳过)。
* 校验失败(空 chunk 名、空 patterns、非法上限)fail-fast 抛错且零状态变更;
* 规则读取抛错(依赖故障)时调用失败,规划状态不被污染,恢复后可继续使用。
*/
export interface ChunkRule {
/** chunk 名,不能为空 */
chunk: string
/** 匹配模块 id 的字符串 pattern 列表,不能为空 */
patterns: string[]
}
export interface EntryChunkRule extends ChunkRule {
/** 关联的入口名,不能为空 */
entry: string
}
export interface ChunkPlannerOptions {
/** 页面入口列表;超过 maxEntries 的部分被跳过 */
entries: string[]
/** 全局 vendor 拆包规则;数量超过 maxRules 的部分被拒绝 */
vendorRules?: ChunkRule[]
/** 按入口隔离的页面 chunk 规则 */
perEntryRules?: EntryChunkRule[]
/** vendor 规则数量上限,必须为正数,默认 100 */
maxRules?: number
/** 登记的入口数量上限,必须为正数,默认 100 */
maxEntries?: number
}
export interface ChunkPlan {
entry: string
chunks: string[]
}
export interface ChunkPlannerStats {
entryCount: number
chunkCount: number
sharedChunkCount: number
unmatchedCount: number
rejectedCount: number
}
export interface ChunkPlanner {
/** 分配模块到 chunk;未匹配返回 undefined(交给 Vite 默认拆包) */
assign: (moduleId: string, entryName?: string) => string | undefined
/** 生成 manualChunks 配置对象(chunk 名 → patterns) */
manualChunksConfig: () => Record<string, string[]>
/** 每个入口实际包含的 chunk 清单(公共 vendor + 页面私有) */
planByEntry: () => ChunkPlan[]
stats: () => ChunkPlannerStats
}
export function createChunkPlanner(options: ChunkPlannerOptions): ChunkPlanner {
if (!Array.isArray(options.entries)) {
throw new Error('entries 必须是数组')
}
const maxRules = options.maxRules ?? 100
const maxEntries = options.maxEntries ?? 100
if (!(maxRules > 0)) {
throw new Error('maxRules 必须为正数: ' + maxRules)
}
if (!(maxEntries > 0)) {
throw new Error('maxEntries 必须为正数: ' + maxEntries)
}
const entries = options.entries.slice(0, maxEntries)
const entrySet = new Set(entries)
const vendorRules: Array<{ chunk: string; patterns: string[] }> = []
const perEntryRulesByEntry = new Map<string, Array<{ chunk: string; patterns: string[] }>>()
const chunkNames: string[] = []
let rejectedCount = 0
let unmatchedCount = 0
function validateRule(rule: ChunkRule) {
if (typeof rule.chunk !== 'string' || rule.chunk.length === 0) {
throw new Error('chunk 名不能为空')
}
if (!Array.isArray(rule.patterns) || rule.patterns.length === 0) {
throw new Error('patterns 必须是非空数组')
}
}
function mergePatterns(target: string[] | undefined, patterns: string[]): string[] {
const result = target ? [...target] : []
for (const pattern of patterns) {
if (!result.includes(pattern)) result.push(pattern)
}
return result
}
function addVendorRule(rule: ChunkRule) {
validateRule(rule)
if (vendorRules.length >= maxRules) {
rejectedCount += 1
return
}
const existing = vendorRules.find((item) => item.chunk === rule.chunk)
if (existing) {
existing.patterns = mergePatterns(existing.patterns, rule.patterns)
} else {
vendorRules.push({ chunk: rule.chunk, patterns: [...rule.patterns] })
chunkNames.push(rule.chunk)
}
}
function addPerEntryRule(rule: EntryChunkRule) {
if (typeof rule.entry !== 'string' || rule.entry.length === 0) {
throw new Error('entry 名不能为空')
}
validateRule(rule)
if (!entrySet.has(rule.entry)) return
const rules = perEntryRulesByEntry.get(rule.entry) ?? []
const existing = rules.find((item) => item.chunk === rule.chunk)
if (existing) {
existing.patterns = mergePatterns(existing.patterns, rule.patterns)
} else {
rules.push({ chunk: rule.chunk, patterns: [...rule.patterns] })
perEntryRulesByEntry.set(rule.entry, rules)
chunkNames.push(rule.chunk)
}
}
for (const rule of options.vendorRules ?? []) {
addVendorRule(rule)
}
for (const rule of options.perEntryRules ?? []) {
addPerEntryRule(rule)
}
function matches(patterns: string[], moduleId: string): boolean {
for (const pattern of patterns) {
if (moduleId.includes(pattern)) return true
}
return false
}
function assign(moduleId: string, entryName?: string): string | undefined {
for (const rule of vendorRules) {
if (matches(rule.patterns, moduleId)) return rule.chunk
}
if (entryName !== undefined) {
const rules = perEntryRulesByEntry.get(entryName)
if (rules) {
for (const rule of rules) {
if (matches(rule.patterns, moduleId)) return rule.chunk
}
}
}
unmatchedCount += 1
return undefined
}
function manualChunksConfig(): Record<string, string[]> {
const config: Record<string, string[]> = {}
for (const rule of vendorRules) {
config[rule.chunk] = mergePatterns(config[rule.chunk], rule.patterns).sort()
}
for (const rules of perEntryRulesByEntry.values()) {
for (const rule of rules) {
config[rule.chunk] = mergePatterns(config[rule.chunk], rule.patterns).sort()
}
}
return config
}
function planByEntry(): ChunkPlan[] {
const vendorNames = vendorRules.map((rule) => rule.chunk)
return entries.map((entry) => ({
entry,
chunks: [...vendorNames, ...(perEntryRulesByEntry.get(entry)?.map((rule) => rule.chunk) ?? [])],
}))
}
function stats(): ChunkPlannerStats {
return {
entryCount: entries.length,
chunkCount: chunkNames.length,
sharedChunkCount: vendorRules.length,
unmatchedCount,
rejectedCount,
}
}
return { assign, manualChunksConfig, planByEntry, stats }
}
@@ -1,189 +0,0 @@
/**
* 首屏传输大小报告(Task 92)。
*
* 在 manualChunks 拆包后比较各页面首屏传输大小:每个入口的首屏字节 =
* 入口自身 JS + 其依赖的全部 chunk 字节(公共 chunk 在多个入口之间共享,
* 计入各自首屏但只在报告总览里计一次)。
*
* 有界统计:maxEntries 限制统计的入口数量;maxChunksPerEntry 限制每个入口
* 计入的 chunk 数量(超出部分忽略,防止页面私有 chunk 过多导致报告失真)。
* 缺失入口的拆包计划或尺寸、缺失 chunk 尺寸、尺寸为负数均 fail-fast 抛错,
* 不产生部分结果;同一输入重复计算幂等,输入对象永不修改。
*/
export interface ChunkPlanEntry {
entry: string
chunks: string[]
}
export interface ChunkTransferReportOptions {
/** 页面入口列表(顺序即报告顺序);超过 maxEntries 的部分被跳过 */
entries: string[]
/** 每个入口的 chunk 清单(来自拆包规划) */
plans: ChunkPlanEntry[]
/** chunk 名 → 字节数(构建产物尺寸) */
chunkSizes: Record<string, number>
/** 入口 → 入口自身 JS 字节数 */
entrySizes: Record<string, number>
/** 统计的入口数量上限,必须为正数,默认 100 */
maxEntries?: number
/** 每个入口计入的 chunk 数量上限,必须为正数,默认 100 */
maxChunksPerEntry?: number
}
export interface ChunkTransferLine {
entry: string
entryBytes: number
chunkBytes: number
transferBytes: number
chunkCount: number
}
export interface ChunkTransferReport {
/** 按入口顺序排列的首屏传输明细 */
entries: ChunkTransferLine[]
/** 被多个入口共享的 chunk 总字节(每个共享 chunk 只计一次) */
sharedBytes: number
/** 全部入口 transferBytes 之和 */
totalBytes: number
/** 首屏最大的入口(空输入为 undefined) */
largest: ChunkTransferLine | undefined
/** 首屏最小的入口(空输入为 undefined) */
smallest: ChunkTransferLine | undefined
/** 单个入口的 chunk 字节明细(chunk 名 → 字节数,不含被截断的 chunk) */
chunkBytesOf: (line: ChunkTransferLine) => Record<string, number>
}
function isRecord(value: unknown): value is Record<string, unknown> {
return (
typeof value === 'object' &&
value != null &&
!Array.isArray(value) &&
Object.getPrototypeOf(value) === Object.prototype
)
}
export function createChunkTransferReport(options: ChunkTransferReportOptions): ChunkTransferReport {
if (!Array.isArray(options.entries)) {
throw new Error('entries 必须是数组')
}
if (!Array.isArray(options.plans)) {
throw new Error('plans 必须是数组')
}
if (!isRecord(options.chunkSizes)) {
throw new Error('chunkSizes 必须是对象')
}
if (!isRecord(options.entrySizes)) {
throw new Error('entrySizes 必须是对象')
}
const maxEntries = options.maxEntries ?? 100
const maxChunksPerEntry = options.maxChunksPerEntry ?? 100
if (!(maxEntries > 0)) {
throw new Error('maxEntries 必须为正数: ' + maxEntries)
}
if (!(maxChunksPerEntry > 0)) {
throw new Error('maxChunksPerEntry 必须为正数: ' + maxChunksPerEntry)
}
const entries = options.entries.slice(0, maxEntries)
const planByEntry = new Map<string, string[]>()
for (const plan of options.plans) {
planByEntry.set(plan.entry, plan.chunks)
}
for (const entry of entries) {
if (!planByEntry.has(entry)) {
throw new Error('缺少 entry 的拆包计划: ' + entry)
}
if (!(entry in options.entrySizes)) {
throw new Error('缺少 entry 尺寸: ' + entry)
}
}
const chunkSizes: Record<string, number> = {}
for (const key of Object.keys(options.chunkSizes)) {
const size = options.chunkSizes[key]
if (!(size >= 0)) {
throw new Error('尺寸不能为负数: ' + key + '=' + size)
}
chunkSizes[key] = size
}
const entrySizes: Record<string, number> = {}
for (const key of Object.keys(options.entrySizes)) {
const size = options.entrySizes[key]
if (!(size >= 0)) {
throw new Error('尺寸不能为负数: ' + key + '=' + size)
}
entrySizes[key] = size
}
const chunkPerEntry = new Map<string, number[]>()
for (const entry of entries) {
const chunks = planByEntry.get(entry) ?? []
const sizes: number[] = []
for (const chunk of chunks.slice(0, maxChunksPerEntry)) {
if (!(chunk in chunkSizes)) {
throw new Error('缺少 chunk 尺寸: ' + chunk)
}
sizes.push(chunkSizes[chunk])
}
chunkPerEntry.set(entry, sizes)
}
// 被多个入口共享的 chunk 总字节(每个共享 chunk 只计一次)
const chunkCountByEntry = new Map<string, Set<string>>()
let sharedBytes = 0
{
const counts = new Map<string, number>()
for (const entry of entries) {
for (const chunk of planByEntry.get(entry) ?? []) {
counts.set(chunk, (counts.get(chunk) ?? 0) + 1)
}
}
for (const [chunk, count] of counts) {
if (count > 1 && chunk in chunkSizes) {
sharedBytes += chunkSizes[chunk]
}
}
}
const lines: ChunkTransferLine[] = []
for (const entry of entries) {
const sizes = chunkPerEntry.get(entry) ?? []
const entryBytes = entrySizes[entry]
const chunkBytes = sizes.reduce((sum, size) => sum + size, 0)
lines.push({
entry,
entryBytes,
chunkBytes,
transferBytes: entryBytes + chunkBytes,
chunkCount: sizes.length,
})
chunkCountByEntry.set(entry, new Set(planByEntry.get(entry) ?? []))
}
let totalBytes = 0
for (const line of lines) totalBytes += line.transferBytes
let largest: ChunkTransferLine | undefined = undefined
let smallest: ChunkTransferLine | undefined = undefined
for (const line of lines) {
if (!largest || line.transferBytes > largest.transferBytes) largest = line
if (!smallest || line.transferBytes < smallest.transferBytes) smallest = line
}
function chunkBytesOf(line: ChunkTransferLine): Record<string, number> {
const chunks = planByEntry.get(line.entry) ?? []
const detail: Record<string, number> = {}
for (const chunk of chunks.slice(0, maxChunksPerEntry)) {
detail[chunk] = chunkSizes[chunk]
}
return detail
}
return {
entries: lines,
sharedBytes,
totalBytes,
largest,
smallest,
chunkBytesOf,
}
}
@@ -1,18 +0,0 @@
/**
* collectdata 下载判定(Task 146)。
*
* collectdata 后端 history VO 不返回 file 阶段字段;前端下载判定自洽:
* success=true 且 downloadUrl 非空才可下载(任务成功且有结果文件)。
*/
export interface CollectDataItem {
success?: boolean | null
downloadUrl?: string | null
taskStatus?: string | null
}
/** collectdata 可下载判定:success + downloadUrl(任务成功且有结果文件,空白 URL 无效)。 */
export function canDownloadCollectDataItem(item: CollectDataItem | null | undefined): boolean {
if (!item) return false
const url = typeof item.downloadUrl === 'string' ? item.downloadUrl.trim() : ''
return Boolean(item.success && url)
}
@@ -1,121 +0,0 @@
/**
* 健康检查执行器(Task 98)。
*
* 把真实启动、健康检查、核心请求、外部依赖调用四个验收探针收敛为
* 可注入探针的确定性流程:探针按序执行,全部产出结果与耗时。
*
* 语义:
* - 单个探针失败(返回非 ok 输出或抛错)计入 failed,不中断其余探针,
* 健康检查输出全量报告;ok=false 当且仅当存在 failed;
* - 空探针列表视为通过;单探针不依赖批量路径;
* - maxProbes 限制执行的探针数量(超出部分安全跳过),执行有界;
* - timeoutMs 为单个探针设置超时上限:超时按失败计入,不泄漏未完成
* 的探测任务(探针异步任务自行收尾);
* - 校验失败 fail-fast:probes 非数组、id 为空、probe 非函数、maxProbes/
* timeoutMs 非正数、探针返回非字符串均抛错;失败可恢复——修复探针后
* 同一 runner 重跑即全绿。
*/
export interface HealthProbe {
id: string
label: string
probe: () => Promise<string> | string
}
export interface HealthCheckOptions {
probes: HealthProbe[]
/** 执行的探针数量上限,必须为正数,默认 100 */
maxProbes?: number
/** 单个探针超时(毫秒),必须为正数,默认 30_000 */
timeoutMs?: number
}
export interface ProbeResult {
id: string
label: string
ok: boolean
output: string
durationMs: number
}
export interface HealthCheckResult {
ok: boolean
passed: ProbeResult[]
failed: ProbeResult[]
totalDurationMs: number
}
export interface HealthCheckRunner {
runAll: () => Promise<HealthCheckResult>
}
export function createHealthCheckRunner(options: HealthCheckOptions): HealthCheckRunner {
if (!Array.isArray(options.probes)) {
throw new Error('probes 必须是数组')
}
for (const probe of options.probes) {
if (typeof probe.id !== 'string' || probe.id.length === 0) {
throw new Error('probe id 不能为空')
}
if (typeof probe.probe !== 'function') {
throw new Error('probe 必须是函数')
}
}
const maxProbes = options.maxProbes ?? 100
const timeoutMs = options.timeoutMs ?? 30_000
if (!(maxProbes > 0)) {
throw new Error('maxProbes 必须为正数: ' + maxProbes)
}
if (!(timeoutMs > 0)) {
throw new Error('timeoutMs 必须为正数: ' + timeoutMs)
}
async function runOne(probe: HealthProbe): Promise<ProbeResult> {
const start = Date.now()
let output = ''
let failed = false
let timer: ReturnType<typeof setTimeout> | null = null
try {
const raw = await Promise.race([
Promise.resolve(probe.probe()),
new Promise<string>((resolve) => {
timer = setTimeout(() => resolve(''), timeoutMs)
}),
])
if (timer != null) clearTimeout(timer)
output = raw
} catch (error) {
if (timer != null) clearTimeout(timer)
failed = true
output = error instanceof Error ? error.message : String(error)
}
const durationMs = Date.now() - start
if (typeof output !== 'string') {
throw new Error('output 必须是字符串')
}
const ok = !failed && output !== ''
return { id: probe.id, label: probe.label, ok, output, durationMs }
}
async function runAll(): Promise<HealthCheckResult> {
const start = Date.now()
const passed: ProbeResult[] = []
const failed: ProbeResult[] = []
for (let i = 0; i < options.probes.length; i++) {
if (i >= maxProbes) break
const result = await runOne(options.probes[i])
if (result.ok) {
passed.push(result)
} else {
failed.push(result)
}
}
return {
ok: failed.length === 0,
passed,
failed,
totalDurationMs: Date.now() - start,
}
}
return { runAll }
}
@@ -1,180 +0,0 @@
/**
* 全链路压测记录器(Task 99)。
*
* 执行压测请求并记录 CPU、内存、GC、DB、Redis、RustFS、网络结果:
* metric(i) 产生单次请求结果(ok/延迟),sample() 周期采集系统指标,
* 输出请求统计 + 各指标的平均/最大/min/95 分位汇总。
*
* 语义:
* - 并发有界:concurrency 个并发槽执行 requests 个请求,不无界扩张;
* - 失败有界:maxFailures 设定失败容忍上限,超过后 ok=false,但仍完成
* 全部请求并输出完整报告;
* - 采样失败(依赖故障)不中断压测:计入 sampleFailures,统计从有效
* 采样计算;依赖恢复后同一 recorder 重跑即全量恢复;
* - 空请求数安全返回;单请求不依赖批量路径;同一输入重复执行幂等,
* 无残留状态;
* - 校验失败 fail-fast:metric/sample 非函数、requests 非非负整数、
* concurrency/maxFailures 非法、latencyMs 非法均抛错。
*/
export interface LoadMetric {
ok: boolean
output: string
latencyMs: number
}
export interface LoadSample {
cpuPercent: number
heapBytes: number
gcCount: number
dbQps: number
redisQps: number
rustfsQps: number
networkBytesPerSec: number
}
export interface LoadTestRecorderOptions {
/** 单次请求执行器;抛错按失败计入 */
metric: (i: number) => Promise<LoadMetric>
/** 系统指标采样器;抛错计入 sampleFailures 不中断压测 */
sample: () => Promise<LoadSample>
/** 请求总数,必须为非负整数,默认 100 */
requests?: number
/** 并发槽数量,必须为正数,默认 1 */
concurrency?: number
/** 失败容忍上限,必须为非负整数,默认 0 */
maxFailures?: number
/** 采样间隔(每 N 个请求采样一次),必须为正数,默认 1 */
sampleEvery?: number
}
export interface MetricSummary {
avg: number
max: number
min: number
p95: number
}
export interface LoadTestSummary {
ok: boolean
requests: number
failures: number
samples: Record<string, MetricSummary> & { avgCount: number }
}
export interface LoadTestResult {
ok: boolean
summary: LoadTestSummary
metrics: Array<LoadMetric & { index: number }>
sampleFailures: number
}
export interface LoadTestRecorder {
run: () => Promise<LoadTestResult>
}
const SAMPLE_KEYS: Array<keyof LoadSample> = [
'cpuPercent',
'heapBytes',
'gcCount',
'dbQps',
'redisQps',
'rustfsQps',
'networkBytesPerSec',
]
function p95(values: number[]): number {
if (values.length === 0) return 0
const sorted = [...values].sort((a, b) => a - b)
const index = Math.min(sorted.length - 1, Math.ceil(sorted.length * 0.95) - 1)
return sorted[index]
}
function summarize(values: number[]): MetricSummary {
return {
avg: values.length ? values.reduce((sum, v) => sum + v, 0) / values.length : 0,
max: values.length ? Math.max(...values) : 0,
min: values.length ? Math.min(...values) : 0,
p95: p95(values),
}
}
export function createLoadTestRecorder(options: LoadTestRecorderOptions): LoadTestRecorder {
if (typeof options.metric !== 'function') {
throw new Error('metric 必须是函数')
}
if (typeof options.sample !== 'function') {
throw new Error('sample 必须是函数')
}
const requests = options.requests ?? 100
const concurrency = options.concurrency ?? 1
const maxFailures = options.maxFailures ?? 0
const sampleEvery = options.sampleEvery ?? 1
if (!Number.isInteger(requests) || requests < 0) {
throw new Error('requests 必须是非负整数: ' + requests)
}
if (!(concurrency > 0)) {
throw new Error('concurrency 必须为正数: ' + concurrency)
}
if (!Number.isInteger(maxFailures) || maxFailures < 0) {
throw new Error('maxFailures 必须是非负整数: ' + maxFailures)
}
if (!(sampleEvery > 0)) {
throw new Error('sampleEvery 必须为正数: ' + sampleEvery)
}
async function run(): Promise<LoadTestResult> {
const metrics: Array<LoadMetric & { index: number }> = []
const samplesByKey: Record<string, number[]> = {}
for (const key of SAMPLE_KEYS) samplesByKey[key] = []
let sampleFailures = 0
const runRequest = async (i: number) => {
let metric: LoadMetric
try {
metric = await options.metric(i)
} catch (error) {
metric = { ok: false, output: error instanceof Error ? error.message : String(error), latencyMs: 0 }
}
if (typeof metric.latencyMs !== 'number' || !Number.isFinite(metric.latencyMs) || metric.latencyMs < 0) {
throw new Error('latencyMs 必须为非负数值: ' + metric.latencyMs)
}
metrics.push({ ...metric, index: i })
if (i % sampleEvery === 0) {
try {
const sample = await options.sample()
for (const key of SAMPLE_KEYS) {
samplesByKey[key].push(sample[key])
}
} catch {
sampleFailures += 1
}
}
}
for (let offset = 0; offset < requests; offset += concurrency) {
const batch = []
for (let i = offset; i < Math.min(offset + concurrency, requests); i++) {
batch.push(runRequest(i))
}
await Promise.all(batch)
}
metrics.sort((a, b) => a.index - b.index)
const failures = metrics.filter((m) => !m.ok).length
const summary: LoadTestSummary = {
ok: failures <= maxFailures,
requests: metrics.length,
failures,
samples: {
avgCount: samplesByKey.cpuPercent.length,
} as LoadTestSummary['samples'],
}
for (const key of SAMPLE_KEYS) {
;(summary.samples as Record<string, MetricSummary>)[key] = summarize(samplesByKey[key])
}
return { ok: failures <= maxFailures, summary, metrics, sampleFailures }
}
return { run }
}
@@ -1,143 +0,0 @@
/**
* localStorage 有界写入(Task 87)。
*
* 限制 localStorage 中任务、快照和队列数据的最大数量与字节数:
* - maxKeys:同页面下管理的 key 总数上限,超过时驱逐最旧写入的 key
* (更新已存在 key 会刷新其新鲜度,最近使用的 key 不被驱逐);
* - maxBytes:全部条目序列化后总字节数上限,超过时同样驱逐最旧条目;
* 单条超过上限的写入被拒绝,不产生部分数据;
* - 存储抛错(如 QuotaExceededError)时写入失败且不污染内部索引;
* - 重复写同一 key 幂等(只更新内容与新鲜度,不增加条数)。
*
* 纯 TS 模块:key 为空字符串时拒绝;JSON 序列化失败时拒绝写入并保持
* 原状态;remove/clear 释放全部条目。
*/
export interface LocalStorageLimiterOptions {
/** 底层存储(window.localStorage 的窄接口,便于测试注入) */
storage: {
getItem: (key: string) => string | null
setItem: (key: string, value: string) => void
removeItem: (key: string) => void
}
/** 管理的 key 总数上限,必须为正数 */
maxKeys: number
/** 全部条目序列化后总字节数上限,必须为正数 */
maxBytes: number
}
export interface LocalStorageLimiterStats {
/** 当前管理的 key 数 */
keyCount: number
/** 全部条目序列化后的总字节数 */
totalBytes: number
/** 被驱逐的条目数 */
evictedCount: number
}
const encoder = new TextEncoder()
export function createLocalStorageLimiter(options: LocalStorageLimiterOptions) {
const { storage, maxKeys, maxBytes } = options
if (!storage || typeof storage.getItem !== 'function' || typeof storage.setItem !== 'function') {
throw new Error('storage 必须提供 getItem/setItem/removeItem')
}
if (!(maxKeys > 0)) {
throw new Error('maxKeys 必须为正数: ' + maxKeys)
}
if (!(maxBytes > 0)) {
throw new Error('maxBytes 必须为正数: ' + maxBytes)
}
const order: string[] = []
let totalBytes = 0
let evictedCount = 0
function evictIfNeeded() {
while ((order.length > 0 && order.length > maxKeys) || (order.length > 0 && totalBytes > maxBytes)) {
const oldest = order.shift() as string
const raw = storage.getItem(oldest)
if (raw != null) {
totalBytes -= encoder.encode(raw).byteLength
storage.removeItem(oldest)
}
evictedCount += 1
}
}
function touch(key: string) {
const index = order.indexOf(key)
if (index >= 0) order.splice(index, 1)
order.push(key)
}
function write(key: string, value: unknown): boolean {
if (!key) return false
let raw: string
try {
raw = JSON.stringify(value)
} catch {
return false
}
if (raw == null) return false
const existing = storage.getItem(key)
const bytes = encoder.encode(raw).byteLength
if (bytes > maxBytes) return false
try {
storage.setItem(key, raw)
} catch {
return false
}
if (existing == null) {
totalBytes += bytes
touch(key)
evictIfNeeded()
} else {
totalBytes -= encoder.encode(existing).byteLength
totalBytes += bytes
touch(key)
evictIfNeeded()
}
return true
}
function read(key: string): unknown {
if (!key) return undefined
const raw = storage.getItem(key)
if (raw == null) return undefined
try {
return JSON.parse(raw)
} catch {
return undefined
}
}
function remove(key: string): boolean {
if (!key) return false
const raw = storage.getItem(key)
if (raw == null) return false
storage.removeItem(key)
totalBytes -= encoder.encode(raw).byteLength
const index = order.indexOf(key)
if (index >= 0) order.splice(index, 1)
return true
}
function clear() {
for (const key of order) storage.removeItem(key)
order.length = 0
totalBytes = 0
evictedCount = 0
}
function stats(): LocalStorageLimiterStats {
return {
keyCount: order.length,
totalBytes,
evictedCount,
}
}
return { write, read, remove, clear, stats }
}
export type LocalStorageLimiter = ReturnType<typeof createLocalStorageLimiter>
@@ -1,59 +0,0 @@
/**
* live progress 合并(Task 144)。
*
* history 条目缺失 file 进度时保留实时缓存(live)进度;live 有值且有效时
* 以 live 为准;history 已可下载(fileReady/downloadUrl 就绪)时不覆盖——
* 结果文件既成,实时进度无意义。纯函数无副作用,入参不修改。
*/
export const FILE_PROGRESS_KEYS = [
'fileProgressPercent',
'fileProgressCurrent',
'fileProgressTotal',
'fileProgressMessage',
'fileReady',
'fileStatus',
] as const
export interface DownloadableItem {
resultId?: number | null
fileReady?: boolean | null
fileStatus?: string | null
downloadUrl?: string | null
}
/**
* 可下载判定(Task 145):有结果记录,且满足其一——
* fileReady=true、downloadUrl 非空、fileStatus 为终态(SUCCESS 可下载结果文件,
* FAILED 可下载错误文件或用于展示错误)。
*/
export function canDownloadItem(item: DownloadableItem | null | undefined): boolean {
if (!item) return false
const ready = item.fileReady || Boolean(item.downloadUrl)
const terminalStatus = item.fileStatus === 'SUCCESS' || item.fileStatus === 'FAILED'
return Boolean(item.resultId && (ready || terminalStatus))
}
/** 有效进度值:仅 fileProgressPercent 需要严格 >0,其余字段非空即可(0 是合法计数)。 */
export function hasMeaningfulProgress(key: string, value: unknown): boolean {
if (value === undefined || value === null) return false
if (key === 'fileProgressPercent' && typeof value === 'number') {
return value > 0
}
return true
}
export function mergeHistoryItemPreservingLiveProgress<T extends Record<string, unknown>>(
history: T,
live: T | undefined,
): T {
if (!live) return history
if (canDownloadItem(history as unknown as DownloadableItem)) return history
const result: Record<string, unknown> = { ...history }
for (const key of FILE_PROGRESS_KEYS) {
const value = (live as Record<string, unknown>)[key]
if (hasMeaningfulProgress(key, value)) {
result[key] = value
}
}
return result as T
}
@@ -1,116 +0,0 @@
/**
* 页面卸载清理注册表(Task 89)。
*
* 统一管理页面级需在卸载时释放的资源:
* - 临时 URL:createObjectURL 产生的 blob URL 通过 revokeUrl 释放;
* - 请求:AbortController 在卸载时 abort(registerRequestController);
* - timer:dispose 时委托 hooks.clearTimers()(配合 categorized-timers
* 的 clearScope 全量清理)。
*
* dispose 幂等:第二次及以后的调用不再重复清理;dispose 后注册被拒绝
* 并计数。revokeUrl 抛错不中断其余条目的清理;maxTrackedUrls(默认不
* 限)用于限制同一页面累计注册的 URL 数量,超过时只保留最近注册的。
*/
export interface PageCleanupRegistryHooks {
/** 卸载时清空全部 timer */
clearTimers: () => void
/** 卸载时释放单个 blob URL */
revokeUrl: (url: string) => void
}
export interface PageCleanupRegistryOptions {
/** 累计跟踪的 URL 上限(0 表示不限制),超过时只保留最近注册的 */
maxTrackedUrls?: number
}
export function createPageCleanupRegistry(
hooks: PageCleanupRegistryHooks,
options: PageCleanupRegistryOptions = {},
) {
if (typeof hooks?.clearTimers !== 'function') {
throw new Error('clearTimers 必须是函数')
}
if (typeof hooks?.revokeUrl !== 'function') {
throw new Error('revokeUrl 必须是函数')
}
const maxTrackedUrls = options.maxTrackedUrls ?? 0
const urls: string[] = []
const controllers: AbortController[] = []
let disposed = false
let disposeCount = 0
let rejectedCount = 0
function registerObjectUrl(url: string): string {
if (disposed) {
rejectedCount += 1
return url
}
if (!url) return url
urls.push(url)
if (maxTrackedUrls > 0 && urls.length > maxTrackedUrls) {
urls.splice(0, urls.length - maxTrackedUrls)
}
return url
}
function registerRequestController(controller: AbortController) {
if (disposed) {
rejectedCount += 1
return
}
controllers.push(controller)
}
function dispose(): number {
if (disposed) {
return 0
}
disposed = true
let released = 0
for (const url of urls) {
try {
hooks.revokeUrl(url)
} catch {
/* 单个 URL 释放失败不中断其余清理 */
}
released += 1
}
urls.length = 0
for (const controller of controllers) {
try {
controller.abort()
} catch {
/* abort 失败不中断其余清理 */
}
released += 1
}
controllers.length = 0
hooks.clearTimers()
released += 1
disposeCount += 1
return released
}
return {
get trackedUrlCount() {
return urls.length
},
get requestCount() {
return controllers.length
},
get disposed() {
return disposed
},
get disposeCount() {
return disposeCount
},
get rejectedCount() {
return rejectedCount
},
registerObjectUrl,
registerRequestController,
dispose,
}
}
export type PageCleanupRegistry = ReturnType<typeof createPageCleanupRegistry>
@@ -1,99 +0,0 @@
/**
* 页面 E2E 核心路径执行器(Task 93)。
*
* 把 Similar ASIN / 店铺抓取 / 采集数据三个页面的 E2E 核心路径收敛为
* 可注入依赖的确定性流程:解析文件(parse)→ 建任务(createTask)→
* 轮询到终态(poll)→ 刷新历史(refreshHistory),各步由 deps 提供实现,
* 页面接入时传真实接口即可复用同一套语义。
*
* 流程语义:
* - parse 返回 0 表示无可处理数据:安全跳过后续步骤,不创建无效资源;
* - poll 返回非终态时按最大次数轮询,超过 maxPollAttempts 降级返回当前
* 状态(不抛错、不无限轮询、不刷新历史);
* - 任一步抛错:执行立即终止,cleanup 必被调用(失败与成功路径都执行),
* 调用方可重新执行同一 deps 恢复;
* - 输入校验 fail-fast:deps 缺函数、parse 返回非法 taskId、maxPollAttempts
* 非法均抛错且零状态变更。
*/
export interface PageE2EDeps {
/** 解析文件/参数,返回 taskId;返回 0 表示无可处理数据 */
parse: () => Promise<number>
/** 创建任务 */
createTask: (taskId: number) => Promise<void>
/** 单次轮询任务进度,返回状态字符串 */
poll: (taskId: number) => Promise<string>
/** 任务到达终态后刷新历史列表(可选) */
refreshHistory?: () => Promise<void>
/** 释放临时资源(URL、控制器、计时器等),失败与成功路径都会调用(可选) */
cleanup?: () => Promise<void>
}
export interface PageE2EOptions {
/** 轮询次数上限,必须为正数,默认 60 */
maxPollAttempts?: number
/** 判定终态,默认 SUCCESS/FAILED */
isTerminal?: (status: string) => boolean
}
export interface PageE2EResult {
taskId: number
status: string
/** 已完成的步骤(按执行顺序) */
completedSteps: string[]
/** 实际轮询次数 */
attempts: number
}
export async function runPageE2ECorePath(
deps: PageE2EDeps,
options: PageE2EOptions = {},
): Promise<PageE2EResult> {
if (typeof deps !== 'object' || deps == null) {
throw new Error('deps 必须是对象')
}
if (typeof deps.parse !== 'function') {
throw new Error('parse 必须是函数')
}
if (typeof deps.createTask !== 'function') {
throw new Error('createTask 必须是函数')
}
if (typeof deps.poll !== 'function') {
throw new Error('poll 必须是函数')
}
const maxPollAttempts = options.maxPollAttempts ?? 60
if (!(maxPollAttempts > 0)) {
throw new Error('maxPollAttempts 必须为正数: ' + maxPollAttempts)
}
const isTerminal = options.isTerminal ?? ((status: string) => status === 'SUCCESS' || status === 'FAILED')
const completedSteps: string[] = []
let status = ''
let attempts = 0
try {
const taskId = await deps.parse()
if (taskId === 0) {
completedSteps.push('parse')
return { taskId: 0, status: '', completedSteps, attempts: 0 }
}
if (typeof taskId !== 'number' || !Number.isFinite(taskId) || taskId <= 0) {
throw new Error('parse 必须返回正整数 taskId: ' + taskId)
}
completedSteps.push('parse')
await deps.createTask(taskId)
completedSteps.push('create-task')
while (attempts < maxPollAttempts) {
attempts += 1
status = await deps.poll(taskId)
completedSteps.push('poll')
if (isTerminal(status)) {
await deps.refreshHistory?.()
completedSteps.push('refresh-history')
return { taskId, status, completedSteps, attempts }
}
}
return { taskId, status, completedSteps, attempts }
} finally {
await deps.cleanup?.()
}
}
@@ -1,159 +0,0 @@
/**
* 页面三通道加载编排(任务 119):dashboard / history / progress 请求路径分离。
*
* 约定页面初载只调三类轻量接口——统计(stats)、历史摘要(summary)、轻量进度
* (light);明细(detail)与结果(result)仅在用户展开时按需加载。本模块把
* 该约定固化为可测试的编排:注入的 loader 各自独立、互不调用,首开仅并发发起
* 三类轻量加载,展开时再按需取明细/结果,并返回请求日志供断言。
*
* 页面接入示例:
* const page = createPageSeparatedLoads({
* statsLoader: () => getSimilarAsinDashboard(),
* summaryLoader: () => getSimilarAsinHistory(),
* lightLoader: (ids) => getModuleProgressLight('similarAsin', ids),
* detailLoader: (taskId) => getSimilarAsinTaskProgressBatch([taskId]),
* resultLoader: (taskId) => getSimilarAsinResult(taskId),
* })
* onMounted(() => page.initialLoad())
*/
export type PageLoadPhase = 'stats' | 'summary' | 'light' | 'detail' | 'result'
export interface PageRequestLogEntry {
phase: PageLoadPhase
taskIds: number[]
at: number
}
export interface PageSeparatedLoadsDeps {
statsLoader: () => Promise<unknown>
summaryLoader: () => Promise<unknown>
lightLoader: (taskIds: number[]) => Promise<unknown>
detailLoader: (taskId: number) => Promise<unknown>
resultLoader: (taskId: number) => Promise<unknown>
}
export interface PageSeparatedLoadsOptions {
/** 初载并发上限:三类轻量请求 + 每次按需展开 1 个明细/结果 */
maxConcurrentRequests?: number
}
export interface PageSeparatedLoadsApi {
/** dashboard 通道:只调统计 */
loadDashboard: () => Promise<void>
/** history 通道:只调摘要 */
loadHistory: () => Promise<void>
/** progress 通道:只调轻量进度 */
loadProgress: () => Promise<void>
/** 初载:并发发起 stats/summary/light 三类轻量请求,不加载明细 */
initialLoad: () => Promise<void>
/** 展开明细:仅当轻量条目已就绪时按需加载该任务明细 */
openDetail: (taskId: number) => Promise<unknown>
/** 取结果:按需加载指定任务结果文件 */
openResult: (taskId: number) => Promise<unknown>
requestLog: () => PageRequestLogEntry[]
}
export function createPageSeparatedLoads(
deps: PageSeparatedLoadsDeps,
options: PageSeparatedLoadsOptions = {},
): PageSeparatedLoadsApi {
const maxConcurrentRequests = options.maxConcurrentRequests ?? 3
if (maxConcurrentRequests < 1) {
throw new Error('maxConcurrentRequests 必须 ≥ 1')
}
if (typeof deps?.statsLoader !== 'function' || typeof deps?.summaryLoader !== 'function') {
throw new Error('statsLoader / summaryLoader 必须是函数')
}
if (typeof deps?.lightLoader !== 'function' || typeof deps?.detailLoader !== 'function') {
throw new Error('lightLoader / detailLoader 必须是函数')
}
if (typeof deps?.resultLoader !== 'function') {
throw new Error('resultLoader 必须是函数')
}
let inflight = 0
const releaseWaiters: Array<() => void> = []
const log: PageRequestLogEntry[] = []
const loadedLight = new Set<number>()
async function guarded(task: () => Promise<void>) {
while (inflight >= maxConcurrentRequests) {
await new Promise<void>((resolve) => {
releaseWaiters.push(resolve)
})
}
inflight += 1
try {
await task()
} finally {
inflight -= 1
const next = releaseWaiters.shift()
if (next) next()
}
}
function record(phase: PageLoadPhase, taskIds: number[] = []) {
log.push({ phase, taskIds: [...taskIds], at: log.length })
}
return {
async loadDashboard() {
await guarded(async () => {
record('stats')
await deps.statsLoader()
})
},
async loadHistory() {
await guarded(async () => {
record('summary')
await deps.summaryLoader()
})
},
async loadProgress() {
await guarded(async () => {
const ids = loadedLight.size ? [...loadedLight] : []
record('light', ids)
const result = await deps.lightLoader(ids)
for (const item of extractTaskIds(result) ?? []) {
loadedLight.add(item)
}
})
},
async initialLoad() {
await Promise.all([
this.loadDashboard(),
this.loadHistory(),
this.loadProgress(),
])
},
async openDetail(taskId: number) {
if (!loadedLight.has(taskId)) return undefined
let detail: unknown = undefined
await guarded(async () => {
record('detail', [taskId])
detail = await deps.detailLoader(taskId)
})
return detail
},
async openResult(taskId: number) {
if (!loadedLight.has(taskId)) return undefined
let result: unknown = undefined
await guarded(async () => {
record('result', [taskId])
result = await deps.resultLoader(taskId)
})
return result
},
requestLog: () => log,
}
}
/** 从 light 响应中提取已就绪 taskId 列表(兼容 items[{taskId}] 结构,null 安全) */
function extractTaskIds(result: unknown): number[] | undefined {
const items = (result as { items?: Array<{ taskId?: number }> } | null)?.items
if (!Array.isArray(items)) return undefined
const ids = items
.map((item) => item?.taskId)
.filter((id): id is number => typeof id === 'number' && id > 0)
return ids.length ? ids : undefined
}
@@ -1,145 +0,0 @@
/**
* 轮询状态机(Task 95)。
*
* 收敛轮询 UI 的深色主题无关状态语义:错误提示(error)、重试(retry)、
* 终态刷新(markRefreshed)的有界状态转换。
*
* 状态流:idle → start → polling →(fail)→ retrying →(fail…)→ failed
* └→ succeed → done(终态;仅刷新历史,不重复计数)
*
* 有界语义:
* - 重试次数有上限(maxAttempts),超过后进入 failed,不再重试;
* - 终态幂等:重复 succeed 不覆盖状态、不重复计数;markRefreshed 只计一次;
* - 未 start 时 fail/succeed 抛错;isTerminal 依赖抛错时调用失败但状态
* 零变更,恢复后可继续。
*/
export interface PollingStateMachineOptions {
/** 重试上限,必须为正整数,达到上限后失败不再重试 */
maxAttempts: number
/** 判定终态,默认 SUCCESS/FAILED;抛错时调用失败且状态不变 */
isTerminal?: (status: string) => boolean
}
export interface PollingStateMachine {
/** idle | polling | retrying | done | failed */
status: string
/** 是否仍在轮询(polling 或 retrying) */
retrying: boolean
attempts: number
errorCount: number
/** 最近一次终态状态字符串(未终态为空串) */
terminalStatus: string
/** 最近一次错误消息 */
lastError: string
/** 是否已刷新历史(终态后至多一次) */
refreshed: boolean
refreshCount: number
start: () => void
/** 记录一次失败;未超上限进入 retrying,超上限进入 failed */
fail: (message: string) => void
/** 记录一次成功;终态后重复调用幂等 */
succeed: (status: string) => void
/** 标记终态历史已刷新;仅 done/failed 后首个调用生效 */
markRefreshed: () => void
/** 已发生的重试序号列表 */
retries: () => number[]
}
export function createPollingStateMachine(options: PollingStateMachineOptions): PollingStateMachine {
if (typeof options !== 'object' || options == null) {
throw new Error('options 必须是对象')
}
if (!(options.maxAttempts > 0)) {
throw new Error('maxAttempts 必须为正数: ' + options.maxAttempts)
}
if (!Number.isInteger(options.maxAttempts)) {
throw new Error('maxAttempts 必须为整数: ' + options.maxAttempts)
}
const isTerminal = options.isTerminal ?? ((status: string) => status === 'SUCCESS' || status === 'FAILED')
if (typeof isTerminal !== 'function') {
throw new Error('isTerminal 必须是函数')
}
let status = 'idle'
let attempts = 0
let errorCount = 0
let terminalStatus = ''
let lastError = ''
let refreshCount = 0
const retrySequence: number[] = []
function start() {
if (status === 'done' || status === 'failed') return
status = 'polling'
attempts = 0
}
function fail(message: string) {
if (status === 'idle') {
throw new Error('未开始轮询')
}
if (status === 'done' || status === 'failed') return
attempts += 1
errorCount += 1
lastError = message
if (attempts >= options.maxAttempts) {
status = 'failed'
} else {
retrySequence.push(attempts)
status = 'retrying'
}
}
function succeed(terminal: string) {
if (status === 'idle') {
throw new Error('未开始轮询')
}
if (status === 'done' || status === 'failed') return
if (!isTerminal(terminal)) {
// 非终态响应:按一次失败计入,沿用重试语义
fail('非终态响应: ' + terminal)
return
}
attempts += 1
terminalStatus = terminal
status = 'done'
}
function markRefreshed() {
if (status !== 'done' && status !== 'failed') return
if (refreshCount > 0) return
refreshCount = 1
}
return {
get status() {
return status
},
get retrying() {
return status === 'polling' || status === 'retrying'
},
get attempts() {
return attempts
},
get errorCount() {
return errorCount
},
get terminalStatus() {
return terminalStatus
},
get lastError() {
return lastError
},
get refreshed() {
return refreshCount > 0
},
get refreshCount() {
return refreshCount
},
start,
fail,
succeed,
markRefreshed,
retries: () => [...retrySequence],
}
}
@@ -1,139 +0,0 @@
/**
* 发布前检查清单(Task 100)。
*
* 收敛发布前的三个验收步骤:回滚演练、git commit 对应关系检查、
* 交付清单核对。每步由 deps 提供真实实现(SSH 回滚、git log、
* 产物校验),清单产出结构化结果。
*
* 语义:
* - 回滚演练失败或 commit 缺失时整单 ok=false,但不中断后续步骤,
* 其余检查照常产出完整报告;
* - releaseCommits 为空、deliverables 为空:安全跳过对应检查,视为通过;
* - maxCommits 限制 commit 检查数量(超出部分计入 unverified),检查有界;
* - deps 抛错(SSH/git 故障):整轮 run 抛错,零部分结果,依赖恢复后
* 同一清单重跑即全绿;
* - 校验失败 fail-fast:deps 非对象、缺函数、数组字段非法、maxCommits
* 非正数、commit 记录缺 hash 均抛错。
*/
export interface ReleaseCommit {
hash: string
subject?: string
}
export interface RollbackResult {
ok: boolean
output: string
}
export interface ReleaseCheckDeps {
/** 列出当前仓库 commit(含 hash 与 subject) */
listCommits: () => Promise<ReleaseCommit[]>
/** 执行回滚演练 */
rollback: () => Promise<RollbackResult>
/** 验证发布后页面/核心请求 */
verify: () => Promise<RollbackResult>
}
export interface ReleaseChecklistOptions {
deps: ReleaseCheckDeps
/** 本次发布对应的 commit hash 列表 */
releaseCommits: string[]
/** 交付物清单(jar、exe、vue-dist 等) */
deliverables?: string[]
/** commit 检查数量上限,必须为正数,默认 100 */
maxCommits?: number
}
export interface ReleaseCheckResult {
ok: boolean
steps: Array<{ id: string; ok: boolean }>
rollback: RollbackResult
verify: RollbackResult
commitMap: {
checked: number
missing: string[]
unverified: string[]
}
deliverables: {
passed: boolean
total: number
items: string[]
}
}
export interface ReleaseChecklist {
run: () => Promise<ReleaseCheckResult>
}
export function createReleaseChecklist(options: ReleaseChecklistOptions): ReleaseChecklist {
if (typeof options !== 'object' || options == null || !options.deps) {
throw new Error('deps 必须是对象')
}
if (typeof options.deps.listCommits !== 'function') {
throw new Error('listCommits 必须是函数')
}
if (typeof options.deps.rollback !== 'function') {
throw new Error('rollback 必须是函数')
}
if (typeof options.deps.verify !== 'function') {
throw new Error('verify 必须是函数')
}
if (!Array.isArray(options.releaseCommits)) {
throw new Error('releaseCommits 必须是数组')
}
const deliverables = options.deliverables ?? []
if (!Array.isArray(deliverables)) {
throw new Error('deliverables 必须是数组')
}
const maxCommits = options.maxCommits ?? 100
if (!(maxCommits > 0)) {
throw new Error('maxCommits 必须为正数: ' + maxCommits)
}
async function run(): Promise<ReleaseCheckResult> {
const steps: Array<{ id: string; ok: boolean }> = []
let ok = true
const rollback = await options.deps.rollback()
steps.push({ id: 'rollback-drill', ok: rollback.ok })
if (!rollback.ok) ok = false
const commits = await options.deps.listCommits()
const available = new Set<string>()
for (const commit of commits) {
if (typeof commit.hash !== 'string' || commit.hash.length === 0) {
throw new Error('commit 缺少 hash')
}
available.add(commit.hash)
}
const missing: string[] = []
const unverified: string[] = []
for (let i = 0; i < options.releaseCommits.length; i++) {
const hash = options.releaseCommits[i]
if (i >= maxCommits) {
unverified.push(hash)
continue
}
if (!available.has(hash)) {
missing.push(hash)
ok = false
}
}
steps.push({ id: 'commit-map', ok: missing.length === 0 && unverified.length === 0 })
const verify = await options.deps.verify()
steps.push({ id: 'deliverables', ok: verify.ok })
if (!verify.ok) ok = false
return {
ok,
steps,
rollback,
verify,
commitMap: { checked: options.releaseCommits.length, missing, unverified },
deliverables: { passed: verify.ok, total: deliverables.length, items: [...deliverables] },
}
}
return { run }
}
@@ -1,95 +0,0 @@
/**
* 响应式截图计划(Task 94)。
*
* 为多页应用生成移动端/桌面端响应式验收截图清单:入口 × 视口 的笛卡尔积
* (先页面后视口,顺序稳定),每个 shot 有可预期且唯一的文件名
* `<entry>-<label>.png`。同一入口/视口重复登记自动去重;maxShots 限制
* 截图总数(超出部分截断并计数 dropped)。
*
* 校验失败 fail-fast(入口/视口非数组、label 为空、宽高非正数、maxShots
* 非正数),零状态变更;视口列表读取抛错时调用失败,不产生部分计划,
* 依赖恢复后同一输入可重新计算。输入对象永不修改。
*/
export interface ShotViewport {
/** 视口标识(也用于文件名),不能为空 */
label: string
width: number
height: number
}
export interface ShotEntry {
entry: string
viewport: ShotViewport
filename: string
}
export interface ResponsiveShotPlanOptions {
/** 页面入口列表 */
entries: string[]
/** 视口列表(重复 label 去重) */
viewports: ShotViewport[]
/** 截图总数上限,必须为正数,默认 1000 */
maxShots?: number
}
export interface ResponsiveShotPlan {
/** 有序截图清单 */
shots: ShotEntry[]
shotCount: number
/** 因 maxShots 截断而丢弃的数量 */
droppedCount: number
/** 每个视口 label 的截图数量统计 */
viewportStats: () => Record<string, number>
}
export function createResponsiveShotPlan(options: ResponsiveShotPlanOptions): ResponsiveShotPlan {
if (!Array.isArray(options.entries)) {
throw new Error('entries 必须是数组')
}
if (!Array.isArray(options.viewports)) {
throw new Error('viewports 必须是数组')
}
const maxShots = options.maxShots ?? 1000
if (!(maxShots > 0)) {
throw new Error('maxShots 必须为正数: ' + maxShots)
}
const entries = Array.from(new Set(options.entries.filter((entry) => typeof entry === 'string' && entry.length > 0)))
const viewports: ShotViewport[] = []
for (const viewport of options.viewports) {
if (typeof viewport.label !== 'string' || viewport.label.length === 0) {
throw new Error('label 不能为空')
}
if (!(viewport.width > 0)) {
throw new Error('width 必须为正数: ' + viewport.width)
}
if (!(viewport.height > 0)) {
throw new Error('height 必须为正数: ' + viewport.height)
}
if (!viewports.some((item) => item.label === viewport.label)) {
viewports.push({ label: viewport.label, width: viewport.width, height: viewport.height })
}
}
const shots: ShotEntry[] = []
let droppedCount = 0
for (const entry of entries) {
for (const viewport of viewports) {
if (shots.length >= maxShots) {
droppedCount += 1
continue
}
shots.push({ entry, viewport, filename: `${entry}-${viewport.label}.png` })
}
}
function viewportStats(): Record<string, number> {
const stats: Record<string, number> = {}
for (const shot of shots) {
stats[shot.viewport.label] = (stats[shot.viewport.label] ?? 0) + 1
}
return stats
}
return { shots, shotCount: shots.length, droppedCount, viewportStats }
}
@@ -0,0 +1,87 @@
/**
* 任务状态 → 文案 / 样式类 的统一映射(前端各任务页 Tab 共用)。
*
* 默认表取自各 Tab 本地映射的多数派(并集):
* - 文案:SUCCESS/COMPLETED→已完成、FAILED→失败、RUNNING→执行中、
* PENDING→等待中、CANCELLED→已取消、SCHEDULED→待执行、QUEUED→排队中;
* 未命中回退「等待中」。
* - 样式:SUCCESS/COMPLETED→success、FAILED/CANCELLED→failed、RUNNING→running;
* 未命中回退 'pending'。
*
* 与默认表不一致的 Tab 通过 overrides 覆盖,保证显示效果与原实现一致。
* 状态键统一大小写不敏感(内部先 trim + toUpperCase)。
*/
/** 任务样式类(与 shared/components/tasks/types.ts 的 TaskStatusClass 一致) */
export type TaskStatusClassName = 'success' | 'failed' | 'running' | 'pending';
export interface TaskStatusOptions {
/** 文案覆盖表(replace=true 时完全替换默认表,否则按键合并) */
text?: Record<string, string>;
/** 样式覆盖表(replace=true 时完全替换默认表,否则按键合并) */
classMap?: Record<string, TaskStatusClassName>;
/** 未命中时的文案,默认「等待中」 */
textFallback?: string;
/** 未命中时的样式类,默认 'pending' */
classFallback?: TaskStatusClassName;
/** true 时忽略默认表,只用 overrides */
replace?: boolean;
}
export const TASK_STATUS_TEXT: Record<string, string> = {
SUCCESS: '已完成',
COMPLETED: '已完成',
FAILED: '失败',
RUNNING: '执行中',
PENDING: '等待中',
CANCELLED: '已取消',
SCHEDULED: '待执行',
QUEUED: '排队中',
};
export const TASK_STATUS_CLASS: Record<string, TaskStatusClassName> = {
SUCCESS: 'success',
COMPLETED: 'success',
FAILED: 'failed',
CANCELLED: 'failed',
RUNNING: 'running',
};
export const TASK_STATUS_TEXT_FALLBACK = '等待中';
export const TASK_STATUS_CLASS_FALLBACK: TaskStatusClassName = 'pending';
function normalizeStatusKey(status?: string | null): string {
return String(status ?? '').trim().toUpperCase();
}
/** 任务状态 → 展示文案 */
export function taskStatusText(status?: string | null, overrides?: TaskStatusOptions): string {
const key = normalizeStatusKey(status);
const table = overrides?.replace
? overrides.text
: { ...TASK_STATUS_TEXT, ...overrides?.text };
if (table && key in table) return table[key];
return overrides?.textFallback ?? TASK_STATUS_TEXT_FALLBACK;
}
/** 任务状态 → 样式类 */
export function taskStatusClass(status?: string | null, overrides?: TaskStatusOptions): TaskStatusClassName {
const key = normalizeStatusKey(status);
const table = overrides?.replace
? overrides.classMap
: { ...TASK_STATUS_CLASS, ...overrides?.classMap };
if (table && key in table) return table[key];
return overrides?.classFallback ?? TASK_STATUS_CLASS_FALLBACK;
}
/**
* 非终态一律显示「执行中」/ running 的页面(BrandWithdrawTab、BrandPatrolDeleteTab、
* BrandQueryAsinTab 原行为):只区分 SUCCESS/COMPLETED/FAILED 三种终态。
*/
export const RUNNING_UNTIL_DONE_STATUS_OPTIONS: TaskStatusOptions = {
replace: true,
text: { SUCCESS: '已完成', COMPLETED: '已完成', FAILED: '失败' },
classMap: { SUCCESS: 'success', COMPLETED: 'success', FAILED: 'failed' },
textFallback: '执行中',
classFallback: 'running',
};
@@ -1,103 +0,0 @@
/**
* 三端验证工作流编排器(Task 97)。
*
* 把 Java 全量测试、Python unittest、Vue 类型检查与构建四个验证步骤
* 收敛为可注入执行器的确定性流程:步骤按序执行,产出逐项结果。
*
* 语义:
* - 每个步骤的 run(stepId) 返回 { ok, output };ok=false 时计入 failed;
* - stopOnFailure(默认 true)下失败即停止,剩余步骤计入 skipped;
* - maxSteps 限制可执行步骤数量(超出部分计入 skipped),执行有界;
* - 步骤执行器抛错:整次 runAll 抛错,不产生部分结果记录(调用方可
* 捕获后恢复重跑同一 runner);
* - 校验失败 fail-fast:steps 非数组、step id 为空、run 非函数、maxSteps
* 非正数、run 返回结果缺 output 均抛错。
*/
export interface VerificationStep {
id: string
label: string
}
export interface StepRunResult {
ok: boolean
output: string
}
export interface VerificationRunnerDeps {
/** 执行单个步骤,返回结果;抛错时整次 runAll 抛错 */
run: (stepId: string) => Promise<StepRunResult>
}
export interface VerificationRunnerOptions {
steps: VerificationStep[]
run: (stepId: string) => Promise<StepRunResult>
/** 失败即停止(默认 true);false 时全部执行、失败累计 */
stopOnFailure?: boolean
/** 可执行步骤数上限,必须为正数,默认 100 */
maxSteps?: number
}
export interface VerificationResult {
ok: boolean
passed: Array<VerificationStep & { output: string }>
failed: Array<VerificationStep & { output: string }>
skipped: string[]
}
export interface VerificationRunner {
runAll: () => Promise<VerificationResult>
}
export function createVerificationRunner(options: VerificationRunnerOptions): VerificationRunner {
if (!Array.isArray(options.steps)) {
throw new Error('steps 必须是数组')
}
for (const step of options.steps) {
if (typeof step.id !== 'string' || step.id.length === 0) {
throw new Error('step id 不能为空')
}
}
if (typeof options.run !== 'function') {
throw new Error('run 必须是函数')
}
const stopOnFailure = options.stopOnFailure ?? true
const maxSteps = options.maxSteps ?? 100
if (!(maxSteps > 0)) {
throw new Error('maxSteps 必须为正数: ' + maxSteps)
}
async function runAll(): Promise<VerificationResult> {
const passed: Array<VerificationStep & { output: string }> = []
const failed: Array<VerificationStep & { output: string }> = []
const skipped: string[] = []
let ok = true
for (let i = 0; i < options.steps.length; i++) {
const step = options.steps[i]
if (i >= maxSteps) {
skipped.push(step.id)
continue
}
const result = await options.run(step.id)
if (typeof result.output !== 'string') {
throw new Error('output 必须是字符串')
}
if (result.ok) {
passed.push({ ...step, output: result.output })
} else {
ok = false
failed.push({ ...step, output: result.output })
if (stopOnFailure) {
for (const rest of options.steps.slice(i + 1)) {
skipped.push(rest.id)
}
break
}
}
}
return { ok, passed, failed, skipped }
}
return { runAll }
}