JobBatch.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace App\Module\System\Models;
  3. use UCore\ModelCore;
  4. /**
  5. * 批量队列任务模型
  6. *
  7. * field start
  8. * @property string $id
  9. * @property string $name
  10. * @property int $total_jobs
  11. * @property int $pending_jobs
  12. * @property int $failed_jobs
  13. * @property array $failed_job_ids
  14. * @property array $options
  15. * @property int $cancelled_at
  16. * @property int $created_at
  17. * @property int $finished_at
  18. * field end
  19. *
  20. */
  21. class JobBatch extends ModelCore
  22. {
  23. protected $table = 'job_batches';
  24. protected $primaryKey = 'id';
  25. protected $keyType = 'string';
  26. public $incrementing = false;
  27. public $timestamps = false;
  28. protected $casts = [
  29. 'total_jobs' => 'integer',
  30. 'pending_jobs' => 'integer',
  31. 'failed_jobs' => 'integer',
  32. 'failed_job_ids' => 'array',
  33. 'options' => 'array',
  34. 'cancelled_at' => 'integer',
  35. 'created_at' => 'integer',
  36. 'finished_at' => 'integer'
  37. ];
  38. protected $appends = [
  39. 'batch_name',
  40. 'created_at_formatted',
  41. 'finished_at_formatted',
  42. 'cancelled_at_formatted',
  43. 'status',
  44. 'progress_percentage',
  45. 'success_jobs',
  46. 'failure_rate',
  47. 'runtime_seconds',
  48. 'runtime_formatted'
  49. ];
  50. // attrlist start
  51. protected $fillable = [
  52. 'id',
  53. 'name',
  54. 'total_jobs',
  55. 'pending_jobs',
  56. 'failed_jobs',
  57. 'failed_job_ids',
  58. 'options',
  59. 'cancelled_at',
  60. 'finished_at',
  61. ];
  62. // attrlist end
  63. /**
  64. * 获取批次名称访问器
  65. */
  66. public function getBatchNameAttribute(): string
  67. {
  68. return $this->name ?? '未命名批次';
  69. }
  70. /**
  71. * 获取格式化的创建时间
  72. */
  73. public function getCreatedAtFormattedAttribute(): string
  74. {
  75. return $this->created_at ? date('Y-m-d H:i:s', $this->created_at) : '';
  76. }
  77. /**
  78. * 获取格式化的完成时间
  79. */
  80. public function getFinishedAtFormattedAttribute(): string
  81. {
  82. return $this->finished_at ? date('Y-m-d H:i:s', $this->finished_at) : '';
  83. }
  84. /**
  85. * 获取格式化的取消时间
  86. */
  87. public function getCancelledAtFormattedAttribute(): string
  88. {
  89. return $this->cancelled_at ? date('Y-m-d H:i:s', $this->cancelled_at) : '';
  90. }
  91. /**
  92. * 获取批次状态
  93. */
  94. public function getStatusAttribute(): string
  95. {
  96. if ($this->cancelled_at) {
  97. return '已取消';
  98. }
  99. if ($this->finished_at) {
  100. return '已完成';
  101. }
  102. if ($this->pending_jobs > 0) {
  103. return '处理中';
  104. }
  105. return '未知';
  106. }
  107. /**
  108. * 获取完成进度百分比
  109. */
  110. public function getProgressPercentageAttribute(): float
  111. {
  112. if ($this->total_jobs == 0) {
  113. return 0;
  114. }
  115. $completed = $this->total_jobs - $this->pending_jobs;
  116. return round(($completed / $this->total_jobs) * 100, 2);
  117. }
  118. /**
  119. * 获取成功任务数
  120. */
  121. public function getSuccessJobsAttribute(): int
  122. {
  123. return $this->total_jobs - $this->pending_jobs - $this->failed_jobs;
  124. }
  125. /**
  126. * 获取失败率
  127. */
  128. public function getFailureRateAttribute(): float
  129. {
  130. if ($this->total_jobs == 0) {
  131. return 0;
  132. }
  133. return round(($this->failed_jobs / $this->total_jobs) * 100, 2);
  134. }
  135. /**
  136. * 获取运行时长(秒)
  137. */
  138. public function getRuntimeSecondsAttribute(): ?int
  139. {
  140. if (!$this->created_at) {
  141. return null;
  142. }
  143. $endTime = $this->finished_at ?? $this->cancelled_at ?? time();
  144. return $endTime - $this->created_at;
  145. }
  146. /**
  147. * 获取格式化的运行时长
  148. */
  149. public function getRuntimeFormattedAttribute(): string
  150. {
  151. $seconds = $this->runtime_seconds;
  152. if ($seconds === null) {
  153. return '';
  154. }
  155. if ($seconds < 60) {
  156. return $seconds . '秒';
  157. } elseif ($seconds < 3600) {
  158. return round($seconds / 60, 1) . '分钟';
  159. } else {
  160. return round($seconds / 3600, 1) . '小时';
  161. }
  162. }
  163. }