update: fix statistics

This commit is contained in:
v2board 2023-05-17 11:28:59 +08:00
parent c42097e92f
commit 4ba6edc328
6 changed files with 40 additions and 64 deletions

View File

@ -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) {
$u = $item['u'];
$d = $item['d'];
$userService->trafficFetch($u, $d, $item['user_id'], $server->toArray(), 'vmess');
$statData[$item['user_id']] = [$u, $d];
}
$statService = new StatisticalService(); foreach ($data as $item) {
$statService->setStartAt(strtotime(date('Y-m-d'))); $formatData[$item['user_id']] = [$item['u'], $item['d']];
$statService->setUserStats(); }
$statService->statUser($server['rate'], $statData); $userService->trafficFetch($server->toArray(), 'vmess', $formatData);
return response([ return response([
'ret' => 1, 'ret' => 1,

View File

@ -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) {
$u = $item['u'];
$d = $item['d'];
$userService->trafficFetch($u, $d, $item['user_id'], $server->toArray(), 'shadowsocks');
$statData[$item['user_id']] = [$u, $d];
}
$statService = new StatisticalService(); foreach ($data as $item) {
$statService->setStartAt(strtotime(date('Y-m-d'))); $formatData[$item['user_id']] = [$item['u'], $item['d']];
$statService->setUserStats(); }
$statService->statUser($server['rate'], $statData); $userService->trafficFetch($server->toArray(), 'shadowsocks', $formatData);
return response([ return response([
'ret' => 1, 'ret' => 1,

View File

@ -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']; $formatData[$item['user_id']] = [$item['u'], $item['d']];
$d = $item['d'];
$userService->trafficFetch($u, $d, $item['user_id'], $server->toArray(), 'trojan');
$statData[$item['user_id']] = [$u, $d];
} }
$userService->trafficFetch($server->toArray(), 'trojan', $formatData);
$statService = new StatisticalService();
$statService->setStartAt(strtotime(date('Y-m-d')));
$statService->setUserStats();
$statService->statUser($server['rate'], $statData);
return response([ return response([
'ret' => 1, 'ret' => 1,

View File

@ -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) { $userService->trafficFetch($this->nodeInfo->toArray(), $this->nodeType, $data);
$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);
return response([ return response([
'data' => true 'data' => true

View File

@ -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] ?? []; $this->serverStats[$serverType] = $this->serverStats[$serverType] ?? [];
if (isset($this->serverStats[$serverType][$serverId])) { if (isset($this->serverStats[$serverType][$serverId])) {
$this->serverStats[$serverType][$serverId][0] += $data[$key][0]; $this->serverStats[$serverType][$serverId][0] += $u;
$this->serverStats[$serverType][$serverId][1] += $data[$key][1]; $this->serverStats[$serverType][$serverId][1] += $d;
} else { } else {
$this->serverStats[$serverType][$serverId] = $data[$key]; $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] ?? []; $this->userStats[$rate] = $this->userStats[$rate] ?? [];
if (isset($this->userStats[$rate][$key])) { if (isset($this->userStats[$rate][$userId])) {
$this->userStats[$rate][$key][0] += $data[$key][0]; $this->userStats[$rate][$userId][0] += $u;
$this->userStats[$rate][$key][1] += $data[$key][1]; $this->userStats[$rate][$userId][1] += $d;
} else { } else {
$this->userStats[$rate][$key] = $data[$key]; $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));
} }

View File

@ -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)
{ {
$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); TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol);
$statService->statServer($server['id'], $protocol, $u, $d);
$statService->statUser($server['rate'], $userId, $u, $d);
}
} }
} }