AutoStatisticsNodeHourlyTraffic.php 1.3 KB

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