仪表板功能完善

This commit is contained in:
2025-10-31 14:23:38 +08:00
parent cafbae4324
commit 778f05daa5
2 changed files with 276 additions and 43 deletions
@@ -1,6 +1,8 @@
package com.emotion.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.emotion.dto.response.DashboardStatsResponse;
import com.emotion.entity.*;
import com.emotion.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@@ -84,14 +86,25 @@ public class DashboardServiceImpl implements DashboardService {
public DashboardStatsResponse.UserStats getUserStats() {
try {
// 获取用户统计数据
Long totalUsers = userService.count();
Long guestUsers = guestUserService.count();
Long totalUsers = userService.count(new LambdaQueryWrapper<User>()
.eq(User::getIsDeleted, 0));
// 获取今日新增用户(这里简化处理,实际可能需要根据创建时间查询)
Long todayNewUsers = 0L; // 可以通过查询今日创建的用户数量来获取
Long guestUsers = guestUserService.count(new LambdaQueryWrapper<GuestUser>()
.eq(GuestUser::getIsDeleted, 0));
// 活跃用户数(这里简化处理,可以根据最近登录时间或活动时间来计算)
Long activeUsers = totalUsers; // 简化处理
// 获取今日新增用户
LocalDateTime todayStart = LocalDate.now().atStartOfDay();
LocalDateTime todayEnd = todayStart.plusDays(1);
Long todayNewUsers = userService.count(new LambdaQueryWrapper<User>()
.eq(User::getIsDeleted, 0)
.ge(User::getCreateTime, todayStart)
.lt(User::getCreateTime, todayEnd));
// 活跃用户数(最近7天有活动的用户)
LocalDateTime sevenDaysAgo = LocalDateTime.now().minusDays(7);
Long activeUsers = userService.count(new LambdaQueryWrapper<User>()
.eq(User::getIsDeleted, 0)
.ge(User::getLastActiveTime, sevenDaysAgo));
return DashboardStatsResponse.UserStats.builder()
.totalUsers(totalUsers)
@@ -113,11 +126,20 @@ public class DashboardServiceImpl implements DashboardService {
@Override
public DashboardStatsResponse.ContentStats getContentStats() {
try {
Long totalConversations = conversationService.count();
Long totalMessages = messageService.count();
Long diaryPosts = diaryPostService.count();
Long communityPosts = communityPostService.count();
Long emotionRecords = emotionRecordService.count();
Long totalConversations = conversationService.count(new LambdaQueryWrapper<Conversation>()
.eq(Conversation::getIsDeleted, 0));
Long totalMessages = messageService.count(new LambdaQueryWrapper<Message>()
.eq(Message::getIsDeleted, 0));
Long diaryPosts = diaryPostService.count(new LambdaQueryWrapper<DiaryPost>()
.eq(DiaryPost::getIsDeleted, 0));
Long communityPosts = communityPostService.count(new LambdaQueryWrapper<CommunityPost>()
.eq(CommunityPost::getIsDeleted, 0));
Long emotionRecords = emotionRecordService.count(new LambdaQueryWrapper<EmotionRecord>()
.eq(EmotionRecord::getIsDeleted, 0));
return DashboardStatsResponse.ContentStats.builder()
.totalConversations(totalConversations)
@@ -141,18 +163,40 @@ public class DashboardServiceImpl implements DashboardService {
@Override
public DashboardStatsResponse.AiServiceStats getAiServiceStats() {
try {
Long totalApiCalls = cozeApiCallService.count();
Long aiConfigCount = aiConfigService.count();
Long totalApiCalls = cozeApiCallService.count(new LambdaQueryWrapper<CozeApiCall>()
.eq(CozeApiCall::getIsDeleted, 0));
// 获取今日API调用次数(简化处理)
Long todayApiCalls = 0L; // 可以通过查询今日的API调用记录来获取
Long aiConfigCount = aiConfigService.count(new LambdaQueryWrapper<AiConfig>()
.eq(AiConfig::getIsDeleted, 0));
// 获取成功和失败的调用次数(简化处理)
Long successfulCalls = totalApiCalls; // 可以通过状态字段查询
Long failedCalls = 0L;
// 获取今日API调用次数
LocalDateTime todayStart = LocalDate.now().atStartOfDay();
LocalDateTime todayEnd = todayStart.plusDays(1);
Long todayApiCalls = cozeApiCallService.count(new LambdaQueryWrapper<CozeApiCall>()
.eq(CozeApiCall::getIsDeleted, 0)
.ge(CozeApiCall::getCreateTime, todayStart)
.lt(CozeApiCall::getCreateTime, todayEnd));
// 平均响应时间(简化处理)
Double avgResponseTime = 500.0; // 可以通过计算duration_ms字段的平均值来获取
// 获取成功和失败的调用次数
Long successfulCalls = cozeApiCallService.count(new LambdaQueryWrapper<CozeApiCall>()
.eq(CozeApiCall::getIsDeleted, 0)
.eq(CozeApiCall::getStatus, "success"));
Long failedCalls = cozeApiCallService.count(new LambdaQueryWrapper<CozeApiCall>()
.eq(CozeApiCall::getIsDeleted, 0)
.eq(CozeApiCall::getStatus, "failed"));
// 计算平均响应时间(从duration_ms字段)
List<CozeApiCall> apiCalls = cozeApiCallService.list(new LambdaQueryWrapper<CozeApiCall>()
.eq(CozeApiCall::getIsDeleted, 0)
.isNotNull(CozeApiCall::getDurationMs)
.last("LIMIT 1000")); // 取最近1000条记录计算平均值
Double avgResponseTime = apiCalls.stream()
.filter(call -> call.getDurationMs() != null)
.mapToInt(CozeApiCall::getDurationMs)
.average()
.orElse(0.0);
return DashboardStatsResponse.AiServiceStats.builder()
.totalApiCalls(totalApiCalls)
@@ -178,9 +222,15 @@ public class DashboardServiceImpl implements DashboardService {
@Override
public DashboardStatsResponse.SystemStats getSystemStats() {
try {
Long adminCount = adminService.count();
Long achievementCount = achievementService.count();
Long rewardCount = rewardService.count();
Long adminCount = adminService.count(new LambdaQueryWrapper<Admin>()
.eq(Admin::getIsDeleted, 0)
.eq(Admin::getStatus, 1)); // 只统计正常状态的管理员
Long achievementCount = achievementService.count(new LambdaQueryWrapper<Achievement>()
.eq(Achievement::getIsDeleted, 0));
Long rewardCount = rewardService.count(new LambdaQueryWrapper<Reward>()
.eq(Reward::getIsDeleted, 0));
// 获取系统运行时间
long uptimeMs = ManagementFactory.getRuntimeMXBean().getUptime();
@@ -210,27 +260,70 @@ public class DashboardServiceImpl implements DashboardService {
List<DashboardStatsResponse.RecentActivity> activities = new ArrayList<>();
try {
// 这里可以添加获取最近活动的逻辑
// 例如:最近的用户注册、消息发送、帖子创建等
// 获取最近注册的用户
List<User> recentUsers = userService.list(new LambdaQueryWrapper<User>()
.eq(User::getIsDeleted, 0)
.orderByDesc(User::getCreateTime)
.last("LIMIT 3"));
// 示例活动
activities.add(DashboardStatsResponse.RecentActivity.builder()
.type("user_register")
.description("新用户注册")
.userId("system")
.username("系统")
.activityTime(LocalDateTime.now().minusMinutes(30))
.extraData(new HashMap<>())
.build());
activities.add(DashboardStatsResponse.RecentActivity.builder()
.type("ai_chat")
.description("AI聊天对话")
.userId("system")
.username("系统")
.activityTime(LocalDateTime.now().minusMinutes(15))
.extraData(new HashMap<>())
.build());
for (User user : recentUsers) {
activities.add(DashboardStatsResponse.RecentActivity.builder()
.type("user_register")
.description("新用户注册: " + (user.getNickname() != null ? user.getNickname() : user.getUsername()))
.userId(user.getId())
.username(user.getNickname() != null ? user.getNickname() : user.getUsername())
.activityTime(user.getCreateTime())
.extraData(new HashMap<>())
.build());
}
// 获取最近的对话
List<Conversation> recentConversations = conversationService.list(new LambdaQueryWrapper<Conversation>()
.eq(Conversation::getIsDeleted, 0)
.orderByDesc(Conversation::getCreateTime)
.last("LIMIT 3"));
for (Conversation conversation : recentConversations) {
Map<String, Object> extraData = new HashMap<>();
extraData.put("conversationId", conversation.getId());
extraData.put("title", conversation.getTitle());
activities.add(DashboardStatsResponse.RecentActivity.builder()
.type("ai_chat")
.description("新的AI对话: " + (conversation.getTitle() != null ? conversation.getTitle() : "未命名对话"))
.userId(conversation.getUserId())
.username("用户")
.activityTime(conversation.getCreateTime())
.extraData(extraData)
.build());
}
// 获取最近的日记帖子
List<DiaryPost> recentDiaryPosts = diaryPostService.list(new LambdaQueryWrapper<DiaryPost>()
.eq(DiaryPost::getIsDeleted, 0)
.orderByDesc(DiaryPost::getCreateTime)
.last("LIMIT 2"));
for (DiaryPost post : recentDiaryPosts) {
Map<String, Object> extraData = new HashMap<>();
extraData.put("postId", post.getId());
extraData.put("title", post.getTitle());
activities.add(DashboardStatsResponse.RecentActivity.builder()
.type("diary_post")
.description("新的日记: " + (post.getTitle() != null ? post.getTitle() : "无标题"))
.userId(post.getUserId())
.username("用户")
.activityTime(post.getCreateTime())
.extraData(extraData)
.build());
}
// 按时间排序,取最新的10条
activities.sort((a, b) -> b.getActivityTime().compareTo(a.getActivityTime()));
if (activities.size() > 10) {
activities = activities.subList(0, 10);
}
} catch (Exception e) {
log.error("获取最近活动失败", e);