diff --git a/.env.example b/.env.example index 4f0fede7..c54041eb 100755 --- a/.env.example +++ b/.env.example @@ -14,7 +14,7 @@ DB_USERNAME=root DB_PASSWORD=123456 BROADCAST_DRIVER=log -CACHE_DRIVER=file +CACHE_DRIVER=redis QUEUE_CONNECTION=redis SESSION_DRIVER=redis SESSION_LIFETIME=120 diff --git a/app/Console/Commands/V2boardStatistics.php b/app/Console/Commands/V2boardStatistics.php index a455aafd..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,82 +47,94 @@ 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(); - DB::beginTransaction(); - 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 - ])) { - DB::rollback(); - throw new \Exception('stat server fail'); + 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]); } - DB::commit(); - $statService->clearStatServer(); } 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(); - 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(); - throw new \Exception('stat user fail'); + 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]); } - DB::commit(); - $statService->clearStatUser(); } 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); } } diff --git a/app/Http/Controllers/Admin/SystemController.php b/app/Http/Controllers/Admin/SystemController.php index cf33b84d..6aeb8a32 100644 --- a/app/Http/Controllers/Admin/SystemController.php +++ b/app/Http/Controllers/Admin/SystemController.php @@ -14,6 +14,7 @@ use Laravel\Horizon\Contracts\MetricsRepository; use Laravel\Horizon\Contracts\SupervisorRepository; use Laravel\Horizon\Contracts\WorkloadRepository; use Laravel\Horizon\WaitTimeCalculator; +use App\Models\Log as LogModel; class SystemController extends Controller { @@ -101,5 +102,18 @@ class SystemController extends Controller return $master->status === 'paused'; })->count(); } -} + public function getSystemLog(Request $request) { + $current = $request->input('current') ? $request->input('current') : 1; + $pageSize = $request->input('page_size') >= 10 ? $request->input('page_size') : 10; + $builder = LogModel::orderBy('created_at', 'DESC') + ->setFilterAllowKeys('level'); + $total = $builder->count(); + $res = $builder->forPage($current, $pageSize) + ->get(); + return response([ + 'data' => $res, + 'total' => $total + ]); + } +} diff --git a/app/Models/Log.php b/app/Models/Log.php index 606b2077..ffdbba2d 100644 --- a/app/Models/Log.php +++ b/app/Models/Log.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Model; class Log extends Model { + use \App\Scope\FilterScope; protected $table = 'v2_log'; protected $dateFormat = 'U'; protected $guarded = ['id']; diff --git a/app/Scope/FilterScope.php b/app/Scope/FilterScope.php new file mode 100644 index 00000000..fcb92a54 --- /dev/null +++ b/app/Scope/FilterScope.php @@ -0,0 +1,50 @@ +validate([ + 'filter.*.key' => "required|in:{$allowKeys}", + 'filter.*.condition' => 'required|in:in,is,not,like,lt,gt', + 'filter.*.value' => 'required' + ]); + $filters = $request->input('filter'); + if ($filters) { + foreach ($filters as $k => $filter) { + if ($filter['condition'] === 'in') { + $builder->whereIn($filter['key'], $filter['value']); + continue; + } + if ($filter['condition'] === 'is') { + $builder->where($filter['key'], $filter['value']); + continue; + } + if ($filter['condition'] === 'not') { + $builder->where($filter['key'], '!=', $filter['value']); + continue; + } + if ($filter['condition'] === 'gt') { + $builder->where($filter['key'], '>', $filter['value']); + continue; + } + if ($filter['condition'] === 'lt') { + $builder->where($filter['key'], '<', $filter['value']); + continue; + } + if ($filter['condition'] === 'like') { + $builder->where($filter['key'], 'like', "%{$filter['value']}%"); + continue; + } + } + } + return $builder; + } +} \ No newline at end of file diff --git a/app/Services/StatisticalService.php b/app/Services/StatisticalService.php index d403912f..328f2045 100644 --- a/app/Services/StatisticalService.php +++ b/app/Services/StatisticalService.php @@ -30,7 +30,7 @@ class StatisticalService { } public function setServerStats() { - $this->serverStats = Cache::get("stat_server_{$this->startAt}"); + $this->serverStats = Cache::store('file')->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::get("stat_user_{$this->startAt}"); + $this->userStats = Cache::store('file')->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::set("stat_server_{$this->startAt}", json_encode($this->serverStats)); + Cache::store('file')->set("stat_server_{$this->startAt}", json_encode($this->serverStats)); } public function statUser($rate, $userId, $u, $d) @@ -107,7 +107,7 @@ class StatisticalService { } else { $this->userStats[$rate][$userId] = [$u, $d]; } - Cache::set("stat_user_{$this->startAt}", json_encode($this->userStats)); + Cache::store('file')->set("stat_user_{$this->startAt}", json_encode($this->userStats)); } public function getStatUserByUserID($userId): array @@ -165,12 +165,12 @@ class StatisticalService { public function clearStatUser() { - Cache::forget("stat_user_{$this->startAt}"); + Cache::store('file')->forget("stat_user_{$this->startAt}"); } public function clearStatServer() { - Cache::forget("stat_server_{$this->startAt}"); + Cache::store('file')->forget("stat_server_{$this->startAt}"); } public function getStatRecord($type) diff --git a/config/app.php b/config/app.php index ce3decb4..619e564c 100755 --- a/config/app.php +++ b/config/app.php @@ -237,5 +237,5 @@ return [ | The only modification by laravel config | */ - 'version' => '1.7.4.1681103823832' + 'version' => '1.7.5.1685907718051' ];