AutoReportNode.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Node;
  4. use App\Models\User;
  5. use App\Notifications\NodeDailyReport;
  6. use Illuminate\Console\Command;
  7. use Log;
  8. use Notification;
  9. class AutoReportNode extends Command
  10. {
  11. protected $signature = 'autoReportNode';
  12. protected $description = '自动报告节点昨日使用情况';
  13. public function handle(): void
  14. {
  15. $jobStartTime = microtime(true);
  16. if (sysConfig('node_daily_notification')) {
  17. $nodeList = Node::whereStatus(1)->with('dailyDataFlows')->get();
  18. if ($nodeList->isNotEmpty()) {
  19. $data = [];
  20. $upload = 0;
  21. $download = 0;
  22. foreach ($nodeList as $node) {
  23. $log = $node->dailyDataFlows()->whereDate('created_at', date('Y-m-d', strtotime('-1 days')))->first();
  24. $data[] = [
  25. 'name' => $node->name,
  26. 'upload' => flowAutoShow($log->u ?? 0),
  27. 'download' => flowAutoShow($log->d ?? 0),
  28. 'total' => $log->traffic ?? '',
  29. ];
  30. $upload += $log->u ?? 0;
  31. $download += $log->d ?? 0;
  32. }
  33. if ($data) {
  34. $data[] = [
  35. 'name' => trans('notification.node.total'),
  36. 'total' => flowAutoShow($upload + $download),
  37. 'upload' => flowAutoShow($upload),
  38. 'download' => flowAutoShow($download),
  39. ];
  40. Notification::send(User::role('Super Admin')->get(), new NodeDailyReport($data));
  41. }
  42. }
  43. }
  44. $jobEndTime = microtime(true);
  45. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  46. Log::info('---【'.$this->description.'】完成---,耗时'.$jobUsedTime.'秒');
  47. }
  48. }