feat: 新增 AI 用量落库与统计接口
- 新增 ai_usage_records 表(系统级表,不按租户过滤)与幂等上报接口 - 新增按时间/任务类型/模型的汇总、趋势、明细查询,仅白名单用户可见 - 登录响应返回 aiAdmin,供前端决定是否展示入口
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package com.guo.learningprogresstracker.dto.request;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 用量查询条件(契约 2.2 / 2.3 / 2.4):summary、series、breakdown 共用同一组筛选参数。
|
||||
*/
|
||||
@Data
|
||||
public class AiUsageQuery {
|
||||
|
||||
/** 起始时刻(epoch millis);为空表示最近 30 天 */
|
||||
private Long from;
|
||||
|
||||
/** 结束时刻(epoch millis);为空表示「到此刻为止」 */
|
||||
private Long to;
|
||||
|
||||
/** 任务类型,精确匹配;为空表示不限 */
|
||||
private String taskType;
|
||||
|
||||
/** 模型名,精确匹配;为空表示不限 */
|
||||
private String model;
|
||||
|
||||
/** series 的分桶粒度:day / hour */
|
||||
private String bucket = "day";
|
||||
|
||||
/** breakdown 的分组维度:taskType / model */
|
||||
private String groupBy = "taskType";
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.guo.learningprogresstracker.dto.request;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* 一条 AI 用量记录(契约 1)。
|
||||
*
|
||||
* <p>token 类字段一律包装类型:{@code null} 表示上游未上报,{@code 0} 表示上游明确上报为 0,
|
||||
* 两者语义不同,不得用 0 顶替 null。</p>
|
||||
*/
|
||||
@Data
|
||||
public class AiUsageRecordRequest {
|
||||
|
||||
/** 幂等键,lpt-ai 生成 */
|
||||
@NotBlank(message = "recordId 不能为空")
|
||||
private String recordId;
|
||||
|
||||
/** 调用发生时间,ISO 8601 UTC,如 {@code 2026-10-10T09:38:23.543Z} */
|
||||
@NotBlank(message = "occurredAt 不能为空")
|
||||
private String occurredAt;
|
||||
|
||||
@NotBlank(message = "taskType 不能为空")
|
||||
private String taskType;
|
||||
|
||||
private String taskId;
|
||||
|
||||
/**
|
||||
* 模型名。
|
||||
*
|
||||
* <p>用 {@code @NotNull} 而不是 {@code @NotBlank}:{@code isLlmCall=false} 的记录
|
||||
* (如 {@code /fetch-title})本来就没调用模型,契约里它的 model 就是空串。
|
||||
* 用 {@code @NotBlank} 会让这一条把整批上报打成 400,而上报方会把 400 当成可重试错误,
|
||||
* outbox 被永久堵死。</p>
|
||||
*/
|
||||
@NotNull(message = "model 不能为空")
|
||||
private String model;
|
||||
|
||||
@NotNull(message = "attempts 不能为空")
|
||||
private Integer attempts;
|
||||
|
||||
@NotNull(message = "isLlmCall 不能为空")
|
||||
private Boolean isLlmCall;
|
||||
|
||||
@NotBlank(message = "usageSource 不能为空")
|
||||
private String usageSource;
|
||||
|
||||
private Integer promptTokens;
|
||||
|
||||
private Integer completionTokens;
|
||||
|
||||
private Integer totalTokens;
|
||||
|
||||
private Integer cacheHitTokens;
|
||||
|
||||
private Integer cacheMissTokens;
|
||||
|
||||
private Integer reasoningTokens;
|
||||
|
||||
private BigDecimal costUsd;
|
||||
|
||||
private String pricingVersion;
|
||||
|
||||
private Boolean peak;
|
||||
|
||||
@NotNull(message = "durationMs 不能为空")
|
||||
private Integer durationMs;
|
||||
|
||||
@NotNull(message = "status 不能为空")
|
||||
private Integer status;
|
||||
|
||||
private String errorCode;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.guo.learningprogresstracker.dto.request;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* lpt-ai 用量上报请求体(契约 1,{@code POST /internal/ai-usage/records})。
|
||||
*/
|
||||
@Data
|
||||
public class AiUsageReportRequest {
|
||||
|
||||
/** 单次最多 200 条(见 {@code AiUsageInternalController.MAX_RECORDS_PER_REPORT}) */
|
||||
@NotNull(message = "records 不能为空")
|
||||
@Valid
|
||||
private List<AiUsageRecordRequest> records;
|
||||
}
|
||||
Reference in New Issue
Block a user