From 4ba6edc328ed29ebf87ce4be5704388252337596 Mon Sep 17 00:00:00 2001 From: v2board Date: Wed, 17 May 2023 11:28:59 +0800 Subject: [PATCH] update: fix statistics --- .../Server/DeepbworkController.php | 16 +++------- .../Server/ShadowsocksTidalabController.php | 16 +++------- .../Server/TrojanTidalabController.php | 13 ++------ .../Controllers/Server/UniProxyController.php | 13 +------- app/Services/StatisticalService.php | 32 ++++++++----------- app/Services/UserService.php | 14 ++++++-- 6 files changed, 40 insertions(+), 64 deletions(-) diff --git a/app/Http/Controllers/Server/DeepbworkController.php b/app/Http/Controllers/Server/DeepbworkController.php index c35268c2..ae4b537e 100644 --- a/app/Http/Controllers/Server/DeepbworkController.php +++ b/app/Http/Controllers/Server/DeepbworkController.php @@ -82,18 +82,12 @@ class DeepbworkController extends Controller Cache::put(CacheKey::get('SERVER_VMESS_ONLINE_USER', $server->id), count($data), 3600); Cache::put(CacheKey::get('SERVER_VMESS_LAST_PUSH_AT', $server->id), time(), 3600); $userService = new UserService(); - $statData = []; - foreach ($data as $item) { - $u = $item['u']; - $d = $item['d']; - $userService->trafficFetch($u, $d, $item['user_id'], $server->toArray(), 'vmess'); - $statData[$item['user_id']] = [$u, $d]; - } + $formatData = []; - $statService = new StatisticalService(); - $statService->setStartAt(strtotime(date('Y-m-d'))); - $statService->setUserStats(); - $statService->statUser($server['rate'], $statData); + foreach ($data as $item) { + $formatData[$item['user_id']] = [$item['u'], $item['d']]; + } + $userService->trafficFetch($server->toArray(), 'vmess', $formatData); return response([ 'ret' => 1, diff --git a/app/Http/Controllers/Server/ShadowsocksTidalabController.php b/app/Http/Controllers/Server/ShadowsocksTidalabController.php index a3cf0016..43d6e544 100644 --- a/app/Http/Controllers/Server/ShadowsocksTidalabController.php +++ b/app/Http/Controllers/Server/ShadowsocksTidalabController.php @@ -74,18 +74,12 @@ class ShadowsocksTidalabController extends Controller Cache::put(CacheKey::get('SERVER_SHADOWSOCKS_ONLINE_USER', $server->id), count($data), 3600); Cache::put(CacheKey::get('SERVER_SHADOWSOCKS_LAST_PUSH_AT', $server->id), time(), 3600); $userService = new UserService(); - $statData = []; - foreach ($data as $item) { - $u = $item['u']; - $d = $item['d']; - $userService->trafficFetch($u, $d, $item['user_id'], $server->toArray(), 'shadowsocks'); - $statData[$item['user_id']] = [$u, $d]; - } + $formatData = []; - $statService = new StatisticalService(); - $statService->setStartAt(strtotime(date('Y-m-d'))); - $statService->setUserStats(); - $statService->statUser($server['rate'], $statData); + foreach ($data as $item) { + $formatData[$item['user_id']] = [$item['u'], $item['d']]; + } + $userService->trafficFetch($server->toArray(), 'shadowsocks', $formatData); return response([ 'ret' => 1, diff --git a/app/Http/Controllers/Server/TrojanTidalabController.php b/app/Http/Controllers/Server/TrojanTidalabController.php index b5942f2d..b47f57b5 100644 --- a/app/Http/Controllers/Server/TrojanTidalabController.php +++ b/app/Http/Controllers/Server/TrojanTidalabController.php @@ -79,18 +79,11 @@ class TrojanTidalabController extends Controller Cache::put(CacheKey::get('SERVER_TROJAN_ONLINE_USER', $server->id), count($data), 3600); Cache::put(CacheKey::get('SERVER_TROJAN_LAST_PUSH_AT', $server->id), time(), 3600); $userService = new UserService(); - $statData = []; + $formatData = []; foreach ($data as $item) { - $u = $item['u']; - $d = $item['d']; - $userService->trafficFetch($u, $d, $item['user_id'], $server->toArray(), 'trojan'); - $statData[$item['user_id']] = [$u, $d]; + $formatData[$item['user_id']] = [$item['u'], $item['d']]; } - - $statService = new StatisticalService(); - $statService->setStartAt(strtotime(date('Y-m-d'))); - $statService->setUserStats(); - $statService->statUser($server['rate'], $statData); + $userService->trafficFetch($server->toArray(), 'trojan', $formatData); return response([ 'ret' => 1, diff --git a/app/Http/Controllers/Server/UniProxyController.php b/app/Http/Controllers/Server/UniProxyController.php index dda55321..3f54fbea 100644 --- a/app/Http/Controllers/Server/UniProxyController.php +++ b/app/Http/Controllers/Server/UniProxyController.php @@ -64,18 +64,7 @@ class UniProxyController extends Controller Cache::put(CacheKey::get('SERVER_' . strtoupper($this->nodeType) . '_ONLINE_USER', $this->nodeInfo->id), count($data), 3600); Cache::put(CacheKey::get('SERVER_' . strtoupper($this->nodeType) . '_LAST_PUSH_AT', $this->nodeInfo->id), time(), 3600); $userService = new UserService(); - foreach (array_keys($data) as $k) { - $u = $data[$k][0]; - $d = $data[$k][1]; - $userService->trafficFetch($u, $d, $k, $this->nodeInfo->toArray(), $this->nodeType); - } - - $statService = new StatisticalService(); - $statService->setStartAt(strtotime(date('Y-m-d'))); - $statService->setUserStats(); - $statService->statUser($this->nodeInfo->rate, $data); - $statService->setServerStats(); - $statService->statServer($this->nodeId, $this->nodeType, $data); + $userService->trafficFetch($this->nodeInfo->toArray(), $this->nodeType, $data); return response([ 'data' => true diff --git a/app/Services/StatisticalService.php b/app/Services/StatisticalService.php index 853600fc..4a4f0385 100644 --- a/app/Services/StatisticalService.php +++ b/app/Services/StatisticalService.php @@ -85,30 +85,26 @@ class StatisticalService { return $data; } - public function statServer($serverId, $serverType, $data) + public function statServer($serverId, $serverType, $u, $d) { - foreach (array_keys($data) as $key) { - $this->serverStats[$serverType] = $this->serverStats[$serverType] ?? []; - if (isset($this->serverStats[$serverType][$serverId])) { - $this->serverStats[$serverType][$serverId][0] += $data[$key][0]; - $this->serverStats[$serverType][$serverId][1] += $data[$key][1]; - } else { - $this->serverStats[$serverType][$serverId] = $data[$key]; - } + $this->serverStats[$serverType] = $this->serverStats[$serverType] ?? []; + if (isset($this->serverStats[$serverType][$serverId])) { + $this->serverStats[$serverType][$serverId][0] += $u; + $this->serverStats[$serverType][$serverId][1] += $d; + } else { + $this->serverStats[$serverType][$serverId] = [$u, $d]; } Cache::set("stat_server_{$this->startAt}", json_encode($this->serverStats)); } - public function statUser($rate, $data) + public function statUser($rate, $userId, $u, $d) { - foreach (array_keys($data) as $key) { - $this->userStats[$rate] = $this->userStats[$rate] ?? []; - if (isset($this->userStats[$rate][$key])) { - $this->userStats[$rate][$key][0] += $data[$key][0]; - $this->userStats[$rate][$key][1] += $data[$key][1]; - } else { - $this->userStats[$rate][$key] = $data[$key]; - } + $this->userStats[$rate] = $this->userStats[$rate] ?? []; + if (isset($this->userStats[$rate][$userId])) { + $this->userStats[$rate][$userId][0] += $u; + $this->userStats[$rate][$userId][1] += $d; + } else { + $this->userStats[$rate][$userId] = [$u, $d]; } Cache::set("stat_user_{$this->startAt}", json_encode($this->userStats)); } diff --git a/app/Services/UserService.php b/app/Services/UserService.php index af625dee..ba82c918 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -168,8 +168,18 @@ class UserService return true; } - public function trafficFetch(int $u, int $d, int $userId, array $server, string $protocol) + public function trafficFetch(array $server, string $protocol, array $data) { - TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol); + $statService = new StatisticalService(); + $statService->setStartAt(strtotime(date('Y-m-d'))); + $statService->setUserStats(); + $statService->setServerStats(); + foreach (array_keys($data) as $userId) { + $u = $data[$userId][0]; + $d = $data[$userId][1]; + TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol); + $statService->statServer($server['id'], $protocol, $u, $d); + $statService->statUser($server['rate'], $userId, $u, $d); + } } }