You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
3.0 KiB
124 lines
3.0 KiB
12 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
|
namespace yii\debug\panels;
|
||
12 years ago
|
|
||
12 years ago
|
use yii\debug\Panel;
|
||
12 years ago
|
use yii\helpers\ArrayHelper;
|
||
12 years ago
|
use yii\log\Logger;
|
||
|
use yii\helpers\Html;
|
||
12 years ago
|
|
||
|
/**
|
||
12 years ago
|
* Debugger panel that collects and displays database queries performed.
|
||
|
*
|
||
12 years ago
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
|
class DbPanel extends Panel
|
||
|
{
|
||
|
public function getName()
|
||
|
{
|
||
|
return 'Database';
|
||
|
}
|
||
12 years ago
|
|
||
|
public function getSummary()
|
||
|
{
|
||
12 years ago
|
$timings = $this->calculateTimings();
|
||
|
$queryCount = count($timings);
|
||
|
$queryTime = 0;
|
||
|
foreach ($timings as $timing) {
|
||
|
$queryTime += $timing[3];
|
||
|
}
|
||
|
$queryTime = number_format($queryTime * 1000) . ' ms';
|
||
|
$url = $this->getUrl();
|
||
12 years ago
|
$output = <<<EOD
|
||
|
<div class="yii-debug-toolbar-block">
|
||
12 years ago
|
<a href="$url" title="Executed $queryCount database queries which took $queryTime.">
|
||
|
DB <span class="label">$queryCount</span> <span class="label">$queryTime</span>
|
||
12 years ago
|
</a>
|
||
12 years ago
|
</div>
|
||
|
EOD;
|
||
|
return $queryCount > 0 ? $output : '';
|
||
|
}
|
||
|
|
||
|
public function getDetail()
|
||
|
{
|
||
12 years ago
|
$timings = $this->calculateTimings();
|
||
12 years ago
|
ArrayHelper::multisort($timings, 3, true);
|
||
11 years ago
|
$rows = [];
|
||
12 years ago
|
foreach ($timings as $timing) {
|
||
|
$duration = sprintf('%.1f ms', $timing[3] * 1000);
|
||
12 years ago
|
$procedure = Html::encode($timing[1]);
|
||
|
$traces = $timing[4];
|
||
|
if (!empty($traces)) {
|
||
11 years ago
|
$procedure .= Html::ul($traces, [
|
||
12 years ago
|
'class' => 'trace',
|
||
|
'item' => function ($trace) {
|
||
|
return "<li>{$trace['file']}({$trace['line']})</li>";
|
||
|
},
|
||
11 years ago
|
]);
|
||
12 years ago
|
}
|
||
12 years ago
|
$rows[] = "<tr><td style=\"width: 80px;\">$duration</td><td>$procedure</td>";
|
||
12 years ago
|
}
|
||
|
$rows = implode("\n", $rows);
|
||
|
|
||
|
return <<<EOD
|
||
|
<h1>Database Queries</h1>
|
||
|
|
||
|
<table class="table table-condensed table-bordered table-striped table-hover" style="table-layout: fixed;">
|
||
|
<thead>
|
||
|
<tr>
|
||
12 years ago
|
<th style="width: 80px;">Time</th>
|
||
12 years ago
|
<th>Query</th>
|
||
|
</tr>
|
||
|
</thead>
|
||
|
<tbody>
|
||
|
$rows
|
||
|
</tbody>
|
||
|
</table>
|
||
|
EOD;
|
||
|
}
|
||
|
|
||
12 years ago
|
private $_timings;
|
||
|
|
||
|
protected function calculateTimings()
|
||
|
{
|
||
|
if ($this->_timings !== null) {
|
||
|
return $this->_timings;
|
||
|
}
|
||
|
$messages = $this->data['messages'];
|
||
11 years ago
|
$timings = [];
|
||
|
$stack = [];
|
||
12 years ago
|
foreach ($messages as $i => $log) {
|
||
|
list($token, $level, $category, $timestamp) = $log;
|
||
12 years ago
|
$log[5] = $i;
|
||
12 years ago
|
if ($level == Logger::LEVEL_PROFILE_BEGIN) {
|
||
|
$stack[] = $log;
|
||
|
} elseif ($level == Logger::LEVEL_PROFILE_END) {
|
||
|
if (($last = array_pop($stack)) !== null && $last[0] === $token) {
|
||
11 years ago
|
$timings[$last[5]] = [count($stack), $token, $last[3], $timestamp - $last[3], $last[4]];
|
||
12 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$now = microtime(true);
|
||
|
while (($last = array_pop($stack)) !== null) {
|
||
|
$delta = $now - $last[3];
|
||
11 years ago
|
$timings[$last[5]] = [count($stack), $last[0], $last[2], $delta, $last[4]];
|
||
12 years ago
|
}
|
||
|
ksort($timings);
|
||
|
return $this->_timings = $timings;
|
||
|
}
|
||
|
|
||
12 years ago
|
public function save()
|
||
|
{
|
||
|
$target = $this->module->logTarget;
|
||
11 years ago
|
$messages = $target->filterMessages($target->messages, Logger::LEVEL_PROFILE, ['yii\db\Command::queryInternal']);
|
||
|
return ['messages' => $messages];
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|