Skip to content
Open
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
9 changes: 7 additions & 2 deletions Clockwork/DataSource/EloquentDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ class EloquentDataSource extends DataSource
// Enable duplicate queries detection
protected $detectDuplicateQueries = false;

// Enable explanation of queries
protected $explain = false;

// Model name to associate with the next executed query, used to map queries to models
public $nextQueryModel;

// Create a new data source instance, takes a database manager, an event dispatcher as arguments and additional
// options as arguments
public function __construct(ConnectionResolverInterface $databaseManager, EventDispatcher $eventDispatcher, $collectQueries = true, $slowThreshold = null, $slowOnly = false, $detectDuplicateQueries = false, $collectModelsActions = true, $collectModelsRetrieved = false)
public function __construct(ConnectionResolverInterface $databaseManager, EventDispatcher $eventDispatcher, $collectQueries = true, $slowThreshold = null, $slowOnly = false, $detectDuplicateQueries = false, $collectModelsActions = true, $collectModelsRetrieved = false, $explain = false)
{
$this->databaseManager = $databaseManager;
$this->eventDispatcher = $eventDispatcher;
Expand All @@ -63,6 +66,7 @@ public function __construct(ConnectionResolverInterface $databaseManager, EventD
$this->detectDuplicateQueries = $detectDuplicateQueries;
$this->collectModelsActions = $collectModelsActions;
$this->collectModelsRetrieved = $collectModelsRetrieved;
$this->explain = $explain;

if ($slowOnly) $this->addFilter(function ($query) { return $query['duration'] > $this->slowThreshold; });
}
Expand Down Expand Up @@ -166,7 +170,8 @@ protected function registerQuery($event)
'time' => microtime(true) - $event->time / 1000,
'trace' => (new Serializer)->trace($trace),
'model' => $this->nextQueryModel,
'tags' => $this->slowThreshold !== null && $event->time > $this->slowThreshold ? [ 'slow' ] : []
'tags' => $this->slowThreshold !== null && $event->time > $this->slowThreshold ? [ 'slow' ] : [],
'explanation'=> $this->explain && str_starts_with($event->sql, 'SELECT') ? array_map(fn ($row) => $row->{'QUERY PLAN'}, $this->databaseManager->connection($event->connectionName)->select('EXPLAIN ANALYZE ' . $event->sql, $event->bindings)) : null
];

$this->nextQueryModel = null;
Expand Down