update: add system log filter

This commit is contained in:
v2board 2023-06-05 03:43:34 +08:00
parent 2285a7c92f
commit 68db4f51b5
4 changed files with 67 additions and 2 deletions

View File

@ -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
]);
}
}

View File

@ -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'];

50
app/Scope/FilterScope.php Normal file
View File

@ -0,0 +1,50 @@
<?php
namespace App\Scope;
use Illuminate\Database\Eloquent\Builder;
trait FilterScope
{
public function scopeSetFilterAllowKeys($builder, ...$allowKeys)
{
$allowKeys = implode(',', $allowKeys);
if (!$allowKeys) return $builder;
$request = request();
$request->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;
}
}

View File

@ -237,5 +237,5 @@ return [
| The only modification by laravel config
|
*/
'version' => '1.7.4.1681103823832'
'version' => '1.7.5.1685907718051'
];