| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- namespace App\Module\OpenAPI\Models;
- use UCore\ModelCore;
- /**
- * OpenAPI调用日志模型
- *
- * 记录所有API调用的详细信息,用于监控、统计和审计
- */
- class OpenApiLog extends ModelCore
- {
- /**
- * 数据表名
- *
- * @var string
- */
- protected $table = 'openapi_logs';
- /**
- * 可批量赋值的属性
- *
- * @var array
- */
- protected $fillable = [
- 'app_id',
- 'request_id',
- 'method',
- 'uri',
- 'headers',
- 'query_params',
- 'body',
- 'response_status',
- 'response_headers',
- 'response_body',
- 'response_time',
- 'ip_address',
- 'user_agent',
- 'error_message',
- 'created_at',
- ];
- /**
- * 属性类型转换
- *
- * @var array
- */
- protected $casts = [
- 'headers' => 'json',
- 'query_params' => 'json',
- 'response_headers' => 'json',
- 'response_time' => 'integer',
- 'response_status' => 'integer',
- 'created_at' => 'datetime',
- ];
- /**
- * 关联应用模型
- *
- * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
- */
- public function app()
- {
- return $this->belongsTo(OpenApiApp::class, 'app_id', 'app_id');
- }
- /**
- * 获取格式化的响应时间
- *
- * @return string
- */
- public function getFormattedResponseTimeAttribute(): string
- {
- if ($this->response_time < 1000) {
- return $this->response_time . 'ms';
- }
-
- return round($this->response_time / 1000, 2) . 's';
- }
- /**
- * 获取状态标签
- *
- * @return string
- */
- public function getStatusLabelAttribute(): string
- {
- $status = $this->response_status;
-
- if ($status >= 200 && $status < 300) {
- return '成功';
- } elseif ($status >= 400 && $status < 500) {
- return '客户端错误';
- } elseif ($status >= 500) {
- return '服务器错误';
- }
-
- return '未知';
- }
- /**
- * 获取状态颜色
- *
- * @return string
- */
- public function getStatusColorAttribute(): string
- {
- $status = $this->response_status;
-
- if ($status >= 200 && $status < 300) {
- return 'success';
- } elseif ($status >= 400 && $status < 500) {
- return 'warning';
- } elseif ($status >= 500) {
- return 'danger';
- }
-
- return 'secondary';
- }
- /**
- * 是否成功响应
- *
- * @return bool
- */
- public function isSuccessful(): bool
- {
- return $this->response_status >= 200 && $this->response_status < 300;
- }
- /**
- * 是否客户端错误
- *
- * @return bool
- */
- public function isClientError(): bool
- {
- return $this->response_status >= 400 && $this->response_status < 500;
- }
- /**
- * 是否服务器错误
- *
- * @return bool
- */
- public function isServerError(): bool
- {
- return $this->response_status >= 500;
- }
- /**
- * 按应用ID查询
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @param string $appId
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeByApp($query, string $appId)
- {
- return $query->where('app_id', $appId);
- }
- /**
- * 按状态码查询
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @param int $status
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeByStatus($query, int $status)
- {
- return $query->where('response_status', $status);
- }
- /**
- * 按时间范围查询
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @param string $start
- * @param string $end
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeByDateRange($query, string $start, string $end)
- {
- return $query->whereBetween('created_at', [$start, $end]);
- }
- /**
- * 只查询成功的请求
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeSuccessful($query)
- {
- return $query->whereBetween('response_status', [200, 299]);
- }
- /**
- * 只查询错误的请求
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeErrors($query)
- {
- return $query->where('response_status', '>=', 400);
- }
- /**
- * 按响应时间排序
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @param string $direction
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeOrderByResponseTime($query, string $direction = 'desc')
- {
- return $query->orderBy('response_time', $direction);
- }
- }
|