From eee96db6fb56b2921ad5327561d2415e53427453 Mon Sep 17 00:00:00 2001 From: lxerxa Date: Wed, 4 Dec 2019 20:58:02 +0800 Subject: [PATCH] logs export && ldap sync memory-overflow --- app/Console/Commands/SyncLdap.php | 3 + app/Http/Controllers/AccessLogsController.php | 57 ++++++++++++++++++- app/Http/Controllers/DirectoryController.php | 9 ++- 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/app/Console/Commands/SyncLdap.php b/app/Console/Commands/SyncLdap.php index 7a67f720d..f9bd86986 100644 --- a/app/Console/Commands/SyncLdap.php +++ b/app/Console/Commands/SyncLdap.php @@ -38,6 +38,9 @@ public function __construct() public function handle() { + ini_set('memory_limit', '-1'); + ini_set('max_execution_time', '0'); + $configs = []; $directories = Directory::where('type', 'OpenLDAP') diff --git a/app/Http/Controllers/AccessLogsController.php b/app/Http/Controllers/AccessLogsController.php index 99829347c..46011314a 100644 --- a/app/Http/Controllers/AccessLogsController.php +++ b/app/Http/Controllers/AccessLogsController.php @@ -9,6 +9,7 @@ use App\Http\Controllers\Controller; use App\System\Eloquent\ApiAccessLogs; +use Maatwebsite\Excel\Facades\Excel; class AccessLogsController extends Controller { @@ -19,6 +20,9 @@ class AccessLogsController extends Controller */ public function index(Request $request) { + ini_set('memory_limit', '-1'); + ini_set('max_execution_time', '0'); + $query = ApiAccessLogs::query(); $uid = $request->input('uid'); @@ -91,12 +95,63 @@ public function index(Request $request) // get total $total = $query->count(); $query->orderBy('_id', 'desc'); - $page_size = 100; + $page_size = $request->input('limit') ? intval($request->input('limit')) : 100; $page = $request->input('page') ?: 1; $query = $query->skip($page_size * ($page - 1))->take($page_size); $logs = $query->get(); + $from = $request->input('from'); + if (isset($from) && $from == 'export') + { + $this->export($logs); + exit(); + } + return Response()->json([ 'ecode' => 0, 'data' => $logs, 'options' => [ 'total' => $total, 'sizePerPage' => $page_size ] ]); } + + /** + * export xls for access logs + * + * @param array $issues + * @return void + */ + public function export($logs) + { + $headers = [ + '用户', + '方法', + 'Url', + '模块', + '请求开始时间', + '请求结束时间', + '请求时长(毫秒)', + '来源IP', + 'User-Agent', + 'Body' + ]; + + $file_name = 'access-logs'; + Excel::create($file_name, function ($excel) use($headers, $logs) { + $excel->sheet('Sheetname', function ($sheet) use($headers, $logs) { + $sheet->appendRow($headers); + foreach ($logs as $log) + { + $tmp = []; + $tmp[] = $log->user ? $log->user['name'] : ''; + $tmp[] = $log->request_method ?: ''; + $tmp[] = $log->request_url ?: ''; + $tmp[] = $log->module ?: ''; + $tmp[] = $log->requested_start_at ? date('Y-m-d h:i:s', intval($log->requested_start_at / 1000)): ''; + $tmp[] = $log->requested_end_at ? date('Y-m-d h:i:s', intval($log->requested_end_at / 1000)) : ''; + $tmp[] = $log->exec_time ?: ''; + $tmp[] = $log->request_source_ip ?: ''; + $tmp[] = $log->request_user_agent ?: ''; + $tmp[] = $log->request_body ? json_encode($log->request_body) : ''; + $sheet->appendRow($tmp); + } + }); + })->download('xls'); + } } diff --git a/app/Http/Controllers/DirectoryController.php b/app/Http/Controllers/DirectoryController.php index 9d81515bc..7cca94f2e 100644 --- a/app/Http/Controllers/DirectoryController.php +++ b/app/Http/Controllers/DirectoryController.php @@ -366,7 +366,8 @@ public function update(Request $request, $id) */ public function destroy($id) { - set_time_limit(0); + ini_set('memory_limit', '-1'); + ini_set('max_execution_time', '0'); $directory = Directory::find($id); if (!$directory) @@ -401,6 +402,9 @@ public function destroy($id) */ public function test($id) { + ini_set('memory_limit', '-1'); + ini_set('max_execution_time', '0'); + $directory = Directory::find($id); if (!$directory) { @@ -423,7 +427,8 @@ public function test($id) */ public function sync($id) { - set_time_limit(0); + ini_set('memory_limit', '-1'); + ini_set('max_execution_time', '0'); $directory = Directory::find($id); if (!$directory)