init
This commit is contained in:
115
vendor/yiisoft/yii2-mongodb/src/debug/ExplainAction.php
vendored
Normal file
115
vendor/yiisoft/yii2-mongodb/src/debug/ExplainAction.php
vendored
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\mongodb\debug;
|
||||
|
||||
use yii\base\Action;
|
||||
use yii\helpers\Json;
|
||||
use yii\web\HttpException;
|
||||
|
||||
/**
|
||||
* ExplainAction provides EXPLAIN information for MongoDB queries
|
||||
*
|
||||
* @author Sergey Smirnov <webdevsega@yandex.ru>
|
||||
* @author Klimov Paul <klimov@zfort.com>
|
||||
* @since 2.0.5
|
||||
*/
|
||||
class ExplainAction extends Action
|
||||
{
|
||||
/**
|
||||
* @var MongoDbPanel related debug toolbar panel
|
||||
*/
|
||||
public $panel;
|
||||
|
||||
|
||||
/**
|
||||
* Runs the explain action
|
||||
* @param int $seq
|
||||
* @param string $tag
|
||||
* @return string explain result content
|
||||
* @throws HttpException if requested log not found
|
||||
*/
|
||||
public function run($seq, $tag)
|
||||
{
|
||||
$this->controller->loadData($tag);
|
||||
|
||||
$timings = $this->panel->calculateTimings();
|
||||
|
||||
if (!isset($timings[$seq])) {
|
||||
throw new HttpException(404, 'Log message not found.');
|
||||
}
|
||||
|
||||
$query = $timings[$seq]['info'];
|
||||
|
||||
if (strpos($query, 'find({') !== 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$query = substr($query, strlen('find('), -1);
|
||||
$result = $this->explainQuery($query);
|
||||
if (!$result) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return Json::encode($result, JSON_PRETTY_PRINT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs explain command over the query
|
||||
*
|
||||
* @param string $queryString query log string.
|
||||
* @return array|false explain results, `false` on failure.
|
||||
*/
|
||||
protected function explainQuery($queryString)
|
||||
{
|
||||
/* @var $connection \yii\mongodb\Connection */
|
||||
$connection = $this->panel->getDb();
|
||||
|
||||
$queryInfo = Json::decode($queryString);
|
||||
if (!isset($queryInfo['ns'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
list($databaseName, $collectionName) = explode('.', $queryInfo['ns'], 2);
|
||||
unset($queryInfo['ns']);
|
||||
|
||||
if (!empty($queryInfo['filer'])) {
|
||||
$queryInfo['filer'] = $this->prepareQueryFiler($queryInfo['filer']);
|
||||
}
|
||||
|
||||
return $connection->createCommand($databaseName)->explain($collectionName, $queryInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare query filer for explain.
|
||||
* Converts BSON object log entries into actual objects.
|
||||
*
|
||||
* @param array $query raw query filter.
|
||||
* @return array|string prepared query
|
||||
*/
|
||||
private function prepareQueryFiler($query)
|
||||
{
|
||||
$result = [];
|
||||
foreach ($query as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
$result[$key] = $this->prepareQueryFiler($value);
|
||||
} elseif (is_string($value) && preg_match('#^(MongoDB\\\\BSON\\\\[A-Za-z]+)\\((.*)\\)$#s', $value, $matches)) {
|
||||
$class = $matches[1];
|
||||
$objectValue = $matches[1];
|
||||
|
||||
try {
|
||||
$result[$key] = new $class($objectValue);
|
||||
} catch (\Exception $e) {
|
||||
$result[$key] = $value;
|
||||
}
|
||||
} else {
|
||||
$result[$key] = $value;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
121
vendor/yiisoft/yii2-mongodb/src/debug/MongoDbPanel.php
vendored
Normal file
121
vendor/yiisoft/yii2-mongodb/src/debug/MongoDbPanel.php
vendored
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\mongodb\debug;
|
||||
|
||||
use Yii;
|
||||
use yii\debug\models\search\Db;
|
||||
use yii\debug\panels\DbPanel;
|
||||
use yii\log\Logger;
|
||||
|
||||
/**
|
||||
* MongoDbPanel panel that collects and displays MongoDB queries performed.
|
||||
*
|
||||
* @property array $profileLogs This property is read-only.
|
||||
*
|
||||
* @author Klimov Paul <klimov@zfort.com>
|
||||
* @since 2.0.1
|
||||
*/
|
||||
class MongoDbPanel extends DbPanel
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public $db = 'mongodb';
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->actions['mongodb-explain'] = [
|
||||
'class' => 'yii\\mongodb\\debug\\ExplainAction',
|
||||
'panel' => $this,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'MongoDB';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSummaryName()
|
||||
{
|
||||
return 'MongoDB';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDetail()
|
||||
{
|
||||
$searchModel = new Db();
|
||||
|
||||
if (!$searchModel->load(Yii::$app->request->getQueryParams())) {
|
||||
$searchModel->load($this->defaultFilter, '');
|
||||
}
|
||||
|
||||
$dataProvider = $searchModel->search($this->getModels());
|
||||
$dataProvider->getSort()->defaultOrder = $this->defaultOrder;
|
||||
|
||||
return Yii::$app->view->render('@yii/mongodb/debug/views/detail', [
|
||||
'panel' => $this,
|
||||
'dataProvider' => $dataProvider,
|
||||
'searchModel' => $searchModel,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all profile logs of the current request for this panel.
|
||||
* @return array
|
||||
*/
|
||||
public function getProfileLogs()
|
||||
{
|
||||
$target = $this->module->logTarget;
|
||||
|
||||
return $target->filterMessages($target->messages, Logger::LEVEL_PROFILE, [
|
||||
'yii\mongodb\Command::*',
|
||||
'yii\mongodb\Query::*',
|
||||
'yii\mongodb\BatchQueryResult::*',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function hasExplain()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getQueryType($timing)
|
||||
{
|
||||
$timing = ltrim($timing);
|
||||
$timing = mb_substr($timing, 0, mb_strpos($timing, '('), 'utf8');
|
||||
$matches = explode('.', $timing);
|
||||
|
||||
return count($matches) ? array_pop($matches) : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function canBeExplained($type)
|
||||
{
|
||||
return $type === 'find';
|
||||
}
|
||||
}
|
||||
116
vendor/yiisoft/yii2-mongodb/src/debug/views/detail.php
vendored
Normal file
116
vendor/yiisoft/yii2-mongodb/src/debug/views/detail.php
vendored
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
/* @var $panel yii\mongodb\debug\MongoDbPanel */
|
||||
/* @var $searchModel yii\debug\models\search\Db */
|
||||
/* @var $dataProvider yii\data\ArrayDataProvider */
|
||||
|
||||
use yii\helpers\Html;
|
||||
use yii\grid\GridView;
|
||||
use yii\web\View;
|
||||
|
||||
echo Html::tag('h1', $panel->getName() . ' Queries');
|
||||
|
||||
echo GridView::widget([
|
||||
'dataProvider' => $dataProvider,
|
||||
'id' => 'db-panel-detailed-grid',
|
||||
'options' => ['class' => 'detail-grid-view table-responsive'],
|
||||
'filterModel' => $searchModel,
|
||||
'filterUrl' => $panel->getUrl(),
|
||||
'columns' => [
|
||||
[
|
||||
'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' => 'type',
|
||||
'value' => function ($data) {
|
||||
return Html::encode($data['type']);
|
||||
},
|
||||
'filter' => $panel->getTypes(),
|
||||
],
|
||||
[
|
||||
'attribute' => 'query',
|
||||
'value' => function ($data) use ($panel) {
|
||||
$query = Html::encode($data['query']);
|
||||
|
||||
if (!empty($data['trace'])) {
|
||||
$query .= Html::ul($data['trace'], [
|
||||
'class' => 'trace',
|
||||
'item' => function ($trace) use ($panel) {
|
||||
return '<li>' . $panel->getTraceLine($trace) . '</li>';
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
if ($panel->canBeExplained($data['type'])) {
|
||||
$query .= Html::tag('p', '', ['class' => 'db-explain-text']);
|
||||
|
||||
$query .= Html::tag(
|
||||
'div',
|
||||
Html::a('[+] Explain', (['mongodb-explain', 'seq' => $data['seq'], 'tag' => Yii::$app->controller->summary['tag']])),
|
||||
['class' => 'db-explain']
|
||||
);
|
||||
}
|
||||
|
||||
return $query;
|
||||
},
|
||||
'format' => 'raw',
|
||||
'options' => [
|
||||
'width' => '60%',
|
||||
],
|
||||
]
|
||||
],
|
||||
]);
|
||||
|
||||
echo Html::tag(
|
||||
'div',
|
||||
Html::a('[+] Explain all', '#'),
|
||||
['id' => 'db-explain-all']
|
||||
);
|
||||
|
||||
$this->registerJs('debug_db_detail();', View::POS_READY);
|
||||
?>
|
||||
|
||||
<script>
|
||||
function debug_db_detail() {
|
||||
$('.db-explain a').on('click', function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
var $explain = $('.db-explain-text', $(this).parent().parent());
|
||||
|
||||
if ($explain.is(':visible')) {
|
||||
$explain.hide();
|
||||
$(this).text('[+] Explain');
|
||||
} else {
|
||||
$explain.load($(this).attr('href')).show();
|
||||
$(this).text('[-] Explain');
|
||||
}
|
||||
});
|
||||
|
||||
$('#db-explain-all a').on('click', function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
$('.db-explain a').click();
|
||||
});
|
||||
}
|
||||
</script>
|
||||
Reference in New Issue
Block a user