#feature: update AI log analysis
This commit is contained in:
@@ -2,18 +2,18 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Clients\AgentClient;
|
||||
use App\Clients\MonoClient;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Collection;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class MessageSyncService
|
||||
{
|
||||
private AgentClient $agentClient;
|
||||
private MonoClient $monoClient;
|
||||
|
||||
public function __construct(AgentClient $agentClient)
|
||||
public function __construct(MonoClient $monoClient)
|
||||
{
|
||||
$this->agentClient = $agentClient;
|
||||
$this->monoClient = $monoClient;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,80 +57,51 @@ class MessageSyncService
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量同步消息到agent
|
||||
* 批量同步消息(通过mono消费)
|
||||
*/
|
||||
public function syncMessages(array $messageIds): array
|
||||
{
|
||||
$messages = $this->getMessagesByIds($messageIds);
|
||||
$results = [];
|
||||
|
||||
foreach ($messages as $message) {
|
||||
$result = $this->syncSingleMessage($message);
|
||||
$results[] = [
|
||||
'msg_id' => $message['msg_id'],
|
||||
'success' => $result['success'],
|
||||
'response' => $result['response'] ?? null,
|
||||
'error' => $result['error'] ?? null,
|
||||
'request_data' => $result['request_data'] ?? null,
|
||||
];
|
||||
foreach ($messageIds as $msgId) {
|
||||
$results[] = $this->syncSingleMessage($msgId);
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步单个消息到agent
|
||||
* 通过mono消费单个消息
|
||||
*/
|
||||
private function syncSingleMessage(array $message): array
|
||||
private function syncSingleMessage(string $msgId): array
|
||||
{
|
||||
try {
|
||||
$requestData = $this->buildAgentRequest($message);
|
||||
$response = $this->monoClient->consumeMessage($msgId);
|
||||
$body = $response->json();
|
||||
|
||||
$response = $this->agentClient->dispatchMessage($requestData);
|
||||
|
||||
if ($response->successful()) {
|
||||
if ($response->successful() && ($body['code'] ?? -1) === 0) {
|
||||
return [
|
||||
'msg_id' => $msgId,
|
||||
'success' => true,
|
||||
'response' => $response->json(),
|
||||
'request_data' => $requestData,
|
||||
'response' => $body,
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
'msg_id' => $msgId,
|
||||
'success' => false,
|
||||
'error' => 'HTTP ' . $response->status() . ': ' . $response->body(),
|
||||
'request_data' => $requestData,
|
||||
'error' => $body['message'] ?? ('HTTP ' . $response->status() . ': ' . $response->body()),
|
||||
'response' => $body,
|
||||
];
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return [
|
||||
'msg_id' => $msgId,
|
||||
'success' => false,
|
||||
'error' => '请求失败: ' . $e->getMessage(),
|
||||
'request_data' => $requestData ?? null,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建agent接口请求数据
|
||||
*/
|
||||
private function buildAgentRequest(array $message): array
|
||||
{
|
||||
$parsedParam = $message['parsed_param'];
|
||||
$parsedProperty = $message['parsed_property'];
|
||||
|
||||
return [
|
||||
'topic_name' => $message['event_type'],
|
||||
'msg_body' => [
|
||||
'id' => $message['msg_id'],
|
||||
'data' => $parsedParam,
|
||||
'timestamp' => $message['timestamp'],
|
||||
'property' => $parsedProperty,
|
||||
],
|
||||
'target_service' => [1], // 默认目标服务
|
||||
'trace_id' => $message['trace_id'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析JSON字段
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user