Browse Source

update: stat command

v2board 2 years ago
parent
commit
32eaf301fe
1 changed files with 34 additions and 33 deletions
  1. 34 33
      app/Console/Commands/V2boardStatistics.php

+ 34 - 33
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);
     }
 }