feat: 新增 AI 用量落库与统计接口
- 新增 ai_usage_records 表(系统级表,不按租户过滤)与幂等上报接口 - 新增按时间/任务类型/模型的汇总、趋势、明细查询,仅白名单用户可见 - 登录响应返回 aiAdmin,供前端决定是否展示入口
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package com.guo.learningprogresstracker.controller;
|
||||
|
||||
import com.guo.learningprogresstracker.dto.AiUsageBalance;
|
||||
import com.guo.learningprogresstracker.dto.AiUsageBreakdownItem;
|
||||
import com.guo.learningprogresstracker.dto.AiUsagePermission;
|
||||
import com.guo.learningprogresstracker.dto.AiUsageSeriesPoint;
|
||||
import com.guo.learningprogresstracker.dto.AiUsageSummary;
|
||||
import com.guo.learningprogresstracker.dto.request.AiUsageQuery;
|
||||
import com.guo.learningprogresstracker.entity.CommonResult;
|
||||
import com.guo.learningprogresstracker.exception.ErrorParameterException;
|
||||
import com.guo.learningprogresstracker.service.AiUsageService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 「AI 用量统计」面板查询接口(契约 2)。
|
||||
*
|
||||
* <p>用量记录是系统级数据,只有白名单用户能看:{@code permission} 用 {@code allowed=false}
|
||||
* 让前端静默隐藏入口,其余接口直接拒绝,避免入口藏起来了、接口却谁都能拉。</p>
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/ai/usage")
|
||||
@RequiredArgsConstructor
|
||||
public class AiUsageController {
|
||||
|
||||
private static final String NO_PERMISSION_MESSAGE = "没有查看权限";
|
||||
|
||||
private final AiUsageService aiUsageService;
|
||||
|
||||
/** 当前用户能否查看用量面板;非白名单返回 200 + allowed=false */
|
||||
@GetMapping("/permission")
|
||||
public CommonResult<AiUsagePermission> permission() {
|
||||
return CommonResult.success(new AiUsagePermission(aiUsageService.isAdmin()));
|
||||
}
|
||||
|
||||
@GetMapping("/summary")
|
||||
public CommonResult<AiUsageSummary> summary(AiUsageQuery query) {
|
||||
if (!aiUsageService.isAdmin()) {
|
||||
return CommonResult.error(NO_PERMISSION_MESSAGE);
|
||||
}
|
||||
return CommonResult.success(aiUsageService.summary(query));
|
||||
}
|
||||
|
||||
@GetMapping("/series")
|
||||
public CommonResult<List<AiUsageSeriesPoint>> series(AiUsageQuery query) throws ErrorParameterException {
|
||||
if (!aiUsageService.isAdmin()) {
|
||||
return CommonResult.error(NO_PERMISSION_MESSAGE);
|
||||
}
|
||||
return CommonResult.success(aiUsageService.series(query));
|
||||
}
|
||||
|
||||
@GetMapping("/breakdown")
|
||||
public CommonResult<List<AiUsageBreakdownItem>> breakdown(AiUsageQuery query) throws ErrorParameterException {
|
||||
if (!aiUsageService.isAdmin()) {
|
||||
return CommonResult.error(NO_PERMISSION_MESSAGE);
|
||||
}
|
||||
return CommonResult.success(aiUsageService.breakdown(query));
|
||||
}
|
||||
|
||||
/** 上游余额;lpt-ai 未配置或查询失败时返回 {@code configured=false},不算错误 */
|
||||
@GetMapping("/balance")
|
||||
public CommonResult<AiUsageBalance> balance() {
|
||||
if (!aiUsageService.isAdmin()) {
|
||||
return CommonResult.error(NO_PERMISSION_MESSAGE);
|
||||
}
|
||||
return CommonResult.success(aiUsageService.balance());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user