diff --git a/app/Http/Controllers/Admin/Settings.php b/app/Http/Controllers/Admin/Settings.php index a2c87b2d..db782248 100644 --- a/app/Http/Controllers/Admin/Settings.php +++ b/app/Http/Controllers/Admin/Settings.php @@ -71,7 +71,7 @@ class Settings extends Controller // set value and save or delete $setting->value = $value; - empty($value) ? $setting->delete() : $setting->save(); + $setting->save(); } return redirect() diff --git a/modules/Profiler/Config/config.php b/modules/Profiler/Config/config.php index cbc4324d..6d01f49e 100644 --- a/modules/Profiler/Config/config.php +++ b/modules/Profiler/Config/config.php @@ -5,8 +5,8 @@ return [ 'translatedName' => 'profiler::module.name', 'translatedDescription' => 'profiler::module.description', 'alias' => 'profiler', - 'handler' => \Modules\Blog\Article\Handlers\Article::class, - 'providers' => [\Modules\Blog\Article\Providers\ArticleServiceProvider::class], + 'handler' => \Modules\Profiler\Handlers\ProfileHandler::class, + 'providers' => [\Modules\Profiler\Providers\ProfilerServiceProvider::class], 'files' => ['start.php'], - 'order' => 0, + 'order' => -100, ]; \ No newline at end of file diff --git a/modules/Profiler/Database/Migrations/.gitkeep b/modules/Profiler/Database/Migrations/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/modules/Profiler/Database/Migrations/2017_03_02_114429_update_http_log_add_load_avg.php b/modules/Profiler/Database/Migrations/2017_03_02_114429_update_http_log_add_load_avg.php new file mode 100644 index 00000000..086f727b --- /dev/null +++ b/modules/Profiler/Database/Migrations/2017_03_02_114429_update_http_log_add_load_avg.php @@ -0,0 +1,35 @@ +double('load_1')->nullable(); + $table->double('load_5')->nullable(); + $table->double('load_15')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('http_log', function (Blueprint $table) { + $table->dropColumn('load_1'); + $table->dropColumn('load_5'); + $table->dropColumn('load_15'); + }); + } +} diff --git a/modules/Profiler/Database/Migrations/2017_03_02_115241_update_query_log_add_request_id.php b/modules/Profiler/Database/Migrations/2017_03_02_115241_update_query_log_add_request_id.php new file mode 100644 index 00000000..b7ef557e --- /dev/null +++ b/modules/Profiler/Database/Migrations/2017_03_02_115241_update_query_log_add_request_id.php @@ -0,0 +1,43 @@ +integer('http_log_id') + ->nullable() + ->unsigned() + ->after('id') + ->comment('FK to http_log'); + + $table->foreign('http_log_id') + ->references('id') + ->on('http_log') + ->onDelete('cascade') + ->onUpdate('cascade'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('queries_log', function (Blueprint $table) { + + // $table->dropForeign(''); + $table->dropColumn('http_log_id'); + }); + } +} diff --git a/modules/Profiler/Entities/HttpLog.php b/modules/Profiler/Entities/HttpLog.php index 6640d90a..cb6f3603 100644 --- a/modules/Profiler/Entities/HttpLog.php +++ b/modules/Profiler/Entities/HttpLog.php @@ -9,6 +9,14 @@ class HttpLog extends Model protected $fillable = [ 'method', 'url', - 'time' + 'time', + 'load_1', + 'load_5', + 'load_15' ]; + + public function queries() + { + return $this->hasMany(QueryLog::class); + } } \ No newline at end of file diff --git a/modules/Profiler/Entities/QueryLog.php b/modules/Profiler/Entities/QueryLog.php index 3c33f3d7..e458bcd3 100644 --- a/modules/Profiler/Entities/QueryLog.php +++ b/modules/Profiler/Entities/QueryLog.php @@ -9,9 +9,15 @@ class QueryLog extends Model protected $fillable = [ 'query', 'bindings', - 'time' + 'time', + 'http_log_id' ]; protected $casts = [ 'bindings' => 'array' ]; + + public function httpLog() + { + return $this->belongsTo(HttpLog::class); + } } \ No newline at end of file diff --git a/modules/Profiler/Handlers/ProfileHandler.php b/modules/Profiler/Handlers/ProfileHandler.php new file mode 100644 index 00000000..0fec0ab6 --- /dev/null +++ b/modules/Profiler/Handlers/ProfileHandler.php @@ -0,0 +1,25 @@ +whereTitle(trans('admin/mainMenu.system')); + $submenu->route('profiler.index', trans('profiler::module.name'), [], 10, ['icon' => 'fa fa-dashboard']); + } +} \ No newline at end of file diff --git a/modules/Profiler/Http/Controllers/.gitkeep b/modules/Profiler/Http/Controllers/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/modules/Profiler/Http/Controllers/ProfilerController.php b/modules/Profiler/Http/Controllers/ProfilerController.php index 75768f6e..e03979e7 100644 --- a/modules/Profiler/Http/Controllers/ProfilerController.php +++ b/modules/Profiler/Http/Controllers/ProfilerController.php @@ -1,12 +1,16 @@ with('queries') + ->paginate(25); + + return view('profiler::index', compact('httpLog')); + } } \ No newline at end of file diff --git a/modules/Profiler/Http/Controllers/QueryController.php b/modules/Profiler/Http/Controllers/QueryController.php new file mode 100644 index 00000000..b19af8fc --- /dev/null +++ b/modules/Profiler/Http/Controllers/QueryController.php @@ -0,0 +1,13 @@ +queries; + + return view('profiler::queries.index', compact('queries')); + } +} \ No newline at end of file diff --git a/modules/Profiler/Http/routes.php b/modules/Profiler/Http/routes.php index e5e43cc3..4b3da4c2 100644 --- a/modules/Profiler/Http/routes.php +++ b/modules/Profiler/Http/routes.php @@ -1,6 +1,14 @@ LaravelLocalization::setLocale(), + 'namespace' => 'Modules\Profiler\Http\Controllers' + ], function () { + Route::group([ + 'middleware' => ['admin'], + 'prefix' => 'profiler' + ], function () { -Route::group(['middleware' => 'web', 'prefix' => 'profiler', 'namespace' => 'Modules\Profiler\Http\Controllers'], function() -{ - Route::get('/', 'ProfilerController@index'); + Route::get('/', 'ProfilerController@index')->name('profiler.index'); + Route::get('{httpLog}/queries', 'QueryController@index')->name('queries.index'); + }); }); \ No newline at end of file diff --git a/modules/Profiler/Middleware/Profiler.php b/modules/Profiler/Middleware/Profiler.php index 50bf958a..01268027 100644 --- a/modules/Profiler/Middleware/Profiler.php +++ b/modules/Profiler/Middleware/Profiler.php @@ -7,6 +7,8 @@ use Modules\Profiler\Entities\QueryLog; class Profiler { + private $queries = []; + public function handle(Request $request, $next) { \DB::enableQueryLog(); @@ -15,28 +17,47 @@ class Profiler public function terminate(Request $request, $response) { - $this->logRequest($request); - $this->logQueries(); + $this->queries = \DB::getQueryLog(); + + $requestId = $this->logRequest($request); + $this->logQueries($requestId); } + /** + * + * + * @param Request $request + * + * @return mixed + */ protected function logRequest(Request $request) { $url = $request->fullUrl(); $method = $request->getMethod(); $time = (microtime(true) - LARAVEL_START) * 1000; - HttpLog::create(['url' => $url, 'method' => $method, 'time' => $time]); + $data = ['url' => $url, 'method' => $method, 'time' => $time]; + + if (function_exists('sys_getloadavg')) { + $load = sys_getloadavg(); + $data['load_1'] = $load[0]; + $data['load_5'] = $load[1]; + $data['load_15'] = $load[2]; + } + + $http = HttpLog::create($data); + + return $http->id; } - protected function logQueries() + protected function logQueries($httpLogId = null) { - $queries = \DB::getQueryLog(); - - foreach ($queries as $query) { + foreach ($this->queries as $query) { QueryLog::create([ - 'query' => $query['query'], - 'bindings' => $query['bindings'], - 'time' => $query['time'] + 'query' => $query['query'], + 'bindings' => $query['bindings'], + 'time' => $query['time'], + 'http_log_id' => $httpLogId ]); } } diff --git a/modules/Profiler/Providers/ProfilerServiceProvider.php b/modules/Profiler/Providers/ProfilerServiceProvider.php index 4e2a282b..f04c476c 100644 --- a/modules/Profiler/Providers/ProfilerServiceProvider.php +++ b/modules/Profiler/Providers/ProfilerServiceProvider.php @@ -11,7 +11,6 @@ class ProfilerServiceProvider extends ModuleServiceProvider $this->alias = 'profiler'; } - /** * Boot the application events. * @@ -28,10 +27,20 @@ class ProfilerServiceProvider extends ModuleServiceProvider { parent::register(); - $this->app['router']->prependMiddlewareToGroup('web', Profiler::class); -// $this->app['router']->prependMiddlewareToGroup('admin', Profiler::class); - } + // register settings form + $registrator = $this->app->make('registerSettingsExtension'); + $registrator->register('profiler', 'profiler::module.name', 'profiler::settings.profiler'); + // turn on profiler for visitor area (middleware group "web") + if (setting('useProfilerInVisitorArea', true)) { + $this->app['router']->prependMiddlewareToGroup('web', Profiler::class); + } + + // use profiler also in admin? + if (setting('useProfilerInAdmin', true)) { + $this->app['router']->prependMiddlewareToGroup('admin', Profiler::class); + } + } protected function setModulePath() { diff --git a/modules/Profiler/Resources/lang/.gitkeep b/modules/Profiler/Resources/lang/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/modules/Profiler/Resources/lang/cs/module.php b/modules/Profiler/Resources/lang/cs/module.php new file mode 100644 index 00000000..4cf8d445 --- /dev/null +++ b/modules/Profiler/Resources/lang/cs/module.php @@ -0,0 +1,15 @@ + 'Profiler', + 'index' => [ + 'createdAt' => 'Datum', + 'httpMethod' => 'HTTP metoda', + 'url' => 'URL', + 'time' => 'Čas', + 'systemLoad' => 'Load serveru' + ], + 'settings' => [ + 'useProfilerInVisitorArea' => 'Zapnout měření ve veřejné části?', + 'useProfilerInAdmin' => 'Zapnout měření v administraci?' + ] +]; \ No newline at end of file diff --git a/modules/Profiler/Resources/views/.gitkeep b/modules/Profiler/Resources/views/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/modules/Profiler/Resources/views/index.blade.php b/modules/Profiler/Resources/views/index.blade.php index 0b3a478a..33ef20c6 100644 --- a/modules/Profiler/Resources/views/index.blade.php +++ b/modules/Profiler/Resources/views/index.blade.php @@ -1,11 +1,37 @@ -@extends('profiler::layouts.master') +@extends('admin.layoutWithNavigation') + +@section('pageHeading', trans('profiler::module.name')) @section('content') - -
- This view is loaded from module: {!! config('profiler.name') !!} -
+| @lang('profiler::module.index.createdAt') | +@lang('profiler::module.index.httpMethod') | +@lang('profiler::module.index.url') | +@lang('profiler::module.index.time') | +@lang('profiler::module.index.systemLoad') | ++ |
| {{$request->created_at->format('d.m.Y G:i')}} | +{{$request->method}} | +{{$request->url}} | +{{number_format($request->time, 3)}} ms | +{{$request->load_1}}, {{$request->load_5}}, {{$request->load_15}} | ++ SQL + | +
| @lang('profiler::module.queries.createdAt') | +@lang('profiler::module.queries.sql') | +@lang('profiler::module.queries.binding') | +@lang('profiler::module.queries.time') | ++ |
| {{$query->created_at->format('d.m.Y G:i')}} | +{{$query->query}} | ++ + {{var_export($query->bindings)}} + | +{{number_format($query->time, 3)}} ms | +