DbPanel.php 2.95 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\debug\panels;
resurtm committed
9

Qiang Xue committed
10
use yii\debug\Panel;
Qiang Xue committed
11
use yii\helpers\ArrayHelper;
resurtm committed
12 13
use yii\log\Logger;
use yii\helpers\Html;
Qiang Xue committed
14 15

/**
16 17
 * Debugger panel that collects and displays database queries performed.
 *
Qiang Xue committed
18 19 20 21 22 23 24 25 26
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class DbPanel extends Panel
{
	public function getName()
	{
		return 'Database';
	}
resurtm committed
27 28 29

	public function getSummary()
	{
Qiang Xue committed
30 31 32 33 34 35 36 37
		$timings = $this->calculateTimings();
		$queryCount = count($timings);
		$queryTime = 0;
		foreach ($timings as $timing) {
			$queryTime += $timing[3];
		}
		$queryTime = number_format($queryTime * 1000) . ' ms';
		$url = $this->getUrl();
resurtm committed
38 39
		$output = <<<EOD
<div class="yii-debug-toolbar-block">
40 41
	<a href="$url" title="Executed $queryCount database queries which took $queryTime.">
		DB <span class="label">$queryCount</span> <span class="label">$queryTime</span>
Qiang Xue committed
42
	</a>
resurtm committed
43 44 45 46 47 48 49
</div>
EOD;
		return $queryCount > 0 ? $output : '';
	}

	public function getDetail()
	{
Qiang Xue committed
50
		$timings = $this->calculateTimings();
Qiang Xue committed
51
		ArrayHelper::multisort($timings, 3, true);
Alexander Makarov committed
52
		$rows = [];
resurtm committed
53 54
		foreach ($timings as $timing) {
			$duration = sprintf('%.1f ms', $timing[3] * 1000);
55 56 57
			$procedure = Html::encode($timing[1]);
			$traces = $timing[4];
			if (!empty($traces)) {
Alexander Makarov committed
58
				$procedure .= Html::ul($traces, [
59 60 61 62
					'class' => 'trace',
					'item' => function ($trace) {
						return "<li>{$trace['file']}({$trace['line']})</li>";
					},
Alexander Makarov committed
63
				]);
64
			}
Qiang Xue committed
65
			$rows[] = "<tr><td style=\"width: 80px;\">$duration</td><td>$procedure</td>";
resurtm committed
66 67 68 69 70 71 72 73 74
		}
		$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>
Qiang Xue committed
75
	<th style="width: 80px;">Time</th>
resurtm committed
76 77 78 79 80 81 82 83 84 85
	<th>Query</th>
</tr>
</thead>
<tbody>
$rows
</tbody>
</table>
EOD;
	}

Qiang Xue committed
86 87 88 89 90 91 92 93
	private $_timings;

	protected function calculateTimings()
	{
		if ($this->_timings !== null) {
			return $this->_timings;
		}
		$messages = $this->data['messages'];
Alexander Makarov committed
94 95
		$timings = [];
		$stack = [];
Qiang Xue committed
96 97
		foreach ($messages as $i => $log) {
			list($token, $level, $category, $timestamp) = $log;
98
			$log[5] = $i;
Qiang Xue committed
99 100 101 102
			if ($level == Logger::LEVEL_PROFILE_BEGIN) {
				$stack[] = $log;
			} elseif ($level == Logger::LEVEL_PROFILE_END) {
				if (($last = array_pop($stack)) !== null && $last[0] === $token) {
Alexander Makarov committed
103
					$timings[$last[5]] = [count($stack), $token, $last[3], $timestamp - $last[3], $last[4]];
Qiang Xue committed
104 105 106 107 108 109 110
				}
			}
		}

		$now = microtime(true);
		while (($last = array_pop($stack)) !== null) {
			$delta = $now - $last[3];
Alexander Makarov committed
111
			$timings[$last[5]] = [count($stack), $last[0], $last[2], $delta, $last[4]];
Qiang Xue committed
112 113 114 115 116
		}
		ksort($timings);
		return $this->_timings = $timings;
	}

resurtm committed
117 118 119
	public function save()
	{
		$target = $this->module->logTarget;
Alexander Makarov committed
120 121
		$messages = $target->filterMessages($target->messages, Logger::LEVEL_PROFILE, ['yii\db\Command::queryInternal']);
		return ['messages' => $messages];
resurtm committed
122
	}
Qiang Xue committed
123
}