'integer', 'response_time' => 'integer', 'status_code' => 'integer', 'details' => 'array', 'checked_at' => 'datetime', ]; /** * 检查类型常量 */ const CHECK_TYPE_HEALTH = 'health'; const CHECK_TYPE_PERFORMANCE = 'performance'; const CHECK_TYPE_AVAILABILITY = 'availability'; /** * 检查状态常量 */ const STATUS_SUCCESS = 'success'; const STATUS_WARNING = 'warning'; const STATUS_ERROR = 'error'; const STATUS_TIMEOUT = 'timeout'; const STATUS_UNKNOWN = 'unknown'; /** * 获取检查类型标签 * * @return string */ public function getCheckTypeLabel(): string { return match ($this->check_type) { self::CHECK_TYPE_HEALTH => '健康检查', self::CHECK_TYPE_PERFORMANCE => '性能检查', self::CHECK_TYPE_AVAILABILITY => '可用性检查', default => '未知类型', }; } /** * 获取状态标签 * * @return string */ public function getStatusLabel(): string { return match ($this->status) { self::STATUS_SUCCESS => '成功', self::STATUS_WARNING => '警告', self::STATUS_ERROR => '错误', self::STATUS_TIMEOUT => '超时', self::STATUS_UNKNOWN => '未知', default => '未知状态', }; } /** * 获取状态颜色 * * @return string */ public function getStatusColor(): string { return match ($this->status) { self::STATUS_SUCCESS => 'success', self::STATUS_WARNING => 'warning', self::STATUS_ERROR => 'danger', self::STATUS_TIMEOUT => 'danger', self::STATUS_UNKNOWN => 'secondary', default => 'secondary', }; } /** * 获取状态图标 * * @return string */ public function getStatusIcon(): string { return match ($this->status) { self::STATUS_SUCCESS => 'fa-check-circle', self::STATUS_WARNING => 'fa-exclamation-triangle', self::STATUS_ERROR => 'fa-times-circle', self::STATUS_TIMEOUT => 'fa-clock', self::STATUS_UNKNOWN => 'fa-question-circle', default => 'fa-question-circle', }; } /** * 检查是否成功 * * @return bool */ public function isSuccessful(): bool { return $this->status === self::STATUS_SUCCESS; } /** * 检查是否有问题 * * @return bool */ public function hasIssue(): bool { return in_array($this->status, [self::STATUS_WARNING, self::STATUS_ERROR, self::STATUS_TIMEOUT]); } /** * 检查是否为严重问题 * * @return bool */ public function isCritical(): bool { return in_array($this->status, [self::STATUS_ERROR, self::STATUS_TIMEOUT]); } /** * 获取响应时间(秒) * * @return float */ public function getResponseTimeInSeconds(): float { return $this->response_time ? $this->response_time / 1000 : 0; } /** * 获取格式化的响应时间 * * @return string */ public function getFormattedResponseTime(): string { if (!$this->response_time) { return '-'; } if ($this->response_time < 1000) { return $this->response_time . 'ms'; } return number_format($this->getResponseTimeInSeconds(), 2) . 's'; } /** * 获取性能评级 * * @return string */ public function getPerformanceGrade(): string { if (!$this->response_time) { return 'N/A'; } if ($this->response_time <= 200) { return 'A'; } elseif ($this->response_time <= 500) { return 'B'; } elseif ($this->response_time <= 1000) { return 'C'; } elseif ($this->response_time <= 2000) { return 'D'; } else { return 'F'; } } /** * 获取性能评级颜色 * * @return string */ public function getPerformanceGradeColor(): string { return match ($this->getPerformanceGrade()) { 'A' => 'success', 'B' => 'info', 'C' => 'warning', 'D' => 'danger', 'F' => 'dark', default => 'secondary', }; } /** * 获取详细信息中的特定值 * * @param string $key * @param mixed $default * @return mixed */ public function getDetail(string $key, $default = null) { return $this->details[$key] ?? $default; } /** * 获取监控摘要 * * @return array */ public function getSummary(): array { return [ 'check_type' => $this->getCheckTypeLabel(), 'status' => $this->getStatusLabel(), 'response_time' => $this->getFormattedResponseTime(), 'performance_grade' => $this->getPerformanceGrade(), 'checked_at' => $this->checked_at->format('Y-m-d H:i:s'), ]; } /** * 关联第三方服务 * * @return BelongsTo */ public function service(): BelongsTo { return $this->belongsTo(ThirdPartyService::class, 'service_id'); } /** * 创建监控记录 * * @param array $data * @return static */ public static function createMonitor(array $data): static { // 自动设置检查时间 if (!isset($data['checked_at'])) { $data['checked_at'] = now(); } // 根据响应时间和状态码自动判断状态 if (!isset($data['status'])) { if (isset($data['status_code'])) { if ($data['status_code'] >= 200 && $data['status_code'] < 300) { $data['status'] = self::STATUS_SUCCESS; } elseif ($data['status_code'] >= 300 && $data['status_code'] < 400) { $data['status'] = self::STATUS_WARNING; } else { $data['status'] = self::STATUS_ERROR; } } elseif (isset($data['response_time'])) { if ($data['response_time'] > 10000) { // 超过10秒认为超时 $data['status'] = self::STATUS_TIMEOUT; } else { $data['status'] = self::STATUS_SUCCESS; } } else { $data['status'] = self::STATUS_UNKNOWN; } } return static::create($data); } /** * 按服务查询 * * @param \Illuminate\Database\Eloquent\Builder $query * @param int $serviceId * @return \Illuminate\Database\Eloquent\Builder */ public function scopeByService($query, int $serviceId) { return $query->where('service_id', $serviceId); } /** * 按检查类型查询 * * @param \Illuminate\Database\Eloquent\Builder $query * @param string $checkType * @return \Illuminate\Database\Eloquent\Builder */ public function scopeByCheckType($query, string $checkType) { return $query->where('check_type', $checkType); } /** * 按状态查询 * * @param \Illuminate\Database\Eloquent\Builder $query * @param string $status * @return \Illuminate\Database\Eloquent\Builder */ public function scopeByStatus($query, string $status) { return $query->where('status', $status); } /** * 查询成功的记录 * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ public function scopeSuccessful($query) { return $query->where('status', self::STATUS_SUCCESS); } /** * 查询有问题的记录 * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ public function scopeWithIssues($query) { return $query->whereIn('status', [self::STATUS_WARNING, self::STATUS_ERROR, self::STATUS_TIMEOUT]); } /** * 查询严重问题的记录 * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ public function scopeCritical($query) { return $query->whereIn('status', [self::STATUS_ERROR, self::STATUS_TIMEOUT]); } /** * 按时间范围查询 * * @param \Illuminate\Database\Eloquent\Builder $query * @param string $startDate * @param string $endDate * @return \Illuminate\Database\Eloquent\Builder */ public function scopeDateRange($query, string $startDate, string $endDate) { return $query->whereBetween('checked_at', [$startDate, $endDate]); } /** * 获取所有检查类型 * * @return array */ public static function getCheckTypes(): array { return [ self::CHECK_TYPE_HEALTH => '健康检查', self::CHECK_TYPE_PERFORMANCE => '性能检查', self::CHECK_TYPE_AVAILABILITY => '可用性检查', ]; } /** * 获取所有状态 * * @return array */ public static function getStatuses(): array { return [ self::STATUS_SUCCESS => '成功', self::STATUS_WARNING => '警告', self::STATUS_ERROR => '错误', self::STATUS_TIMEOUT => '超时', self::STATUS_UNKNOWN => '未知', ]; } }