Skip to content

Commit

Permalink
Added web profiler data collection for query logging.
Browse files Browse the repository at this point in the history
To enable, set your `atlas.yaml` config for `atlas.orm.atlas.log_queries` to
`true`. Then look in the full-screen profiler for the new Atlas menu tab.

Also fixed a configuration bug on `atlas.orm.atlas.transaction_class`.
  • Loading branch information
Paul M. Jones committed Aug 8, 2018
1 parent ca085b3 commit 282dad6
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 3 deletions.
53 changes: 53 additions & 0 deletions DataCollector/AtlasCollector.php
@@ -0,0 +1,53 @@
<?php
namespace Atlas\Symfony\DataCollector;

use Atlas\Orm\Atlas;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Exception;

class AtlasCollector extends DataCollector
{
protected $atlas;

public function __construct(Atlas $atlas)
{
$this->atlas = $atlas;
}

public function collect(
Request $request,
Response $response,
Exception $exception = null
) {
$this->data = $this->atlas->getQueries();
}

public function reset()
{
$this->data = [];
}

public function getName()
{
return 'atlas.collector';
}

public function getQueries()
{
return $this->data;
}

public function getCount()
{
return count($this->data);
}

public function getDuration()
{
return array_reduce($this->data, function ($time, $query) {
return $time + $query['duration'];
});
}
}
8 changes: 8 additions & 0 deletions DependencyInjection/AtlasExtension.php
Expand Up @@ -20,6 +20,7 @@
use Atlas\Cli\Skeleton;
use Atlas\Orm\Atlas;
use Atlas\Symfony\Command\SkeletonCommand;
use Atlas\Symfony\DataCollector\AtlasCollector;

