-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from cybercog/feature/db-query-explain
Add QueryExplanation
- Loading branch information
Showing
12 changed files
with
336 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
database/migrations/2018_09_24_003000_create_sense_request_db_query_explanations_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Laravel Sense. | ||
* | ||
* (c) Anton Komarev <a.komarev@cybercog.su> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateSenseRequestDbQueryExplanationsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('sense_request_db_query_explanations', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->unsignedBigInteger('query_id'); | ||
$table->json('result'); | ||
$table->timestamps(); | ||
|
||
$table->index('query_id'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('sense_request_db_query_explanations'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
@extends('sense::app') | ||
|
||
@section('content') | ||
<h2 class="mt-4 mb-4">Query</h2> | ||
|
||
<table class="table"> | ||
<tr> | ||
<th> | ||
# | ||
</th> | ||
<th class="uuid"> | ||
Correlation ID | ||
</th> | ||
<th> | ||
Connection | ||
</th> | ||
<th> | ||
SQL | ||
</th> | ||
<th> | ||
Time | ||
</th> | ||
<th> | ||
Explain | ||
</th> | ||
<th> | ||
Created at | ||
</th> | ||
<th> | ||
Updated at | ||
</th> | ||
</tr> | ||
<tr> | ||
<td class="text-center"> | ||
{{ $query->id }} | ||
</td> | ||
<td class="uuid"> | ||
<a href="/sense/requests/{{ $query->request->correlation_id }}"> | ||
{{ $query->request->correlation_id }} | ||
</a> | ||
</td> | ||
<td class="text-center"> | ||
{{ $query->connection }} | ||
</td> | ||
<td> | ||
{{ $query->sql }} | ||
</td> | ||
<td class="text-center"> | ||
{{ $query->time }} | ||
</td> | ||
<td> | ||
<a href="/sense/queries/{{ $query->id }}"> | ||
<kbd>EXPLAIN</kbd> | ||
</a> | ||
</td> | ||
<td class="text-center"> | ||
{{ $query->created_at }} | ||
</td> | ||
<td class="text-center"> | ||
{{ $query->updated_at }} | ||
</td> | ||
</tr> | ||
</table> | ||
|
||
<h3 class="mt-4 mb-4">Query Explain</h3> | ||
|
||
@if (count($query->explanation) > 0) | ||
@foreach($query->explanation->result as $explain) | ||
<table class="table"> | ||
<tr> | ||
<th>Key</th> | ||
<th>Value</th> | ||
</tr> | ||
@foreach ($explain as $key => $value) | ||
<tr> | ||
<th class="text-center"> | ||
{{ $key }} | ||
</th> | ||
<td class="text-center"> | ||
{{ $value ?? '-' }} | ||
</td> | ||
</tr> | ||
@endforeach | ||
</table> | ||
@endforeach | ||
@else | ||
<p>There are no explains for this query.</p> | ||
@endif | ||
@endsection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Laravel Sense. | ||
* | ||
* (c) Anton Komarev <a.komarev@cybercog.su> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cog\Laravel\Sense\Db\QueryExplanation\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class QueryExplanation extends Model | ||
{ | ||
protected $connection = 'sense'; | ||
|
||
protected $table = 'sense_request_db_query_explanations'; | ||
|
||
protected $fillable = [ | ||
'query_id', | ||
'result', | ||
]; | ||
|
||
protected $casts = [ | ||
'result' => 'json', | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Laravel Sense. | ||
* | ||
* (c) Anton Komarev <a.komarev@cybercog.su> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cog\Laravel\Sense\Http\Controllers\Queries\Get; | ||
|
||
use Cog\Laravel\Sense\Db\Query\Models\Query; | ||
use Cog\Laravel\Sense\Http\Controllers\Controller; | ||
|
||
class Action extends Controller | ||
{ | ||
public function __invoke($query, Request $request) | ||
{ | ||
$query = Query::query()->whereKey($query); | ||
|
||
$query->with([ | ||
]); | ||
|
||
$queryModel = $query->firstOrFail(); | ||
|
||
return new Response($queryModel); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Laravel Sense. | ||
* | ||
* (c) Anton Komarev <a.komarev@cybercog.su> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cog\Laravel\Sense\Http\Controllers\Queries\Get; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class Request extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Laravel Sense. | ||
* | ||
* (c) Anton Komarev <a.komarev@cybercog.su> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cog\Laravel\Sense\Http\Controllers\Queries\Get; | ||
|
||
use Illuminate\Contracts\Support\Responsable; | ||
|
||
class Response implements Responsable | ||
{ | ||
/** | ||
* @var \Cog\Laravel\Sense\Db\Query\Models\Query | ||
*/ | ||
private $query; | ||
|
||
public function __construct($query) | ||
{ | ||
$this->query = $query; | ||
} | ||
|
||
public function toResponse($request) | ||
{ | ||
return $request->wantsJson() ? $this->toJson() : $this->toHtml(); | ||
} | ||
|
||
private function toHtml() | ||
{ | ||
return view('sense::queries.view', [ | ||
'query' => $this->query, | ||
]); | ||
} | ||
|
||
private function toJson() | ||
{ | ||
return []; | ||
} | ||
} |
Oops, something went wrong.