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

57 lines
1.9 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\Node;
use App\Models\User;
use App\Notifications\NodeDailyReport;
use Illuminate\Console\Command;
use Log;
use Notification;
class DailyNodeReport extends Command
{
protected $signature = 'dailyNodeReport';
protected $description = '自动报告节点昨日使用情况';
public function handle()
{
$jobStartTime = microtime(true);
if (sysConfig('node_daily_notification')) {
$nodeList = Node::whereStatus(1)->with('dailyDataFlows')->get();
if ($nodeList->isNotEmpty()) {
$data = [];
$upload = 0;
$download = 0;
foreach ($nodeList as $node) {
$log = $node->dailyDataFlows()->whereDate('created_at', date('Y-m-d', strtotime('-1 days')))->first();
$data[] = [
'name' => $node->name,
'upload' => flowAutoShow($log->u ?? 0),
'download' => flowAutoShow($log->d ?? 0),
'total' => $log->traffic ?? '',
];
$upload += $log->u ?? 0;
$download += $log->d ?? 0;
}
if ($data) {
$data[] = [
'name' => trans('notification.node.total'),
'total' => flowAutoShow($upload + $download),
'upload' => flowAutoShow($upload),
'download' => flowAutoShow($download),
];
Notification::send(User::role('Super Admin')->get(), new NodeDailyReport($data));
}
}
}
$jobEndTime = microtime(true);
$jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
Log::info('---【'.$this->description.'】完成---,耗时'.$jobUsedTime.'秒');
}
}