DailyNodeReport.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 Illuminate\Database\Eloquent\Builder;
  8. use Log;
  9. use Notification;
  10. class DailyNodeReport extends Command
  11. {
  12. protected $signature = 'dailyNodeReport';
  13. protected $description = '自动报告节点昨日使用情况';
  14. public function handle(): void
  15. {
  16. $jobTime = microtime(true);
  17. if (sysConfig('node_daily_notification')) {
  18. $date = date('Y-m-d', strtotime('-1 days'));
  19. $nodeList = Node::with('dailyDataFlows')->whereHas('dailyDataFlows', function (Builder $query) use ($date) {
  20. $query->whereDate('created_at', $date);
  21. })->get();
  22. if ($nodeList->isNotEmpty()) {
  23. $data = [];
  24. $upload = 0;
  25. $download = 0;
  26. foreach ($nodeList as $node) {
  27. $log = $node->dailyDataFlows()->whereDate('created_at', $date)->first();
  28. $data[] = [
  29. 'name' => $node->name,
  30. 'upload' => formatBytes($log->u ?? 0),
  31. 'download' => formatBytes($log->d ?? 0),
  32. 'total' => $log->traffic ?? '',
  33. ];
  34. $upload += $log->u ?? 0;
  35. $download += $log->d ?? 0;
  36. }
  37. if ($data) {
  38. $data[] = [
  39. 'name' => trans('notification.node.total'),
  40. 'total' => formatBytes($upload + $download),
  41. 'upload' => formatBytes($upload),
  42. 'download' => formatBytes($download),
  43. ];
  44. Notification::send(User::role('Super Admin')->get(), new NodeDailyReport($data));
  45. }
  46. }
  47. }
  48. $jobTime = round(microtime(true) - $jobTime, 4);
  49. Log::info(__('----「:job」Completed, Used :time seconds ----', ['job' => $this->description, 'time' => $jobTime]));
  50. }
  51. }