yii2-arangodb/panels/arangodb/ArangoDbPanel.php

158 lines
4.5 KiB
PHP
Raw Normal View History

2014-08-05 13:28:36 +08:00
<?php
2017-04-14 04:44:05 +07:00
namespace explosivebit\arangodb\panels\arangodb;
2014-08-05 13:28:36 +08:00
2017-04-14 04:44:05 +07:00
use explosivebit\arangodb\panels\arangodb\models\ArangoDb;
2014-08-05 13:28:36 +08:00
use Yii;
use yii\debug\Panel;
use yii\log\Logger;
class ArangoDbPanel extends Panel
{
/**
* @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;
/**
* 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()
{
$target = $this->module->logTarget;
return $target->filterMessages(
$target->messages,
Logger::LEVEL_PROFILE,
[
2017-04-14 04:44:05 +07:00
'explosivebit\arangodb\Query::query',
'explosivebit\arangodb\Query::insert',
'explosivebit\arangodb\Query::update',
'explosivebit\arangodb\Query::remove',
'explosivebit\arangodb\Query::execute',
2014-08-05 13:28:36 +08:00
]
);
}
/**
* Calculates given request profile timings.
*
* @return array timings [token, category, timestamp, traces, nesting level, elapsed time]
*/
protected function calculateTimings()
{
if ($this->_timings === null) {
$this->_timings = Yii::getLogger()->calculateTimings($this->data['arango-messages']);
}
return $this->_timings;
}
/**
* Returns total query time.
*
* @param array $timings
* @return integer total time
*/
protected function getTotalQueryTime($timings)
{
$queryTime = 0;
foreach ($timings as $timing) {
$queryTime += $timing['duration'];
}
return $queryTime;
}
public function getName()
{
return 'Arango';
}
public function getSummary()
{
$timings = $this->calculateTimings();
$queryCount = count($timings);
$queryTime = number_format($this->getTotalQueryTime($timings) * 1000) . ' ms';
return \Yii::$app->view->render(
2017-04-14 04:44:05 +07:00
'@explosivebit/arangodb/panels/arangodb/views/summary',
2014-08-05 13:28:36 +08:00
[
'timings' => $this->calculateTimings(),
'queryCount' => $queryCount,
'queryTime' => $queryTime,
'panel' => $this,
]
);
}
/**
* @inheritdoc
*/
public function getDetail()
{
$searchModel = new ArangoDb();
$dataProvider = $searchModel->search(Yii::$app->request->getQueryParams(), $this->getModels());
2017-04-14 04:44:05 +07:00
return Yii::$app->view->render('@explosivebit/arangodb/panels/arangodb/views/detail', [
2014-08-05 13:28:36 +08:00
'panel' => $this,
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
]);
}
/**
* Returns an array of models that represents logs of the current request.
* Can be used with data providers such as \yii\data\ArrayDataProvider.
* @return array models
*/
protected function getModels()
{
if ($this->_models === null) {
$this->_models = [];
$timings = $this->calculateTimings();
foreach ($timings as $seq => $dbTiming) {
$this->_models[] = [
2014-08-12 16:54:30 +08:00
'type' => $this->getQueryType($dbTiming['category']),
2014-08-05 13:28:36 +08:00
'query' => $dbTiming['info'],
'duration' => ($dbTiming['duration'] * 1000), // in milliseconds
'trace' => $dbTiming['trace'],
'timestamp' => ($dbTiming['timestamp'] * 1000), // in milliseconds
'seq' => $seq,
];
}
}
return $this->_models;
}
2014-08-12 16:54:30 +08:00
protected function getQueryType($category) {
switch ($category) {
2017-04-14 04:44:05 +07:00
case 'explosivebit\arangodb\Query::query' :
2014-08-12 16:54:30 +08:00
return 'SELECT';
2017-04-14 04:44:05 +07:00
case 'explosivebit\arangodb\Query::insert' :
2014-08-12 16:54:30 +08:00
return 'INSERT';
2017-04-14 04:44:05 +07:00
case 'explosivebit\arangodb\Query::update' :
2014-08-12 16:54:30 +08:00
return 'UPDATE';
2017-04-14 04:44:05 +07:00
case 'explosivebit\arangodb\Query::remove' :
2014-08-12 16:54:30 +08:00
return 'REMOVE';
2017-04-14 04:44:05 +07:00
case 'explosivebit\arangodb\Query::execute' :
2014-08-12 16:54:30 +08:00
return 'EXECUTE';
default :
return '';
}
}
2014-08-05 13:28:36 +08:00
public function save()
{
return ['arango-messages' => $this->getProfileLogs()];
}
}