diff --git a/app/Console/Commands/V2boardStatistics.php b/app/Console/Commands/V2boardStatistics.php deleted file mode 100644 index e1d4bdad..00000000 --- a/app/Console/Commands/V2boardStatistics.php +++ /dev/null @@ -1,140 +0,0 @@ -statUser(); - $this->statServer(); - $this->stat(); - info('统计任务执行完毕。耗时:' . (microtime(true) - $startAt) / 1000); - } - - private function statServer() - { - try { - DB::beginTransaction(); - $createdAt = time(); - $recordAt = strtotime('-1 day', strtotime(date('Y-m-d'))); - $statService = new StatisticalService(); - $statService->setStartAt($recordAt); - $statService->setServerStats(); - $stats = $statService->getStatServer(); - foreach ($stats as $stat) { - if (!StatServer::insert([ - 'server_id' => $stat['server_id'], - 'server_type' => $stat['server_type'], - 'u' => $stat['u'], - 'd' => $stat['d'], - 'created_at' => $createdAt, - 'updated_at' => $createdAt, - 'record_type' => 'd', - 'record_at' => $recordAt - ])) { - throw new \Exception('stat server fail'); - } - } - DB::commit(); - $statService->clearStatServer(); - } catch (\Exception $e) { - DB::rollback(); - \Log::error($e->getMessage(), ['exception' => $e]); - } - } - - private function statUser() - { - try { - DB::beginTransaction(); - $createdAt = time(); - $recordAt = strtotime('-1 day', strtotime(date('Y-m-d'))); - $statService = new StatisticalService(); - $statService->setStartAt($recordAt); - $statService->setUserStats(); - $stats = $statService->getStatUser(); - 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 - ])) { - throw new \Exception('stat user fail'); - } - } - DB::commit(); - $statService->clearStatUser(); - } catch (\Exception $e) { - DB::rollback(); - \Log::error($e->getMessage(), ['exception' => $e]); - } - } - - private function stat() - { - try { - $endAt = strtotime(date('Y-m-d')); - $startAt = strtotime('-1 day', $endAt); - $statisticalService = new StatisticalService(); - $statisticalService->setStartAt($startAt); - $statisticalService->setEndAt($endAt); - $data = $statisticalService->generateStatData(); - $data['record_at'] = $startAt; - $data['record_type'] = 'd'; - $statistic = Stat::where('record_at', $startAt) - ->where('record_type', 'd') - ->first(); - if ($statistic) { - $statistic->update($data); - return; - } - Stat::create($data); - } catch (\Exception $e) { - \Log::error($e->getMessage(), ['exception' => $e]); - } - } -} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index abd55171..a23b7e21 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -28,7 +28,7 @@ class Kernel extends ConsoleKernel { Cache::put(CacheKey::get('SCHEDULE_LAST_CHECK_AT', null), time()); // v2board - $schedule->command('v2board:statistics')->dailyAt('0:10'); + //$schedule->command('v2board:statistics')->dailyAt('0:10'); // check $schedule->command('check:order')->everyMinute(); $schedule->command('check:commission')->everyMinute(); diff --git a/app/Jobs/StatServerJob.php b/app/Jobs/StatServerJob.php new file mode 100644 index 00000000..93a42acd --- /dev/null +++ b/app/Jobs/StatServerJob.php @@ -0,0 +1,78 @@ +onQueue('stat'); + $this->u = $u; + $this->d = $d; + $this->server = $server; + $this->protocol = $protocol; + $this->recordType = $recordType; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + $recordAt = strtotime(date('Y-m-d')); + if ($this->recordType === 'm') { + // + } + + $data = StatServer::lockForUpdate() + ->where('record_at', $recordAt) + ->where('server_id', $this->server['id']) + ->where('server_type', $this->protocol) + ->first(); + if ($data) { + try { + $data->update([ + 'u' => $data['u'] + $this->u, + 'd' => $data['d'] + $this->d + ]); + } catch (\Exception $e) { + abort(500, '节点统计数据更新失败'); + } + } else { + if (!StatServer::create([ + 'server_id' => $this->server['id'], + 'server_type' => $this->protocol, + 'u' => $this->u, + 'd' => $this->d, + 'record_type' => $this->recordType, + 'record_at' => $recordAt + ])) { + abort(500, '节点统计数据创建失败'); + } + } + } +} diff --git a/app/Jobs/StatUserJob.php b/app/Jobs/StatUserJob.php new file mode 100644 index 00000000..0d1dd5b0 --- /dev/null +++ b/app/Jobs/StatUserJob.php @@ -0,0 +1,80 @@ +onQueue('stat'); + $this->u = $u; + $this->d = $d; + $this->userId = $userId; + $this->server = $server; + $this->protocol = $protocol; + $this->recordType = $recordType; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + $recordAt = strtotime(date('Y-m-d')); + if ($this->recordType === 'm') { + // + } + + $data = StatUser::where('record_at', $recordAt) + ->where('server_rate', $this->server['rate']) + ->where('user_id', $this->userId) + ->first(); + if ($data) { + try { + $data->update([ + 'u' => $data['u'] + ($this->u * $this->server['rate']), + 'd' => $data['d'] + ($this->d * $this->server['rate']) + ]); + } catch (\Exception $e) { + abort(500, '用户统计数据更新失败'); + } + } else { + if (!StatUser::create([ + 'user_id' => $this->userId, + 'server_rate' => $this->server['rate'], + 'u' => $this->u, + 'd' => $this->d, + 'record_type' => $this->recordType, + 'record_at' => $recordAt + ])) { + abort(500, '用户统计数据创建失败'); + } + } + } +} diff --git a/app/Services/StatisticalService.php b/app/Services/StatisticalService.php index 328f2045..1ebff0b7 100644 --- a/app/Services/StatisticalService.php +++ b/app/Services/StatisticalService.php @@ -30,7 +30,7 @@ class StatisticalService { } public function setServerStats() { - $this->serverStats = Cache::store('file')->get("stat_server_{$this->startAt}"); + $this->serverStats = Cache::get("stat_server_{$this->startAt}"); $this->serverStats = json_decode($this->serverStats, true) ?? []; if (!is_array($this->serverStats)) { $this->serverStats = []; @@ -38,7 +38,7 @@ class StatisticalService { } public function setUserStats() { - $this->userStats = Cache::store('file')->get("stat_user_{$this->startAt}"); + $this->userStats = Cache::get("stat_user_{$this->startAt}"); $this->userStats = json_decode($this->userStats, true) ?? []; if (!is_array($this->userStats)) { $this->userStats = []; @@ -95,7 +95,7 @@ class StatisticalService { } else { $this->serverStats[$serverType][$serverId] = [$u, $d]; } - Cache::store('file')->set("stat_server_{$this->startAt}", json_encode($this->serverStats)); + Cache::put("stat_server_{$this->startAt}", json_encode($this->serverStats), 6000); } public function statUser($rate, $userId, $u, $d) @@ -107,7 +107,7 @@ class StatisticalService { } else { $this->userStats[$rate][$userId] = [$u, $d]; } - Cache::store('file')->set("stat_user_{$this->startAt}", json_encode($this->userStats)); + Cache::put("stat_user_{$this->startAt}", json_encode($this->userStats), 6000); } public function getStatUserByUserID($userId): array @@ -165,12 +165,12 @@ class StatisticalService { public function clearStatUser() { - Cache::store('file')->forget("stat_user_{$this->startAt}"); + Cache::forget("stat_user_{$this->startAt}"); } public function clearStatServer() { - Cache::store('file')->forget("stat_server_{$this->startAt}"); + Cache::forget("stat_server_{$this->startAt}"); } public function getStatRecord($type) diff --git a/app/Services/UserService.php b/app/Services/UserService.php index ba82c918..466e27c5 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -170,16 +170,18 @@ class UserService public function trafficFetch(array $server, string $protocol, array $data) { - $statService = new StatisticalService(); - $statService->setStartAt(strtotime(date('Y-m-d'))); - $statService->setUserStats(); - $statService->setServerStats(); + //$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]; + StatServerJob::dispatch($u, $d, $server, $protocol, 'd'); + StatUserJob::dispatch($u, $d, $userId, $server, $protocol, 'd'); TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol); - $statService->statServer($server['id'], $protocol, $u, $d); - $statService->statUser($server['rate'], $userId, $u, $d); + //$statService->statServer($server['id'], $protocol, $u, $d); + //$statService->statUser($server['rate'], $userId, $u, $d); } } } diff --git a/config/horizon.php b/config/horizon.php index 4ad9e24a..9559a76b 100644 --- a/config/horizon.php +++ b/config/horizon.php @@ -175,6 +175,7 @@ return [ 'queue' => [ 'order_handle', 'traffic_fetch', + 'stat', 'send_email', 'send_email_mass', 'send_telegram',