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

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