From fcb6b5f545f911c168626f8e7084bf4311a44006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=87=AA=E8=BE=BE?= <980324341@qq.com> Date: Sun, 25 Oct 2026 14:51:57 +0800 Subject: [PATCH] =?UTF-8?q?fix(release):=20AP=20=E9=83=A8=E5=88=86?= =?UTF-8?q?=E7=BC=BA=E5=8F=A3=E5=87=BA=E8=A1=A8=E3=80=81=E5=B9=B3=E5=8F=B0?= =?UTF-8?q?=E8=87=AA=E8=B4=B9=E8=AE=A1=E6=AC=A1=E4=B8=8E=E5=B7=A5=E4=BD=9C?= =?UTF-8?q?=E5=8F=B0=E9=A2=84=E7=AD=BE=E5=90=8D=20TTL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - AP 抓取缺行不再一律判死,按覆盖率判定;≥95% 出表成功并保留缺口提示 - 无归属代理提取记 0 号平台自费桶,后台可见并推管理员日告警 - 工作台安装包预签名从 180s 提到可配置 3600s,避免慢网上传在途过期 --- .../src/pages/records/workbench-client-api.ts | 2 +- .../aiimage/config/CostGuardProperties.java | 6 ++ .../AppearancePatentResultJobSupport.java | 63 ++++++++++++++++--- .../service/CostGuardScheduler.java | 62 +++++++++++++++++- .../service/UserSecretUsageService.java | 51 +++++++++------ .../WorkbenchClientPackageService.java | 18 ++++-- .../resources/application-local.example.yml | 4 ++ .../src/main/resources/application.yml | 5 ++ .../AppearancePatentPartialGapStatusTest.java | 61 ++++++++++++++++++ .../service/CostGuardSchedulerTest.java | 30 +++++++++ .../service/UserSecretUsageServiceTest.java | 34 ++++++++++ .../WorkbenchClientPackageServiceTest.java | 4 +- 12 files changed, 305 insertions(+), 35 deletions(-) create mode 100644 backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentPartialGapStatusTest.java diff --git a/admin-frontend-vue/src/pages/records/workbench-client-api.ts b/admin-frontend-vue/src/pages/records/workbench-client-api.ts index c69bec595..a58e5a36f 100644 --- a/admin-frontend-vue/src/pages/records/workbench-client-api.ts +++ b/admin-frontend-vue/src/pages/records/workbench-client-api.ts @@ -32,7 +32,7 @@ export interface WorkbenchClientUploadTarget { fileName: string } -/** 第一步:向后端申请直传 PUT 预签名地址(登录会话签发,3 分钟有效)。 */ +/** 第一步:向后端申请直传 PUT 预签名地址(登录会话签发,默认 1 小时有效)。 */ export async function requestWorkbenchClientPresign( fileName: string, version: string, diff --git a/backend-java/src/main/java/com/nanri/aiimage/config/CostGuardProperties.java b/backend-java/src/main/java/com/nanri/aiimage/config/CostGuardProperties.java index f9ba764ae..1dec38fef 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/config/CostGuardProperties.java +++ b/backend-java/src/main/java/com/nanri/aiimage/config/CostGuardProperties.java @@ -38,6 +38,12 @@ public class CostGuardProperties { /** 当日代理提取次数上限;0=关闭。 */ private int dailyProxyThreshold = 8_000; + /** + * 平台自费(无归属 uid)代理提取的当日告警线;0=关闭。 + * 这笔钱无法归到任何账号名下,不能走暂停闸,只能推管理员告警。 + */ + private int platformProxyDailyThreshold = 2_000; + /** 当日 LLM 调用次数上限;0=关闭。 */ private int dailyLlmCallsThreshold = 6_000; diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentResultJobSupport.java b/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentResultJobSupport.java index ef48c6164..a9f8175da 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentResultJobSupport.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentResultJobSupport.java @@ -212,7 +212,13 @@ public class AppearancePatentResultJobSupport { } } } - boolean failed = finalError != null && !finalError.isBlank(); + // 抓取缺行是“部分数据缺口”,不是致命失败:只要已有可用结果记录、且错误里没有 + // 出表/任务级故障,就先进入组装;最终是否成功由 completeLlmFileJob 按覆盖率判定。 + // 只有真正凑不出结果(result == null)时才维持 FAILED。 + boolean hasError = finalError != null && !finalError.isBlank(); + boolean partialGapSalvageable = hasError && isPartialDataGapError(finalError) + && assembleWorkbook && result != null; + boolean failed = hasError && !partialGapSalvageable; boolean waitingForAssemble = !failed && assembleWorkbook && !shouldAssembleSynchronously(); task.setStatus(statusResolver.resolve(waitingForAssemble, failed)); task.setSuccessFileCount(failed ? 0 : 1); @@ -375,13 +381,16 @@ public class AppearancePatentResultJobSupport { AppearancePatentRowOutcome.Facts deliveryFacts = assembleResultWorkbook(task, result); progressSupport.saveFileBuildProgress(task, job, totalProgressUnits, totalProgressUnits - 1, "正在上传结果文件"); fileResultMapper.updateById(result); - boolean taskAlreadyFailed = STATUS_FAILED.equals(task.getStatus()) - || (task.getErrorMessage() != null && !task.getErrorMessage().isBlank()); String existingError = task.getErrorMessage(); - task.setStatus(statusResolver.resolve(false, taskAlreadyFailed)); - task.setSuccessFileCount(taskAlreadyFailed ? 0 : 1); - task.setFailedFileCount(taskAlreadyFailed ? 1 : 0); - task.setErrorMessage(deliverySupport.appendDeliveryGapMessage(taskAlreadyFailed ? existingError : null, deliveryFacts)); + boolean taskAlreadyFailed = STATUS_FAILED.equals(task.getStatus()); + boolean failed = shouldFailAfterAssembly(taskAlreadyFailed, existingError, + deliveryFacts.expectedRows(), deliveryFacts.coverage(), + properties.getLlmCoverageMinRatio()); + task.setStatus(statusResolver.resolve(false, failed)); + task.setSuccessFileCount(failed ? 0 : 1); + task.setFailedFileCount(failed ? 1 : 0); + // 成功时也保留缺口文案:用户要看到“N 行抓取失败”,但不能因此把整单判死、逼他重跑。 + task.setErrorMessage(deliverySupport.appendDeliveryGapMessage(existingError, deliveryFacts)); task.setUpdatedAt(LocalDateTime.now()); task.setFinishedAt(LocalDateTime.now()); fileTaskMapper.updateById(task); @@ -390,6 +399,46 @@ public class AppearancePatentResultJobSupport { return true; } + /** + * 出表后的最终状态判定:已判死 / 覆盖率低于下限 / 非部分缺口的错误 → FAILED。 + * + *

