From 68db4f51b560ef3f2baaae890003e4da78c64450 Mon Sep 17 00:00:00 2001 From: v2board Date: Mon, 5 Jun 2023 03:43:34 +0800 Subject: [PATCH] update: add system log filter --- .../Controllers/Admin/SystemController.php | 16 +++++- app/Models/Log.php | 1 + app/Scope/FilterScope.php | 50 +++++++++++++++++++ config/app.php | 2 +- 4 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 app/Scope/FilterScope.php 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/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' ];