JobBatch.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 $fillable = [
  29. 'id',
  30. 'name',
  31. 'total_jobs',
  32. 'pending_jobs',
  33. 'failed_jobs',
  34. 'failed_job_ids',
  35. 'options',
  36. 'cancelled_at',
  37. 'created_at',
  38. 'finished_at'
  39. ];
  40. protected $casts = [
  41. 'total_jobs' => 'integer',
  42. 'pending_jobs' => 'integer',
  43. 'failed_jobs' => 'integer',
  44. 'failed_job_ids' => 'array',
  45. 'options' => 'array',
  46. 'cancelled_at' => 'integer',
  47. 'created_at' => 'integer',
  48. 'finished_at' => 'integer'
  49. ];
  50. protected $appends = [
  51. 'batch_name',
  52. 'created_at_formatted',
  53. 'finished_at_formatted',
  54. 'cancelled_at_formatted',
  55. 'status',
  56. 'progress_percentage',
  57. 'success_jobs',
  58. 'failure_rate',
  59. 'runtime_seconds',
  60. 'runtime_formatted'
  61. ];
  62. // attrlist start
  63. protected $fillable = [
  64. 'id',
  65. 'name',
  66. 'total_jobs',
  67. 'pending_jobs',
  68. 'failed_jobs',
  69. 'failed_job_ids',
  70. 'options',
  71. 'cancelled_at',
  72. 'finished_at',
  73. ];
  74. // attrlist end
  75. /**
  76. * 获取批次名称访问器
  77. */
  78. public function getBatchNameAttribute(): string
  79. {
  80. return $this->name ?? '未命名批次';
  81. }
  82. /**
  83. * 获取格式化的创建时间
  84. */
  85. public function getCreatedAtFormattedAttribute(): string
  86. {
  87. return $this->created_at ? date('Y-m-d H:i:s', $this->created_at) : '';
  88. }
  89. /**
  90. * 获取格式化的完成时间
  91. */
  92. public function getFinishedAtFormattedAttribute(): string
  93. {
  94. return $this->finished_at ? date('Y-m-d H:i:s', $this->finished_at) : '';
  95. }
  96. /**
  97. * 获取格式化的取消时间
  98. */
  99. public function getCancelledAtFormattedAttribute(): string
  100. {
  101. return $this->cancelled_at ? date('Y-m-d H:i:s', $this->cancelled_at) : '';
  102. }
  103. /**
  104. * 获取批次状态
  105. */
  106. public function getStatusAttribute(): string
  107. {
  108. if ($this->cancelled_at) {
  109. return '已取消';
  110. }
  111. if ($this->finished_at) {
  112. return '已完成';
  113. }
  114. if ($this->pending_jobs > 0) {
  115. return '处理中';
  116. }
  117. return '未知';
  118. }
  119. /**
  120. * 获取完成进度百分比
  121. */
  122. public function getProgressPercentageAttribute(): float
  123. {
  124. if ($this->total_jobs == 0) {
  125. return 0;
  126. }
  127. $completed = $this->total_jobs - $this->pending_jobs;
  128. return round(($completed / $this->total_jobs) * 100, 2);
  129. }
  130. /**
  131. * 获取成功任务数
  132. */
  133. public function getSuccessJobsAttribute(): int
  134. {
  135. return $this->total_jobs - $this->pending_jobs - $this->failed_jobs;
  136. }
  137. /**
  138. * 获取失败率
  139. */
  140. public function getFailureRateAttribute(): float
  141. {
  142. if ($this->total_jobs == 0) {
  143. return 0;
  144. }
  145. return round(($this->failed_jobs / $this->total_jobs) * 100, 2);
  146. }
  147. /**
  148. * 获取运行时长(秒)
  149. */
  150. public function getRuntimeSecondsAttribute(): ?int
  151. {
  152. if (!$this->created_at) {
  153. return null;
  154. }
  155. $endTime = $this->finished_at ?? $this->cancelled_at ?? time();
  156. return $endTime - $this->created_at;
  157. }
  158. /**
  159. * 获取格式化的运行时长
  160. */
  161. public function getRuntimeFormattedAttribute(): string
  162. {
  163. $seconds = $this->runtime_seconds;
  164. if ($seconds === null) {
  165. return '';
  166. }
  167. if ($seconds < 60) {
  168. return $seconds . '秒';
  169. } elseif ($seconds < 3600) {
  170. return round($seconds / 60, 1) . '分钟';
  171. } else {
  172. return round($seconds / 3600, 1) . '小时';
  173. }
  174. }
  175. }