ViewTaskLogsAction.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace App\Module\Cleanup\AdminControllers\Actions;
  3. use App\Module\Cleanup\Models\CleanupTask;
  4. use Dcat\Admin\Actions\RowAction;
  5. use Dcat\Admin\Actions\Response;
  6. use Illuminate\Http\Request;
  7. /**
  8. * 查看任务日志Action
  9. *
  10. * 用于查看清理任务的执行日志
  11. */
  12. class ViewTaskLogsAction extends RowAction
  13. {
  14. /**
  15. * 按钮标题
  16. */
  17. protected $title = '查看日志';
  18. /**
  19. * 处理请求
  20. */
  21. public function handle(Request $request)
  22. {
  23. try {
  24. $taskId = $this->getKey();
  25. $task = CleanupTask::with('logs')->find($taskId);
  26. if (!$task) {
  27. return $this->response()
  28. ->error('任务不存在');
  29. }
  30. $logs = $task->logs()->orderBy('created_at', 'desc')->get();
  31. if ($logs->isEmpty()) {
  32. return $this->response()
  33. ->info('该任务暂无执行日志');
  34. }
  35. $html = '<div class="table-responsive"><table class="table table-striped table-sm">';
  36. $html .= '<thead><tr><th>表名</th><th>清理类型</th><th>删除前</th><th>删除后</th><th>删除数量</th><th>执行时间</th><th>状态</th><th>执行时间</th></tr></thead>';
  37. $html .= '<tbody>';
  38. $cleanupTypes = [
  39. 1 => '清空表',
  40. 2 => '删除所有',
  41. 3 => '按时间删除',
  42. 4 => '按用户删除',
  43. 5 => '按条件删除',
  44. ];
  45. foreach ($logs as $log) {
  46. $cleanupType = $cleanupTypes[$log->cleanup_type] ?? '未知';
  47. $status = empty($log->error_message) ?
  48. '<span class="badge badge-success">成功</span>' :
  49. '<span class="badge badge-danger">失败</span>';
  50. $html .= "<tr>";
  51. $html .= "<td>{$log->table_name}</td>";
  52. $html .= "<td>{$cleanupType}</td>";
  53. $html .= "<td>" . number_format($log->before_count) . "</td>";
  54. $html .= "<td>" . number_format($log->after_count) . "</td>";
  55. $html .= "<td>" . number_format($log->deleted_records) . "</td>";
  56. $html .= "<td>" . number_format($log->execution_time, 3) . "s</td>";
  57. $html .= "<td>{$status}</td>";
  58. $html .= "<td>{$log->created_at}</td>";
  59. $html .= "</tr>";
  60. // 如果有错误信息,显示在下一行
  61. if (!empty($log->error_message)) {
  62. $html .= "<tr class='table-danger'>";
  63. $html .= "<td colspan='8'><small><strong>错误:</strong>{$log->error_message}</small></td>";
  64. $html .= "</tr>";
  65. }
  66. }
  67. $html .= '</tbody></table></div>';
  68. // 添加统计信息
  69. $totalDeleted = $logs->sum('deleted_records');
  70. $totalTime = $logs->sum('execution_time');
  71. $successCount = $logs->where('error_message', '')->count();
  72. $failCount = $logs->where('error_message', '!=', '')->count();
  73. $html .= '<div class="row mt-3">';
  74. $html .= '<div class="col-md-3"><div class="card card-body text-center">';
  75. $html .= '<h5 class="text-primary">' . number_format($totalDeleted) . '</h5>';
  76. $html .= '<small>总删除记录数</small>';
  77. $html .= '</div></div>';
  78. $html .= '<div class="col-md-3"><div class="card card-body text-center">';
  79. $html .= '<h5 class="text-info">' . number_format($totalTime, 3) . 's</h5>';
  80. $html .= '<small>总执行时间</small>';
  81. $html .= '</div></div>';
  82. $html .= '<div class="col-md-3"><div class="card card-body text-center">';
  83. $html .= '<h5 class="text-success">' . $successCount . '</h5>';
  84. $html .= '<small>成功操作数</small>';
  85. $html .= '</div></div>';
  86. $html .= '<div class="col-md-3"><div class="card card-body text-center">';
  87. $html .= '<h5 class="text-danger">' . $failCount . '</h5>';
  88. $html .= '<small>失败操作数</small>';
  89. $html .= '</div></div>';
  90. $html .= '</div>';
  91. return $this->response()
  92. ->success('任务执行日志')
  93. ->detail($html);
  94. } catch (\Exception $e) {
  95. return $this->response()
  96. ->error('查看失败:' . $e->getMessage());
  97. }
  98. }
  99. /**
  100. * 渲染按钮
  101. */
  102. public function render()
  103. {
  104. return <<<HTML
  105. <a href="javascript:void(0);" class="btn btn-info btn-xs" data-action="{$this->getHandleRoute()}">
  106. <i class="fa fa-file-text"></i> {$this->title}
  107. </a>
  108. HTML;
  109. }
  110. }