mirror of
https://github.com/ProxyPanel/ProxyPanel.git
synced 2026-04-12 23:48:53 +00:00
1. 修复原版本中节点/用户每日流量日志记录错误; 原版:在30号看到的29号的总流量,其实是28号当天产生的流量;依次类推全部流量都错位1天; 现:日流量表精确到当天,天流量表精确到当前; 2. 修复原流量折线图的日期-流量配对错误; 原版:并不是按照记录日期,而是按照有的记录; 现:按照记录日期排序; ----- 以上皆为SSRPanel中遗留的问题代码----- 3. 简化,规范化日期转换 4. 清理多余代码;
66 lines
2.5 KiB
PHP
66 lines
2.5 KiB
PHP
<?php
|
||
|
||
namespace App\Console\Commands;
|
||
|
||
use App\Components\Helpers;
|
||
use App\Components\PushNotification;
|
||
use App\Models\User;
|
||
use App\Models\UserTrafficHourly;
|
||
use Illuminate\Console\Command;
|
||
use Log;
|
||
|
||
class UserTrafficAbnormalAutoWarning extends Command {
|
||
protected static $systemConfig;
|
||
protected $signature = 'userTrafficAbnormalAutoWarning';
|
||
protected $description = '用户流量异常警告';
|
||
|
||
public function __construct() {
|
||
parent::__construct();
|
||
self::$systemConfig = Helpers::systemConfig();
|
||
}
|
||
|
||
public function handle(): void {
|
||
$jobStartTime = microtime(true);
|
||
|
||
// 用户流量异常警告
|
||
$this->userTrafficAbnormalWarning();
|
||
|
||
$jobEndTime = microtime(true);
|
||
$jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
|
||
|
||
Log::info('---【'.$this->description.'】完成---,耗时'.$jobUsedTime.'秒');
|
||
}
|
||
|
||
// 用户流量异常警告
|
||
private function userTrafficAbnormalWarning(): void {
|
||
// 1小时内流量异常用户(多往前取5分钟,防止数据统计任务执行时间过长导致没有数据)
|
||
$userTotalTrafficList = UserTrafficHourly::query()
|
||
->whereNodeId(0)
|
||
->where('total', '>', MB * 50)
|
||
->where('created_at', '>=', date('Y-m-d H:i:s', time() - 3900))
|
||
->groupBy('user_id')
|
||
->selectRaw("user_id, sum(total) as totalTraffic")
|
||
->get(); // 只统计100M以上的记录,加快查询速度
|
||
if(!$userTotalTrafficList->isEmpty()){
|
||
$title = "流量异常用户提醒";
|
||
|
||
foreach($userTotalTrafficList as $vo){
|
||
$user = User::find($vo->user_id);
|
||
|
||
// 推送通知管理员
|
||
if($vo->totalTraffic > self::$systemConfig['traffic_ban_value'] * GB){
|
||
$traffic = UserTrafficHourly::query()
|
||
->userHourly($vo->user_id)
|
||
->where('created_at', '>=', date('Y-m-d H:i:s', time() - 3900))
|
||
->selectRaw("user_id, sum(`u`) as totalU, sum(`d`) as totalD, sum(total) as totalTraffic")
|
||
->firstOrFail();
|
||
|
||
$content = "用户**{$user->email}(ID:{$user->id})**,最近1小时**上行流量:".flowAutoShow($traffic->totalU).",下行流量:".flowAutoShow($traffic->totalD).",共计:".flowAutoShow($traffic->totalTraffic)."**。";
|
||
|
||
PushNotification::send($title, $content);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|