FEATURE: add first basics of profiler

This commit is contained in:
Michal Kváček
2017-02-22 08:54:26 +01:00
parent 5132278ee1
commit c027ffe2df
23 changed files with 293 additions and 0 deletions
+12
View File
@@ -0,0 +1,12 @@
<?php
return [
'name' => 'Profiler',
'translatedName' => 'profiler::module.name',
'translatedDescription' => 'profiler::module.description',
'alias' => 'profiler',
'handler' => \Modules\Blog\Article\Handlers\Article::class,
'providers' => [\Modules\Blog\Article\Providers\ArticleServiceProvider::class],
'files' => ['start.php'],
'order' => 0,
];
@@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateQueriesLogTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('queries_log', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->text('query');
$table->text('bindings');
$table->double('time');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('queries_log');
}
}
@@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateHttpLogTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('http_log', function(Blueprint $table)
{
$table->increments('id');
$table->timestamps();
$table->string('method');
$table->text('url');
$table->double('time');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('http_log');
}
}
@@ -0,0 +1,20 @@
<?php namespace Modules\Profiler\Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class ProfilerDatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
// $this->call("OthersTableSeeder");
}
}
+14
View File
@@ -0,0 +1,14 @@
<?php
namespace Modules\Profiler\Entities;
use Illuminate\Database\Eloquent\Model;
class HttpLog extends Model
{
public $table = 'http_log';
protected $fillable = [
'method',
'url',
'time'
];
}
+17
View File
@@ -0,0 +1,17 @@
<?php
namespace Modules\Profiler\Entities;
use Illuminate\Database\Eloquent\Model;
class QueryLog extends Model
{
public $table = 'queries_log';
protected $fillable = [
'query',
'bindings',
'time'
];
protected $casts = [
'bindings' => 'array'
];
}
@@ -0,0 +1,12 @@
<?php namespace Modules\Profiler\Http\Controllers;
use Pingpong\Modules\Routing\Controller;
class ProfilerController extends Controller {
public function index()
{
return view('profiler::index');
}
}
+6
View File
@@ -0,0 +1,6 @@
<?php
Route::group(['middleware' => 'web', 'prefix' => 'profiler', 'namespace' => 'Modules\Profiler\Http\Controllers'], function()
{
Route::get('/', 'ProfilerController@index');
});
+43
View File
@@ -0,0 +1,43 @@
<?php
namespace Modules\Profiler\Middleware;
use Illuminate\Http\Request;
use Modules\Profiler\Entities\HttpLog;
use Modules\Profiler\Entities\QueryLog;
class Profiler
{
public function handle(Request $request, $next)
{
\DB::enableQueryLog();
return $next($request);
}
public function terminate(Request $request, $response)
{
$this->logRequest($request);
$this->logQueries();
}
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]);
}
protected function logQueries()
{
$queries = \DB::getQueryLog();
foreach ($queries as $query) {
QueryLog::create([
'query' => $query['query'],
'bindings' => $query['bindings'],
'time' => $query['time']
]);
}
}
}
@@ -0,0 +1,8 @@
<?php
namespace Modules\Profiler\Providers;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class ProfilerEventServiceProvider extends ServiceProvider
{
}
@@ -0,0 +1,40 @@
<?php
namespace Modules\Profiler\Providers;
use App\Providers\ModuleServiceProvider;
use Modules\Profiler\Middleware\Profiler;
class ProfilerServiceProvider extends ModuleServiceProvider
{
protected function setModuleAlias()
{
$this->alias = 'profiler';
}
/**
* Boot the application events.
*
* @return void
*/
public function boot()
{
parent::boot();
$this->app->register(ProfilerEventServiceProvider::class);
}
public function register()
{
parent::register();
$this->app['router']->prependMiddlewareToGroup('web', Profiler::class);
// $this->app['router']->prependMiddlewareToGroup('admin', Profiler::class);
}
protected function setModulePath()
{
$this->path = __DIR__;
}
}
@@ -0,0 +1,11 @@
@extends('profiler::layouts.master')
@section('content')
<h1>Hello World</h1>
<p>
This view is loaded from module: {!! config('profiler.name') !!}
</p>
@stop
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Module Profiler</title>
</head>
<body>
@yield('content')
</body>
</html>
+15
View File
@@ -0,0 +1,15 @@
{
"name": "pingpong-modules/profiler",
"description": "",
"authors": [
{
"name": "Pingpong Labs",
"email": "pingpong.labs@gmail.com"
}
],
"autoload": {
"psr-4": {
"Modules\\Profiler\\": ""
}
}
}
+15
View File
@@ -0,0 +1,15 @@
<?php
/*
|--------------------------------------------------------------------------
| Register Namespaces And Routes
|--------------------------------------------------------------------------
|
| When a module starting, this file will executed automatically. This helps
| to register some namespaces like translator or view. Also this file
| will load the routes file for each module. You may also modify
| this file as you want.
|
*/
require __DIR__ . '/Http/routes.php';