DbPanel.php 3.38 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
{
23 24 25 26 27 28 29 30 31 32 33

	/**
	 * @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;

Qiang Xue committed
34 35 36 37
	public function getName()
	{
		return 'Database';
	}
resurtm committed
38 39 40

	public function getSummary()
	{
Qiang Xue committed
41 42
		$timings = $this->calculateTimings();
		$queryCount = count($timings);
43 44
		$queryTime = number_format($this->getTotalQueryTime($timings) * 1000) . ' ms';

45
		return Yii::$app->view->render('panels/db/summary', [
46 47 48 49 50
			'timings' => $this->calculateTimings(), 
			'panel' => $this,
			'queryCount' => $queryCount,
			'queryTime' => $queryTime,
		]);
resurtm committed
51 52 53 54
	}

	public function getDetail()
	{
55
		$searchModel = new Db();
Mark committed
56
		$dataProvider = $searchModel->search(Yii::$app->request->get(), $this->getModels());
resurtm committed
57

58
		return Yii::$app->view->render('panels/db/detail', [
59 60 61 62 63
			'panel' => $this,
			'dataProvider' => $dataProvider,
			'searchModel' => $searchModel,
		]);
	}
Qiang Xue committed
64

65 66 67 68
	/**
	 * Calculates given request profile messages timings.
	 * @return array timings [token, category, timestamp, traces, nesting level, elapsed time]
	 */
Qiang Xue committed
69 70
	protected function calculateTimings()
	{
71 72
		if ($this->_timings === null) {
			$this->_timings = Yii::$app->getLog()->calculateTimings($this->data['messages']);
Qiang Xue committed
73
		}
74
		return $this->_timings;
Qiang Xue committed
75 76
	}

resurtm committed
77 78 79
	public function save()
	{
		$target = $this->module->logTarget;
80
		$messages = $target->filterMessages($target->messages, Logger::LEVEL_PROFILE, ['yii\db\Command::query', 'yii\db\Command::execute']);
Alexander Makarov committed
81
		return ['messages' => $messages];
resurtm committed
82
	}
83 84 85 86 87 88 89 90 91 92 93

	/**
	 * Returns total queries time.
	 * @param array $timings
	 * @return integer total time
	 */
	protected function getTotalQueryTime($timings)
	{
		$queryTime = 0;

		foreach ($timings as $timing) {
94
			$queryTime += $timing['duration'];
95 96 97 98 99 100 101 102 103 104 105 106
		}

		return $queryTime;
	}

	/**
	 * Returns array of models that represents logs of the current request. Can be used with data providers,
	 * like yii\data\ArrayDataProvider.
	 * @return array models
	 */
	protected function getModels()
	{
107
		if ($this->_models === null) {
108 109 110
			$this->_models = [];
			$timings = $this->calculateTimings();

111
			foreach($timings as $seq => $dbTiming) {
112
				$this->_models[] = 	[
113 114
					'type' => $this->detectQueryType($dbTiming['info']),
					'query' => $dbTiming['info'],
115
					'duration' => ($dbTiming['duration'] * 1000), // in milliseconds
116
					'trace' => $dbTiming['trace'],
117
					'timestamp' => ($dbTiming['timestamp'] * 1000), // in milliseconds
118
					'seq' => $seq,
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
				];
			}
		}
		return $this->_models;
	}

	/**
	 * Detects databse timing type. Detecting is produced through simple parsing to the first space|tab|new row.
	 * First word before space is timing type. If there is no such words, timing will have empty type.
	 * @param string $timing timing procedure string
	 * @return string query type select|insert|delete|etc
	 */
	protected function detectQueryType($timing)
	{
		$timing = ltrim($timing);
		preg_match('/^([a-zA-z]*)/', $timing, $matches);
		return count($matches) ? $matches[0] : '';
	}

Qiang Xue committed
138
}