AutoStatisticsNodeDailyTraffic.php 1.4 KB

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