From 7be655339607f2e1aa147bff7d52ef8ea4d384ca Mon Sep 17 00:00:00 2001 From: v2board Date: Sat, 15 Apr 2023 19:34:54 +0800 Subject: [PATCH] update: statistical service --- app/Console/Commands/SendRemindMail.php | 1 + app/Console/Commands/V2boardStatistics.php | 28 ++++++++++ .../Server/DeepbworkController.php | 6 +++ .../Server/ShadowsocksTidalabController.php | 6 +++ .../Server/TrojanTidalabController.php | 6 +++ .../Controllers/Server/UniProxyController.php | 4 ++ app/Jobs/TrafficFetchJob.php | 6 +-- app/Services/OrderService.php | 6 ++- app/Services/StatisticalService.php | 53 +++++++++++++++++++ app/Services/UserService.php | 1 - 10 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 app/Services/StatisticalService.php diff --git a/app/Console/Commands/SendRemindMail.php b/app/Console/Commands/SendRemindMail.php index 048c2b19..8a069fba 100644 --- a/app/Console/Commands/SendRemindMail.php +++ b/app/Console/Commands/SendRemindMail.php @@ -45,6 +45,7 @@ class SendRemindMail extends Command $mailService = new MailService(); foreach ($users as $user) { if ($user->remind_expire) $mailService->remindExpire($user); + if ($user->remind_traffic) $mailService->remindTraffic($user); } } } diff --git a/app/Console/Commands/V2boardStatistics.php b/app/Console/Commands/V2boardStatistics.php index 3778f32c..0e77d1cf 100644 --- a/app/Console/Commands/V2boardStatistics.php +++ b/app/Console/Commands/V2boardStatistics.php @@ -2,10 +2,13 @@ namespace App\Console\Commands; +use App\Models\StatUser; +use App\Services\StatisticalService; use Illuminate\Console\Command; use App\Models\Order; use App\Models\StatOrder; use App\Models\CommissionLog; +use Illuminate\Support\Facades\DB; class V2boardStatistics extends Command { @@ -44,6 +47,31 @@ class V2boardStatistics extends Command $this->statOrder(); } + private function statUser() + { + $createdAt = time(); + $recordAt = strtotime('-1 day', strtotime(date('Y-m-d'))); + $statService = new StatisticalService($recordAt); + $stats = $statService->getStatUser(); + DB::beginTransaction(); + foreach ($stats as $stat) { + if (!StatUser::insert([ + 'user_id' => $stat['user_Id'], + 'u' => $stat['u'], + 'd' => $stat['d'], + 'server_rate' => $stat['server_rate'], + 'created_at' => $createdAt, + 'updated_at' => $createdAt, + 'record_type' => 'd', + 'record_at' => $recordAt + ])) { + DB::rollback(); + break; + } + } + DB::commit(); + } + private function statOrder() { $endAt = strtotime(date('Y-m-d')); diff --git a/app/Http/Controllers/Server/DeepbworkController.php b/app/Http/Controllers/Server/DeepbworkController.php index 5db797e7..99839fa9 100644 --- a/app/Http/Controllers/Server/DeepbworkController.php +++ b/app/Http/Controllers/Server/DeepbworkController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Server; use App\Services\ServerService; +use App\Services\StatisticalService; use App\Services\UserService; use App\Utils\CacheKey; use Illuminate\Http\Request; @@ -81,12 +82,17 @@ 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]; } + $statService = new StatisticalService(); + $statService->statUser($server['rate'], $statData); + return response([ 'ret' => 1, 'msg' => 'ok' diff --git a/app/Http/Controllers/Server/ShadowsocksTidalabController.php b/app/Http/Controllers/Server/ShadowsocksTidalabController.php index 01b6cfac..a575637d 100644 --- a/app/Http/Controllers/Server/ShadowsocksTidalabController.php +++ b/app/Http/Controllers/Server/ShadowsocksTidalabController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers\Server; use App\Models\ServerShadowsocks; use App\Services\ServerService; +use App\Services\StatisticalService; use App\Services\UserService; use App\Utils\CacheKey; use Illuminate\Http\Request; @@ -73,12 +74,17 @@ 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]; } + $statService = new StatisticalService(); + $statService->statUser($server['rate'], $statData); + return response([ 'ret' => 1, 'msg' => 'ok' diff --git a/app/Http/Controllers/Server/TrojanTidalabController.php b/app/Http/Controllers/Server/TrojanTidalabController.php index b4357eb8..2276917b 100644 --- a/app/Http/Controllers/Server/TrojanTidalabController.php +++ b/app/Http/Controllers/Server/TrojanTidalabController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Server; use App\Services\ServerService; +use App\Services\StatisticalService; use App\Services\UserService; use App\Utils\CacheKey; use Illuminate\Http\Request; @@ -78,12 +79,17 @@ 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 = []; 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]; } + $statService = new StatisticalService(); + $statService->statUser($server['rate'], $statData); + return response([ 'ret' => 1, 'msg' => 'ok' diff --git a/app/Http/Controllers/Server/UniProxyController.php b/app/Http/Controllers/Server/UniProxyController.php index d9bfed99..3dfb10dd 100644 --- a/app/Http/Controllers/Server/UniProxyController.php +++ b/app/Http/Controllers/Server/UniProxyController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Server; use App\Services\ServerService; +use App\Services\StatisticalService; use App\Services\UserService; use App\Utils\CacheKey; use App\Utils\Helper; @@ -69,6 +70,9 @@ class UniProxyController extends Controller $userService->trafficFetch($u, $d, $k, $this->nodeInfo->toArray(), $this->nodeType); } + $statService = new StatisticalService(); + $statService->statUser($this->nodeInfo->rate, $data); + return response([ 'data' => true ]); diff --git a/app/Jobs/TrafficFetchJob.php b/app/Jobs/TrafficFetchJob.php index 2d2e077c..fd848d89 100644 --- a/app/Jobs/TrafficFetchJob.php +++ b/app/Jobs/TrafficFetchJob.php @@ -50,8 +50,8 @@ class TrafficFetchJob implements ShouldQueue $user->t = time(); $user->u = $user->u + ($this->u * $this->server['rate']); $user->d = $user->d + ($this->d * $this->server['rate']); - if (!$user->save()) throw new \Exception('流量更新失败'); - $mailService = new MailService(); - $mailService->remindTraffic($user); + if (!$user->save()) { + info("流量更新失败\n未记录用户ID:{$this->userId}\n未记录上行:{$user->u}\n未记录下行:{$user->d}"); + } } } diff --git a/app/Services/OrderService.php b/app/Services/OrderService.php index e79bfb8c..0133222e 100644 --- a/app/Services/OrderService.php +++ b/app/Services/OrderService.php @@ -223,7 +223,11 @@ class OrderService $order->paid_at = time(); $order->callback_no = $callbackNo; if (!$order->save()) return false; - OrderHandleJob::dispatch($order->trade_no); + try { + OrderHandleJob::dispatchNow($order->trade_no); + } catch (\Exception $e) { + return false; + } return true; } diff --git a/app/Services/StatisticalService.php b/app/Services/StatisticalService.php new file mode 100644 index 00000000..f8691c83 --- /dev/null +++ b/app/Services/StatisticalService.php @@ -0,0 +1,53 @@ +recordAt = $recordAt ?? strtotime(date('Y-m-d')); + $this->userStats = Cache::get("stat_user_{$this->recordAt}"); + $this->userStats = json_decode($this->userStats, true) ?? []; + if (!is_array($this->userStats)) { + $this->userStats = []; + } + } + + public function statUser($rate, $data) + { + 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]; + } + } + Cache::set("stat_user_{$this->recordAt}", json_encode($this->userStats)); + } + + + public function getStatUser() + { + $stats = []; + foreach ($this->userStats as $k => $v) { + foreach (array_keys($v) as $userId) { + if (isset($v[$userId])) { + $stats[] = [ + 'server_rate' => $k, + 'u' => $v[$userId][0], + 'd' => $v[$userId][1], + 'user_id' => $userId + ]; + } + } + } + return $stats; + } +} diff --git a/app/Services/UserService.php b/app/Services/UserService.php index e3631579..e3da24d1 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -172,6 +172,5 @@ class UserService { TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol); StatServerJob::dispatch($u, $d, $server, $protocol, 'd'); - StatUserJob::dispatch($u, $d, $userId, $server, $protocol, 'd'); } }