| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- <?php
- namespace App\Module\ThirdParty\Events;
- use App\Module\ThirdParty\Models\ThirdPartyService;
- use App\Module\ThirdParty\Models\ThirdPartyMonitor;
- use Illuminate\Foundation\Events\Dispatchable;
- use Illuminate\Queue\SerializesModels;
- /**
- * 监控告警事件
- *
- * 当第三方服务监控检测到异常时触发此事件
- */
- class MonitorAlertEvent
- {
- use Dispatchable, SerializesModels;
- /**
- * 第三方服务实例
- *
- * @var ThirdPartyService
- */
- public ThirdPartyService $service;
- /**
- * 监控记录实例
- *
- * @var ThirdPartyMonitor
- */
- public ThirdPartyMonitor $monitor;
- /**
- * 告警类型
- *
- * @var string
- */
- public string $alertType;
- /**
- * 告警级别
- *
- * @var string
- */
- public string $alertLevel;
- /**
- * 告警消息
- *
- * @var string
- */
- public string $alertMessage;
- /**
- * 监控数据
- *
- * @var array
- */
- public array $monitorData;
- /**
- * 告警时间
- *
- * @var \Carbon\Carbon
- */
- public \Carbon\Carbon $alertTime;
- /**
- * 额外数据
- *
- * @var array
- */
- public array $metadata;
- // 告警类型常量
- public const ALERT_TYPE_HEALTH = 'health'; // 健康检查告警
- public const ALERT_TYPE_PERFORMANCE = 'performance'; // 性能告警
- public const ALERT_TYPE_AVAILABILITY = 'availability'; // 可用性告警
- public const ALERT_TYPE_ERROR_RATE = 'error_rate'; // 错误率告警
- public const ALERT_TYPE_RESPONSE_TIME = 'response_time'; // 响应时间告警
- // 告警级别常量
- public const LEVEL_INFO = 'INFO';
- public const LEVEL_WARNING = 'WARNING';
- public const LEVEL_CRITICAL = 'CRITICAL';
- /**
- * 创建事件实例
- *
- * @param ThirdPartyService $service
- * @param ThirdPartyMonitor $monitor
- * @param string $alertType
- * @param string $alertLevel
- * @param string $alertMessage
- * @param array $monitorData
- * @param array $metadata
- */
- public function __construct(
- ThirdPartyService $service,
- ThirdPartyMonitor $monitor,
- string $alertType,
- string $alertLevel,
- string $alertMessage,
- array $monitorData = [],
- array $metadata = []
- ) {
- $this->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,
- ]);
- }
- }
|