Commit 981748f6 by zhouhaibao

dify超时异常返回处理

1 parent d6cc2f39
...@@ -8,6 +8,7 @@ import cn.com.glsx.dj.peanalysis.remote.BusinessSceneAnalysisRemote; ...@@ -8,6 +8,7 @@ import cn.com.glsx.dj.peanalysis.remote.BusinessSceneAnalysisRemote;
import cn.com.glsx.dj.supervisor.dto.*; import cn.com.glsx.dj.supervisor.dto.*;
import cn.com.glsx.dj.supervisor.remote.SupervisorDashboardRemote; import cn.com.glsx.dj.supervisor.remote.SupervisorDashboardRemote;
import cn.com.glsx.dj.supervisor.request.*; import cn.com.glsx.dj.supervisor.request.*;
import cn.com.glsx.dj.supervisor.service.AiAnalysisService;
import cn.com.glsx.dj.supervisor.util.CommonUtil; import cn.com.glsx.dj.supervisor.util.CommonUtil;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse; import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.exception.RpcServiceException; import cn.com.glsx.framework.core.exception.RpcServiceException;
...@@ -55,6 +56,9 @@ public class SupervisorDashboardController { ...@@ -55,6 +56,9 @@ public class SupervisorDashboardController {
@Resource @Resource
private BusinessSceneAnalysisRemote businessSceneAnalysisRemote; private BusinessSceneAnalysisRemote businessSceneAnalysisRemote;
@Autowired
private AiAnalysisService aiAnalysisService;
/** /**
* 获取督导首页数据 * 获取督导首页数据
* <p> * <p>
...@@ -108,11 +112,20 @@ public class SupervisorDashboardController { ...@@ -108,11 +112,20 @@ public class SupervisorDashboardController {
req.setEndDate(request.getEndDate()); req.setEndDate(request.getEndDate());
req.setSortField(request.getSortField()); req.setSortField(request.getSortField());
req.setSortDirection(request.getSortDirection()); req.setSortDirection(request.getSortDirection());
req.setNeedAiAnalysis(true); req.setNeedAiAnalysis(false);
try { try {
RpcResponse<SupervisorDashboardVO> result = supervisorDashboardRemote.getDashboardData(req); RpcResponse<SupervisorDashboardVO> result = supervisorDashboardRemote.getDashboardData(req);
if (result.isSuccess()) { if (result.isSuccess()) {
return RopResponse.success(result.getResult()); SupervisorDashboardVO supervisorDashboardVO = result.getResult();
List<EmployeeRanking> storeAndEmployeePhoneList = supervisorDashboardVO.getEmployeeList();
// 4. 触发 AI 分析
if (!storeAndEmployeePhoneList.isEmpty()) {
// 此处为同步演示,使用开始时间-结束日期作为周期参数
String period = req.getStartDate() + "-" + req.getEndDate();
String aiSuggestion = aiAnalysisService.generateSuggestion(req.getSupervisorId(), period, storeAndEmployeePhoneList, req.getBizs());
supervisorDashboardVO.setAiAnalysisSuggestion(aiSuggestion);
}
return RopResponse.success(supervisorDashboardVO);
} else { } else {
return RopResponse.error(result.getError(), result.getMessage()); return RopResponse.error(result.getError(), result.getMessage());
} }
...@@ -176,12 +189,21 @@ public class SupervisorDashboardController { ...@@ -176,12 +189,21 @@ public class SupervisorDashboardController {
req.setEndDate(request.getEndDate()); req.setEndDate(request.getEndDate());
req.setSortField(request.getSortField()); req.setSortField(request.getSortField());
req.setSortDirection(request.getSortDirection()); req.setSortDirection(request.getSortDirection());
req.setNeedAiAnalysis(true); req.setNeedAiAnalysis(false);
try{ try{
RpcResponse<SupervisorDashboardVO> result = supervisorDashboardRemote.getDashboardBehaviorData( RpcResponse<SupervisorDashboardVO> result = supervisorDashboardRemote.getDashboardBehaviorData(
req); req);
if (result.isSuccess()) { if (result.isSuccess()) {
return RopResponse.success(result.getResult()); SupervisorDashboardVO supervisorDashboardVO = result.getResult();
List<EmployeeRanking> storeAndEmployeePhoneList = supervisorDashboardVO.getEmployeeList();
// 4. 触发 AI 分析
if (!storeAndEmployeePhoneList.isEmpty()) {
// 此处为同步演示,使用开始时间-结束日期作为周期参数
String period = req.getStartDate() + "-" + req.getEndDate();
String aiSuggestion = aiAnalysisService.generateBehaviorSuggestion(req.getSupervisorId(), period, storeAndEmployeePhoneList, req.getBizs());
supervisorDashboardVO.setAiAnalysisSuggestion(aiSuggestion);
}
return RopResponse.success(supervisorDashboardVO);
} else { } else {
return RopResponse.error(result.getError(), result.getMessage()); return RopResponse.error(result.getError(), result.getMessage());
} }
......
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议(仅限 Top 3)

    1. [安全性] 敏感信息(API Key)硬编码在源码中

    • 问题描述:在 AiAnalysisService 类中,PERFORMANCE_API_KEY 和 BEHAVIOR_API_KEY 被直接硬编码在代码中。这属于高危安全漏洞,一旦代码库泄露,极易导致外部 AI 服务的 Token 被盗用,造成严重的财产损失或数据泄露。
    • 优化建议:将 API Key 和 URL 提取到配置文件(如 application.yml)或分布式配置中心(如 Nacos、Apollo)中。使用 @Value 或 @ConfigurationProperties 进行注入。 java @Value("${ai.dify.performance-api-key}") private String performanceApiKey;

    2. [健壮性与性能] Controller 中同步调用长耗时外部 API

    • 问题描述:在 SupervisorDashboardController 中,获取到列表数据后,直接在当前线程同步调用 aiAnalysisService.generateSuggestion()。根据代码中的 OkHttp 配置,该外部调用最长可能耗时 35 秒。这会导致 Tomcat 的工作线程被长时间阻塞,在高并发场景下极易耗尽线程池,引发服务雪崩(假死);同时也会导致前端页面长时间白屏,用户体验极差。
    • 优化建议:
      • 方案 A(推荐):接口解耦。当前接口仅返回基础统计数据,前端通过另一个独立的接口异步请求 AI 分析建议,实现延迟加载(骨架屏/Loading 状态)。
      • 方案 B:如果必须在同一个接口返回,且对实时性要求不高,可使用 CompletableFuture 设置较短的超时时间(如 3 秒),超时则返回默认提示语,不阻塞主流程。

    3. [可维护性] 异常处理极不规范,违背阿里巴巴 Java 开发手册

    • 问题描述:在 AiAnalysisService 处理 Redis 缓存异常时,多次使用了 e.printStackTrace() 来吞掉异常。根据《阿里巴巴 Java 开发手册》异常处理规约,绝对不允许使用 e.printStackTrace(),因为它会将异常输出到标准错误流,不仅难以被日志收集系统(如 ELK)采集,还可能导致控制台缓冲区满了之后阻塞应用。
    • 优化建议:必须使用日志框架(如 SLF4J)记录异常信息,保留堆栈轨迹。 java catch (Exception e) { // 记录日志,而不是 e.printStackTrace() log.error("读取/写入 Redis 缓存失败, cacheKey: {}", cacheKey, e); } (附带提醒:代码中 requestBody.put("user", "abd-123"); 存在硬编码测试数据的嫌疑,应替换为真实的当前用户 ID)

    二、评分明细

    评审维度 满分 得分 扣分原因说明
    1. 功能正确性与健壮性 40分 30分 核心业务逻辑实现正常,但同步阻塞调用外部长耗时 API 存在严重的健壮性隐患(-10分)。
    2. 安全性与潜在风险 30分 15分 生产级别的 API Key 直接硬编码在源码中,存在极高的安全泄露风险(-15分)。
    3. 最佳实践与可维护性 20分 14分 多处使用 e.printStackTrace() 违背阿里规范(-4分);存在测试用的硬编码 "abd-123"(-2分)。
    4. 性能与资源利用 5分 3分 引入了 Redis 缓存设计较好,但 Controller 层的同步长耗时调用严重影响了系统吞吐量(-2分)。
    5. 提交信息质量 5分 4分 提交信息 "dify超时异常返回处理" 仅描述了部分修改,未涵盖新增的整个 AI 业务逻辑分析(-1分)。

    总分:66分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!