NodeDailyTrafficStatistics.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Node;
  4. use Illuminate\Console\Command;
  5. use Log;
  6. class NodeDailyTrafficStatistics extends Command
  7. {
  8. protected $signature = 'nodeDailyTrafficStatistics';
  9. protected $description = '节点每日流量统计';
  10. public function handle()
  11. {
  12. $jobStartTime = microtime(true);
  13. foreach (Node::whereStatus(1)->orderBy('id')->with('userDataFlowLogs')->whereHas('userDataFlowLogs')->get() as $node) {
  14. $this->statisticsByNode($node);
  15. }
  16. $jobEndTime = microtime(true);
  17. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  18. Log::info('---【'.$this->description.'】完成---,耗时'.$jobUsedTime.'秒');
  19. }
  20. private function statisticsByNode(Node $node)
  21. {
  22. $traffic = $node->userDataFlowLogs()
  23. ->whereBetween('log_time', [strtotime(date('Y-m-d')), time()])
  24. ->selectRaw('sum(`u`) as u, sum(`d`) as d')->first();
  25. if ($traffic && $total = $traffic->u + $traffic->d) { // 有数据才记录
  26. $node->dailyDataFlows()->create([
  27. 'u' => $traffic->u,
  28. 'd' => $traffic->d,
  29. 'total' => $total,
  30. 'traffic' => flowAutoShow($total),
  31. ]);
  32. }
  33. }
  34. }