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_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,

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_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,

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_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,

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) . '_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

View File

@ -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));
}

View File

@ -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);
}
}
}