OpenApiLog.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace App\Module\OpenAPI\Models;
  3. use UCore\ModelCore;
  4. /**
  5. * OpenAPI调用日志模型
  6. *
  7. * 记录所有API调用的详细信息,用于监控、统计和审计
  8. */
  9. class OpenApiLog extends ModelCore
  10. {
  11. /**
  12. * 数据表名
  13. *
  14. * @var string
  15. */
  16. protected $table = 'openapi_logs';
  17. /**
  18. * 可批量赋值的属性
  19. *
  20. * @var array
  21. */
  22. protected $fillable = [
  23. 'app_id',
  24. 'request_id',
  25. 'method',
  26. 'uri',
  27. 'headers',
  28. 'query_params',
  29. 'body',
  30. 'response_status',
  31. 'response_headers',
  32. 'response_body',
  33. 'response_time',
  34. 'ip_address',
  35. 'user_agent',
  36. 'error_message',
  37. 'created_at',
  38. ];
  39. /**
  40. * 属性类型转换
  41. *
  42. * @var array
  43. */
  44. protected $casts = [
  45. 'headers' => 'json',
  46. 'query_params' => 'json',
  47. 'response_headers' => 'json',
  48. 'response_time' => 'integer',
  49. 'response_status' => 'integer',
  50. 'created_at' => 'datetime',
  51. ];
  52. /**
  53. * 关联应用模型
  54. *
  55. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  56. */
  57. public function app()
  58. {
  59. return $this->belongsTo(OpenApiApp::class, 'app_id', 'app_id');
  60. }
  61. /**
  62. * 获取格式化的响应时间
  63. *
  64. * @return string
  65. */
  66. public function getFormattedResponseTimeAttribute(): string
  67. {
  68. if ($this->response_time < 1000) {
  69. return $this->response_time . 'ms';
  70. }
  71. return round($this->response_time / 1000, 2) . 's';
  72. }
  73. /**
  74. * 获取状态标签
  75. *
  76. * @return string
  77. */
  78. public function getStatusLabelAttribute(): string
  79. {
  80. $status = $this->response_status;
  81. if ($status >= 200 && $status < 300) {
  82. return '成功';
  83. } elseif ($status >= 400 && $status < 500) {
  84. return '客户端错误';
  85. } elseif ($status >= 500) {
  86. return '服务器错误';
  87. }
  88. return '未知';
  89. }
  90. /**
  91. * 获取状态颜色
  92. *
  93. * @return string
  94. */
  95. public function getStatusColorAttribute(): string
  96. {
  97. $status = $this->response_status;
  98. if ($status >= 200 && $status < 300) {
  99. return 'success';
  100. } elseif ($status >= 400 && $status < 500) {
  101. return 'warning';
  102. } elseif ($status >= 500) {
  103. return 'danger';
  104. }
  105. return 'secondary';
  106. }
  107. /**
  108. * 是否成功响应
  109. *
  110. * @return bool
  111. */
  112. public function isSuccessful(): bool
  113. {
  114. return $this->response_status >= 200 && $this->response_status < 300;
  115. }
  116. /**
  117. * 是否客户端错误
  118. *
  119. * @return bool
  120. */
  121. public function isClientError(): bool
  122. {
  123. return $this->response_status >= 400 && $this->response_status < 500;
  124. }
  125. /**
  126. * 是否服务器错误
  127. *
  128. * @return bool
  129. */
  130. public function isServerError(): bool
  131. {
  132. return $this->response_status >= 500;
  133. }
  134. /**
  135. * 按应用ID查询
  136. *
  137. * @param \Illuminate\Database\Eloquent\Builder $query
  138. * @param string $appId
  139. * @return \Illuminate\Database\Eloquent\Builder
  140. */
  141. public function scopeByApp($query, string $appId)
  142. {
  143. return $query->where('app_id', $appId);
  144. }
  145. /**
  146. * 按状态码查询
  147. *
  148. * @param \Illuminate\Database\Eloquent\Builder $query
  149. * @param int $status
  150. * @return \Illuminate\Database\Eloquent\Builder
  151. */
  152. public function scopeByStatus($query, int $status)
  153. {
  154. return $query->where('response_status', $status);
  155. }
  156. /**
  157. * 按时间范围查询
  158. *
  159. * @param \Illuminate\Database\Eloquent\Builder $query
  160. * @param string $start
  161. * @param string $end
  162. * @return \Illuminate\Database\Eloquent\Builder
  163. */
  164. public function scopeByDateRange($query, string $start, string $end)
  165. {
  166. return $query->whereBetween('created_at', [$start, $end]);
  167. }
  168. /**
  169. * 只查询成功的请求
  170. *
  171. * @param \Illuminate\Database\Eloquent\Builder $query
  172. * @return \Illuminate\Database\Eloquent\Builder
  173. */
  174. public function scopeSuccessful($query)
  175. {
  176. return $query->whereBetween('response_status', [200, 299]);
  177. }
  178. /**
  179. * 只查询错误的请求
  180. *
  181. * @param \Illuminate\Database\Eloquent\Builder $query
  182. * @return \Illuminate\Database\Eloquent\Builder
  183. */
  184. public function scopeErrors($query)
  185. {
  186. return $query->where('response_status', '>=', 400);
  187. }
  188. /**
  189. * 按响应时间排序
  190. *
  191. * @param \Illuminate\Database\Eloquent\Builder $query
  192. * @param string $direction
  193. * @return \Illuminate\Database\Eloquent\Builder
  194. */
  195. public function scopeOrderByResponseTime($query, string $direction = 'desc')
  196. {
  197. return $query->orderBy('response_time', $direction);
  198. }
  199. }