#feature: AI analysis update

This commit is contained in:
2026-01-15 15:39:49 +08:00
parent ae6c169f5f
commit bbe68839e3
7 changed files with 343 additions and 62 deletions

View File

@@ -13,8 +13,7 @@ class LogAnalysisService
public function __construct(
private readonly SlsService $slsService,
private readonly AiService $aiService,
private readonly CodeContextService $codeContextService,
private readonly ConfigService $configService,
private readonly CodeAnalysisService $codeAnalysisService,
private readonly DingTalkService $dingTalkService
) {}
@@ -97,39 +96,34 @@ class LogAnalysisService
// 3. 分析每个分组
$results = [];
$settings = $this->configService->get('log_analysis.settings', []);
$maxLogsPerApp = $settings['max_logs_per_app'] ?? 500;
foreach ($grouped as $appName => $appLogs) {
$appLogsCollection = collect($appLogs);
// 限制每个 app 的日志数量
if ($appLogsCollection->count() > $maxLogsPerApp) {
$appLogsCollection = $appLogsCollection->take($maxLogsPerApp);
}
// 获取代码上下文(如果需要)
$codeContext = null;
if ($mode === AnalysisMode::LogsWithCode) {
$repoPath = $this->codeContextService->getRepoPath($appName);
if ($repoPath) {
$codeContext = $this->codeContextService->extractRelevantCode($repoPath, $appLogsCollection);
}
}
// 准备日志内容
$logsContent = $this->formatLogsForAnalysis($appLogsCollection);
// AI 分析
try {
$results[$appName] = $this->aiService->analyzeLogs($logsContent, $codeContext);
$results[$appName] = $this->aiService->analyzeLogs($logsContent, null);
$results[$appName]['log_count'] = $appLogsCollection->count();
$results[$appName]['has_code_context'] = $codeContext !== null;
// 如果是 logs+code 模式,且 impact 为 high/medium触发代码分析
if ($mode === AnalysisMode::LogsWithCode) {
$impact = $results[$appName]['impact'] ?? 'unknown';
if (in_array($impact, ['high', 'medium'])) {
$codeAnalysisResult = $this->codeAnalysisService->analyze(
$appName,
$logsContent,
$results[$appName]['summary'] ?? null
);
$results[$appName]['code_analysis'] = $codeAnalysisResult;
}
}
} catch (\Exception $e) {
$results[$appName] = [
'error' => $e->getMessage(),
'log_count' => $appLogsCollection->count(),
'has_code_context' => false,
];
}
}
@@ -180,11 +174,14 @@ class LogAnalysisService
$grouped = $this->slsService->groupByAppName($logs);
$statistics = $this->slsService->getStatistics($logs);
// 使用传入的 limit 参数,如果未指定则默认显示全部
$displayLimit = $limit ?? $logs->count();
return [
'total' => $logs->count(),
'statistics' => $statistics,
'grouped' => array_map(fn($g) => count($g), $grouped),
'logs' => $logs->take(100)->values()->toArray(),
'logs' => $logs->take($displayLimit)->values()->toArray(),
];
}
@@ -228,11 +225,14 @@ class LogAnalysisService
}
}
// 使用传入的 limit 参数,如果未指定则默认显示全部
$displayLimit = $limit ?? $allLogs->count();
return [
'total' => $statistics['total'],
'statistics' => $statistics,
'grouped_by_logstore' => $groupedByLogstore,
'logs' => $allLogs->take(100)->values()->toArray(),
'logs' => $allLogs->take($displayLimit)->values()->toArray(),
];
}