Skip to content

Commit

Permalink
improved debug rendering with Timing STILL redo NOT FINISHED
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Jan 19, 2017
1 parent b6234de commit 935c7cf
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 92 deletions.
110 changes: 28 additions & 82 deletions src/debug/DebugPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -31,7 +28,7 @@ public function init()
$this->actions['hiart-query'] = [
'class' => DebugAction::class,
'panel' => $this,
'db' => $this->db,
'db' => $this->db,
];
}

Expand All @@ -48,102 +45,51 @@ 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 = <<<HTML
<div class="yii-debug-toolbar__block">
<a href="$url" title="Executed $queryCount queries which took $queryTime.">
HiArt
<span class="yii-debug-toolbar__label">$queryCount</span>
<span class="yii-debug-toolbar__label">$queryTime</span>
</a>
</div>
HTML;

return $queryCount > 0 ? $output : '';

return $this->render('summary', [
'url' => $this->getUrl(),
'count' => count($timings),
'total' => number_format($total * 1000) . ' ms',
]);
}

/**
* {@inheritdoc}
*/
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 "<li>{$trace['file']}({$trace['line']})</li>";
},
]);
}

$ajaxUrl = Url::to(['hiart-query', 'logId' => $logId, 'tag' => $this->tag]);
$runLink = Html::a('run query', $ajaxUrl, [
'class' => 'hiart-link',
'data' => ['id' => $logId],
]) . '<br/>';

$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']) . '<br/>',
'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 = [];
Expand All @@ -167,7 +113,7 @@ public function calculateTimings()
}
ksort($timings);

return $this->_timings = $timings;
return $timings;
}

/**
Expand Down
135 changes: 135 additions & 0 deletions src/debug/Timing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php
/**
* Tools to use API as ActiveRecord for Yii2
*
* @link https://github.com/hiqdev/yii2-hiart
* @package yii2-hiart
* @license BSD-3-Clause
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
*/

namespace hiqdev\hiart\debug;

use hiqdev\hiart\Request;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Url;

class Timing
{
protected $panel;

protected $logId;
protected $duration;
protected $traces;
protected $dbname;
protected $method;
protected $uri;
protected $body;

public function __construct(DebugPanel $panel, $logId)
{
$this->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 "<li>{$trace['file']}({$trace['line']})</li>";
},
]);
}

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);
}
}
28 changes: 18 additions & 10 deletions src/views/debug/detail.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
.boolean { color: blue; }
.null { color: magenta; }
.key { color: red; }
.white-space-normal { white-space: normal; }
CSS
);

Expand Down Expand Up @@ -57,7 +58,7 @@ function syntaxHighlight(json) {
},
error: function (jqXHR, textStatus, errorThrown) {
result.find('.time').html('');
result.find('.result').html('<span style="color: #c00;">Error: ' + errorThrown + ' - ' + textStatus + '</span><br />' + jqXHR.responseText);
result.find('.result').html('<span style="color: #c00">Error: ' + errorThrown + ' - ' + textStatus + '</span><br />' + jqXHR.responseText);
},
dataType: 'json'
});
Expand All @@ -69,22 +70,29 @@ function syntaxHighlight(json) {

<h1>HiArt Queries</h1>

<table class="table table-condensed table-bordered table-striped table-hover" style="table-layout: fixed;">
<table class="table table-condensed table-bordered table-striped table-hover" style="table-layout: fixed">
<thead>
<tr>
<th style="width: 10%;">Time</th>
<th style="width: 75%;">Url / Query</th>
<th style="width: 15%;">Run Query on node</th>
<th style="width: 10%">Time</th>
<th style="width: 80%">Url / Query</th>
<th style="width: 10%">Run Query</th>
</tr>
</thead>
<tbody>
<?php foreach ($rows as $row) : ?>
<?php foreach ($timings as $timing) : ?>
<tr>
<td style="width: 10%;"><?= $row['duration'] ?></td>
<td style="width: 75%;"><div><b><?= $row['urlEncoded'] ?></b><br/><p><?= $row['bodyEncoded'] ?></p><?= $row['traceString'] ?></div></td>
<td style="width: 15%;"><?= $row['runLink'] ?><?= $row['newTabLink'] ?></td>
<td style="width: 10%"><?= $timing->getDuration() ?></td>
<td style="width: 75%" class="white-space-normal">
<b><?= $timing->getMethod() ?> <?= $timing->getUrlEncoded() ?></b><br/>
<p><?= $timing->getBodyEncoded() ?></p>
<?= $timing->getTrace() ?>
</div></td>
<td style="width: 15%" class="white-space-normal">
<?= $timing->getRunLink() ?><br/>
<?= $timing->getNewTabLink() ?>
</td>
</tr>
<tr style="display: none;" class="hiart-wrapper" data-id="<?= $row['logId'] ?>">
<tr style="display: none" class="hiart-wrapper" data-id="<?= $timing->getLogId() ?>">
<td class="time"></td><td colspan="3" class="result"></td>
</tr>
<?php endforeach ?>
Expand Down
10 changes: 10 additions & 0 deletions src/views/debug/summary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

?>
<div class="yii-debug-toolbar__block">
<a href="<?= $url ?>" title="Executed <?= $count ?> queries which took <?= $total ?>.">
HiArt
<span class="yii-debug-toolbar__label"><?= $count ?></span>
<span class="yii-debug-toolbar__label"><?= $total ?></span>
</a>
</div>

0 comments on commit 935c7cf

Please sign in to comment.