Skip to content

Commit

Permalink
Merge pull request #19 from cybercog/feature/db-query-explain
Browse files Browse the repository at this point in the history
Add QueryExplanation
  • Loading branch information
antonkomarev authored Oct 7, 2018
2 parents 78f962d + 060f55b commit a745ae9
Show file tree
Hide file tree
Showing 12 changed files with 336 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to `laravel-sense` will be documented in this file.

## [0.5.0] - 2018-10-07

### Added

- `QueryExplanation` model added ([#19](https://github.com/cybercog/laravel-sense/pull/19))

## [0.4.0] - 2018-10-07

### Added
Expand Down Expand Up @@ -38,6 +44,7 @@ All notable changes to `laravel-sense` will be documented in this file.

- Initial release

[0.5.0]: https://github.com/cybercog/laravel-sense/compare/0.4.0...0.5.0
[0.4.0]: https://github.com/cybercog/laravel-sense/compare/0.3.0...0.4.0
[0.3.0]: https://github.com/cybercog/laravel-sense/compare/0.2.1...0.3.0
[0.2.1]: https://github.com/cybercog/laravel-sense/compare/0.2.0...0.2.1
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ If you discover any security related issues, please email open@cybercog.su inste
## Alternatives

- [barryvdh/laravel-debugbar](https://github.com/barryvdh/laravel-debugbar)
- [itsgoingd/clockwork](itsgoingd/clockwork)
- [itsgoingd/clockwork](https://github.com/itsgoingd/clockwork)

*Feel free to add more alternatives as Pull Request.*

Expand Down
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');
}
}
8 changes: 8 additions & 0 deletions resources/views/queries/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
<th>
Time
</th>
<th>
Explain
</th>
<th>
Created at
</th>
Expand All @@ -45,6 +48,11 @@
<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>
Expand Down
89 changes: 89 additions & 0 deletions resources/views/queries/view.blade.php
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
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
Route::get('/urls/{url}')->uses('Urls\Get\Action');

Route::get('/queries')->uses('Queries\Collect\Action');
Route::get('/queries/{query}')->uses('Queries\Get\Action');

Route::get('/{view?}', 'AppController')->where('view', '(.*)')->name('sense.index');
7 changes: 7 additions & 0 deletions src/Db/Query/Models/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

namespace Cog\Laravel\Sense\Db\Query\Models;

use Cog\Laravel\Sense\Db\QueryExplanation\Models\QueryExplanation;
use Cog\Laravel\Sense\Request\Models\Request;
use Cog\Laravel\Sense\Statement\Models\Statement;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;

class Query extends Model
{
Expand Down Expand Up @@ -45,4 +47,9 @@ public function statement(): BelongsTo
{
return $this->belongsTo(Statement::class, 'statement_id');
}

public function explanation(): HasOne
{
return $this->hasOne(QueryExplanation::class, 'query_id');
}
}
32 changes: 32 additions & 0 deletions src/Db/QueryExplanation/Models/QueryExplanation.php
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',
];
}
32 changes: 32 additions & 0 deletions src/Http/Controllers/Queries/Get/Action.php
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);
}
}
40 changes: 40 additions & 0 deletions src/Http/Controllers/Queries/Get/Request.php
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 [
];
}
}
46 changes: 46 additions & 0 deletions src/Http/Controllers/Queries/Get/Response.php
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 [];
}
}
Loading

0 comments on commit a745ae9

Please sign in to comment.