update: stat command

This commit is contained in:
v2board 2023-06-05 14:33:58 +08:00
parent 948177f22e
commit 32eaf301fe

View File

@ -4,12 +4,9 @@ namespace App\Console\Commands;
use App\Models\StatServer; use App\Models\StatServer;
use App\Models\StatUser; use App\Models\StatUser;
use App\Models\User;
use App\Services\StatisticalService; use App\Services\StatisticalService;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use App\Models\Order;
use App\Models\Stat; use App\Models\Stat;
use App\Models\CommissionLog;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
class V2boardStatistics extends Command class V2boardStatistics extends Command
@ -50,19 +47,19 @@ class V2boardStatistics extends Command
$this->statUser(); $this->statUser();
$this->statServer(); $this->statServer();
$this->stat(); $this->stat();
$this->info('耗时' . (microtime(true) - $startAt)); info('统计任务执行完毕。耗时:' . (microtime(true) - $startAt) / 1000);
} }
private function statServer() 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 { try {
DB::beginTransaction(); 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) { foreach ($stats as $stat) {
if (!StatServer::insert([ if (!StatServer::insert([
'server_id' => $stat['server_id'], 'server_id' => $stat['server_id'],
@ -81,20 +78,20 @@ class V2boardStatistics extends Command
$statService->clearStatServer(); $statService->clearStatServer();
} catch (\Exception $e) { } catch (\Exception $e) {
DB::rollback(); DB::rollback();
$this->error($e->getMessage()); \Log::error($e->getMessage(), ['exception' => $e]);
} }
} }
private function statUser() 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 { try {
DB::beginTransaction(); 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) { foreach ($stats as $stat) {
if (!StatUser::insert([ if (!StatUser::insert([
'user_id' => $stat['user_id'], 'user_id' => $stat['user_id'],
@ -113,27 +110,31 @@ class V2boardStatistics extends Command
$statService->clearStatUser(); $statService->clearStatUser();
} catch (\Exception $e) { } catch (\Exception $e) {
DB::rollback(); DB::rollback();
$this->error($e->getMessage()); \Log::error($e->getMessage(), ['exception' => $e]);
} }
} }
private function stat() private function stat()
{ {
$endAt = strtotime(date('Y-m-d')); try {
$startAt = strtotime('-1 day', $endAt); $endAt = strtotime(date('Y-m-d'));
$statisticalService = new StatisticalService(); $startAt = strtotime('-1 day', $endAt);
$statisticalService->setStartAt($startAt); $statisticalService = new StatisticalService();
$statisticalService->setEndAt($endAt); $statisticalService->setStartAt($startAt);
$data = $statisticalService->generateStatData(); $statisticalService->setEndAt($endAt);
$data['record_at'] = $startAt; $data = $statisticalService->generateStatData();
$data['record_type'] = 'd'; $data['record_at'] = $startAt;
$statistic = Stat::where('record_at', $startAt) $data['record_type'] = 'd';
->where('record_type', 'd') $statistic = Stat::where('record_at', $startAt)
->first(); ->where('record_type', 'd')
if ($statistic) { ->first();
$statistic->update($data); if ($statistic) {
return; $statistic->update($data);
return;
}
Stat::create($data);
} catch (\Exception $e) {
\Log::error($e->getMessage(), ['exception' => $e]);
} }
Stat::create($data);
} }
} }