From e2b82e13e81d3dbc1a2f840a778d16f0ce0842de Mon Sep 17 00:00:00 2001 From: Martynas Sudintas Date: Fri, 19 Dec 2014 16:43:53 +0200 Subject: [PATCH] Profiler improvements --- .../ElasticsearchDataCollector.php | 60 ++++++-- .../ONGRElasticsearchExtension.php | 7 +- Resources/views/Profiler/profiler.html.twig | 134 +++++++++++------- .../ElasticsearchDataCollectorTest.php} | 18 ++- .../ElasticsearchDataCollectorTest.php} | 8 +- 5 files changed, 153 insertions(+), 74 deletions(-) rename Service/ESDataCollector.php => DataCollector/ElasticsearchDataCollector.php (71%) rename Tests/Functional/{Service/ESDataCollectorTest.php => DataCollector/ElasticsearchDataCollectorTest.php} (82%) rename Tests/Unit/{Service/ESDataCollectorTest.php => DataCollector/ElasticsearchDataCollectorTest.php} (61%) diff --git a/Service/ESDataCollector.php b/DataCollector/ElasticsearchDataCollector.php similarity index 71% rename from Service/ESDataCollector.php rename to DataCollector/ElasticsearchDataCollector.php index 11a631af..539112fd 100644 --- a/Service/ESDataCollector.php +++ b/DataCollector/ElasticsearchDataCollector.php @@ -9,10 +9,11 @@ * 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; @@ -20,7 +21,7 @@ /** * Data collector for profiling elasticsearch bundle. */ -class ESDataCollector implements DataCollectorInterface +class ElasticsearchDataCollector implements DataCollectorInterface { const UNDEFINED_ROUTE = 'undefined_route'; @@ -34,6 +35,11 @@ class ESDataCollector implements DataCollectorInterface */ private $data = []; + /** + * @var array + */ + private $managers = []; + /** * Adds logger to look for collector handler. * @@ -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} */ @@ -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) : ''; @@ -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. * diff --git a/DependencyInjection/ONGRElasticsearchExtension.php b/DependencyInjection/ONGRElasticsearchExtension.php index c4208ff2..612c769f 100644 --- a/DependencyInjection/ONGRElasticsearchExtension.php +++ b/DependencyInjection/ONGRElasticsearchExtension.php @@ -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; @@ -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)]); } diff --git a/Resources/views/Profiler/profiler.html.twig b/Resources/views/Profiler/profiler.html.twig index 027b97e4..af4b8a8b 100644 --- a/Resources/views/Profiler/profiler.html.twig +++ b/Resources/views/Profiler/profiler.html.twig @@ -28,65 +28,93 @@ {% endblock %} -{% block panel %} -

Queries

-
- {% if collector.queryCount == 0 %} -

- No queries. -

- {% else %} -
+ + {% endfor %} + + + {% if not loop.last %}

  • {% endif %} + {% endfor %} + + {% endif %} +{% endblock %} + +{% block panel %} +

    Queries

    + {{ block('queries') }} +

    Managers

    + {% if collector.managers %} + {% include 'WebProfilerBundle:Profiler:table.html.twig' with {data: collector.managers} only %} + {% else %} +

    + No managers. +

    + {% endif %}