service = $service; $this->monitor = $monitor; $this->alertType = $alertType; $this->alertLevel = $alertLevel; $this->alertMessage = $alertMessage; $this->monitorData = $monitorData; $this->alertTime = now(); $this->metadata = $metadata; } /** * 获取事件数据数组 * * @return array */ public function toArray(): array { return [ 'service_id' => $this->service->id, 'service_code' => $this->service->code, 'service_name' => $this->service->name, 'monitor_id' => $this->monitor->id, 'alert_type' => $this->alertType, 'alert_level' => $this->alertLevel, 'alert_message' => $this->alertMessage, 'monitor_data' => $this->monitorData, 'alert_time' => $this->alertTime->toISOString(), 'metadata' => $this->metadata, ]; } /** * 判断是否为严重告警 * * @return bool */ public function isCritical(): bool { return $this->alertLevel === self::LEVEL_CRITICAL; } /** * 判断是否为警告告警 * * @return bool */ public function isWarning(): bool { return $this->alertLevel === self::LEVEL_WARNING; } /** * 获取告警标题 * * @return string */ public function getAlertTitle(): string { $serviceName = $this->service->name; $typeLabel = $this->getAlertTypeLabel(); return "第三方服务监控告警 - {$serviceName} ({$typeLabel})"; } /** * 获取告警类型标签 * * @return string */ public function getAlertTypeLabel(): string { return match ($this->alertType) { self::ALERT_TYPE_HEALTH => '健康检查', self::ALERT_TYPE_PERFORMANCE => '性能监控', self::ALERT_TYPE_AVAILABILITY => '可用性监控', self::ALERT_TYPE_ERROR_RATE => '错误率监控', self::ALERT_TYPE_RESPONSE_TIME => '响应时间监控', default => '未知类型', }; } /** * 获取详细告警信息 * * @return array */ public function getDetailedInfo(): array { $info = [ 'service' => [ 'id' => $this->service->id, 'code' => $this->service->code, 'name' => $this->service->name, 'type' => $this->service->type, 'status' => $this->service->status, ], 'alert' => [ 'type' => $this->alertType, 'type_label' => $this->getAlertTypeLabel(), 'level' => $this->alertLevel, 'message' => $this->alertMessage, 'time' => $this->alertTime->toISOString(), ], 'monitor' => [ 'id' => $this->monitor->id, 'check_type' => $this->monitor->check_type, 'status' => $this->monitor->status, 'response_time' => $this->monitor->response_time, 'checked_at' => $this->monitor->checked_at?->toISOString(), ], 'data' => $this->monitorData, 'metadata' => $this->metadata, ]; return $info; } /** * 获取建议操作 * * @return array */ public function getSuggestedActions(): array { $actions = []; switch ($this->alertType) { case self::ALERT_TYPE_HEALTH: $actions[] = '检查服务健康状态'; $actions[] = '验证服务配置'; $actions[] = '检查网络连接'; if ($this->isCritical()) { $actions[] = '启用备用服务'; $actions[] = '通知相关人员'; } break; case self::ALERT_TYPE_PERFORMANCE: $actions[] = '分析性能瓶颈'; $actions[] = '检查服务负载'; $actions[] = '优化调用频率'; if ($this->isCritical()) { $actions[] = '考虑降级服务'; } break; case self::ALERT_TYPE_AVAILABILITY: $actions[] = '检查服务可用性'; $actions[] = '验证服务状态'; $actions[] = '检查依赖服务'; if ($this->isCritical()) { $actions[] = '切换到备用服务'; } break; case self::ALERT_TYPE_ERROR_RATE: $actions[] = '分析错误日志'; $actions[] = '检查调用参数'; $actions[] = '验证认证信息'; if ($this->isCritical()) { $actions[] = '暂停相关调用'; } break; case self::ALERT_TYPE_RESPONSE_TIME: $actions[] = '检查网络延迟'; $actions[] = '分析服务响应'; $actions[] = '优化请求参数'; if ($this->isCritical()) { $actions[] = '增加超时时间'; $actions[] = '考虑异步处理'; } break; } return $actions; } /** * 判断是否需要立即处理 * * @return bool */ public function requiresImmediateAction(): bool { return $this->isCritical() && in_array($this->alertType, [ self::ALERT_TYPE_HEALTH, self::ALERT_TYPE_AVAILABILITY, ]); } }