瀏覽代碼

查表性能优化

兔姬桑 4 年之前
父節點
當前提交
97cf59edcb

+ 13 - 10
app/Console/Commands/AutoJob.php

@@ -132,9 +132,11 @@ class AutoJob extends Command {
 	// 封禁账号
 	private function blockUsers(): void {
 		// 封禁1小时内流量异常账号
+		$userList = User::activeUser()->whereBanTime(null);
 		if(sysConfig('is_traffic_ban')){
-			$userList = User::activeUser()->whereBanTime(null)->get();
-			foreach($userList as $user){
+			$trafficBanValue = sysConfig('traffic_ban_value');
+			$trafficBanTime = sysConfig('traffic_ban_time');
+			foreach($userList->get() as $user){
 				// 对管理员豁免
 				if($user->is_admin){
 					continue;
@@ -142,21 +144,20 @@ class AutoJob extends Command {
 
 				// 多往前取5分钟,防止数据统计任务执行时间过长导致没有数据
 				$totalTraffic = UserHourlyDataFlow::userRecentUsed($user->id)->sum('total');
-				if($totalTraffic >= sysConfig('traffic_ban_value') * GB){
+				if($totalTraffic >= $trafficBanValue * GB){
 					$user->update([
 						'enable'   => 0,
-						'ban_time' => strtotime("+".sysConfig('traffic_ban_time')." minutes")
+						'ban_time' => strtotime("+".$trafficBanTime." minutes")
 					]);
 
 					// 写入日志
-					$this->addUserBanLog($user->id, sysConfig('traffic_ban_time'), '【临时封禁代理】-1小时内流量异常');
+					$this->addUserBanLog($user->id, $trafficBanTime, '【临时封禁代理】-1小时内流量异常');
 				}
 			}
 		}
 
 		// 禁用流量超限用户
-		$userList = User::activeUser()->whereBanTime(null)->whereRaw("u + d >= transfer_enable")->get();
-		foreach($userList as $user){
+		foreach($userList->whereRaw("u + d >= transfer_enable")->get() as $user){
 			$user->update(['enable' => 0]);
 
 			// 写入日志
@@ -195,8 +196,9 @@ class AutoJob extends Command {
 	// 端口回收与分配
 	private function dispatchPort(): void {
 		## 自动分配端口
+		$isRandPort = sysConfig('is_rand_port');
 		foreach(User::activeUser()->wherePort(0)->get() as $user){
-			$port = sysConfig('is_rand_port')? Helpers::getRandPort() : Helpers::getOnlyPort();
+			$port = $isRandPort? Helpers::getRandPort() : Helpers::getOnlyPort();
 
 			$user->update(['port' => $port]);
 		}
@@ -211,11 +213,12 @@ class AutoJob extends Command {
 	// 检测节点是否离线
 	private function checkNodeStatus(): void {
 		if(sysConfig('is_node_offline')){
+			$offlineCheckTimes = sysConfig('offline_check_times');
 			$onlineNode = NodeHeartBeat::recently()->distinct()->pluck('node_id')->toArray();
 			foreach(Node::whereIsRelay(0)->whereStatus(1)->get() as $node){
 				// 10分钟内无节点负载信息则认为是后端炸了
 				$nodeTTL = !in_array($node->id, $onlineNode);
-				if($nodeTTL && sysConfig('offline_check_times')){
+				if($nodeTTL && $offlineCheckTimes){
 					// 已通知次数
 					$cacheKey = 'offline_check_times'.$node->id;
 					if(Cache::has($cacheKey)){
@@ -226,7 +229,7 @@ class AutoJob extends Command {
 						$times = 1;
 					}
 
-					if($times < sysConfig('offline_check_times')){
+					if($times < $offlineCheckTimes){
 						Cache::increment($cacheKey);
 						PushNotification::send('节点异常警告', "节点**{$node->name}【{$node->ip}】**异常:**心跳异常,可能离线了**");
 					}

+ 2 - 1
app/Console/Commands/DailyJob.php

@@ -40,8 +40,9 @@ class DailyJob extends Command {
 	private function expireUser(): void {
 		// 过期用户处理
 		$userList = User::activeUser()->where('expired_at', '<', date('Y-m-d'))->get();
+		$isBanStatus = sysConfig('is_ban_status');
 		foreach($userList as $user){
-			if(sysConfig('is_ban_status')){
+			if($isBanStatus){
 				$user->update([
 					'u'               => 0,
 					'd'               => 0,

+ 4 - 4
app/Console/Commands/NodeBlockedDetection.php

@@ -36,11 +36,11 @@ class NodeBlockedDetection extends Command {
 
 	// 监测节点状态
 	private function checkNodes(): void {
-		$nodeList = Node::whereIsRelay(0)->whereStatus(1)->where('detection_type', '>', 0)->get();
+		$detectionCheckTimes = sysConfig('detection_check_times');
 		$sendText = false;
 		$message = "| 线路 | 协议 | 状态 |\r\n| ------ | ------ | ------ |\r\n";
 		$additionalMessage = '';
-		foreach($nodeList as $node){
+		foreach(Node::whereIsRelay(0)->whereStatus(1)->where('detection_type', '>', 0)->get() as $node){
 			$info = false;
 			if($node->detection_type == 0){
 				continue;
@@ -73,7 +73,7 @@ class NodeBlockedDetection extends Command {
 			}
 
 			// 节点检测次数
-			if($info && sysConfig('detection_check_times')){
+			if($info && $detectionCheckTimes){
 				// 已通知次数
 				$cacheKey = 'detection_check_times'.$node->id;
 				if(Cache::has($cacheKey)){
@@ -84,7 +84,7 @@ class NodeBlockedDetection extends Command {
 					$times = 1;
 				}
 
-				if($times < sysConfig('detection_check_times')){
+				if($times < $detectionCheckTimes){
 					Cache::increment($cacheKey);
 				}else{
 					Cache::forget($cacheKey);

+ 1 - 2
app/Console/Commands/ServiceTimer.php

@@ -26,8 +26,7 @@ class ServiceTimer extends Command {
 	// 扣减用户到期商品的流量
 	private function decGoodsTraffic(): void {
 		//获取失效的套餐
-		$orders = Order::activePlan()->where('expired_at', '<=', date('Y-m-d H:i:s'))->get();
-		foreach($orders as $order){
+		foreach(Order::activePlan()->where('expired_at', '<=', date('Y-m-d H:i:s'))->with('user')->get() as $order){
 			// 清理全部流量,重置重置日期和等级 TODO 可用流量变动日志加入至UserObserver
 			$user = $order->user;
 			$user->update([

+ 2 - 1
app/Console/Commands/UserExpireAutoWarning.php

@@ -29,6 +29,7 @@ class UserExpireAutoWarning extends Command {
 	}
 
 	private function userExpireWarning(): void {
+		$expireDays = sysConfig('expire_days');
 		// 只取SSR没被禁用的用户,其他不用管
 		foreach(User::whereEnable(1)->get() as $user){
 			// 用户名不是邮箱的跳过
@@ -44,7 +45,7 @@ class UserExpireAutoWarning extends Command {
 
 				$logId = Helpers::addNotificationLog($title, $content, 1, $user->email);
 				Mail::to($user->email)->send(new userExpireWarningToday($logId));
-			}elseif($lastCanUseDays > 0 && $lastCanUseDays <= sysConfig('expire_days')){
+			}elseif($lastCanUseDays > 0 && $lastCanUseDays <= $expireDays){
 				$title = '账号过期提醒';
 				$content = '您的账号还剩'.$lastCanUseDays.'天即将过期。';
 

+ 5 - 6
app/Console/Commands/UserTrafficAbnormalAutoWarning.php

@@ -3,7 +3,6 @@
 namespace App\Console\Commands;
 
 use App\Components\PushNotification;
-use App\Models\User;
 use App\Models\UserHourlyDataFlow;
 use Illuminate\Console\Command;
 use Log;
@@ -27,18 +26,18 @@ class UserTrafficAbnormalAutoWarning extends Command {
 	// 用户流量异常警告
 	private function userTrafficAbnormalWarning(): void {
 		// 1小时内流量异常用户(多往前取5分钟,防止数据统计任务执行时间过长导致没有数据)
-		$userTotalTrafficList = UserHourlyDataFlow::whereNodeId(0)
+		$userTotalTrafficLogs = UserHourlyDataFlow::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以上的记录,加快查询速度
+		$trafficBanValue = sysConfig('traffic_ban_value');
 
-		foreach($userTotalTrafficList as $vo){
-			$user = User::find($vo->user_id);
-
+		foreach($userTotalTrafficLogs->load('user') as $log){
 			// 推送通知管理员
-			if($vo->totalTraffic > sysConfig('traffic_ban_value') * GB){
+			if($log->totalTraffic > $trafficBanValue * GB){
+				$user = $log->user;
 				$traffic = UserHourlyDataFlow::userRecentUsed($user->id)
 				                             ->selectRaw("user_id, sum(`u`) as totalU, sum(`d`) as totalD, sum(total) as totalTraffic")
 				                             ->first();

+ 2 - 1
app/Console/Commands/UserTrafficAutoWarning.php

@@ -29,6 +29,7 @@ class UserTrafficAutoWarning extends Command {
 
 	// 用户流量超过警告阈值自动发邮件提醒
 	private function userTrafficWarning(): void {
+		$trafficWarningPercent = sysConfig('traffic_warning_percent');
 		foreach(User::activeUser()->where('transfer_enable', '>', 0)->get() as $user){
 			// 用户名不是邮箱的跳过
 			if(false === filter_var($user->email, FILTER_VALIDATE_EMAIL)){
@@ -36,7 +37,7 @@ class UserTrafficAutoWarning extends Command {
 			}
 
 			$usedPercent = round(($user->d + $user->u) / $user->transfer_enable, 2) * 100; // 已使用流量百分比
-			if($usedPercent >= sysConfig('traffic_warning_percent')){
+			if($usedPercent >= $trafficWarningPercent){
 				$logId = Helpers::addNotificationLog("流量提醒", '流量已使用:'.$usedPercent.'%,请保持关注。', 1, $user->email);
 				Mail::to($user->email)->send(new userTrafficWarning($logId, $usedPercent));
 			}

+ 1 - 1
app/Http/Controllers/AdminController.php

@@ -66,7 +66,7 @@ class AdminController extends Controller {
 		$view['totalUserCount'] = User::count(); // 总用户数
 		$view['enableUserCount'] = User::whereEnable(1)->count(); // 有效用户数
 		$view['activeUserCount'] = User::where('t', '>=', $past)->count(); // 活跃用户数
-		$view['unActiveUserCount'] = User::whereBetween('t', [1, $past])->whereEnable(1)->count(); // 不活跃用户数
+		$view['unActiveUserCount'] = User::whereEnable(1)->whereBetween('t', [1, $past])->count(); // 不活跃用户数
 		$view['onlineUserCount'] = User::where('t', '>=', strtotime("-10 minutes"))->count(); // 10分钟内在线用户数
 		$view['expireWarningUserCount'] = User::whereBetween('expired_at', [
 			date('Y-m-d'),

+ 1 - 1
app/Services/UserService.php

@@ -18,7 +18,7 @@ class UserService {
 	}
 
 	// 添加用户余额
-	public function updateCredit(int $credit): bool {
+	public function updateCredit(float $credit): bool {
 		self::$user->credit += $credit;
 		return self::$user->credit >= 0 && self::$user->save();
 	}