Skip to content

Commit

Permalink
Add option to toggle query background on/off (#1196)
Browse files Browse the repository at this point in the history
* Add option to toggle query background on/off

* provide Application instance to QueryCollector

* Implement requested changes
  • Loading branch information
tadhgboyle authored Jun 14, 2021
1 parent 7904e3b commit 70b8975
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
1 change: 1 addition & 0 deletions config/debugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
'backtrace' => true, // Use a backtrace to find the origin of the query in your files.
'backtrace_exclude_paths' => [], // Paths to exclude from backtrace. (in addition to defaults)
'timeline' => false, // Add the queries to the timeline
'duration_background' => true, // Show shaded background on each query relative to how long it took to execute.
'explain' => [ // Show EXPLAIN output on queries
'enabled' => false,
'types' => ['SELECT'], // Deprecated setting, is always only SELECT
Expand Down
39 changes: 26 additions & 13 deletions src/DataCollector/QueryCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class QueryCollector extends PDOCollector
protected $renderSqlWithParams = false;
protected $findSource = false;
protected $middleware = [];
protected $durationBackground = true;
protected $explainQuery = false;
protected $explainTypes = ['SELECT']; // ['SELECT', 'INSERT', 'UPDATE', 'DELETE']; for MySQL 5.6.3+
protected $showHints = false;
Expand Down Expand Up @@ -89,6 +90,16 @@ public function mergeBacktraceExcludePaths(array $excludePaths)
$this->backtraceExcludePaths = array_merge($this->backtraceExcludePaths, $excludePaths);
}

/**
* Enable/disable the shaded duration background on queries
*
* @param bool $enabled
*/
public function setDurationBackground($enabled)
{
$this->durationBackground = $enabled;
}

/**
* Enable/disable the EXPLAIN queries
*
Expand Down Expand Up @@ -493,23 +504,25 @@ public function collect()
}
}

if ($totalTime > 0) {
// For showing background measure on Queries tab
$start_percent = 0;
if ($this->durationBackground) {
if ($totalTime > 0) {
// For showing background measure on Queries tab
$start_percent = 0;

foreach ($statements as $i => $statement) {
if (! isset($statement['duration'])) {
continue;
}
foreach ($statements as $i => $statement) {
if (!isset($statement['duration'])) {
continue;
}

$width_percent = $statement['duration'] / $totalTime * 100;
$width_percent = $statement['duration'] / $totalTime * 100;

$statements[$i] = array_merge($statement, [
'start_percent' => round($start_percent, 3),
'width_percent' => round($width_percent, 3),
]);
$statements[$i] = array_merge($statement, [
'start_percent' => round($start_percent, 3),
'width_percent' => round($width_percent, 3),
]);

$start_percent += $width_percent;
$start_percent += $width_percent;
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/LaravelDebugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ function ($level, $message = null, $context = null) use ($logger) {
$queryCollector->mergeBacktraceExcludePaths($excludePaths);
}

$queryCollector->setDurationBackground($this->app['config']->get('debugbar.options.db.duration_background'));

if ($this->app['config']->get('debugbar.options.db.explain.enabled')) {
$types = $this->app['config']->get('debugbar.options.db.explain.types');
$queryCollector->setExplainSource(true, $types);
Expand Down

0 comments on commit 70b8975

Please sign in to comment.