Junjie
2 天以前 25971ce968a1a25273e90838aa18fa77a0e8b622
src/main/java/com/zy/system/controller/DashboardController.java
@@ -5,10 +5,12 @@
import com.core.common.R;
import com.zy.ai.entity.AiAutoTuneJob;
import com.zy.ai.entity.AiChatSession;
import com.zy.ai.entity.AiTokenUsage;
import com.zy.ai.entity.LlmCallLog;
import com.zy.ai.entity.LlmRouteConfig;
import com.zy.ai.enums.AiPromptScene;
import com.zy.ai.mapper.AiChatSessionMapper;
import com.zy.ai.mapper.AiTokenUsageMapper;
import com.zy.ai.service.AiAutoTuneJobService;
import com.zy.ai.service.LlmCallLogService;
import com.zy.ai.service.LlmRouteConfigService;
@@ -65,6 +67,8 @@
    private AiAutoTuneJobService aiAutoTuneJobService;
    @Autowired
    private AiChatSessionMapper aiChatSessionMapper;
    @Autowired
    private AiTokenUsageMapper aiTokenUsageMapper;
    @Autowired
    private DevicePingFileStorageService devicePingFileStorageService;
@@ -327,49 +331,37 @@
    private Map<String, Object> buildAiStats() {
        Map<String, Object> result = new LinkedHashMap<>();
        // 从独立累计表读取 token 统计
        long tokenTotal = 0L;
        long promptTokenTotal = 0L;
        long completionTokenTotal = 0L;
        long llmCallCountTotal = 0L;
        try {
            AiTokenUsage tokenUsage = aiTokenUsageMapper.selectById(1);
            if (tokenUsage != null) {
                promptTokenTotal = safeCount(tokenUsage.getPromptTokens());
                completionTokenTotal = safeCount(tokenUsage.getCompletionTokens());
                tokenTotal = safeCount(tokenUsage.getTotalTokens());
                llmCallCountTotal = safeCount(tokenUsage.getLlmCallCount());
            }
        } catch (Exception e) {
            log.warn("dashboard ai token usage load failed: {}", safeMessage(e));
        }
        // 会话统计(保留用于显示会话数和提问轮次)
        long askCount = 0L;
        long sessionCount = 0L;
        long autoTunePromptTokenTotal = 0L;
        long autoTuneCompletionTokenTotal = 0L;
        long autoTuneTokenTotal = 0L;
        try {
            List<AiChatSession> sessions = aiChatSessionMapper.selectList(new QueryWrapper<AiChatSession>()
                    .select("id", "sum_prompt_tokens", "sum_completion_tokens", "sum_total_tokens", "ask_count"));
                    .select("id", "ask_count"));
            sessionCount = sessions == null ? 0L : sessions.size();
            if (sessions != null) {
                for (AiChatSession session : sessions) {
                    promptTokenTotal += safeCount(session == null ? null : session.getSumPromptTokens());
                    completionTokenTotal += safeCount(session == null ? null : session.getSumCompletionTokens());
                    tokenTotal += safeCount(session == null ? null : session.getSumTotalTokens());
                    askCount += safeCount(session == null ? null : session.getAskCount());
                }
            }
        } catch (Exception e) {
            log.warn("dashboard ai session stats load failed: {}", safeMessage(e));
        }
        try {
            List<Map<String, Object>> autoTuneRows = aiAutoTuneJobService.listMaps(new QueryWrapper<AiAutoTuneJob>()
                    .select("COALESCE(SUM(prompt_tokens), 0) AS prompt_token_total",
                            "COALESCE(SUM(completion_tokens), 0) AS completion_token_total",
                            "COALESCE(SUM(total_tokens), 0) AS token_total")
                    .eq("prompt_scene_code", AiPromptScene.AUTO_TUNE_DISPATCH.getCode()));
            Map<String, Object> autoTuneRow = autoTuneRows == null || autoTuneRows.isEmpty()
                    ? Collections.emptyMap()
                    : autoTuneRows.get(0);
            autoTunePromptTokenTotal = toLong(autoTuneRow.get("prompt_token_total"));
            autoTuneCompletionTokenTotal = toLong(autoTuneRow.get("completion_token_total"));
            autoTuneTokenTotal = toLong(autoTuneRow.get("token_total"));
            // Agent 自动调参不生成 sys_ai_chat_session,会单独落到 sys_ai_auto_tune_job。
            promptTokenTotal += autoTunePromptTokenTotal;
            completionTokenTotal += autoTuneCompletionTokenTotal;
            tokenTotal += autoTuneTokenTotal;
        } catch (Exception e) {
            log.warn("dashboard ai auto tune token stats load failed: {}", safeMessage(e));
        }
        List<LlmRouteConfig> routes = Collections.emptyList();
@@ -444,9 +436,7 @@
        overview.put("tokenTotal", tokenTotal);
        overview.put("promptTokenTotal", promptTokenTotal);
        overview.put("completionTokenTotal", completionTokenTotal);
        overview.put("autoTuneTokenTotal", autoTuneTokenTotal);
        overview.put("autoTunePromptTokenTotal", autoTunePromptTokenTotal);
        overview.put("autoTuneCompletionTokenTotal", autoTuneCompletionTokenTotal);
        overview.put("llmCallCountTotal", llmCallCountTotal);
        overview.put("askCount", askCount);
        overview.put("sessionCount", sessionCount);
        overview.put("routeTotal", routeTotal);