Skip to content

Commit

Permalink
Merge pull request #12 from socialblue/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
mbroersen committed May 28, 2019
2 parents 24648a9 + 388638c commit 62256ee
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 16 deletions.
73 changes: 67 additions & 6 deletions resources/views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,77 @@
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<title>Laravel Query Adviser</title>
</head>
<body>
<h1>Query Adviser</h1>
<section>
<div>
<h1>Query Adviser</h1>
<section>
<div>
@foreach($queries as $time => $querys)
<div class="query-group">
{{date("Y-m-d H:i:s")}}
</div>

@if(is_array($querys[0]))
@foreach($querys as $query)
<div class="query">
<div class="text">
{{Socialblue\LaravelQueryAdviser\Helper\QueryBuilderHelper::combineQueryAndBindings($query['sql'] ?? $query[0], $query['bindings'] ?? $query[1])}}
</div>
<div class="btn">
EXEC | EXPLAIN
</div>
</div>
@endforeach
@endif

@endforeach

</div>
</section>
<style>
.query-group {
font-family: Consolas;
font-size: 24px;
font-weight: bold;
padding: 10px 4px 4px 10px;
background: rgba(128,128,128, 0.4);
border: 1px solid rgba(128, 128, 128, 0.6);
position: relative;
clear: both;
}
.query {
position: relative;
clear: both;
width: 90%;
border: 1px solid rgba(128, 128, 128, 0.6);
font-family: Consolas;
font-size: 16px;
margin: 4px;
}
.query .text {
position: relative;
width: calc(95% - 100px);
max-height: 40px;
padding: 10px 4px 4px 10px;
overflow-y: scroll;
}
.query .btn {
position: relative;
margin: 10px;
width: 80px;
float: right;
left: 5px;
right: 5px;
font-size: 10px;
text-decoration: underline;
text-transform: uppercase;
}
</style>

</div>
</section>

</body>
</html>
Expand Down
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
use Illuminate\Support\Facades\Route;

Route::get('/query', 'QueryController@index');

Route::prefix('api')->group(function () {
Route::get('/query/get', 'QueryController@get');
});
9 changes: 9 additions & 0 deletions src/Http/Controllers/QueryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

namespace Socialblue\LaravelQueryAdviser\Http\Controllers;

use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class QueryController extends Controller
{
public function index() {
return view('QueryAdviser::index', [
'queries' => Cache::get(config('laravel-query-adviser.cache.key'), [])
]);
}


public function get(Request $request)
{
return Cache::get(config('laravel-query-adviser.cache.key'), []);
Expand Down
23 changes: 13 additions & 10 deletions src/LaravelQueryAdviserServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public function boot()
* Optional methods to load your package assets
*/
// $this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'laravel-query-adviser');
$this->loadViewsFrom(__DIR__.'/../resources/views', 'laravel-query-adviser');
$this->loadViewsFrom(__DIR__.'/../resources/views', 'QueryAdviser');
// $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
Route::group([
'prefix' => 'query-adviser',
'namespace' => 'SocialBlue\LaravelQueryAdviser\Http\Controllers',
'namespace' => 'Socialblue\LaravelQueryAdviser\Http\Controllers',
'middleware' =>'web',
], function () {
$this->loadRoutesFrom(__DIR__.'/../routes/web.php');
Expand Down Expand Up @@ -55,6 +55,16 @@ public function boot()
// Registering package commands.
// $this->commands([]);
}

DB::listen(function($query) {
$data = Cache::get(config('laravel-query-adviser.cache.key'), []);
$data[time()][] = ['sql' => $query->sql, 'bindings' => $query->bindings, 'time' => $query->time];

if (count($data) > config('laravel-query-adviser.cache.max_entries')) {
array_shift($data);
}
Cache::put(config('laravel-query-adviser.cache.key'), $data, config('laravel-query-adviser.cache.ttl'));
});
}

/**
Expand All @@ -70,13 +80,6 @@ public function register()
return new LaravelQueryAdviser;
});

DB::listen(function($query) {
$data = Cache::get(config('laravel-query-adviser.cache.key'), []);
$data[time()] = $query;
if (count($data) > config('laravel-query-adviser.cache.maxEntries')) {
array_shift($data);
}
Cache::put(config('laravel-query-adviser.cache.key'), $data, config('laravel-query-adviser.cache.ttl'));
});

}
}

0 comments on commit 62256ee

Please sign in to comment.