Class yii\debug\panels\DbPanel
Inheritance | yii\debug\panels\DbPanel » yii\debug\Panel » yii\base\Component |
---|---|
Available since extension's version | 2.0 |
Source Code | https://github.com/yiisoft/yii2-debug/blob/master/src/panels/DbPanel.php |
Debugger panel that collects and displays database queries performed.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$actions | array | Array of actions to add to the debug modules default controller. | yii\debug\Panel |
$criticalQueryThreshold | integer | The threshold for determining whether the request has involved critical number of DB queries. | yii\debug\panels\DbPanel |
$data | mixed | Data associated with panel | yii\debug\Panel |
$db | string | The name of the database component to use for executing (explain) queries | yii\debug\panels\DbPanel |
$defaultFilter | array | The default filter to apply to the database queries. | yii\debug\panels\DbPanel |
$defaultOrder | array | The default ordering of the database queries. | yii\debug\panels\DbPanel |
$detail | string | Content that is displayed in debugger detail view. | yii\debug\Panel |
$error | yii\debug\FlattenException|null | Error while saving the panel | yii\debug\Panel |
$id | string | Panel unique identifier. | yii\debug\Panel |
$module | yii\debug\Module | yii\debug\Panel | |
$name | string | Name of the panel. | yii\debug\Panel |
$profileLogs | array | This property is read-only. | yii\debug\panels\DbPanel |
$summary | string | Content that is displayed at debug toolbar. | yii\debug\Panel |
$summaryName | string | Short name of the panel, which will be use in summary. | yii\debug\panels\DbPanel |
$tag | string | Request data set identifier. | yii\debug\Panel |
$url | string | URL pointing to panel detail view. | yii\debug\Panel |
Public Methods
Method | Description | Defined By |
---|---|---|
calculateTimings() | Calculates given request profile timings. | yii\debug\panels\DbPanel |
canBeExplained() | Check if given query type can be explained. | yii\debug\panels\DbPanel |
countDuplicateQuery() | Return associative array, where key is query string and value is number of occurrences the same query in array. | yii\debug\panels\DbPanel |
getDb() | Returns a reference to the DB component associated with the panel | yii\debug\panels\DbPanel |
getDetail() | yii\debug\panels\DbPanel | |
getError() | yii\debug\Panel | |
getName() | yii\debug\panels\DbPanel | |
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'. | yii\debug\panels\DbPanel |
getSummary() | yii\debug\panels\DbPanel | |
getSummaryName() | yii\debug\panels\DbPanel | |
getTraceLine() | Returns a trace line | yii\debug\Panel |
getTypes() | Returns array query types | yii\debug\panels\DbPanel |
getUrl() | yii\debug\Panel | |
hasError() | yii\debug\Panel | |
init() | yii\debug\panels\DbPanel | |
isEnabled() | Checks whether this panel is enabled. | yii\debug\panels\DbPanel |
isQueryCountCritical() | Check if given queries count is critical according settings. | yii\debug\panels\DbPanel |
load() | Loads data into the panel | yii\debug\Panel |
save() | Saves data to be later used in debugger detail view. | yii\debug\panels\DbPanel |
setError() | yii\debug\Panel | |
sumDuplicateQueries() | Returns sum of all duplicated queries | yii\debug\panels\DbPanel |
Protected Methods
Method | Description | Defined By |
---|---|---|
getModels() | Returns an array of models that represents logs of the current request. | yii\debug\panels\DbPanel |
getQueryType() | Returns database query type. | yii\debug\panels\DbPanel |
getTotalQueryTime() | Returns total query time. | yii\debug\panels\DbPanel |
hasExplain() | yii\debug\panels\DbPanel |
Property Details
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.
The name of the database component to use for executing (explain) queries
The default filter to apply to the database queries. In the format of [ property => value ], for example: [ 'type' => 'SELECT' ]
The default ordering of the database queries. In the format of [ property => sort direction ], for example: [ 'duration' => SORT_DESC ]
Short name of the panel, which will be use in summary. This property is read-only.
Method Details
Calculates given request profile timings.
public array calculateTimings ( ) | ||
return | array |
Timings [token, category, timestamp, traces, nesting level, elapsed time] |
---|
public function calculateTimings()
{
if ($this->_timings === null) {
$this->_timings = Yii::getLogger()->calculateTimings(isset($this->data['messages']) ? $this->data['messages'] : []);
}
return $this->_timings;
}
Check if given query type can be explained.
public static boolean canBeExplained ( $type ) | ||
$type | string |
Query type |
public static function canBeExplained($type)
{
return $type !== 'SHOW';
}
Return associative array, where key is query string and value is number of occurrences the same query in array.
public array countDuplicateQuery ( $timings ) | ||
$timings |
public function countDuplicateQuery($timings)
{
$query = ArrayHelper::getColumn($timings, 'info');
return array_count_values($query);
}
Returns a reference to the DB component associated with the panel
public \yii\db\Connection getDb ( ) |
public function getDb()
{
return Yii::$app->get($this->db);
}
public string getDetail ( ) | ||
return | string |
Content that is displayed in debugger detail view |
---|
public function getDetail()
{
$searchModel = new Db();
if (!$searchModel->load(Yii::$app->request->getQueryParams())) {
$searchModel->load($this->defaultFilter, '');
}
$models = $this->getModels();
$dataProvider = $searchModel->search($models);
$dataProvider->getSort()->defaultOrder = $this->defaultOrder;
$sumDuplicates = $this->sumDuplicateQueries($models);
return Yii::$app->view->render('panels/db/detail', [
'panel' => $this,
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
'hasExplain' => $this->hasExplain(),
'sumDuplicates' => $sumDuplicates,
]);
}
Defined in: yii\debug\Panel::getError()
public yii\debug\FlattenException|null getError ( ) |
public function getError()
{
return $this->error;
}
Returns an array of models that represents logs of the current request.
Can be used with data providers such as \yii\data\ArrayDataProvider.
protected array getModels ( ) | ||
return | array |
Models |
---|
protected function getModels()
{
if ($this->_models === null) {
$this->_models = [];
$timings = $this->calculateTimings();
$duplicates = $this->countDuplicateQuery($timings);
foreach ($timings as $seq => $dbTiming) {
$this->_models[] = [
'type' => $this->getQueryType($dbTiming['info']),
'query' => $dbTiming['info'],
'duration' => ($dbTiming['duration'] * 1000), // in milliseconds
'trace' => $dbTiming['trace'],
'timestamp' => ($dbTiming['timestamp'] * 1000), // in milliseconds
'seq' => $seq,
'duplicate' => $duplicates[$dbTiming['info']],
];
}
}
return $this->_models;
}
public string getName ( ) | ||
return | string |
Name of the panel |
---|
public function getName()
{
return 'Database';
}
Returns all profile logs of the current request for this panel. It includes categories such as: 'yii\db\Command::query', 'yii\db\Command::execute'.
public array getProfileLogs ( ) |
public function getProfileLogs()
{
$target = $this->module->logTarget;
return $target->filterMessages($target->messages, Logger::LEVEL_PROFILE, ['yii\db\Command::query', 'yii\db\Command::execute']);
}
Returns database query type.
protected string getQueryType ( $timing ) | ||
$timing | string |
Timing procedure string |
return | string |
Query type such as select, insert, delete, etc. |
---|
protected function getQueryType($timing)
{
$timing = ltrim($timing);
preg_match('/^([a-zA-z]*)/', $timing, $matches);
return count($matches) ? mb_strtoupper($matches[0], 'utf8') : '';
}
public string getSummary ( ) | ||
return | string |
Content that is displayed at debug toolbar |
---|
public function getSummary()
{
$timings = $this->calculateTimings();
$queryCount = count($timings);
$queryTime = number_format($this->getTotalQueryTime($timings) * 1000) . ' ms';
return Yii::$app->view->render('panels/db/summary', [
'timings' => $this->calculateTimings(),
'panel' => $this,
'queryCount' => $queryCount,
'queryTime' => $queryTime,
]);
}
public string getSummaryName ( ) | ||
return | string |
Short name of the panel, which will be use in summary. |
---|
public function getSummaryName()
{
return 'DB';
}
Returns total query time.
protected integer getTotalQueryTime ( $timings ) | ||
$timings | array | |
return | integer |
Total time |
---|
protected function getTotalQueryTime($timings)
{
$queryTime = 0;
foreach ($timings as $timing) {
$queryTime += $timing['duration'];
}
return $queryTime;
}
Defined in: yii\debug\Panel::getTraceLine()
Returns a trace line
public string getTraceLine ( $options ) | ||
$options | array |
The array with trace |
return | string |
The trace line |
---|
public function getTraceLine($options)
{
if (!isset($options['text'])) {
$options['text'] = "{$options['file']}:{$options['line']}";
}
$traceLine = $this->module->traceLine;
if ($traceLine === false) {
return $options['text'];
}
$options['file'] = str_replace('\\', '/', $options['file']);
$rawLink = $traceLine instanceof \Closure ? $traceLine($options, $this) : $traceLine;
return strtr($rawLink, ['{file}' => $options['file'], '{line}' => $options['line'], '{text}' => $options['text']]);
}
Returns array query types
public array getTypes ( ) |
public function getTypes()
{
return array_reduce(
$this->_models,
function ($result, $item) {
$result[$item['type']] = $item['type'];
return $result;
},
[]
);
}
Defined in: yii\debug\Panel::getUrl()
public string getUrl ( $additionalParams = null ) | ||
$additionalParams | null|array |
Optional additional parameters to add to the route |
return | string |
URL pointing to panel detail view |
---|
public function getUrl($additionalParams = null)
{
$route = [
'/' . $this->module->id . '/default/view',
'panel' => $this->id,
'tag' => $this->tag,
];
if (is_array($additionalParams)){
$route = ArrayHelper::merge($route, $additionalParams);
}
return Url::toRoute($route);
}
Defined in: yii\debug\Panel::hasError()
public boolean hasError ( ) |
public function hasError()
{
return $this->error !== null;
}
protected boolean hasExplain ( ) | ||
return | boolean |
Whether the DB component has support for EXPLAIN queries |
---|
protected function hasExplain()
{
$db = $this->getDb();
if (!($db instanceof \yii\db\Connection)) {
return false;
}
switch ($db->getDriverName()) {
case 'mysql':
case 'sqlite':
case 'pgsql':
case 'cubrid':
return true;
default:
return false;
}
}
public void init ( ) |
public function init()
{
$this->actions['db-explain'] = [
'class' => 'yii\\debug\\actions\\db\\ExplainAction',
'panel' => $this,
];
}
Checks whether this panel is enabled.
public boolean isEnabled ( ) | ||
return | boolean |
Whether this panel is enabled. |
---|
public function isEnabled()
{
try {
$this->getDb();
} catch (InvalidConfigException $exception) {
return false;
}
return parent::isEnabled();
}
Check if given queries count is critical according settings.
public boolean isQueryCountCritical ( $count ) | ||
$count | integer |
Queries count |
public function isQueryCountCritical($count)
{
return (($this->criticalQueryThreshold !== null) && ($count > $this->criticalQueryThreshold));
}
Defined in: yii\debug\Panel::load()
Loads data into the panel
public void load ( $data ) | ||
$data | mixed |
public function load($data)
{
$this->data = $data;
}
Saves data to be later used in debugger detail view.
This method is called on every page where debugger is enabled.
public mixed save ( ) | ||
return | mixed |
Data to be saved |
---|
public function save()
{
return ['messages' => $this->getProfileLogs()];
}
Defined in: yii\debug\Panel::setError()
public void setError ( yii\debug\FlattenException $error ) | ||
$error | yii\debug\FlattenException |
public function setError(FlattenException $error)
{
$this->error = $error;
}
Returns sum of all duplicated queries
public integer sumDuplicateQueries ( $modelData ) | ||
$modelData |
public function sumDuplicateQueries($modelData)
{
$numDuplicates = 0;
$duplicates = ArrayHelper::getColumn($modelData, 'duplicate');
foreach ($duplicates as $duplicate) {
if ($duplicate > 1) {
$numDuplicates++;
}
}
return $numDuplicates;
}