feat: 优化
This commit is contained in:
@@ -48,110 +48,118 @@ public class AiUsageStatsServiceImpl implements AiUsageStatsService {
|
||||
|
||||
@Override
|
||||
public AiUsageOverviewRespVO getOverview(LocalDateTime startTime, LocalDateTime endTime, String bizType) {
|
||||
// 查询业务类型统计
|
||||
List<Map<String, Object>> bizTypeStatsList = pointRecordMapper.selectBizTypeStats(startTime, endTime);
|
||||
return TenantUtils.executeIgnore(() -> {
|
||||
// 查询业务类型统计
|
||||
List<Map<String, Object>> bizTypeStatsList = pointRecordMapper.selectBizTypeStats(startTime, endTime);
|
||||
|
||||
// 计算汇总数据
|
||||
long totalCallCount = 0;
|
||||
long totalConsumePoints = 0;
|
||||
long totalTokens = 0;
|
||||
// 计算汇总数据
|
||||
long totalCallCount = 0;
|
||||
long totalConsumePoints = 0;
|
||||
long totalTokens = 0;
|
||||
|
||||
List<AiUsageOverviewRespVO.BizTypeStats> bizTypeStats = new ArrayList<>();
|
||||
for (Map<String, Object> stats : bizTypeStatsList) {
|
||||
String bt = (String) stats.get("bizType");
|
||||
// 如果指定了业务类型,只统计该类型
|
||||
if (bizType != null && !bizType.isEmpty() && !bizType.equals(bt)) {
|
||||
continue;
|
||||
List<AiUsageOverviewRespVO.BizTypeStats> bizTypeStats = new ArrayList<>();
|
||||
for (Map<String, Object> stats : bizTypeStatsList) {
|
||||
String bt = (String) stats.get("bizType");
|
||||
// 如果指定了业务类型,只统计该类型
|
||||
if (bizType != null && !bizType.isEmpty() && !bizType.equals(bt)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
long callCount = getLongValue(stats, "callCount");
|
||||
long consumePoints = getLongValue(stats, "consumePoints");
|
||||
long tokens = getLongValue(stats, "totalTokens");
|
||||
|
||||
totalCallCount += callCount;
|
||||
totalConsumePoints += consumePoints;
|
||||
totalTokens += tokens;
|
||||
|
||||
bizTypeStats.add(AiUsageOverviewRespVO.BizTypeStats.builder()
|
||||
.bizType(bt)
|
||||
.bizTypeName(BIZ_TYPE_NAMES.getOrDefault(bt, bt))
|
||||
.callCount(callCount)
|
||||
.consumePoints(consumePoints)
|
||||
.totalTokens(tokens)
|
||||
.build());
|
||||
}
|
||||
|
||||
long callCount = getLongValue(stats, "callCount");
|
||||
long consumePoints = getLongValue(stats, "consumePoints");
|
||||
long tokens = getLongValue(stats, "totalTokens");
|
||||
// 查询活跃用户数
|
||||
Long activeUserCount = pointRecordMapper.selectActiveUserCount(startTime, endTime, bizType);
|
||||
|
||||
totalCallCount += callCount;
|
||||
totalConsumePoints += consumePoints;
|
||||
totalTokens += tokens;
|
||||
|
||||
bizTypeStats.add(AiUsageOverviewRespVO.BizTypeStats.builder()
|
||||
.bizType(bt)
|
||||
.bizTypeName(BIZ_TYPE_NAMES.getOrDefault(bt, bt))
|
||||
.callCount(callCount)
|
||||
.consumePoints(consumePoints)
|
||||
.totalTokens(tokens)
|
||||
.build());
|
||||
}
|
||||
|
||||
// 查询活跃用户数
|
||||
Long activeUserCount = pointRecordMapper.selectActiveUserCount(startTime, endTime, bizType);
|
||||
|
||||
return AiUsageOverviewRespVO.builder()
|
||||
.totalCallCount(totalCallCount)
|
||||
.totalConsumePoints(totalConsumePoints)
|
||||
.totalTokens(totalTokens)
|
||||
.activeUserCount(activeUserCount)
|
||||
.bizTypeStats(bizTypeStats)
|
||||
.build();
|
||||
return AiUsageOverviewRespVO.builder()
|
||||
.totalCallCount(totalCallCount)
|
||||
.totalConsumePoints(totalConsumePoints)
|
||||
.totalTokens(totalTokens)
|
||||
.activeUserCount(activeUserCount)
|
||||
.bizTypeStats(bizTypeStats)
|
||||
.build();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<AiUsageUserStatsRespVO> getUserStatsPage(AiUsageStatsPageReqVO reqVO) {
|
||||
// 查询用户统计数据
|
||||
List<Map<String, Object>> userStatsList = pointRecordMapper.selectUserStats(
|
||||
reqVO.getStartTime(), reqVO.getEndTime(), reqVO.getBizType(), reqVO.getUserId(), reqVO.getMobile());
|
||||
return TenantUtils.executeIgnore(() -> {
|
||||
// 查询用户统计数据
|
||||
List<Map<String, Object>> userStatsList = pointRecordMapper.selectUserStats(
|
||||
reqVO.getStartTime(), reqVO.getEndTime(), reqVO.getBizType(), reqVO.getUserId(), reqVO.getMobile());
|
||||
|
||||
// 计算总数
|
||||
long total = userStatsList.size();
|
||||
// 计算总数
|
||||
long total = userStatsList.size();
|
||||
|
||||
// 分页处理
|
||||
int fromIndex = (reqVO.getPageNo() - 1) * reqVO.getPageSize();
|
||||
int toIndex = Math.min(fromIndex + reqVO.getPageSize(), userStatsList.size());
|
||||
// 分页处理
|
||||
int fromIndex = (reqVO.getPageNo() - 1) * reqVO.getPageSize();
|
||||
int toIndex = Math.min(fromIndex + reqVO.getPageSize(), userStatsList.size());
|
||||
|
||||
if (fromIndex >= userStatsList.size()) {
|
||||
return new PageResult<>(Collections.emptyList(), total);
|
||||
}
|
||||
if (fromIndex >= userStatsList.size()) {
|
||||
return new PageResult<>(Collections.emptyList(), total);
|
||||
}
|
||||
|
||||
List<Map<String, Object>> pagedList = userStatsList.subList(fromIndex, toIndex);
|
||||
List<Map<String, Object>> pagedList = userStatsList.subList(fromIndex, toIndex);
|
||||
|
||||
// 转换为 VO
|
||||
List<AiUsageUserStatsRespVO> list = pagedList.stream()
|
||||
.map(this::convertToUserStatsVO)
|
||||
.collect(Collectors.toList());
|
||||
// 转换为 VO
|
||||
List<AiUsageUserStatsRespVO> list = pagedList.stream()
|
||||
.map(this::convertToUserStatsVO)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return new PageResult<>(list, total);
|
||||
return new PageResult<>(list, total);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AiUsageAppStatsRespVO> getAppStats(LocalDateTime startTime, LocalDateTime endTime, String bizType) {
|
||||
// 查询应用统计数据
|
||||
List<Map<String, Object>> appStatsList = pointRecordMapper.selectAppStats(
|
||||
startTime, endTime, bizType);
|
||||
return TenantUtils.executeIgnore(() -> {
|
||||
// 查询应用统计数据
|
||||
List<Map<String, Object>> appStatsList = pointRecordMapper.selectAppStats(
|
||||
startTime, endTime, bizType);
|
||||
|
||||
// 获取服务配置信息
|
||||
Map<String, AiServiceConfigDO> configMap = aiServiceConfigMapper.selectList(
|
||||
new LambdaQueryWrapperX<AiServiceConfigDO>().eq(AiServiceConfigDO::getStatus, 1))
|
||||
.stream()
|
||||
.collect(Collectors.toMap(AiServiceConfigDO::getServiceCode, c -> c, (a, b) -> a));
|
||||
// 获取服务配置信息
|
||||
Map<String, AiServiceConfigDO> configMap = aiServiceConfigMapper.selectList(
|
||||
new LambdaQueryWrapperX<AiServiceConfigDO>().eq(AiServiceConfigDO::getStatus, 1))
|
||||
.stream()
|
||||
.collect(Collectors.toMap(AiServiceConfigDO::getServiceCode, c -> c, (a, b) -> a));
|
||||
|
||||
// 转换为 VO
|
||||
return appStatsList.stream()
|
||||
.map(stats -> convertToAppStatsVO(stats, configMap))
|
||||
.collect(Collectors.toList());
|
||||
// 转换为 VO
|
||||
return appStatsList.stream()
|
||||
.map(stats -> convertToAppStatsVO(stats, configMap))
|
||||
.collect(Collectors.toList());
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public AiUsageTrendRespVO getTrend(LocalDateTime startTime, LocalDateTime endTime, String bizType, String type) {
|
||||
// 查询趋势数据
|
||||
List<Map<String, Object>> trendList = pointRecordMapper.selectTrend(
|
||||
startTime, endTime, bizType, "day".equals(type) ? "%Y-%m-%d" : "%Y-%m-%d %H:00");
|
||||
return TenantUtils.executeIgnore(() -> {
|
||||
// 查询趋势数据
|
||||
List<Map<String, Object>> trendList = pointRecordMapper.selectTrend(
|
||||
startTime, endTime, bizType, "day".equals(type) ? "%Y-%m-%d" : "%Y-%m-%d %H:00");
|
||||
|
||||
// 转换为 VO
|
||||
List<AiUsageTrendRespVO.TrendItem> items = trendList.stream()
|
||||
.map(this::convertToTrendItem)
|
||||
.collect(Collectors.toList());
|
||||
// 转换为 VO
|
||||
List<AiUsageTrendRespVO.TrendItem> items = trendList.stream()
|
||||
.map(this::convertToTrendItem)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return AiUsageTrendRespVO.builder()
|
||||
.trendList(items)
|
||||
.build();
|
||||
return AiUsageTrendRespVO.builder()
|
||||
.trendList(items)
|
||||
.build();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user