| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- <?php
- namespace App\Module\ThirdParty\Models;
- use UCore\ModelCore;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- /**
- * 第三方服务监控记录模型
- *
- * @property int $id 主键ID
- * @property int $service_id 服务ID
- * @property string $check_type 检查类型
- * @property string $status 检查状态
- * @property int|null $response_time 响应时间
- * @property int|null $status_code HTTP状态码
- * @property string|null $error_message 错误信息
- * @property array|null $details 详细信息
- * @property \Carbon\Carbon $checked_at 检查时间
- */
- class ThirdPartyMonitor extends ModelCore
- {
- /**
- * 数据表名
- *
- * @var string
- */
- protected $table = 'thirdparty_monitors';
- /**
- * 禁用updated_at字段
- *
- * @var bool
- */
- public $timestamps = false;
- // field start
- /**
- * 可批量赋值的属性
- *
- * @var array
- */
- // attrlist start
- protected $fillable = [
- 'service_id',
- 'check_type',
- 'status',
- 'response_time',
- 'status_code',
- 'error_message',
- 'details',
- 'checked_at',
- ];
- // attrlist end
- // field end
- /**
- * 属性类型转换
- *
- * @var array
- */
- protected $casts = [
- 'service_id' => '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 => '未知',
- ];
- }
- }
|