add ArangoDb debug panel

This commit is contained in:
evgen-d 2014-08-05 09:28:36 +04:00
parent e6bac2988d
commit 859a637f42
4 changed files with 286 additions and 0 deletions

View File

@ -0,0 +1,138 @@
<?php
namespace devgroup\arangodb\panels\arangodb;
use devgroup\arangodb\panels\arangodb\models\ArangoDb;
use Yii;
use yii\debug\Panel;
use yii\helpers\VarDumper;
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,
[
'devgroup\arangodb\Query::query',
'devgroup\arangodb\Query::execute',
]
);
}
/**
* 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(
'@devgroup/arangodb/panels/arangodb/views/summary',
[
'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());
return Yii::$app->view->render('@devgroup/arangodb/panels/arangodb/views/detail', [
'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[] = [
'query' => $dbTiming['info'],
'duration' => ($dbTiming['duration'] * 1000), // in milliseconds
'trace' => $dbTiming['trace'],
'timestamp' => ($dbTiming['timestamp'] * 1000), // in milliseconds
'seq' => $seq,
];
}
}
return $this->_models;
}
public function save()
{
return ['arango-messages' => $this->getProfileLogs()];
}
}

View File

@ -0,0 +1,67 @@
<?php
namespace devgroup\arangodb\panels\arangodb\models;
use yii\data\ArrayDataProvider;
use yii\debug\components\search\Filter;
use yii\debug\models\search\Base;
class ArangoDb extends Base
{
/**
* @var integer query attribute input search value
*/
public $query;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['query'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'query' => 'Query',
];
}
/**
* Returns data provider with filled models. Filter applied if needed.
*
* @param array $params an array of parameter values indexed by parameter names
* @param array $models data to return provider for
* @return \yii\data\ArrayDataProvider
*/
public function search($params, $models)
{
$dataProvider = new ArrayDataProvider([
'allModels' => $models,
'pagination' => false,
'sort' => [
'attributes' => ['duration', 'seq', 'query'],
'defaultOrder' => [
'duration' => SORT_DESC,
],
],
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
$filter = new Filter();
$this->addCondition($filter, 'query', true);
$dataProvider->allModels = $filter->filter($models);
return $dataProvider;
}
}

View File

@ -0,0 +1,69 @@
<?php
/* @var $panel yii\debug\panels\DbPanel */
/* @var $searchModel yii\debug\models\search\Db */
/* @var $dataProvider yii\data\ArrayDataProvider */
use yii\helpers\Html;
use yii\grid\GridView;
?>
<h1>Arango Database Queries</h1>
<?php
echo GridView::widget([
'dataProvider' => $dataProvider,
'id' => 'arango-db-panel-detailed-grid',
'options' => ['class' => 'detail-grid-view'],
'filterModel' => $searchModel,
'filterUrl' => $panel->getUrl(),
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'seq',
'label' => 'Time',
'value' => function ($data) {
$timeInSeconds = $data['timestamp'] / 1000;
$millisecondsDiff = (int) (($timeInSeconds - (int) $timeInSeconds) * 1000);
return date('H:i:s.', $timeInSeconds) . sprintf('%03d', $millisecondsDiff);
},
'headerOptions' => [
'class' => 'sort-numerical'
]
],
[
'attribute' => 'duration',
'value' => function ($data) {
return sprintf('%.1f ms', $data['duration']);
},
'options' => [
'width' => '10%',
],
'headerOptions' => [
'class' => 'sort-numerical'
]
],
[
'attribute' => 'query',
'value' => function ($data) {
$query = Html::encode($data['query']);
if (!empty($data['trace'])) {
$query .= Html::ul($data['trace'], [
'class' => 'trace',
'item' => function ($trace) {
return "<li>{$trace['file']} ({$trace['line']})</li>";
},
]);
}
return $query;
},
'format' => 'html',
'options' => [
'width' => '60%',
],
]
],
]);

View File

@ -0,0 +1,12 @@
<?php
/* @var $panel \devgroup\arangodb\panels\arangodb\ArangoDbPanel */
/* @var $queryCount integer */
/* @var $queryTime integer */
?>
<?php if ($queryCount): ?>
<div class="yii-debug-toolbar-block">
<a href="<?= $panel->getUrl() ?>" title="Executed <?= $queryCount ?> database queries which took <?= $queryTime ?>.">
Ar. DB <span class="label label-info"><?= $queryCount ?></span> <span class="label"><?= $queryTime ?></span>
</a>
</div>
<?php endif; ?>