Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@
* file that was distributed with this source code.
*/

namespace ONGR\ElasticsearchBundle\Service;
namespace ONGR\ElasticsearchBundle\DataCollector;

use Monolog\Logger;
use ONGR\ElasticsearchBundle\Logger\Handler\CollectionHandler;
use ONGR\ElasticsearchBundle\Service\JsonFormatter;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;

/**
* Data collector for profiling elasticsearch bundle.
*/
class ESDataCollector implements DataCollectorInterface
class ElasticsearchDataCollector implements DataCollectorInterface
{
const UNDEFINED_ROUTE = 'undefined_route';

Expand All @@ -34,6 +35,11 @@ class ESDataCollector implements DataCollectorInterface
*/
private $data = [];

/**
* @var array
*/
private $managers = [];

/**
* Adds logger to look for collector handler.
*
Expand Down Expand Up @@ -96,6 +102,28 @@ public function getQueries()
return $this->data['queries'];
}

/**
* @return array
*/
public function getManagers()
{
if (is_array(reset($this->managers))) {
foreach ($this->managers as $name => &$manager) {
$manager = $name === 'default' ? 'es.manager' : sprintf('es.manager.%s', $name);
}
}

return $this->managers;
}

/**
* @param array $managers
*/
public function setManagers($managers)
{
$this->managers = $managers;
}

/**
* {@inheritdoc}
*/
Expand All @@ -118,12 +146,7 @@ private function handleRecords($route, $records)
// First record will never have context.
if (!empty($record['context'])) {
$this->addTime($record['context']['duration']);
$this->data['queries'][$route][] = [
'body' => JsonFormatter::prettify(trim($queryBody, "'")),
'method' => $record['context']['method'],
'uri' => $record['context']['uri'],
'time' => $record['context']['duration'] * 100,
];
$this->addQuery($route, $record, $queryBody);
} else {
$position = strpos($record['message'], '-d');
$queryBody = $position !== false ? substr($record['message'], $position + 3) : '';
Expand All @@ -145,6 +168,27 @@ private function addTime($time)
$this->data['time'] += $time;
}

/**
* Adds query to collected data array.
*
* @param string $route
* @param array $record
* @param string $queryBody
*/
private function addQuery($route, $record, $queryBody)
{
parse_str(parse_url($record['context']['uri'], PHP_URL_QUERY), $httpParameters);
$this->data['queries'][$route][] = array_merge(
[
'body' => JsonFormatter::prettify(trim($queryBody, "'")),
'method' => $record['context']['method'],
'httpParameters' => $httpParameters,
'time' => $record['context']['duration'] * 100,
],
array_diff_key(parse_url($record['context']['uri']), array_flip(['query']))
);
}

/**
* Increases query count.
*
Expand Down
7 changes: 5 additions & 2 deletions DependencyInjection/ONGRElasticsearchExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

Expand Down Expand Up @@ -143,8 +144,10 @@ private function getLogTraceDefinition()
*/
private function getDataCollectorDefinition($loggers = [])
{
$collector = new Definition('ONGR\ElasticsearchBundle\Service\ESDataCollector');

$collector = new Definition('ONGR\ElasticsearchBundle\DataCollector\ElasticsearchDataCollector');
$collector->addMethodCall('setManagers', [new Parameter('es.managers')]);


foreach ($loggers as $logger) {
$collector->addMethodCall('addLogger', [new Reference($logger)]);
}
Expand Down
134 changes: 81 additions & 53 deletions Resources/views/Profiler/profiler.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -28,65 +28,93 @@
</span>
{% endblock %}

{% block panel %}
<h2>Queries</h2>
<div>
{% if collector.queryCount == 0 %}
<p>
<em>No queries.</em>
</p>
{% else %}
<ul class="queries" id="queriesPlaceholder">
{% for i, route in collector.queries %}
<li>
<h3>Route: <i>{{ i }}</i></h3>
<ul>
{% for q, query in route %}
<li class="{{ cycle(['odd', 'even'], i) }}" data-extra-info="{{ query.time }}" data-target-id="{{ i ~ q }}">
<div id="queryNo-{{ i ~ q }}">
<div>
<span>
<strong>Path:</strong> {{ query.uri }}
</span>
</div>
<div>
<span>
<strong>Method:</strong> {{ query.method }}
</span>
</div>
<div>
<span>
<strong>Took:</strong> {{ query.time }} ms
</span>
</div>
{% if query.body is not empty %}
<div onclick="return expandQuery(this);" title="Expand query" data-target-id="code-{{ i ~q }}" style="cursor: pointer;">
{% block queries %}
{% if collector.queryCount == 0 %}
<p>
<em>No queries.</em>
</p>
{% else %}
<ul class="queries" id="queriesPlaceholder">
{% for i, route in collector.queries %}
<li>
<h3>Route: <i>{{ i }}</i></h3>
<ul>
{% for q, query in route %}
<li class="{{ cycle(['odd', 'even'], i) }}" data-extra-info="{{ query.time }}" data-target-id="{{ i ~ q }}">
<div id="queryNo-{{ i ~ q }}">
<div>
<span>
<strong>Method:</strong> {{ query.method }}
</span>
</div>
<div>
<span>
<strong>Host: </strong>
{% if query.scheme is defined %}{{- query.scheme ~ "://" -}}{% endif %}
{{- query.host -}}
{% if query.port is defined %}:{{ query.port }}{% endif %}
</span>
</div>
<div>
<span>
<strong>Path:</strong> {{ query.path }}
</span>
</div>
<div>
<span>
<strong>Http parameters: </strong>
{% if query.httpParameters is not empty -%}
{{ profiler_dump(query.httpParameters) }}
{%- else -%}
<small>empty</small>
{%- endif -%}
</span>
</div>
<div>
<span>
<strong>Took:</strong> {{ query.time }} ms
</span>
</div>
{% if query.body is not empty %}
<div onclick="return expandQuery(this);" title="Expand query" data-target-id="code-{{ i ~q }}" style="cursor: pointer;">
<span id="smallcode-{{ i ~ q }}">
<strong>Body:</strong> <small>{{ query.body|slice(0, 100) }}</small>
</span>
<img alt="+" src="{{ asset('bundles/framework/images/blue_picto_more.gif') }}" style="display: inline;" />
<img alt="-" src="{{ asset('bundles/framework/images/blue_picto_less.gif') }}" style="display: none;" />
</div>
<code id="code-{{ i ~ q }}" class="hide">
<pre class="code">{{ query.body }}</pre>
</code>
{% else %}
<div>
<img alt="+" src="{{ asset('bundles/framework/images/blue_picto_more.gif') }}" style="display: inline;" />
<img alt="-" src="{{ asset('bundles/framework/images/blue_picto_less.gif') }}" style="display: none;" />
</div>
<code id="code-{{ i ~ q }}" class="hide">
<pre class="code">{{ query.body }}</pre>
</code>
{% else %}
<div>
<span>
<strong>Body:</strong> <small>empty</small>
</span>
</div>
{% endif %}
</div>
</li>
{% endfor %}
</ul>
</li>
{% if not loop.last %}<li><hr/></li>{% endif %}
{% endfor %}
</ul>
{% endif %}
</div>
</div>
{% endif %}
</div>
</li>
{% endfor %}
</ul>
</li>
{% if not loop.last %}<li><hr/></li>{% endif %}
{% endfor %}
</ul>
{% endif %}
{% endblock %}

{% block panel %}
<h2>Queries</h2>
{{ block('queries') }}
<h2>Managers</h2>
{% if collector.managers %}
{% include 'WebProfilerBundle:Profiler:table.html.twig' with {data: collector.managers} only %}
{% else %}
<p>
<em>No managers.</em>
</p>
{% endif %}

<script type="text/javascript">//<![CDATA[
function expandQuery(link) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
* file that was distributed with this source code.
*/

namespace ONGR\ElasticsearchBundle\Tests\Functional\Service;
namespace ONGR\ElasticsearchBundle\Tests\Functional\DataCollector;

use ONGR\ElasticsearchBundle\Service\ESDataCollector;
use ONGR\ElasticsearchBundle\DataCollector\ElasticsearchDataCollector;
use ONGR\ElasticsearchBundle\Test\ElasticsearchTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ESDataCollectorTest extends ElasticsearchTestCase
class ElasticsearchDataCollectorTest extends ElasticsearchTestCase
{
const START_QUERY_COUNT = 8;

Expand Down Expand Up @@ -54,7 +54,7 @@ public function testGetQueryCount()
$repository = $manager->getRepository('AcmeTestBundle:Product');

$document = $repository->createDocument();
$document->title = 'Awesomo';
$document->title = 'tuna';

$manager->persist($document);
$manager->commit();
Expand Down Expand Up @@ -85,7 +85,7 @@ public function testGetQueries()
$repository->find(2);
$queries = $this->getCollector()->getQueries();

$lastQuery = end($queries[ESDataCollector::UNDEFINED_ROUTE]);
$lastQuery = end($queries[ElasticsearchDataCollector::UNDEFINED_ROUTE]);
$time = $lastQuery['time'];
unset($lastQuery['time']);

Expand All @@ -94,15 +94,19 @@ public function testGetQueries()
[
'body' => '',
'method' => 'GET',
'uri' => 'http://127.0.0.1:9200/ongr-elasticsearch-bundle-test/product/2',
'path' => '/ongr-elasticsearch-bundle-test/product/2',
'host' => '127.0.0.1',
'httpParameters' => [],
'scheme' => 'http',
'port' => 9200,
],
$lastQuery,
'Logged data did not match expected data.'
);
}

/**
* @return ESDataCollector
* @return ElasticsearchDataCollector
*/
private function getCollector()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
* file that was distributed with this source code.
*/

namespace ONGR\ElasticsearchBundle\Tests\Unit\Service;
namespace ONGR\ElasticsearchBundle\Tests\Unit\DataCollector;

use ONGR\ElasticsearchBundle\Service\ESDataCollector;
use ONGR\ElasticsearchBundle\DataCollector\ElasticsearchDataCollector;

class ESDataCollectorTest extends \PHPUnit_Framework_TestCase
class ElasticsearchDataCollectorTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests if correct name is being returned.
*/
public function testGetName()
{
$collector = new ESDataCollector();
$collector = new ElasticsearchDataCollector();
$this->assertEquals('es', $collector->getName());
}
}