/**
* Atlas extension for Symfony.
Expand Down Expand Up @@ -53,5 +54,12 @@ public function load(array $configs, ContainerBuilder $containerBuilder)
$containerBuilder->register(SkeletonCommand::class)
->addTag('console.command')
->setAutowired(true);

$containerBuilder->register(AtlasCollector::class)
->addTag('data_collector', [
'template' => '@Atlas/Collector/atlas.html.twig',
'id' => 'atlas.collector'
])
->setAutowired(true);
}
}
5 changes: 5 additions & 0 deletions DependencyInjection/Configuration.php
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Atlas\Orm\Transaction\AutoCommit;

/**
* Configuration for Symfony.
Expand Down Expand Up @@ -70,6 +71,10 @@ public function getConfigTreeBuilder()
->arrayNode('atlas')
->children()
->scalarNode('transaction_class')
->defaultValue(AutoCommit::class)
->end()
->booleanNode('log_queries')
->defaultValue(false)
->end()
->end()
->end()
Expand Down
14 changes: 12 additions & 2 deletions DependencyInjection/Factory.php
Expand Up @@ -42,8 +42,12 @@ public static function newAtlas(array $config, ContainerInterface $container) :
self::addConnections($connectionLocator, 'read', $config);
self::addConnections($connectionLocator, 'write', $config);

$transactionClass = $config['atlas']['orm']['transaction_class']
$transactionClass = $config['orm']['atlas']['transaction_class']
?? AutoCommit::CLASS;
if ($transactionClass == 'Atlas\\Orm\\AutoCommit') {
// make allowance for a buggy config from earlier releases
$transactionClass = AutoCommit::CLASS;
}
$builder->setTransactionClass($transactionClass);

$factory = function ($class) use ($container) {
Expand All @@ -55,7 +59,13 @@ public static function newAtlas(array $config, ContainerInterface $container) :
};
$builder->setFactory($factory);

return $builder->newAtlas();
$atlas = $builder->newAtlas();

if ($config['orm']['atlas']['log_queries']) {
$atlas->logQueries();
}

return $atlas;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion Resources/config/atlas.yaml
Expand Up @@ -37,7 +37,8 @@ atlas:

orm:
atlas:
transaction_class: 'Atlas\Orm\AutoCommit'
# transaction_class: 'Atlas\Orm\Transaction\AutoTransact'
# log_queries: false

cli:
config:
Expand Down
100 changes: 100 additions & 0 deletions Resources/views/Collector/atlas.html.twig
@@ -0,0 +1,100 @@
{% extends '@WebProfiler/Profiler/layout.html.twig' %}

{% block menu %}
<span class="label">
<span class="icon">
<svg
version="1.1"
id="Layer_1"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
x="0px"
y="0px"
width="24px"
height="24px"
viewBox="0 0 24 24"
enable-background="new 0 0 24 24"
xml:space="preserve">
<image
id="image0"
width="24"
height="24"
x="0"
y="0"
xlink:href="data:image/png;base64,
iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAQAAABKfvVzAAAAAmJLR0QA/4ePzL8AAAAHdElNRQfi
CAgLHxiw/wouAAABjXpUWHRSYXcgcHJvZmlsZSB0eXBlIGljYwAAOI21U1FuBCEI/fcUPQICgh5n
UCfp/S9Q1HEzu+k27UdfYnYFhMeDCZ+1ho+BxDnAACJLVFCQCjwtIE26smJCVkaElFNJBwJoO919
+Ol+mp8MQCVIFFJS4JggAVe48Hr/CadXHYziNjTCtpn1ZAa9ZFFrLLWxWWeWbi2appJfk4XfVLzh
EJakJHRxuRhLCt4YKOpSJsqlEJKqKwTbni8Gcag21VrXUrc9iN4dBtshTw8q3xKxD2Axiu0qgOCM
JMtmEs/tqPLmgRd+k+ip5wfMhS1jmlJd/uLjB3qNecJfxf6/RDo6ysqPRExy+pSKohRRcRf6Uq6d
kuT/i3Qh7FgRCAkoUhrv9sY/MTopjeXcCxqj1Pr4NG7IND8ToGbngln4LpAPm4EWXe2Bw8p3cZgp
r4pgk1HNVryF6G3FW9yeEopjMsZrz1palMmmHcpZg49eiOheKkLldU+tT+aE64GWmfxsBVZHB87f
dtDSiNpc9QrSwxdujO5mPAAwtQAAAqlJREFUOMuF099L3XUcx/HH93vO0ePZnEY7pqx0M5eag9RF
YVFgtH6MSioKKqLugwl1XdCULqIV9QcEC0ZEJHS1RoMCidqNcxfNWdtamqA7ir9/bOfHp4sdtVg/
np+bD28+Tz7v1+fDOwL2+1WUDq3u1x3vT2XjCkrX8rOli0b8FI+XNu5xDkTUWhClw6Hka/U9e+vu
SNbI2IkVaxZNFq5cnf6xcNy3NsoCstHAna8+n3lIowo7BGmsi626bsKwr9YufRbeNksiImnwsTeO
pXo1qJQSRPKKIFalzkG9qal7L1U4HYU4QWPDs/26UKtkFzJSUqpQo6RW0O2Ihuc0JcRFMqUMseim
9fdaKSNTlAyYcUyVJnOYF1kRxIoS5sXmcMaHZhAkoVnWEU94WJMKVYK0yJqSa66b9L1T7tbsMjeE
Gh8Yddxp1ertVKVGsGTdqmlLWryn0zNsCkGlpxxy2Vk/mzQhj5RbtOjTpVmlnLAtTBmXValdO0oK
ikhIim0ybmprfyDO9YThUAz/RjEMh54Q5xwo39Cq11u69em0+0apTMGsUV8b8agFY5stpfV72efe
l1fvdnUyWHPVH6alPOBTWSe3MoSioMOgBRdd8LucNWTsdlCbFrWYUVTOraM6NxT+j6FQndNRbinv
HfNeUO2fWfalj+S3n7VVvxO+8KQH7VUjJUKQt+iKH5xU9KaPndvMEIWn9TnlG0MitWqksWHRgmCf
1z2u5JMgkIyE5dmlyWyXl7xozoSprdBZezS6VQJnzS5ZjoiIDRwunP/PyOfD4YJ3xVF5SncZbJs9
GkbCyk1HV8JIOBra5gzYtT3TJN0Xv3LbI+2N7VX7oqwdWJXzWxhbH5uY+a50IjqjEP4iiEiEPTp1
uivOJlMU8qWcX4wajadCsfxn/gT2rF0brV9pXwAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAxOC0wOC0w
OFQxMTozMToyNC0wNzowMHLVNgMAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMTgtMDgtMDhUMTE6MzE6
MjQtMDc6MDADiI6/AAAAKHRFWHRpY2M6Y29weXJpZ2h0AENvcHlyaWdodCBBcHBsZSBJbmMuLCAy
MDE1Uf15/AAAABp0RVh0aWNjOmRlc2NyaXB0aW9uAERpc3BsYXkgUDOPebu8AAAAG3RFWHRpY2M6
bWFudWZhY3R1cmVyAERpc3BsYXkgUDMy4RxJAAAAFHRFWHRpY2M6bW9kZWwARGlzcGxheSBQM95n
/9EAAAAASUVORK5CYII="
/>
</svg>
</span>
<strong>Atlas</strong>
</span>
{% endblock %}

{% block panel %}

<h2>Atlas</h2>

<h3>Queries</h3>

<p>{{ collector.count }} queries, {{ collector.duration|number_format(5) }} seconds.

<table>
<tr>
<th></th>
<th>Connection</th>
<th>Duration</th>
<th>Statement</th>
<th>Values</th>
</tr>
{% for query in collector.queries %}
<tr>
<td>{{ loop.index }}</td>
<td>{{ query.connection }}</td>
<td>{{ query.duration|number_format(5) }}</td>
<td>{{ query.statement|trim }}<br />
<a onclick="javascript:document.getElementById('atlas-trace-{{ loop.index }}').style.display='block';">Trace</a>
<pre style="display: none;" id="atlas-trace-{{ loop.index }}">{{ query.trace|trim }}"</pre>
</td>
<td>
<dl>
{% for key, val in query.values %}
<dt>{{ key }} => </dt>
<dd>{{ val }}</dd>
{% else %}
--
{% endfor %}
</dl>
</td>
</tr>
{% else %}
<tr>
<td colspan="4">No queries.</td>
</tr>
{% endfor %}
</table>
{% endblock %}

0 comments on commit 282dad6

Please sign in to comment.