抓取缺行只允许把缺口写进消息,不允许把已经拿到 95% 以上结论的任务整单判死—— + * 那会让用户重跑,二次烧掉整单 LLM 密钥(现网 32288/32252 就是 3/2 行缺口烧掉整单)。 + */ + static boolean shouldFailAfterAssembly(boolean taskAlreadyFailed, String existingError, + int expectedRows, double coverage, double minCoverage) { + if (taskAlreadyFailed) { + return true; + } + boolean hasError = existingError != null && !existingError.isBlank(); + if (hasError && !isPartialDataGapError(existingError)) { + return true; + } + if (expectedRows <= 0) { + return hasError; + } + return coverage + 1e-9 < minCoverage; + } + + /** + * 只认“少量行抓取/采集缺失”这类可带缺口交付的错误;出表失败、任务级故障仍按致命处理。 + */ + static boolean isPartialDataGapError(String error) { + if (error == null || error.isBlank()) { + return false; + } + if (error.contains("结果文件生成失败") + || error.contains("结果文件生成多次失败") + || error.contains("Generate appearance patent result failed") + || error.contains("任务不存在") + || error.contains("任务已自动结束")) { + return false; + } + return error.contains("行抓取失败(未取得商品页数据)") + || error.contains("行未采集到数据") + || error.contains("行未完成检测"); + } + /** * 失败兜底:组装作业重试耗尽(WAIT_LLM 停放被 stuck 棘轮判死、或组装异常连吃 5 次重试)后调用, * 用已回传的分片与已拿到结论的行尽力生成可下载的部分结果文件。 diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/usersecret/service/CostGuardScheduler.java b/backend-java/src/main/java/com/nanri/aiimage/modules/usersecret/service/CostGuardScheduler.java index 05b590fc5..97c4157f9 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/usersecret/service/CostGuardScheduler.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/usersecret/service/CostGuardScheduler.java @@ -40,6 +40,9 @@ public class CostGuardScheduler { static final String LOCK_NAME = "cost-guard-scan"; + /** 无归属购买的汇总桶(见 UserSecretUsageService.PLATFORM_USER_ID):不暂停,只告警管理员。 */ + static final long PLATFORM_USER_ID = UserSecretUsageService.PLATFORM_USER_ID; + private final CostGuardProperties properties; private final UserSecretUsageMapper usageMapper; private final UserTaskGuardService guardService; @@ -77,7 +80,9 @@ public class CostGuardScheduler { void runOnce() { long nowMillis = System.currentTimeMillis(); int windowSeconds = Math.max(60, properties.getWindowSeconds()); - Map current = loadTodayTotals(); + Map all = loadAllTotals(); + UserTotals platform = all.remove(PLATFORM_USER_ID); + Map current = all; Map deltas = new LinkedHashMap<>(); int paused = 0; for (Map.Entry entry : current.entrySet()) { @@ -123,11 +128,57 @@ public class CostGuardScheduler { } lastDeltas.clear(); lastDeltas.putAll(deltas); + scanPlatform(platform, nowMillis); maybeRefreshShared(true); log.info("[cost-guard] 扫描完成 用户={} 本轮暂停={} 共享密钥快照={}ms", current.size(), paused, sharedSecretMonitorService.snapshotAtMillis()); } + /** + * 平台自费桶只告警、不暂停:无归属购买没有可暂停的账号,但金额是真实的, + * 超过告警线就推全体管理员,避免像 2026-10 那次一样在后台账上完全不可见 + * (cookie 生成器一天买了 3900+ 次 3 分钟档 IP,页面上一个数字都没有)。 + * + *

基线同样落共享 store,跨实例按真实 elapsed 归一化;同一天只报一次 + * (crossed 只在「本轮跨过」时为真),告警内容的数字随当日累计更新。 + */ + private void scanPlatform(UserTotals platform, long nowMillis) { + if (platform == null) { + return; + } + int threshold = properties.getPlatformProxyDailyThreshold(); + if (threshold <= 0) { + return; + } + CostGuardBaselineStore.Baseline before = baselineStore.get(PLATFORM_USER_ID); + String reason = null; + // 跨天时当日累计会变小:此时上一天的基线(可能已高于告警线)不能拿来判 crossed, + // 否则今天首次越过告警线会被静默吞掉。 + boolean newDay = before != null && platform.proxy < before.proxy; + if (before == null || newDay) { + if (platform.proxy >= threshold) { + reason = platformReason(platform.proxy, threshold); + } + } else if (crossed(before.proxy, platform.proxy, threshold)) { + reason = platformReason(platform.proxy, threshold); + } + baselineStore.put(PLATFORM_USER_ID, new CostGuardBaselineStore.Baseline( + platform.proxy, platform.llmCalls, platform.llmTokens, nowMillis)); + if (reason == null) { + return; + } + int pushed = notificationDispatchService.pushToAdmins(NotificationService.SCENE_COST_GUARD, + NotificationService.LEVEL_WARNING, "平台自费代理提取超告警线", + reason + "。这笔消耗没有归属账号、不能自动暂停,请检查 cookie 生成器 / 平台兜底池的提取链接档位。", + "cost_guard_platform_proxy:" + LocalDate.now(), null); + log.warn("[cost-guard] 平台自费代理提取告警 次数={} 阈值={} 管理员落库={}", + platform.proxy, threshold, pushed); + } + + private String platformReason(long count, int threshold) { + return "今日平台自费(无归属)代理提取累计 " + count + " 次,超过告警线 " + threshold + " 次"; + } + /** * 用户点击继续时调用:把基线对齐到当前值(而不是删掉基线)。删掉会让下一轮走「首轮」 * 分支;首轮现在会按日累计判暂停,用户刚点继续就会被立刻再暂停。 @@ -148,10 +199,17 @@ public class CostGuardScheduler { } public Map loadTodayTotals() { + Map totals = loadAllTotals(); + totals.remove(PLATFORM_USER_ID); + return totals; + } + + /** 含平台自费桶(0 号)的当日聚合;按账号暂停的那条路必须先摘掉它。 */ + private Map loadAllTotals() { List rows = usageMapper.sumByUserAndModule(LocalDate.now()); Map totals = new LinkedHashMap<>(); for (UserSecretUsageAggregate row : rows) { - if (row.getUserId() == null || row.getUserId() <= 0) { + if (row.getUserId() == null || row.getUserId() < 0) { continue; } accumulate(totals.computeIfAbsent(row.getUserId(), key -> new UserTotals()), row); diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/usersecret/service/UserSecretUsageService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/usersecret/service/UserSecretUsageService.java index 6103e50fc..bbcf4f6f3 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/usersecret/service/UserSecretUsageService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/usersecret/service/UserSecretUsageService.java @@ -42,6 +42,13 @@ public class UserSecretUsageService { /** 单次上报上限:防止异常客户端把计数写爆。 */ private static final int MAX_REPORT_COUNT = 10_000; + /** + * 平台自费桶:无归属 uid 的代理提取(cookie 生成器、平台兜底池)记到 0 号, + * 后台用量统计显示为「平台自费」,损耗扫描只告警不暂停。只允许 proxy 走这个桶。 + */ + public static final long PLATFORM_USER_ID = 0L; + static final String PLATFORM_USER_LABEL = "平台自费"; + private final UserSecretUsageMapper usageMapper; private final AdminUserMapper adminUserMapper; private final AdminAuthSupport adminAuthSupport; @@ -66,13 +73,18 @@ public class UserSecretUsageService { /** 内部通道上报入口:模块非法返回 false(供接口回执),写库失败只告警。 */ public boolean recordReported(Long userId, String moduleKey, int count) { - if (userId == null || userId <= 0 || count <= 0) { + if (userId == null || userId < 0 || count <= 0) { return false; } UserSecretModule module = UserSecretModule.of(moduleKey).orElse(null); if (module == null) { return false; } + // 0 号只收代理:LLM 一定挂在具体账号的密钥上,平台没有"公共 LLM 密钥"这回事。 + if (userId == PLATFORM_USER_ID && module != UserSecretModule.PROXY) { + log.warn("[user-secret-usage] 平台自费桶只收 proxy 计次 userId=0 module={}", moduleKey); + return false; + } return recordModule(userId, module, Math.min(count, MAX_REPORT_COUNT), 0L, 0L); } @@ -270,31 +282,32 @@ public class UserSecretUsageService { private void fillRowMeta(List pageItems) { List userIds = pageItems.stream() .map(AdminUserSecretUsageRowVo::getUserId) - .filter(id -> id != null) + .filter(id -> id != null && id > PLATFORM_USER_ID) .toList(); - if (userIds.isEmpty()) { - return; - } Map usernames = new HashMap<>(); - for (AdminUserEntity user : adminUserMapper.selectBatchIds(userIds)) { - if (user.getId() != null) { - usernames.put(user.getId(), normalize(user.getUsername())); - } - } Map> groups = new LinkedHashMap<>(); - for (UserGroupRef ref : adminGroupMapper.selectGroupNamesByUserIds(userIds)) { - if (ref.getUserId() == null) { - continue; + if (!userIds.isEmpty()) { + for (AdminUserEntity user : adminUserMapper.selectBatchIds(userIds)) { + if (user.getId() != null) { + usernames.put(user.getId(), normalize(user.getUsername())); + } } - String name = normalize(ref.getGroupName()); - if (name.isEmpty()) { - continue; + for (UserGroupRef ref : adminGroupMapper.selectGroupNamesByUserIds(userIds)) { + if (ref.getUserId() == null) { + continue; + } + String name = normalize(ref.getGroupName()); + if (name.isEmpty()) { + continue; + } + groups.computeIfAbsent(ref.getUserId(), key -> new ArrayList<>()).add(name); } - groups.computeIfAbsent(ref.getUserId(), key -> new ArrayList<>()).add(name); } for (AdminUserSecretUsageRowVo item : pageItems) { - item.setUsername(usernames.getOrDefault(item.getUserId(), "")); - item.setGroups(groups.getOrDefault(item.getUserId(), List.of())); + boolean platform = item.getUserId() != null && item.getUserId() == PLATFORM_USER_ID; + item.setUsername(platform ? PLATFORM_USER_LABEL + : usernames.getOrDefault(item.getUserId(), "")); + item.setGroups(platform ? List.of() : groups.getOrDefault(item.getUserId(), List.of())); } } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/workbenchclient/service/WorkbenchClientPackageService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/workbenchclient/service/WorkbenchClientPackageService.java index 1c98383f7..30975bed9 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/workbenchclient/service/WorkbenchClientPackageService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/workbenchclient/service/WorkbenchClientPackageService.java @@ -7,6 +7,7 @@ import com.nanri.aiimage.modules.workbenchclient.mapper.WorkbenchClientPackageMa import com.nanri.aiimage.modules.workbenchclient.model.entity.WorkbenchClientPackageEntity; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.math.BigInteger; @@ -44,8 +45,12 @@ public class WorkbenchClientPackageService { /** 版本号长度上限,与 biz_workbench_client_package.version VARCHAR(64) 对齐。 */ private static final int MAX_VERSION_LENGTH = 64; - /** 直传预签名有效期:3 分钟足够数百 MB 级安装包上传。 */ - private static final int PRESIGN_EXPIRY_SECONDS = 180; + /** 直传预签名默认有效期:90MB 级包在慢公网入口上传时,3 分钟窗口会在途过期。 */ + private static final int DEFAULT_PRESIGN_EXPIRY_SECONDS = 3600; + + /** 允许配置覆盖;上下限防止误配成 0 或过长窗口。 */ + @Value("${aiimage.workbench-client.presign-expiry-seconds:3600}") + private int presignExpirySeconds = DEFAULT_PRESIGN_EXPIRY_SECONDS; /** 公开版本列表上限:够客户端做「指定版本更新」下拉即可。 */ private static final int PUBLIC_LIST_LIMIT = 50; @@ -118,6 +123,10 @@ public class WorkbenchClientPackageService { return result; } + private int effectivePresignExpirySeconds() { + return Math.max(60, Math.min(presignExpirySeconds, 24 * 3600)); + } + /** * 签发直传预签名:对象 key 由「毫秒时间戳 + 随机 UUID + 安全版本号」组成。 * UUID 是 CR-08 的修复点:只靠毫秒时间戳时,多实例时钟相同或同毫秒两名管理员 @@ -130,9 +139,10 @@ public class WorkbenchClientPackageService { + OBJECT_NAME_FORMATTER.format(LocalDateTime.now()) + "-" + UUID.randomUUID().toString().replace("-", "") + "-" + safeVersionKey(normalizedVersion) + ".zip"; + int expirySeconds = effectivePresignExpirySeconds(); log.info("[workbench-client] 签发客户端包直传预签名 version={} fileName={} objectKey={} expirySeconds={}", - normalizedVersion, normalizedName, objectKey, PRESIGN_EXPIRY_SECONDS); - String uploadUrl = ossStorageService.presignWorkbenchClientUpload(objectKey, PRESIGN_EXPIRY_SECONDS); + normalizedVersion, normalizedName, objectKey, expirySeconds); + String uploadUrl = ossStorageService.presignWorkbenchClientUpload(objectKey, expirySeconds); Map result = new LinkedHashMap<>(); result.put("object_key", objectKey); result.put("upload_url", uploadUrl); diff --git a/backend-java/src/main/resources/application-local.example.yml b/backend-java/src/main/resources/application-local.example.yml index c22541b59..d93805877 100644 --- a/backend-java/src/main/resources/application-local.example.yml +++ b/backend-java/src/main/resources/application-local.example.yml @@ -14,6 +14,7 @@ AIIMAGE_COST_GUARD_ENABLED=true AIIMAGE_COST_GUARD_PROXY_WINDOW_THRESHOLD=600 AIIMAGE_COST_GUARD_LLM_WINDOW_CALLS_THRESHOLD=180 AIIMAGE_COST_GUARD_LLM_WINDOW_TOKENS_THRESHOLD=600000 +AIIMAGE_COST_GUARD_PLATFORM_PROXY_DAILY_THRESHOLD=2000 AIIMAGE_COST_GUARD_SHARED_ACCOUNTS_THRESHOLD=3 AIIMAGE_DB_URL=jdbc:mysql://127.0.0.1:3306/aiimage?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false @@ -29,6 +30,9 @@ AIIMAGE_DIGITAL_HUMAN_OSS_BUCKET=nanri-ai-digital-human AIIMAGE_OSS_ACCESS_KEY_ID=change-me AIIMAGE_OSS_ACCESS_KEY_SECRET=change-me +# 工作台客户端包直传预签名有效期(秒);90MB 级包在慢公网入口需要更长窗口。 +AIIMAGE_WORKBENCH_CLIENT_PRESIGN_EXPIRY_SECONDS=3600 + AIIMAGE_TRANSIENT_STORAGE_ENABLED=true AIIMAGE_TRANSIENT_STORAGE_ENDPOINT=http://121.196.149.225:9000 AIIMAGE_TRANSIENT_STORAGE_BUCKET=change-me diff --git a/backend-java/src/main/resources/application.yml b/backend-java/src/main/resources/application.yml index 914231012..355a3649a 100644 --- a/backend-java/src/main/resources/application.yml +++ b/backend-java/src/main/resources/application.yml @@ -217,6 +217,9 @@ aiimage: heap-ratio-warn: ${AIIMAGE_CAPACITY_PLAN_HEAP_RATIO_WARN:0.70} direct-ratio-warn: ${AIIMAGE_CAPACITY_PLAN_DIRECT_RATIO_WARN:0.15} temp-disk-ratio-warn: ${AIIMAGE_CAPACITY_PLAN_TEMP_DISK_RATIO_WARN:0.50} + workbench-client: + # 直传预签名有效期:90MB 级安装包在慢公网入口上传时,3 分钟窗口会在途过期。 + presign-expiry-seconds: ${AIIMAGE_WORKBENCH_CLIENT_PRESIGN_EXPIRY_SECONDS:3600} brand-progress: ttl-hours: ${AIIMAGE_BRAND_PROGRESS_TTL_HOURS:24} failed-ttl-hours: ${AIIMAGE_BRAND_PROGRESS_FAILED_TTL_HOURS:2} @@ -512,6 +515,8 @@ aiimage: llm-window-calls-threshold: ${AIIMAGE_COST_GUARD_LLM_WINDOW_CALLS_THRESHOLD:180} llm-window-tokens-threshold: ${AIIMAGE_COST_GUARD_LLM_WINDOW_TOKENS_THRESHOLD:600000} daily-proxy-threshold: ${AIIMAGE_COST_GUARD_DAILY_PROXY_THRESHOLD:8000} + # 无归属购买(平台自费)没有可暂停的账号:只推管理员告警,不暂停。 + platform-proxy-daily-threshold: ${AIIMAGE_COST_GUARD_PLATFORM_PROXY_DAILY_THRESHOLD:2000} daily-llm-calls-threshold: ${AIIMAGE_COST_GUARD_DAILY_LLM_CALLS_THRESHOLD:6000} daily-llm-tokens-threshold: ${AIIMAGE_COST_GUARD_DAILY_LLM_TOKENS_THRESHOLD:20000000} shared-accounts-threshold: ${AIIMAGE_COST_GUARD_SHARED_ACCOUNTS_THRESHOLD:3} diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentPartialGapStatusTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentPartialGapStatusTest.java new file mode 100644 index 000000000..5a308e524 --- /dev/null +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentPartialGapStatusTest.java @@ -0,0 +1,61 @@ +package com.nanri.aiimage.modules.appearancepatent.service.support; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class AppearancePatentPartialGapStatusTest { + + private static final String PARTIAL_GAP = + "有 3 行抓取失败(未取得商品页数据):B0833QW1CB;B0CZ41T8ZQ;B08R1WH7SS"; + + @Test + void crawlGapIsPartialNotFatal() { + assertTrue(AppearancePatentResultJobSupport.isPartialDataGapError(PARTIAL_GAP)); + assertTrue(AppearancePatentResultJobSupport.isPartialDataGapError( + "有 1 行未完成检测、有 2 行未采集到数据(结果文件中这些行已单独标注)")); + } + + @Test + void assemblyFailureIsStillFatal() { + assertFalse(AppearancePatentResultJobSupport.isPartialDataGapError( + "已收到 12 个结果分片,但结果文件生成多次失败,任务已自动结束")); + assertFalse(AppearancePatentResultJobSupport.isPartialDataGapError( + "Generate appearance patent result failed")); + assertFalse(AppearancePatentResultJobSupport.isPartialDataGapError(null)); + assertFalse(AppearancePatentResultJobSupport.isPartialDataGapError(" ")); + } + + @Test + void coverageAtOrAboveFloorSurvivesCrawlGap() { + assertFalse(AppearancePatentResultJobSupport.shouldFailAfterAssembly( + false, PARTIAL_GAP, 1_000, 0.997, 0.95)); + assertFalse(AppearancePatentResultJobSupport.shouldFailAfterAssembly( + false, PARTIAL_GAP, 1_000, 0.95, 0.95)); + } + + @Test + void coverageBelowFloorStillFails() { + assertTrue(AppearancePatentResultJobSupport.shouldFailAfterAssembly( + false, PARTIAL_GAP, 1_000, 0.90, 0.95)); + assertTrue(AppearancePatentResultJobSupport.shouldFailAfterAssembly( + false, null, 1_000, 0.50, 0.95)); + } + + @Test + void fatalErrorOrTerminalTaskNeverRevives() { + assertTrue(AppearancePatentResultJobSupport.shouldFailAfterAssembly( + true, PARTIAL_GAP, 1_000, 1.0, 0.95)); + assertTrue(AppearancePatentResultJobSupport.shouldFailAfterAssembly( + false, "结果文件生成多次失败,任务已自动结束", 1_000, 1.0, 0.95)); + } + + @Test + void emptyRowAccountKeepsLegacyOutcome() { + assertFalse(AppearancePatentResultJobSupport.shouldFailAfterAssembly( + false, null, 0, 0.0, 0.95)); + assertTrue(AppearancePatentResultJobSupport.shouldFailAfterAssembly( + false, PARTIAL_GAP, 0, 0.0, 0.95)); + } +} diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/usersecret/service/CostGuardSchedulerTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/usersecret/service/CostGuardSchedulerTest.java index 5423d9313..6b160ce58 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/usersecret/service/CostGuardSchedulerTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/usersecret/service/CostGuardSchedulerTest.java @@ -96,6 +96,36 @@ class CostGuardSchedulerTest { anyString(), contains("继续任务"), isNull()); } + @Test + void platformProxyDailyOverLimitNotifiesAdminsWithoutPausingAnyone() { + // 无归属购买记在 0 号桶:金额真实,但没有可暂停的账号,只能推管理员告警。 + when(usageMapper.sumByUserAndModule(any(LocalDate.class))) + .thenReturn(List.of(row(0L, "proxy", 2_500, 0))); + + scheduler.runOnce(); + + verify(guardService, never()).isPaused(anyLong()); + verify(guardService, never()).pause(anyLong(), anyString()); + verify(notificationDispatchService).pushToAdmins( + eq(NotificationService.SCENE_COST_GUARD), eq(NotificationService.LEVEL_WARNING), + anyString(), contains("平台自费"), anyString(), isNull()); + } + + @Test + void platformNewDayBaselineDoesNotSwallowAlert() { + // 昨天基线已高于告警线;今天累计变小是跨天,今天首次越线仍必须告警。 + baselineStore.put(0L, new CostGuardBaselineStore.Baseline( + 5_000, 0, 0, System.currentTimeMillis() - 120_000L)); + when(usageMapper.sumByUserAndModule(any(LocalDate.class))) + .thenReturn(List.of(row(0L, "proxy", 2_100, 0))); + + scheduler.runOnce(); + + verify(notificationDispatchService).pushToAdmins( + eq(NotificationService.SCENE_COST_GUARD), eq(NotificationService.LEVEL_WARNING), + anyString(), contains("平台自费"), anyString(), isNull()); + } + @Test void llmCallsWindowJumpPauses() { when(usageMapper.sumByUserAndModule(any(LocalDate.class))) diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/usersecret/service/UserSecretUsageServiceTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/usersecret/service/UserSecretUsageServiceTest.java index 81d8edffa..18eb95108 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/usersecret/service/UserSecretUsageServiceTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/usersecret/service/UserSecretUsageServiceTest.java @@ -111,6 +111,40 @@ class UserSecretUsageServiceTest { verify(usageMapper).increment(eq(7L), eq("proxy"), eq(LocalDate.now()), eq(10_000)); } + @Test + void recordReportedAllowsPlatformBucketOnlyForProxy() { + UserSecretUsageService service = newService(); + when(usageMapper.increment(any(), any(), any(), any(Integer.class))).thenReturn(1); + + assertThat(service.recordReported(0L, "proxy", 3)).isTrue(); + assertThat(service.recordReported(0L, "appearance-patent", 3)).isFalse(); + assertThat(service.recordReported(-1L, "proxy", 3)).isFalse(); + + verify(usageMapper).increment(eq(0L), eq("proxy"), eq(LocalDate.now()), eq(3)); + } + + @Test + void adminPageLabelsPlatformBucket() { + UserSecretUsageService service = newService(); + when(adminAuthSupport.currentRole(any())).thenReturn("super_admin"); + when(usageMapper.selectList(any())).thenReturn(List.of( + usageRow(0L, "proxy", 50), + usageRow(1L, "proxy", 2))); + AdminUserEntity user1 = new AdminUserEntity(); + user1.setId(1L); + user1.setUsername("张三"); + when(adminUserMapper.selectBatchIds(any())).thenReturn(List.of(user1)); + when(adminGroupMapper.selectGroupNamesByUserIds(any())).thenReturn(List.of()); + + var page = service.adminPage(new AdminUserEntity(), new AdminUserSecretUsageQuery(), ReadScope.unfiltered()); + + var platformRow = page.getItems().stream() + .filter(item -> item.getUserId() == 0L).findFirst().orElseThrow(); + assertThat(platformRow.getUsername()).isEqualTo("平台自费"); + assertThat(platformRow.getProxyCount()).isEqualTo(50); + assertThat(platformRow.getGroups()).isEmpty(); + } + @Test void adminPageAggregatesByUserAndSortsByTotal() { UserSecretUsageService service = newService(); diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/workbenchclient/service/WorkbenchClientPackageServiceTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/workbenchclient/service/WorkbenchClientPackageServiceTest.java index 895e6941c..3ca90015f 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/workbenchclient/service/WorkbenchClientPackageServiceTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/workbenchclient/service/WorkbenchClientPackageServiceTest.java @@ -64,7 +64,7 @@ class WorkbenchClientPackageServiceTest { @Test void presignUsesIndependentPrefixAndReturnsUploadUrl() { - when(ossStorageService.presignWorkbenchClientUpload(anyString(), eq(180))) + when(ossStorageService.presignWorkbenchClientUpload(anyString(), eq(3600))) .thenReturn("https://oss.aishufu.top/client/workbench-client/versions/put"); when(ossStorageService.getWorkbenchClientDownloadUrl(anyString())) .thenReturn("https://download.aishufu.top/client/workbench-client/versions/1.2.3.zip"); @@ -82,7 +82,7 @@ class WorkbenchClientPackageServiceTest { @Test void presignKeysAreUniqueForTheSameVersion() { // 同毫秒/同版本两次预签名必须不同:只靠时间戳会在 A/B 实例或并发上传时覆盖对象 - when(ossStorageService.presignWorkbenchClientUpload(anyString(), eq(180))).thenReturn("put"); + when(ossStorageService.presignWorkbenchClientUpload(anyString(), eq(3600))).thenReturn("put"); when(ossStorageService.getWorkbenchClientDownloadUrl(anyString())).thenReturn("download"); String first = (String) service().presignPackage("pkg.zip", "1.2.3").get("object_key");