DbPanel.php 4.17 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

10
use Yii;
Qiang Xue committed
11
use yii\debug\Panel;
resurtm committed
12
use yii\log\Logger;
13
use yii\debug\models\search\Db;
Qiang Xue committed
14 15

/**
16 17
 * Debugger panel that collects and displays database queries performed.
 *
18 19
 * @property array $profileLogs This property is read-only.
 *
Qiang Xue committed
20 21 22 23 24
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class DbPanel extends Panel
{
Mark committed
25
	/**
26 27 28
	 * @var integer the threshold for determining whether the request has involved 
	 * critical number of DB queries. If the number of queries exceeds this number, 
	 * the execution is considered taking critical number of DB queries.
Mark committed
29
	 */
30
	public $criticalQueryThreshold;
31 32 33 34 35 36 37 38 39 40
	/**
	 * @var array db queries info extracted to array as models, to use with data provider.
	 */
	private $_models;

	/**
	 * @var array current database request timings
	 */
	private $_timings;

41 42 43
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
44 45 46 47
	public function getName()
	{
		return 'Database';
	}
resurtm committed
48

49 50 51
	/**
	 * @inheritdoc
	 */
resurtm committed
52 53
	public function getSummary()
	{
Qiang Xue committed
54 55
		$timings = $this->calculateTimings();
		$queryCount = count($timings);
56 57
		$queryTime = number_format($this->getTotalQueryTime($timings) * 1000) . ' ms';

58
		return Yii::$app->view->render('panels/db/summary', [
Tobias Munk committed
59
			'timings' => $this->calculateTimings(),
60 61 62 63
			'panel' => $this,
			'queryCount' => $queryCount,
			'queryTime' => $queryTime,
		]);
resurtm committed
64 65
	}

66 67 68
	/**
	 * @inheritdoc
	 */
resurtm committed
69 70
	public function getDetail()
	{
71
		$searchModel = new Db();
72
		$dataProvider = $searchModel->search(Yii::$app->request->getQueryParams(), $this->getModels());
resurtm committed
73

74
		return Yii::$app->view->render('panels/db/detail', [
75 76 77 78 79
			'panel' => $this,
			'dataProvider' => $dataProvider,
			'searchModel' => $searchModel,
		]);
	}
Qiang Xue committed
80

81
	/**
82 83
	 * Calculates given request profile timings.
	 *
84 85
	 * @return array timings [token, category, timestamp, traces, nesting level, elapsed time]
	 */
Qiang Xue committed
86 87
	protected function calculateTimings()
	{
88 89
		if ($this->_timings === null) {
			$this->_timings = Yii::$app->getLog()->calculateTimings($this->data['messages']);
Qiang Xue committed
90
		}
91
		return $this->_timings;
Qiang Xue committed
92 93
	}

94 95 96
	/**
	 * @inheritdoc
	 */
resurtm committed
97
	public function save()
Mark committed
98 99 100 101 102 103 104 105 106 107
	{
		return ['messages' => $this->getProfileLogs()];
	}

	/**
	 * Returns all profile logs of the current request for this panel. It includes categories such as:
	 * 'yii\db\Command::query', 'yii\db\Command::execute'.
	 * @return array
	 */
	public function getProfileLogs()
resurtm committed
108 109
	{
		$target = $this->module->logTarget;
Mark committed
110
		return $target->filterMessages($target->messages, Logger::LEVEL_PROFILE, ['yii\db\Command::query', 'yii\db\Command::execute']);
resurtm committed
111
	}
112 113

	/**
114 115
	 * Returns total query time.
	 *
116 117 118 119 120 121 122 123
	 * @param array $timings
	 * @return integer total time
	 */
	protected function getTotalQueryTime($timings)
	{
		$queryTime = 0;

		foreach ($timings as $timing) {
124
			$queryTime += $timing['duration'];
125 126 127 128 129 130
		}

		return $queryTime;
	}

	/**
131 132
	 * Returns an  array of models that represents logs of the current request.
	 * Can be used with data providers such as \yii\data\ArrayDataProvider.
133 134 135 136
	 * @return array models
	 */
	protected function getModels()
	{
137
		if ($this->_models === null) {
138 139 140
			$this->_models = [];
			$timings = $this->calculateTimings();

Tobias Munk committed
141
			foreach ($timings as $seq => $dbTiming) {
142
				$this->_models[] = 	[
143
					'type' => $this->getQueryType($dbTiming['info']),
144
					'query' => $dbTiming['info'],
145
					'duration' => ($dbTiming['duration'] * 1000), // in milliseconds
146
					'trace' => $dbTiming['trace'],
147
					'timestamp' => ($dbTiming['timestamp'] * 1000), // in milliseconds
148
					'seq' => $seq,
149 150 151 152 153 154 155
				];
			}
		}
		return $this->_models;
	}

	/**
156 157
	 * Returns databse query type.
	 *
158
	 * @param string $timing timing procedure string
159
	 * @return string query type such as select, insert, delete, etc.
160
	 */
161
	protected function getQueryType($timing)
162 163 164 165 166
	{
		$timing = ltrim($timing);
		preg_match('/^([a-zA-z]*)/', $timing, $matches);
		return count($matches) ? $matches[0] : '';
	}
Mark committed
167 168 169

	/**
	 * Check if given queries count is critical according settings.
170 171
	 * 
	 * @param integer $count queries count
Mark committed
172 173
	 * @return boolean
	 */
174
	public function isQueryCountCritical($count)
Mark committed
175
	{
176
		return (($this->criticalQueryThreshold !== null) && ($count > $this->criticalQueryThreshold));
Mark committed
177
	}
Mark committed
178

Qiang Xue committed
179
}