DailyNodeReport.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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')->whereDate('created_at', date('Y-m-d', strtotime('yesterday')))->orderBy('node_id')->get();
  18. $data = [];
  19. $sum_u = 0;
  20. $sum_d = 0;
  21. foreach ($nodeDailyLogs as $log) {
  22. $data[] = [
  23. 'name' => $log->node->name,
  24. 'upload' => formatBytes($log->u),
  25. 'download' => formatBytes($log->d),
  26. 'total' => formatBytes($log->u + $log->d),
  27. ];
  28. $sum_u += $log->u;
  29. $sum_d += $log->d;
  30. }
  31. if ($data) {
  32. $data[] = [
  33. 'name' => trans('notification.node.total'),
  34. 'upload' => formatBytes($sum_u),
  35. 'download' => formatBytes($sum_d),
  36. 'total' => formatBytes($sum_u + $sum_d),
  37. ];
  38. $superAdmins = User::role('Super Admin')->get();
  39. if ($superAdmins->isNotEmpty()) {
  40. Notification::send($superAdmins, new NodeDailyReport($data));
  41. }
  42. }
  43. }
  44. $jobTime = round(microtime(true) - $jobTime, 4);
  45. Log::info(__('----「:job」Completed, Used :time seconds ----', ['job' => $this->description, 'time' => $jobTime]));
  46. }
  47. }