From 32eaf301fefa8094098d04bdd39c9494bf2a9a24 Mon Sep 17 00:00:00 2001 From: v2board Date: Mon, 5 Jun 2023 14:33:58 +0800 Subject: [PATCH] update: stat command --- app/Console/Commands/V2boardStatistics.php | 67 +++++++++++----------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/app/Console/Commands/V2boardStatistics.php b/app/Console/Commands/V2boardStatistics.php index 9f26c0ee..e1d4bdad 100644 --- a/app/Console/Commands/V2boardStatistics.php +++ b/app/Console/Commands/V2boardStatistics.php @@ -4,12 +4,9 @@ namespace App\Console\Commands; use App\Models\StatServer; use App\Models\StatUser; -use App\Models\User; use App\Services\StatisticalService; use Illuminate\Console\Command; -use App\Models\Order; use App\Models\Stat; -use App\Models\CommissionLog; use Illuminate\Support\Facades\DB; class V2boardStatistics extends Command @@ -50,19 +47,19 @@ class V2boardStatistics extends Command $this->statUser(); $this->statServer(); $this->stat(); - $this->info('耗时' . (microtime(true) - $startAt)); + info('统计任务执行完毕。耗时:' . (microtime(true) - $startAt) / 1000); } private function statServer() { - $createdAt = time(); - $recordAt = strtotime('-1 day', strtotime(date('Y-m-d'))); - $statService = new StatisticalService(); - $statService->setStartAt($recordAt); - $statService->setServerStats(); - $stats = $statService->getStatServer(); 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'], @@ -81,20 +78,20 @@ class V2boardStatistics extends Command $statService->clearStatServer(); } catch (\Exception $e) { DB::rollback(); - $this->error($e->getMessage()); + \Log::error($e->getMessage(), ['exception' => $e]); } } private function statUser() { - $createdAt = time(); - $recordAt = strtotime('-1 day', strtotime(date('Y-m-d'))); - $statService = new StatisticalService(); - $statService->setStartAt($recordAt); - $statService->setUserStats(); - $stats = $statService->getStatUser(); 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'], @@ -113,27 +110,31 @@ class V2boardStatistics extends Command $statService->clearStatUser(); } catch (\Exception $e) { DB::rollback(); - $this->error($e->getMessage()); + \Log::error($e->getMessage(), ['exception' => $e]); } } private function stat() { - $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; + 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]); } - Stat::create($data); } }