AutoStatisticsNodeDailyTraffic.php 1.1 KB

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