Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ If you use **Lumen**, you need to copy the config file manually and register the
$this->app->register(\BeyondCode\QueryDetector\LumenQueryDetectorServiceProvider::class);
```

If you need additional logic to run when the package detects unoptimized queries, you can listen to the `\BeyondCode\QueryDetector\Events\QueryDetected` event and write a listener to run your own handler. (e.g. send warning to Sentry/Bugsnag, send Slack notification, etc.)

### Testing

``` bash
Expand Down
26 changes: 26 additions & 0 deletions src/Events/QueryDetected.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace BeyondCode\QueryDetector\Events;

use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;

class QueryDetected {
use SerializesModels;

/** @var Collection */
protected $queries;

public function __construct(Collection $queries)
{
$this->queries = $queries;
}

/**
* @return Collection
*/
public function getQueries()
{
return $this->queries;
}
}
9 changes: 8 additions & 1 deletion src/QueryDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Builder;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Database\Eloquent\Relations\Relation;
use BeyondCode\QueryDetector\Events\QueryDetected;

class QueryDetector
{
Expand Down Expand Up @@ -167,7 +168,13 @@ public function getDetectedQueries(): Collection
}
}

return $queries->where('count', '>', config('querydetector.threshold', 1))->values();
$queries = $queries->where('count', '>', config('querydetector.threshold', 1))->values();

if ($queries->isNotEmpty()) {
event(new QueryDetected($queries));
}

return $queries;
}

protected function applyOutput(Response $response)
Expand Down
38 changes: 38 additions & 0 deletions tests/QueryDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace BeyondCode\QueryDetector\Tests;

use Route;
use Illuminate\Support\Facades\Event;
use BeyondCode\QueryDetector\QueryDetector;
use BeyondCode\QueryDetector\Events\QueryDetected;
use BeyondCode\QueryDetector\Tests\Models\Post;
use BeyondCode\QueryDetector\Tests\Models\Author;
use BeyondCode\QueryDetector\Tests\Models\Comment;
Expand Down Expand Up @@ -224,4 +226,40 @@ public function it_ignores_redirects()

$this->assertCount(1, $queries);
}

/** @test */
public function it_fires_an_event_if_detects_n1_query()
{
Event::fake();

Route::get('/', function (){
$authors = Author::all();

foreach ($authors as $author) {
$author->profile;
}
});

$this->get('/');

Event::assertDispatched(QueryDetected::class);
}

/** @test */
public function it_does_not_fire_an_event_if_there_is_no_n1_query()
{
Event::fake();

Route::get('/', function (){
$authors = Author::with('profile')->get();

foreach ($authors as $author) {
$author->profile;
}
});

$this->get('/');

Event::assertNotDispatched(QueryDetected::class);
}
}