DailyNodeReport.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\NodeDailyDataFlow;
  4. use App\Models\User;
  5. use App\Notifications\NodeDailyReport;
  6. use Illuminate\Console\Command;
  7. use Log;
  8. use Notification;
  9. class DailyNodeReport extends Command
  10. {
  11. protected $signature = 'dailyNodeReport';
  12. protected $description = '自动报告节点昨日使用情况';
  13. public function handle(): void
  14. {
  15. $jobTime = microtime(true);
  16. if (sysConfig('node_daily_notification')) {
  17. $nodeDailyLogs = NodeDailyDataFlow::with('node:id,name')->has('node')->orderBy('node_id')->whereDate('created_at', date('Y-m-d', strtotime('yesterday')))->get();
  18. $data = [];
  19. foreach ($nodeDailyLogs as $log) {
  20. $data[] = [
  21. 'name' => $log->node->name,
  22. 'upload' => formatBytes($log->u),
  23. 'download' => formatBytes($log->d),
  24. 'total' => formatBytes($log->u + $log->d),
  25. ];
  26. }
  27. if ($data) {
  28. $u = $nodeDailyLogs->sum('u');
  29. $d = $nodeDailyLogs->sum('d');
  30. $data[] = [
  31. 'name' => trans('notification.node.total'),
  32. 'upload' => formatBytes($u),
  33. 'download' => formatBytes($d),
  34. 'total' => formatBytes($u + $d),
  35. ];
  36. Notification::send(User::role('Super Admin')->get(), new NodeDailyReport($data));
  37. }
  38. }
  39. $jobTime = round(microtime(true) - $jobTime, 4);
  40. Log::info(__('----「:job」Completed, Used :time seconds ----', ['job' => $this->description, 'time' => $jobTime]));
  41. }
  42. }