feat(密钥管理): 代理配置后台明文展示 + 按次统计各密钥调用损耗
- 后台密钥管理「代理设置」列改为完整明文展示(含账号密码),便于运维核对; 新增 full 字段仅对代理模块下发,密钥两列保持脱敏 - 新增 biz_user_secret_usage_daily(V117):用户 × 模块 × 天累计真实对外请求次数 - 计次口径:LLM 每次真实 HTTP 请求(含重试)计 1 次;代理每次成功提取计 1 次 - LLM 埋点走 SecretUsageContext 上下文(批次外设置、线程池内快照恢复) - 新增内部上报接口 /api/internal/user-secret-usage(X-Internal-Token) - 新增后台页「密钥用量统计」:日期范围 + 用户名 + 分组筛选,含范围内汇总
This commit is contained in:
+18
-2
@@ -7,6 +7,9 @@ import com.nanri.aiimage.config.HttpClientPool;
|
||||
import com.nanri.aiimage.metrics.ExternalCallMetricsRecorder;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
|
||||
import com.nanri.aiimage.modules.brand.client.BrandCheckClient;
|
||||
import com.nanri.aiimage.modules.usersecret.service.UserSecretUsageService;
|
||||
import com.nanri.aiimage.modules.usersecret.support.SecretUsageContext;
|
||||
import com.nanri.aiimage.modules.usersecret.support.UserSecretModule;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
@@ -97,6 +100,7 @@ public class AppearancePatentLlmClient {
|
||||
private final ObjectMapper objectMapper;
|
||||
private final ExternalCallMetricsRecorder externalCallMetrics;
|
||||
private final BrandCheckClient brandCheckClient;
|
||||
private final UserSecretUsageService userSecretUsageService;
|
||||
|
||||
private volatile RestClient sharedRestClient;
|
||||
private volatile ExecutorService rowExecutor;
|
||||
@@ -117,9 +121,11 @@ public class AppearancePatentLlmClient {
|
||||
}
|
||||
Semaphore concurrency = new Semaphore(Math.max(1, properties.getLlmRowConcurrency()));
|
||||
ExecutorService executor = rowExecutor();
|
||||
// 行级处理跑在 worker 线程:把「当前任务归属用户」快照带过去,供 LLM 请求计次
|
||||
SecretUsageContext.Context usageContext = SecretUsageContext.snapshot();
|
||||
List<CompletableFuture<AppearancePatentResultRowDto>> futures = new ArrayList<>(rows.size());
|
||||
for (AppearancePatentResultRowDto row : rows) {
|
||||
futures.add(CompletableFuture.supplyAsync(() -> {
|
||||
futures.add(CompletableFuture.supplyAsync(SecretUsageContext.wrap(() -> {
|
||||
try {
|
||||
concurrency.acquire();
|
||||
try {
|
||||
@@ -131,7 +137,7 @@ public class AppearancePatentLlmClient {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException("LLM row inspect interrupted");
|
||||
}
|
||||
}, executor));
|
||||
}, usageContext), executor));
|
||||
}
|
||||
List<AppearancePatentResultRowDto> merged = new ArrayList<>(rows.size());
|
||||
for (int i = 0; i < futures.size(); i++) {
|
||||
@@ -288,6 +294,7 @@ public class AppearancePatentLlmClient {
|
||||
List<String> images,
|
||||
String apiKey,
|
||||
String responseFormat) {
|
||||
recordSecretUsage();
|
||||
Map<String, Object> body = buildChatBody(model, system, userText, images, responseFormat);
|
||||
log.info("[appearance-patent] llm request model={} url={} body={}",
|
||||
model, joinUrl(properties.getLlmHost(), "/v1/chat/completions"),
|
||||
@@ -841,6 +848,15 @@ public class AppearancePatentLlmClient {
|
||||
sleepQuietly(Math.max(1, attemptIndex) * 1500L);
|
||||
}
|
||||
|
||||
/** 密钥调用计次:每次真实 HTTP 请求(含重试)计 1 次;无任务上下文(无人归属)时跳过。 */
|
||||
private void recordSecretUsage() {
|
||||
SecretUsageContext.Context context = SecretUsageContext.current();
|
||||
if (context == null) {
|
||||
return;
|
||||
}
|
||||
userSecretUsageService.record(context.userId(), UserSecretModule.APPEARANCE_PATENT.key(), 1);
|
||||
}
|
||||
|
||||
private void sleepQuietly(long delayMillis) {
|
||||
try {
|
||||
Thread.sleep(delayMillis);
|
||||
|
||||
+3
-1
@@ -93,6 +93,7 @@ import java.util.zip.ZipOutputStream;
|
||||
import com.nanri.aiimage.modules.task.model.vo.TaskProgressLightBatchVo;
|
||||
import com.nanri.aiimage.modules.task.service.TaskProgressLightAssembler;
|
||||
import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService;
|
||||
import com.nanri.aiimage.modules.usersecret.support.SecretUsageContext;
|
||||
import com.nanri.aiimage.modules.usersecret.support.UserSecretModule;
|
||||
|
||||
@Service
|
||||
@@ -802,7 +803,8 @@ public class AppearancePatentTaskService {
|
||||
long startedAt = System.currentTimeMillis();
|
||||
List<AppearancePatentResultRowDto> llmRows;
|
||||
try {
|
||||
llmRows = llmClient.inspectRows(batchRows, prompt, apiKey);
|
||||
llmRows = SecretUsageContext.call(task.getUserId(), UserSecretModule.APPEARANCE_PATENT.key(),
|
||||
() -> llmClient.inspectRows(batchRows, prompt, apiKey));
|
||||
} catch (Exception ex) {
|
||||
String message = firstNonBlank(ex.getMessage(), "LLM 检测失败");
|
||||
log.warn("[appearance-patent] llm batch failed taskId={} jobId={} rows={} batch={}/{} err={}",
|
||||
|
||||
Reference in New Issue
Block a user