feat(phase-B75): 计次上报加 source 维度(V188)——「谁在烧」一列读数,归因不再扫设备日志
- V188:biz_user_secret_usage_daily 加 source varchar(24) NOT NULL DEFAULT 'legacy'(存量行诚实落 legacy 不回填),唯一键 uk_user_module_date 扩成四元组(user/module/date/source),加列与建索引均 information_schema 判存可重跑(列按 V157 款、索引按 V184 款),八段式含验证 SQL 与人工回滚
This commit is contained in:
+3
-3
@@ -46,9 +46,9 @@ public class InternalUserSecretUsageController {
|
||||
return ApiResponse.fail("上报参数不完整");
|
||||
}
|
||||
boolean accepted = userSecretUsageService.recordReported(
|
||||
body.getUserId(), body.getModule(), body.getCount());
|
||||
log.info("[internal-usage] 计次上报 userId={} module={} count={} accepted={}",
|
||||
body.getUserId(), body.getModule(), body.getCount(), accepted);
|
||||
body.getUserId(), body.getModule(), body.getCount(), body.getSource());
|
||||
log.info("[internal-usage] 计次上报 userId={} module={} count={} source={} accepted={}",
|
||||
body.getUserId(), body.getModule(), body.getCount(), body.getSource(), accepted);
|
||||
if (!accepted) {
|
||||
return ApiResponse.fail("模块或参数非法,未记录");
|
||||
}
|
||||
|
||||
+12
-5
@@ -13,27 +13,34 @@ import java.util.List;
|
||||
public interface UserSecretUsageMapper extends BaseMapper<UserSecretUsageEntity> {
|
||||
|
||||
/**
|
||||
* 按唯一键 uk_user_module_date 原子累加计次:命中则 call_count 自增,否则插入。
|
||||
* 并发下无需应用层加锁(引擎层唯一键冲突转更新)。
|
||||
* 按唯一键 uk_user_module_date_source(V188:user_id+module_key+business_date+source)
|
||||
* 原子累加计次:命中则 call_count 自增,否则插入。并发下无需应用层加锁
|
||||
* (引擎层唯一键冲突转更新)。source=上报方自报身份(client/brand16890/brand15126/
|
||||
* pt_worker),Java 自产行=backend,缺省/非法=legacy——同一天多路各占一行,
|
||||
* 读侧按模块聚合对多出的行透明。
|
||||
*/
|
||||
int increment(@Param("userId") Long userId,
|
||||
@Param("moduleKey") String moduleKey,
|
||||
@Param("businessDate") LocalDate businessDate,
|
||||
@Param("count") int count);
|
||||
@Param("count") int count,
|
||||
@Param("source") String source);
|
||||
|
||||
/**
|
||||
* 按唯一键 uk_user_module_date 累加 token 读数。
|
||||
* 按唯一键 uk_user_module_date_source 累加 token 读数。
|
||||
*
|
||||
* <p>刻意与 {@link #increment} 分成两条:计次发生在**发请求之前**(口径含重试,请求没回来也要算一次),
|
||||
* 而 token 只有响应回来才知道。合并成一条 upsert 就会把"已发出但超时"的那些请求从计次里漏掉。
|
||||
* <p>这里是 UPDATE 而不是 upsert:行由前一条 increment 保证存在;真不存在就是 0 行受影响,
|
||||
* token 读数丢掉也不该反过来影响计次。
|
||||
* <p>WHERE 必须带 source(V188):唯一键扩成四元组后,同一 (user, module, date) 可能
|
||||
* 有 legacy 与 backend 多行,不带 source 会把 token 记到历史行上造成双计。
|
||||
*/
|
||||
int addTokens(@Param("userId") Long userId,
|
||||
@Param("moduleKey") String moduleKey,
|
||||
@Param("businessDate") LocalDate businessDate,
|
||||
@Param("promptTokens") long promptTokens,
|
||||
@Param("completionTokens") long completionTokens);
|
||||
@Param("completionTokens") long completionTokens,
|
||||
@Param("source") String source);
|
||||
|
||||
/**
|
||||
* 当日用量按用户 × 模块聚合(损耗扫描每分钟读一次)。
|
||||
|
||||
+4
@@ -16,4 +16,8 @@ public class UserSecretUsageReportRequest {
|
||||
|
||||
@Schema(description = "本次新增调用次数(真实对外请求)", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
private Integer count;
|
||||
|
||||
@Schema(description = "上报方来源(V188):client/brand16890/brand15126/pt_worker;"
|
||||
+ "缺省或非法落 legacy 桶。旧上报方不带该字段,兼容。", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
|
||||
private String source;
|
||||
}
|
||||
|
||||
+3
-1
@@ -9,7 +9,7 @@ import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/** 用户密钥调用计次(按天聚合,一行 = 用户 × 模块 × 日期)。 */
|
||||
/** 用户密钥调用计次(按天聚合,一行 = 用户 × 模块 × 日期 × 来源(V188))。 */
|
||||
@Data
|
||||
@TableName("biz_user_secret_usage_daily")
|
||||
public class UserSecretUsageEntity {
|
||||
@@ -18,6 +18,8 @@ public class UserSecretUsageEntity {
|
||||
private Long id;
|
||||
private Long userId;
|
||||
private String moduleKey;
|
||||
/** 计次来源(V188):client/brand16890/brand15126/pt_worker/backend;存量行=legacy。 */
|
||||
private String source;
|
||||
private LocalDate businessDate;
|
||||
private Integer callCount;
|
||||
/** 输入 token 累计(网关 usage.prompt_tokens)。0 = 该天没有 token 证据,不等于没消耗。 */
|
||||
|
||||
+36
-7
@@ -49,6 +49,11 @@ public class UserSecretUsageService {
|
||||
public static final long PLATFORM_USER_ID = 0L;
|
||||
static final String PLATFORM_USER_LABEL = "平台自费";
|
||||
|
||||
/** 计次来源(V188):Java 自产行(LLM record/recordTokens)落 backend。 */
|
||||
static final String SOURCE_BACKEND = "backend";
|
||||
/** 存量行与缺省/非法报文的落桶:历史行无法追溯是哪一路报的,不猜测。 */
|
||||
public static final String SOURCE_LEGACY = "legacy";
|
||||
|
||||
private final UserSecretUsageMapper usageMapper;
|
||||
private final AdminUserMapper adminUserMapper;
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
@@ -68,11 +73,16 @@ public class UserSecretUsageService {
|
||||
log.warn("[user-secret-usage] 忽略未知模块计次 userId={} module={}", userId, moduleKey);
|
||||
return;
|
||||
}
|
||||
recordModule(userId, module, count, 0L, 0L);
|
||||
recordModule(userId, module, count, 0L, 0L, SOURCE_BACKEND);
|
||||
}
|
||||
|
||||
/** 内部通道上报入口:模块非法返回 false(供接口回执),写库失败只告警。 */
|
||||
/** 内部通道上报入口(3 参旧形状):source 缺省落 legacy 桶。模块非法返回 false。 */
|
||||
public boolean recordReported(Long userId, String moduleKey, int count) {
|
||||
return recordReported(userId, moduleKey, count, null);
|
||||
}
|
||||
|
||||
/** 内部通道上报入口(V188 带 source):模块非法返回 false,写库失败只告警。 */
|
||||
public boolean recordReported(Long userId, String moduleKey, int count, String source) {
|
||||
if (userId == null || userId < 0 || count <= 0) {
|
||||
return false;
|
||||
}
|
||||
@@ -85,7 +95,8 @@ public class UserSecretUsageService {
|
||||
log.warn("[user-secret-usage] 平台自费桶只收 proxy 计次 userId=0 module={}", moduleKey);
|
||||
return false;
|
||||
}
|
||||
return recordModule(userId, module, Math.min(count, MAX_REPORT_COUNT), 0L, 0L);
|
||||
return recordModule(userId, module, Math.min(count, MAX_REPORT_COUNT), 0L, 0L,
|
||||
normalizeSource(source));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -106,18 +117,36 @@ public class UserSecretUsageService {
|
||||
if (module == null) {
|
||||
return;
|
||||
}
|
||||
recordModule(userId, module, 0, promptTokens, completionTokens);
|
||||
recordModule(userId, module, 0, promptTokens, completionTokens, SOURCE_BACKEND);
|
||||
}
|
||||
|
||||
/**
|
||||
* source 规整(V188):小写、^[a-z0-9][a-z0-9_-]{0,23}$ 以外的一律落 legacy——
|
||||
* 上报方是可信内部调用方,但字符串仍可能写错;落 legacy 而不是拒收,
|
||||
* 计次比归因更贵(宁可归因模糊也不丢账)。
|
||||
*/
|
||||
static String normalizeSource(String raw) {
|
||||
if (raw == null) {
|
||||
return SOURCE_LEGACY;
|
||||
}
|
||||
String trimmed = raw.trim().toLowerCase();
|
||||
if (trimmed.isEmpty() || trimmed.length() > 24
|
||||
|| !trimmed.matches("[a-z0-9][a-z0-9_-]*")) {
|
||||
return SOURCE_LEGACY;
|
||||
}
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
private boolean recordModule(Long userId, UserSecretModule module, int count,
|
||||
long promptTokens, long completionTokens) {
|
||||
long promptTokens, long completionTokens, String source) {
|
||||
LocalDate today = LocalDate.now();
|
||||
try {
|
||||
if (count > 0) {
|
||||
usageMapper.increment(userId, module.key(), today, count);
|
||||
usageMapper.increment(userId, module.key(), today, count, source);
|
||||
}
|
||||
if (promptTokens > 0 || completionTokens > 0) {
|
||||
usageMapper.addTokens(userId, module.key(), today, promptTokens, completionTokens);
|
||||
usageMapper.addTokens(userId, module.key(), today, promptTokens, completionTokens,
|
||||
source);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
|
||||
Reference in New Issue
Block a user