DbPanel.php 4.11 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.
 *
Qiang Xue committed
18 19 20 21 22
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class DbPanel extends Panel
{
Mark committed
23
	/**
24 25 26
	 * @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
27
	 */
28
	public $criticalQueryThreshold;
29 30 31 32 33 34 35 36 37 38
	/**
	 * @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;

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

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

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

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

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

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

92 93 94
	/**
	 * @inheritdoc
	 */
resurtm committed
95
	public function save()
Mark committed
96 97 98 99 100 101 102 103 104 105
	{
		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
106 107
	{
		$target = $this->module->logTarget;
Mark committed
108
		return $target->filterMessages($target->messages, Logger::LEVEL_PROFILE, ['yii\db\Command::query', 'yii\db\Command::execute']);
resurtm committed
109
	}
110 111

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

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

		return $queryTime;
	}

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

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

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

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

Qiang Xue committed
177
}