diff --git a/src/debug/DebugPanel.php b/src/debug/DebugPanel.php index 6e7ec29..529daa7 100644 --- a/src/debug/DebugPanel.php +++ b/src/debug/DebugPanel.php @@ -13,10 +13,7 @@ use hiqdev\hiart\Command; use Yii; use yii\base\ViewContextInterface; -use yii\helpers\ArrayHelper; -use yii\helpers\Html; -use yii\helpers\StringHelper; -use yii\helpers\Url; +use yii\base\InvalidConfigException; use yii\log\Logger; /** @@ -31,7 +28,7 @@ public function init() $this->actions['hiart-query'] = [ 'class' => DebugAction::class, 'panel' => $this, - 'db' => $this->db, + 'db' => $this->db, ]; } @@ -48,25 +45,17 @@ public function getName() */ public function getSummary() { - $timings = $this->calculateTimings(); - $queryCount = count($timings); - $queryTime = 0; + $timings = $this->getTimings(); + $total = 0; foreach ($timings as $timing) { - $queryTime += $timing[3]; + $total += $timing[3]; } - $queryTime = number_format($queryTime * 1000) . ' ms'; - $url = $this->getUrl(); - $output = << - - HiArt - $queryCount - $queryTime - - -HTML; - - return $queryCount > 0 ? $output : ''; + + return $this->render('summary', [ + 'url' => $this->getUrl(), + 'count' => count($timings), + 'total' => number_format($total * 1000) . ' ms', + ]); } /** @@ -74,76 +63,33 @@ public function getSummary() */ public function getDetail() { - $apiUrl = null; - $timings = $this->calculateTimings(); - ArrayHelper::multisort($timings, 3, SORT_DESC); + return $this->render('detail', [ + 'timings' => Timing::buildAll($this), + ]); + } - // Try to get API URL + public function getBaseUri($dbname) + { try { - $component = Yii::$app->get('hiart'); - $apiUrl = (StringHelper::endsWith($component->config['base_uri'], - '/')) ? $component->config['base_uri'] : $component->config['base_uri'] . '/'; - } catch (\yii\base\InvalidConfigException $e) { - // Pass + return Yii::$app->get($dbname)->getBaseUri(); + } catch (InvalidConfigException $e) { + return null; } + } - $rows = []; - foreach ($timings as $logId => $timing) { - $message = $timing[1]; - $traces = $timing[4]; - if (($pos = mb_strpos($message, '#')) !== false) { - $url = mb_substr($message, 0, $pos); - $body = mb_substr($message, $pos + 1); - } else { - $url = $message; - $body = null; - } - - $traceString = ''; - if (!empty($traces)) { - $traceString .= Html::ul($traces, [ - 'class' => 'trace', - 'item' => function ($trace) { - return "
  • {$trace['file']}({$trace['line']})
  • "; - }, - ]); - } - - $ajaxUrl = Url::to(['hiart-query', 'logId' => $logId, 'tag' => $this->tag]); - $runLink = Html::a('run query', $ajaxUrl, [ - 'class' => 'hiart-link', - 'data' => ['id' => $logId], - ]) . '
    '; - - $path = preg_replace('/^[A-Z]+\s+/', '', $url); - if (strpos($path, '?') !== false) { - $newTabUrl = $apiUrl . rtrim($path, '&') . '&' . $body; - } else { - $newTabUrl = $apiUrl . $path . '?' . $body; - } + private $_timings; - $rows[] = [ - 'logId' => $logId, - 'duration' => sprintf('%.1f ms', $timing[3] * 1000), - 'traceString' => $traceString, - 'runLink' => $runLink, - 'newTabLink' => Html::a('to new tab', $newTabUrl, ['target' => '_blank']) . '
    ', - 'urlEncoded' => Html::encode((isset($apiUrl)) ? str_replace(' ', ' ' . $apiUrl, $url) : $url), - 'bodyEncoded' => Html::encode($body), - ]; + public function getTimings() + { + if ($this->_timings === null) { + $this->_timings = $this->calculateTimings(); } - return $this->render('detail', compact('rows')); + return $this->_timings; } - private $_timings; - public function calculateTimings() { - if ($this->_timings !== null) { - return $this->_timings; - } - $messages = $this->data['messages']; $timings = []; $stack = []; @@ -167,7 +113,7 @@ public function calculateTimings() } ksort($timings); - return $this->_timings = $timings; + return $timings; } /** diff --git a/src/debug/Timing.php b/src/debug/Timing.php new file mode 100644 index 0000000..17fd5cd --- /dev/null +++ b/src/debug/Timing.php @@ -0,0 +1,135 @@ +panel = $panel; + $this->logId = $logId; + } + + public static function buildAll(DebugPanel $panel) + { + $rawTimings = $panel->getTimings(); + ArrayHelper::multisort($rawTimings, 3, SORT_DESC); + + $timings = []; + foreach ($rawTimings as $logId => $rawTiming) { + $timings[] = static::buildOne($panel, $logId, $rawTiming); + } + + return $timings; + } + + public static function buildOne($panel, $logId, $rawTiming) + { + $new = new static($panel, $logId); + $new->updateFromRaw($rawTiming); + + return $new; + } + + public function updateFromRaw($rawTiming) + { + $this->duration = $rawTiming[3]; + $this->traces = $rawTiming[4]; + $profile = $rawTiming[1]; + + foreach (Request::decodeProfile($profile) as $key => $value) { + $this->{$key} = $value; + } + } + + public function getLogId() + { + return $this->logId; + } + + public function getMethod() + { + return $this->method; + } + + public function getUrlEncoded() + { + return Html::encode($this->getFullUri()); + } + + public function getBodyEncoded() + { + return Html::encode($this->body); + } + + public function getDuration() + { + return sprintf('%.1f ms', $this->duration * 1000); + } + + public function getTrace() + { + $result = ''; + if (!empty($this->traces)) { + $result .= Html::ul($this->traces, [ + 'class' => 'trace', + 'item' => function ($trace) { + return "
  • {$trace['file']}({$trace['line']})
  • "; + }, + ]); + } + + return $result; + } + + public function getRunLink() + { + $ajaxUrl = Url::to(['hiart-query', 'logId' => $this->logId, 'tag' => $this->panel->tag]); + + return Html::a('run query', $ajaxUrl, [ + 'class' => 'hiart-link', + 'data' => ['id' => $this->logId], + ]); + } + + public function getNewTabLink() + { + $sign = strpos($this->uri, '?') === false ? '?' : ''; + $newTabUrl = rtrim($this->getFullUri(), '&') . $sign . $this->body; + + return Html::a('to new tab', $newTabUrl, ['target' => '_blank']); + } + + public function getFullUri() + { + return $this->getBaseUri() . '/'. $this->uri; + } + + public function getBaseUri() + { + $this->panel->getBaseUri($this->dbname); + } +} diff --git a/src/views/debug/detail.php b/src/views/debug/detail.php index 7513e81..b799b09 100644 --- a/src/views/debug/detail.php +++ b/src/views/debug/detail.php @@ -8,6 +8,7 @@ .boolean { color: blue; } .null { color: magenta; } .key { color: red; } + .white-space-normal { white-space: normal; } CSS ); @@ -57,7 +58,7 @@ function syntaxHighlight(json) { }, error: function (jqXHR, textStatus, errorThrown) { result.find('.time').html(''); - result.find('.result').html('Error: ' + errorThrown + ' - ' + textStatus + '
    ' + jqXHR.responseText); + result.find('.result').html('Error: ' + errorThrown + ' - ' + textStatus + '
    ' + jqXHR.responseText); }, dataType: 'json' }); @@ -69,22 +70,29 @@ function syntaxHighlight(json) {

    HiArt Queries

    - +
    - - - + + + - + - - - + + + - + diff --git a/src/views/debug/summary.php b/src/views/debug/summary.php new file mode 100644 index 0000000..1148407 --- /dev/null +++ b/src/views/debug/summary.php @@ -0,0 +1,10 @@ + +
    + + HiArt + + + +
    TimeUrl / QueryRun Query on nodeTimeUrl / QueryRun Query

    getDuration() ?> + getMethod() ?> getUrlEncoded() ?>
    +

    getBodyEncoded() ?>

    + getTrace() ?> +
    + getRunLink() ?>
    + getNewTabLink() ?> +