JobBatch.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace App\Module\System\Models;
  3. use UCore\ModelCore;
  4. /**
  5. * 批量队列任务模型
  6. *
  7. * field start
  8. * @property string $id 批次ID
  9. * @property string $name 批次名称
  10. * @property int $total_jobs 总任务数
  11. * @property int $pending_jobs 待处理任务数
  12. * @property int $failed_jobs 失败任务数
  13. * @property string $failed_job_ids 失败任务ID列表
  14. * @property string $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. public static $attrlist = [
  64. 'id',
  65. 'name',
  66. 'total_jobs',
  67. 'pending_jobs',
  68. 'failed_jobs',
  69. 'failed_job_ids',
  70. 'options',
  71. 'cancelled_at',
  72. 'created_at',
  73. 'finished_at'
  74. ];
  75. //attrlist end
  76. /**
  77. * 获取批次名称访问器
  78. */
  79. public function getBatchNameAttribute(): string
  80. {
  81. return $this->name ?? '未命名批次';
  82. }
  83. /**
  84. * 获取格式化的创建时间
  85. */
  86. public function getCreatedAtFormattedAttribute(): string
  87. {
  88. return $this->created_at ? date('Y-m-d H:i:s', $this->created_at) : '';
  89. }
  90. /**
  91. * 获取格式化的完成时间
  92. */
  93. public function getFinishedAtFormattedAttribute(): string
  94. {
  95. return $this->finished_at ? date('Y-m-d H:i:s', $this->finished_at) : '';
  96. }
  97. /**
  98. * 获取格式化的取消时间
  99. */
  100. public function getCancelledAtFormattedAttribute(): string
  101. {
  102. return $this->cancelled_at ? date('Y-m-d H:i:s', $this->cancelled_at) : '';
  103. }
  104. /**
  105. * 获取批次状态
  106. */
  107. public function getStatusAttribute(): string
  108. {
  109. if ($this->cancelled_at) {
  110. return '已取消';
  111. }
  112. if ($this->finished_at) {
  113. return '已完成';
  114. }
  115. if ($this->pending_jobs > 0) {
  116. return '处理中';
  117. }
  118. return '未知';
  119. }
  120. /**
  121. * 获取完成进度百分比
  122. */
  123. public function getProgressPercentageAttribute(): float
  124. {
  125. if ($this->total_jobs == 0) {
  126. return 0;
  127. }
  128. $completed = $this->total_jobs - $this->pending_jobs;
  129. return round(($completed / $this->total_jobs) * 100, 2);
  130. }
  131. /**
  132. * 获取成功任务数
  133. */
  134. public function getSuccessJobsAttribute(): int
  135. {
  136. return $this->total_jobs - $this->pending_jobs - $this->failed_jobs;
  137. }
  138. /**
  139. * 获取失败率
  140. */
  141. public function getFailureRateAttribute(): float
  142. {
  143. if ($this->total_jobs == 0) {
  144. return 0;
  145. }
  146. return round(($this->failed_jobs / $this->total_jobs) * 100, 2);
  147. }
  148. /**
  149. * 获取运行时长(秒)
  150. */
  151. public function getRuntimeSecondsAttribute(): ?int
  152. {
  153. if (!$this->created_at) {
  154. return null;
  155. }
  156. $endTime = $this->finished_at ?? $this->cancelled_at ?? time();
  157. return $endTime - $this->created_at;
  158. }
  159. /**
  160. * 获取格式化的运行时长
  161. */
  162. public function getRuntimeFormattedAttribute(): string
  163. {
  164. $seconds = $this->runtime_seconds;
  165. if ($seconds === null) {
  166. return '';
  167. }
  168. if ($seconds < 60) {
  169. return $seconds . '秒';
  170. } elseif ($seconds < 3600) {
  171. return round($seconds / 60, 1) . '分钟';
  172. } else {
  173. return round($seconds / 3600, 1) . '小时';
  174. }
  175. }
  176. }