Files
ProxyPanel/app/Console/Commands/NodeDailyTrafficStatistics.php
兔姬桑 396fbbad56 优化 自动任务
1. 使用chunk分段处理大规模数据处理;
2. 优化部分任务的查询逻辑;
3. 提取各任务时间戳至,config/tasks.php,允许机场主自定义各任务;
2021-01-24 23:21:43 -05:00

44 lines
1.3 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\Node;
use Illuminate\Console\Command;
use Log;
class NodeDailyTrafficStatistics extends Command
{
protected $signature = 'nodeDailyTrafficStatistics';
protected $description = '节点每日流量统计';
public function handle()
{
$jobStartTime = microtime(true);
foreach (Node::whereStatus(1)->orderBy('id')->with('userDataFlowLogs')->whereHas('userDataFlowLogs')->get() as $node) {
$this->statisticsByNode($node);
}
$jobEndTime = microtime(true);
$jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
Log::info('---【'.$this->description.'】完成---,耗时'.$jobUsedTime.'秒');
}
private function statisticsByNode(Node $node)
{
$traffic = $node->userDataFlowLogs()
->whereBetween('log_time', [strtotime(date('Y-m-d')), time()])
->selectRaw('sum(`u`) as u, sum(`d`) as d')->first();
if ($traffic && $total = $traffic->u + $traffic->d) { // 有数据才记录
$node->dailyDataFlows()->create([
'u' => $traffic->u,
'd' => $traffic->d,
'total' => $total,
'traffic' => flowAutoShow($total),
]);
}
}
}