DbPanel.php 3.88 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 96 97
	public function save()
	{
		$target = $this->module->logTarget;
98
		$messages = $target->filterMessages($target->messages, Logger::LEVEL_PROFILE, ['yii\db\Command::query', 'yii\db\Command::execute']);
Alexander Makarov committed
99
		return ['messages' => $messages];
resurtm committed
100
	}
101 102

	/**
103 104
	 * Returns total query time.
	 *
105 106 107 108 109 110 111 112
	 * @param array $timings
	 * @return integer total time
	 */
	protected function getTotalQueryTime($timings)
	{
		$queryTime = 0;

		foreach ($timings as $timing) {
113
			$queryTime += $timing['duration'];
114 115 116 117 118 119
		}

		return $queryTime;
	}

	/**
120 121
	 * Returns an  array of models that represents logs of the current request.
	 * Can be used with data providers such as \yii\data\ArrayDataProvider.
122 123 124 125
	 * @return array models
	 */
	protected function getModels()
	{
126
		if ($this->_models === null) {
127 128 129
			$this->_models = [];
			$timings = $this->calculateTimings();

Tobias Munk committed
130
			foreach ($timings as $seq => $dbTiming) {
131
				$this->_models[] = 	[
132
					'type' => $this->getQueryType($dbTiming['info']),
133
					'query' => $dbTiming['info'],
134
					'duration' => ($dbTiming['duration'] * 1000), // in milliseconds
135
					'trace' => $dbTiming['trace'],
136
					'timestamp' => ($dbTiming['timestamp'] * 1000), // in milliseconds
137
					'seq' => $seq,
138 139 140 141 142 143 144
				];
			}
		}
		return $this->_models;
	}

	/**
145 146
	 * Returns databse query type.
	 *
147
	 * @param string $timing timing procedure string
148
	 * @return string query type such as select, insert, delete, etc.
149
	 */
150
	protected function getQueryType($timing)
151 152 153 154 155
	{
		$timing = ltrim($timing);
		preg_match('/^([a-zA-z]*)/', $timing, $matches);
		return count($matches) ? $matches[0] : '';
	}
Mark committed
156 157 158

	/**
	 * Check if given queries count is critical according settings.
159 160
	 * 
	 * @param integer $count queries count
Mark committed
161 162
	 * @return boolean
	 */
163
	public function isQueryCountCritical($count)
Mark committed
164
	{
165
		return (($this->criticalQueryThreshold !== null) && ($count > $this->criticalQueryThreshold));
Mark committed
166
	}
Qiang Xue committed
167
}