#feature: AI analysis update
This commit is contained in:
196
app/Services/CodeAnalysisService.php
Normal file
196
app/Services/CodeAnalysisService.php
Normal file
@@ -0,0 +1,196 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Symfony\Component\Process\Exception\ProcessFailedException;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class CodeAnalysisService
|
||||
{
|
||||
public const TOOL_CLAUDE = 'claude';
|
||||
public const TOOL_CODEX = 'codex';
|
||||
|
||||
private int $timeout = 300;
|
||||
|
||||
public function __construct(
|
||||
private readonly CodeContextService $codeContextService,
|
||||
private readonly ConfigService $configService
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 使用配置的工具在项目中分析日志问题
|
||||
*
|
||||
* @param string $appName 应用名称
|
||||
* @param string $logsContent 日志内容
|
||||
* @param string|null $aiSummary AI 初步分析摘要
|
||||
* @return array 分析结果
|
||||
*/
|
||||
public function analyze(string $appName, string $logsContent, ?string $aiSummary): array
|
||||
{
|
||||
$repoPath = $this->codeContextService->getRepoPath($appName);
|
||||
|
||||
if (!$repoPath) {
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => "未配置项目路径: {$appName}",
|
||||
];
|
||||
}
|
||||
|
||||
$tool = $this->getConfiguredTool();
|
||||
|
||||
try {
|
||||
$prompt = $this->buildPrompt($logsContent, $aiSummary);
|
||||
$output = $this->runTool($tool, $repoPath, $prompt);
|
||||
|
||||
return [
|
||||
'success' => true,
|
||||
'output' => $output,
|
||||
'repo_path' => $repoPath,
|
||||
'tool' => $tool,
|
||||
];
|
||||
} catch (ProcessFailedException $e) {
|
||||
Log::error("{$tool} execution failed", [
|
||||
'app_name' => $appName,
|
||||
'repo_path' => $repoPath,
|
||||
'tool' => $tool,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => "{$tool} 执行失败: " . $e->getProcess()->getErrorOutput(),
|
||||
'tool' => $tool,
|
||||
];
|
||||
} catch (\Exception $e) {
|
||||
Log::error("Code analysis failed", [
|
||||
'app_name' => $appName,
|
||||
'repo_path' => $repoPath,
|
||||
'tool' => $tool,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => $e->getMessage(),
|
||||
'tool' => $tool,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置的分析工具
|
||||
*/
|
||||
public function getConfiguredTool(): string
|
||||
{
|
||||
$tool = $this->configService->get('log_analysis.code_analysis_tool', self::TOOL_CLAUDE);
|
||||
|
||||
if (!in_array($tool, [self::TOOL_CLAUDE, self::TOOL_CODEX])) {
|
||||
return self::TOOL_CLAUDE;
|
||||
}
|
||||
|
||||
return $tool;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置分析工具
|
||||
*/
|
||||
public function setTool(string $tool): void
|
||||
{
|
||||
if (!in_array($tool, [self::TOOL_CLAUDE, self::TOOL_CODEX])) {
|
||||
throw new \InvalidArgumentException("不支持的工具: {$tool}");
|
||||
}
|
||||
|
||||
$this->configService->set(
|
||||
'log_analysis.code_analysis_tool',
|
||||
$tool,
|
||||
'代码分析工具 (claude/codex)'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取可用的工具列表
|
||||
*/
|
||||
public function getAvailableTools(): array
|
||||
{
|
||||
return [
|
||||
self::TOOL_CLAUDE => [
|
||||
'name' => 'Claude CLI',
|
||||
'description' => 'Anthropic Claude 命令行工具',
|
||||
],
|
||||
self::TOOL_CODEX => [
|
||||
'name' => 'Codex CLI',
|
||||
'description' => 'OpenAI Codex 命令行工具',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建提示词
|
||||
*/
|
||||
private function buildPrompt(string $logsContent, ?string $aiSummary): string
|
||||
{
|
||||
$prompt = "分析以下错误日志,在代码库中排查根本原因并给出具体优化方案:\n\n";
|
||||
$prompt .= "=== 日志内容 ===\n{$logsContent}\n\n";
|
||||
|
||||
if ($aiSummary) {
|
||||
$prompt .= "=== AI 初步分析 ===\n{$aiSummary}\n\n";
|
||||
}
|
||||
|
||||
$prompt .= "请:\n";
|
||||
$prompt .= "1. 定位相关代码文件\n";
|
||||
$prompt .= "2. 分析根本原因\n";
|
||||
$prompt .= "3. 给出具体修复方案\n";
|
||||
|
||||
return $prompt;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行分析工具
|
||||
*/
|
||||
private function runTool(string $tool, string $workingDirectory, string $prompt): string
|
||||
{
|
||||
return match ($tool) {
|
||||
self::TOOL_CODEX => $this->runCodex($workingDirectory, $prompt),
|
||||
default => $this->runClaude($workingDirectory, $prompt),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行 Claude CLI 命令
|
||||
*/
|
||||
private function runClaude(string $workingDirectory, string $prompt): string
|
||||
{
|
||||
$process = new Process(
|
||||
['claude', '--print', $prompt],
|
||||
$workingDirectory
|
||||
);
|
||||
$process->setTimeout($this->timeout);
|
||||
$process->mustRun();
|
||||
|
||||
return trim($process->getOutput());
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行 Codex CLI 命令
|
||||
*/
|
||||
private function runCodex(string $workingDirectory, string $prompt): string
|
||||
{
|
||||
$process = new Process(
|
||||
['codex', '--quiet', '--full-auto', $prompt],
|
||||
$workingDirectory
|
||||
);
|
||||
$process->setTimeout($this->timeout);
|
||||
$process->mustRun();
|
||||
|
||||
return trim($process->getOutput());
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置超时时间
|
||||
*/
|
||||
public function setTimeout(int $timeout): void
|
||||
{
|
||||
$this->timeout = $timeout;
|
||||
}
|
||||
}
|
||||
@@ -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(),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user