Browse Source

update: fix statistics

v2board 2 years ago
parent
commit
4ba6edc328

+ 4 - 10
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_ONLINE_USER', $server->id), count($data), 3600);
         Cache::put(CacheKey::get('SERVER_VMESS_LAST_PUSH_AT', $server->id), time(), 3600);
         Cache::put(CacheKey::get('SERVER_VMESS_LAST_PUSH_AT', $server->id), time(), 3600);
         $userService = new UserService();
         $userService = new UserService();
-        $statData = [];
+        $formatData = [];
+
         foreach ($data as $item) {
         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[$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(), 'vmess', $formatData);
 
 
         return response([
         return response([
             'ret' => 1,
             'ret' => 1,

+ 4 - 10
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_ONLINE_USER', $server->id), count($data), 3600);
         Cache::put(CacheKey::get('SERVER_SHADOWSOCKS_LAST_PUSH_AT', $server->id), time(), 3600);
         Cache::put(CacheKey::get('SERVER_SHADOWSOCKS_LAST_PUSH_AT', $server->id), time(), 3600);
         $userService = new UserService();
         $userService = new UserService();
-        $statData = [];
+        $formatData = [];
+
         foreach ($data as $item) {
         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[$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(), 'shadowsocks', $formatData);
 
 
         return response([
         return response([
             'ret' => 1,
             'ret' => 1,

+ 3 - 10
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_ONLINE_USER', $server->id), count($data), 3600);
         Cache::put(CacheKey::get('SERVER_TROJAN_LAST_PUSH_AT', $server->id), time(), 3600);
         Cache::put(CacheKey::get('SERVER_TROJAN_LAST_PUSH_AT', $server->id), time(), 3600);
         $userService = new UserService();
         $userService = new UserService();
-        $statData = [];
+        $formatData = [];
         foreach ($data as $item) {
         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([
         return response([
             'ret' => 1,
             'ret' => 1,

+ 1 - 12
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) . '_ONLINE_USER', $this->nodeInfo->id), count($data), 3600);
         Cache::put(CacheKey::get('SERVER_' . strtoupper($this->nodeType) . '_LAST_PUSH_AT', $this->nodeInfo->id), time(), 3600);
         Cache::put(CacheKey::get('SERVER_' . strtoupper($this->nodeType) . '_LAST_PUSH_AT', $this->nodeInfo->id), time(), 3600);
         $userService = new UserService();
         $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([
         return response([
             'data' => true
             'data' => true

+ 14 - 18
app/Services/StatisticalService.php

@@ -85,30 +85,26 @@ class StatisticalService {
         return $data;
         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));
         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));
         Cache::set("stat_user_{$this->startAt}", json_encode($this->userStats));
     }
     }

+ 12 - 2
app/Services/UserService.php

@@ -168,8 +168,18 @@ class UserService
         return true;
         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);
+        }
     }
     }
 }
 }