1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
| @Component @Tool @Slf4j public class ExceptionAnalysisTool {
@Autowired private ExceptionLogService exceptionLogService;
@Autowired private AiAnalysisService aiAnalysisService;
@ToolMethod(description = "分析指定的异常日志,提供解决方案建议") public String analyzeException( @ToolParameter(description = "异常日志ID") String exceptionId, @ToolParameter(description = "是否需要详细分析", required = false) Boolean detailed) {
try { Optional<ExceptionLog> exceptionLogOpt = exceptionLogService.findById(exceptionId); if (!exceptionLogOpt.isPresent()) { return "未找到指定的异常日志"; }
ExceptionLog exceptionLog = exceptionLogOpt.get();
StringBuilder analysis = new StringBuilder(); analysis.append("🔍 异常分析报告\n"); analysis.append("================\n\n"); analysis.append(String.format("异常类型: %s\n", exceptionLog.getExceptionType())); analysis.append(String.format("异常消息: %s\n", exceptionLog.getExceptionMessage())); analysis.append(String.format("发生时间: %s\n", exceptionLog.getExceptionTime())); analysis.append(String.format("严重程度: %s\n", exceptionLog.getSeverity())); analysis.append(String.format("异常分类: %s\n", exceptionLog.getCategory())); analysis.append(String.format("服务名称: %s\n", exceptionLog.getServiceName())); analysis.append(String.format("请求URI: %s %s\n", exceptionLog.getHttpMethod(), exceptionLog.getRequestUri()));
if (StringUtils.hasText(exceptionLog.getUserId())) { analysis.append(String.format("用户ID: %s\n", exceptionLog.getUserId())); }
analysis.append("\n📋 堆栈跟踪:\n"); analysis.append(exceptionLog.getStackTrace());
if (Boolean.TRUE.equals(detailed)) { analysis.append("\n🤖 AI分析结果:\n"); try { String aiAnalysis = aiAnalysisService.analyzeException(exceptionLog); analysis.append(aiAnalysis);
exceptionLog.setAiAnalysis(aiAnalysis); exceptionLogService.save(exceptionLog);
} catch (Exception e) { analysis.append("AI分析失败: ").append(e.getMessage()); } }
return analysis.toString();
} catch (Exception e) { log.error("分析异常日志失败", e); return "分析异常日志时发生错误: " + e.getMessage(); } }
@ToolMethod(description = "查询指定时间范围内的异常统计信息") public String getExceptionStatistics( @ToolParameter(description = "开始时间,格式:yyyy-MM-dd HH:mm:ss") String startTimeStr, @ToolParameter(description = "结束时间,格式:yyyy-MM-dd HH:mm:ss") String endTimeStr) {
try { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime startTime = LocalDateTime.parse(startTimeStr, formatter); LocalDateTime endTime = LocalDateTime.parse(endTimeStr, formatter);
ExceptionStatistics statistics = exceptionLogService.getStatistics(startTime, endTime);
StringBuilder result = new StringBuilder(); result.append("📊 异常统计报告\n"); result.append("================\n\n"); result.append(String.format("统计时间: %s 至 %s\n", startTimeStr, endTimeStr)); result.append(String.format("总异常数: %d\n", statistics.getTotalCount()));
if (statistics.getExceptionTypes() != null) { result.append(String.format("异常类型数: %d\n", statistics.getExceptionTypes().size())); result.append("主要异常类型:\n"); statistics.getExceptionTypes().stream() .limit(10) .forEach(type -> result.append(String.format(" - %s\n", type))); }
if (statistics.getCategories() != null) { result.append("\n异常分类:\n"); statistics.getCategories().forEach(category -> result.append(String.format(" - %s\n", category))); }
if (statistics.getSeverities() != null) { result.append("\n严重程度分布:\n"); statistics.getSeverities().forEach(severity -> result.append(String.format(" - %s\n", severity))); }
return result.toString();
} catch (Exception e) { log.error("查询异常统计失败", e); return "查询异常统计时发生错误: " + e.getMessage(); } }
@ToolMethod(description = "搜索与指定异常相似的历史异常") public String findSimilarExceptions( @ToolParameter(description = "异常类型") String exceptionType, @ToolParameter(description = "异常消息") String exceptionMessage, @ToolParameter(description = "最大返回数量", required = false) Integer limit) {
try { if (limit == null) { limit = 10; }
ExceptionLogCriteria criteria = ExceptionLogCriteria.builder() .exceptionType(exceptionType) .startTime(LocalDateTime.now().minusDays(30)) .endTime(LocalDateTime.now()) .build();
List<ExceptionLog> similarExceptions = exceptionLogService.findByCriteria(criteria);
StringBuilder result = new StringBuilder(); result.append("🔍 相似异常搜索结果\n"); result.append("===================\n\n"); result.append(String.format("搜索条件: %s\n", exceptionType)); result.append(String.format("找到异常数: %d\n\n", similarExceptions.size()));
similarExceptions.stream() .limit(limit) .forEach(ex -> { result.append(String.format("ID: %s\n", ex.getId())); result.append(String.format("时间: %s\n", ex.getExceptionTime())); result.append(String.format("消息: %s\n", ex.getExceptionMessage())); result.append(String.format("状态: %s\n", ex.getStatus())); result.append(String.format("用户: %s\n", ex.getUserId() != null ? ex.getUserId() : "未知")); result.append("---\n"); });
return result.toString();
} catch (Exception e) { log.error("搜索相似异常失败", e); return "搜索相似异常时发生错误: " + e.getMessage(); } }
@ToolMethod(description = "分析异常发生趋势和模式") public String getExceptionTrends( @ToolParameter(description = "分析天数", required = false) Integer days) {
try { if (days == null) { days = 7; }
LocalDateTime endTime = LocalDateTime.now(); LocalDateTime startTime = endTime.minusDays(days);
List<ExceptionTrend> trends = getExceptionTrendsByDay(startTime, endTime);
StringBuilder result = new StringBuilder(); result.append("📈 异常趋势分析\n"); result.append("===============\n\n"); result.append(String.format("分析周期: 最近%d天\n", days)); result.append(String.format("数据点数: %d\n\n", trends.size()));
result.append("日期\t\t异常数量\t主要类型\n"); result.append("----------------------------------------\n");
for (ExceptionTrend trend : trends) { result.append(String.format("%s\t%d\t\t%s\n", trend.getDate().toLocalDate(), trend.getCount(), trend.getTopExceptionType() != null ? trend.getTopExceptionType() : "无")); }
return result.toString();
} catch (Exception e) { log.error("获取异常趋势失败", e); return "获取异常趋势时发生错误: " + e.getMessage(); } }
private List<ExceptionTrend> getExceptionTrendsByDay(LocalDateTime startTime, LocalDateTime endTime) { return new ArrayList<>(); } }
|