Merge pull request #27 from gimler/data_collector
add data collector for client class
This commit is contained in:
commit
8a9dc124ec
32
Client.php
Normal file
32
Client.php
Normal file
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
namespace FOQ\ElasticaBundle;
|
||||
|
||||
use Elastica_Client;
|
||||
use FOQ\ElasticaBundle\Logger\ElasticaLogger;
|
||||
|
||||
/**
|
||||
* @author Gordon Franke <info@nevalon.de>
|
||||
*/
|
||||
class Client extends Elastica_Client
|
||||
{
|
||||
protected $logger;
|
||||
|
||||
public function setLogger(ElasticaLogger $logger)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function request($path, $method, $data = array())
|
||||
{
|
||||
$start = microtime(true);
|
||||
$response = parent::request($path, $method, $data);
|
||||
|
||||
if (null !== $this->logger) {
|
||||
$time = microtime(true) - $start;
|
||||
$this->logger->logQuery($path, $method, $data, $time);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
50
DataCollector/ElasticaDataCollector.php
Normal file
50
DataCollector/ElasticaDataCollector.php
Normal file
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace FOQ\ElasticaBundle\DataCollector;
|
||||
|
||||
use FOQ\ElasticaBundle\Logger\ElasticaLogger;
|
||||
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* Data collector collecting elastica statistics.
|
||||
*
|
||||
* @author Gordon Franke <info@nevalon.de>
|
||||
*/
|
||||
class ElasticaDataCollector extends DataCollector
|
||||
{
|
||||
protected $logger;
|
||||
|
||||
public function __construct(ElasticaLogger $logger)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function collect(Request $request, Response $response, \Exception $exception = null)
|
||||
{
|
||||
$this->data['nb_queries'] = $this->logger->getNbQueries();
|
||||
$this->data['queries'] = $this->logger->getQueries();
|
||||
}
|
||||
|
||||
public function getQueryCount()
|
||||
{
|
||||
return $this->data['nb_queries'];
|
||||
}
|
||||
|
||||
public function getQueries()
|
||||
{
|
||||
return $this->data['queries'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'elastica';
|
||||
}
|
||||
}
|
|
@ -65,9 +65,13 @@ class FOQElasticaExtension extends Extension
|
|||
{
|
||||
$clientIds = array();
|
||||
foreach ($clients as $name => $clientConfig) {
|
||||
$clientDef = new Definition('%foq_elastica.client.class%', array($clientConfig));
|
||||
$clientDef = $container->getDefinition('foq_elastica.client');
|
||||
$clientDef->replaceArgument(0, array($clientConfig));
|
||||
|
||||
$clientId = sprintf('foq_elastica.client.%s', $name);
|
||||
|
||||
$container->setDefinition($clientId, $clientDef);
|
||||
|
||||
$clientIds[$name] = $clientId;
|
||||
}
|
||||
|
||||
|
|
73
Logger/ElasticaLogger.php
Normal file
73
Logger/ElasticaLogger.php
Normal file
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
namespace FOQ\ElasticaBundle\Logger;
|
||||
|
||||
use Symfony\Component\HttpKernel\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Logger for the Elastica.
|
||||
*
|
||||
* The {@link logQuery()} method is configured as the logger callable in the
|
||||
* service container.
|
||||
*
|
||||
* @author Gordon Franke <info@nevalon.de>
|
||||
*/
|
||||
class ElasticaLogger
|
||||
{
|
||||
protected $logger;
|
||||
protected $queries;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param LoggerInterface $logger The Symfony logger
|
||||
*/
|
||||
public function __construct(LoggerInterface $logger = null)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
$this->queries = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs a query.
|
||||
*
|
||||
* @param string $path Path to call
|
||||
* @param string $method Rest method to use (GET, POST, DELETE, PUT)
|
||||
* @param array $data arguments
|
||||
* @param float $time execution time
|
||||
*/
|
||||
public function logQuery($path, $method, $data, $time)
|
||||
{
|
||||
$this->queries[] = array(
|
||||
'path' => $path,
|
||||
'method' => $method,
|
||||
'data' => $data,
|
||||
'executionMS' => $time
|
||||
);
|
||||
|
||||
if (null !== $this->logger) {
|
||||
$message = sprintf("%s (%s) %0.2f ms", $path, $method, $time * 1000);
|
||||
$this->logger->info($message, (array) $data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of queries that have been logged.
|
||||
*
|
||||
* @return integer The number of queries logged
|
||||
*/
|
||||
public function getNbQueries()
|
||||
{
|
||||
return count($this->queries);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a human-readable array of queries logged.
|
||||
*
|
||||
* @return array An array of queries
|
||||
*/
|
||||
public function getQueries()
|
||||
{
|
||||
return $this->queries;
|
||||
}
|
||||
}
|
|
@ -5,14 +5,32 @@
|
|||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
|
||||
|
||||
<parameters>
|
||||
<parameter key="foq_elastica.client.class">Elastica_Client</parameter>
|
||||
<parameter key="foq_elastica.client.class">FOQ\ElasticaBundle\Client</parameter>
|
||||
<parameter key="foq_elastica.index.class">Elastica_Index</parameter>
|
||||
<parameter key="foq_elastica.type.class">Elastica_Type</parameter>
|
||||
<parameter key="foq_elastica.logger.class">FOQ\ElasticaBundle\Logger\ElasticaLogger</parameter>
|
||||
<parameter key="foq_elastica.data_collector.class">FOQ\ElasticaBundle\DataCollector\ElasticaDataCollector</parameter>
|
||||
</parameters>
|
||||
|
||||
|
||||
<services>
|
||||
|
||||
<service id="elastica.logger" class="%foq_elastica.logger.class%">
|
||||
<argument type="service" id="logger" on-invalid="null" />
|
||||
<tag name="monolog.logger" channel="elastica" />
|
||||
</service>
|
||||
<service id="elastica.data_collector" class="%foq_elastica.data_collector.class%" public="true">
|
||||
<tag name="data_collector" template="FOQElasticaBundle:Collector:elastica" id="elastica" />
|
||||
<argument type="service" id="elastica.logger" />
|
||||
</service>
|
||||
|
||||
<service id="foq_elastica.client" class="%foq_elastica.client.class%">
|
||||
<argument /> <!-- config -->
|
||||
<call method="setLogger">
|
||||
<argument type="service" id="elastica.logger" />
|
||||
</call>
|
||||
</service>
|
||||
|
||||
<service id="foq_elastica.index_manager" class="FOQ\ElasticaBundle\IndexManager">
|
||||
<argument /> <!-- indexes -->
|
||||
<argument /> <!-- default index -->
|
||||
|
|
BIN
Resources/public/images/elastica.png
Normal file
BIN
Resources/public/images/elastica.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1 KiB |
50
Resources/views/Collector/elastica.html.twig
Normal file
50
Resources/views/Collector/elastica.html.twig
Normal file
|
@ -0,0 +1,50 @@
|
|||
{% extends 'WebProfilerBundle:Profiler:layout.html.twig' %}
|
||||
|
||||
{% block toolbar %}
|
||||
{% set icon %}
|
||||
<img width="16" height="20" alt="elastica" style="border-width: 0; vertical-align: middle; margin-right: 5px;" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAUCAQAAAAua3X8AAAAAXNSR0IArs4c6QAAAAJiS0dEAP+Hj8y/AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH2woEDCcGS0MnjgAAAi1JREFUKM9l0UtIVGEYxvHn/c53OqY2ZwJTs5wZDbSNbnJGKnIRJILSIsmWbYKgoIWK2WVpRdFOaJGtWnShRchgdKG0TaQVZZghKdhMGppTOZ0zt3O+722hSNBv++z+D2FdH67hbJFoECep1QjQpByST/uXAVqbe3EdfQG6xUfJsb4ZStnYaX4RPRfihA29cd1R/LLuTUV2E8zyGfvrMSNtNBLQsY0j7lxTCz+03sYeV8JCsLK82Sx69vn1YXHPaLdxnwdooaJT19Q+2pXZLExQBkul0arClJM5IDig9+juwAPaYawGHYskJJlQqcIErW5ZViQ0OMfjg8ukhUFELFjCZMtQM+7HAnwSDM1abud8QgezoeJy0iabJEm5v/KpsFEQKuwHfQ1kr+Rotvm3bUgGg5XjjUbdcPCyEfmkR+jGvHr3vSFd6JqrTq/aCeRmyoaPJKPBya2nqKVHXeK9r94DY9bIxJ9GEyYIWVC+5ENlV3dC5m9Tty4BgCd9dqMLdYdXZKlIJX7MxqcTAGLV0cWm/cDp2jN8nDsTa1Uj9ZGlUDp8CBA+fCgAcijLrnI71rsn1QlYOlZFQkPD8S625Q+6yNwtTLcDAOYzybjOMRZZKCieQ+vNMjhu5uoLf2TjPB72zpfVCAWFkPF8IJuLDY5N4R8FeI7vY3d1/ULdPvwnRHaqtB+QCrC4LRJgyQAUCBoefPws8gNKCJK8wuP6HDTAAAgaPjx48MjP6VHwXxwGAXzUXZKuAAAAAElFTkSuQmCC"/>
|
||||
{% endset %}
|
||||
{% set text %}
|
||||
<span>{{ collector.querycount }}</span>
|
||||
{% endset %}
|
||||
{% include 'WebProfilerBundle:Profiler:toolbar_item.html.twig' with { 'link': profiler_url } %}
|
||||
{% endblock %}
|
||||
|
||||
{% block menu %}
|
||||
<span class="label">
|
||||
<span class="icon"><img src="{{ asset('bundles/foqelastica/images/elastica.png') }}" alt="" /></span>
|
||||
<strong>Elastica</strong>
|
||||
<span class="count">
|
||||
<span>{{ collector.querycount }}</span>
|
||||
</span>
|
||||
</span>
|
||||
{% endblock %}
|
||||
|
||||
{% block panel %}
|
||||
<h2>Queries</h2>
|
||||
|
||||
{% if not collector.queries %}
|
||||
<p>
|
||||
<em>Query logging is disabled.</em>
|
||||
<p>
|
||||
{% elseif not collector.querycount %}
|
||||
<p>
|
||||
<em>No queries.</em>
|
||||
</p>
|
||||
{% else %}
|
||||
<ul class="alt">
|
||||
{% for query in collector.queries %}
|
||||
<li class="{{ cycle(['odd', 'even'], loop.index) }}">
|
||||
<strong>Path</strong>: {{ query.path }}<br />
|
||||
<strong>Method</strong>: {{ query.method }}
|
||||
<div>
|
||||
<code>{{ query.data|yaml_encode }}</code>
|
||||
</div>
|
||||
<small>
|
||||
<strong>Time</strong>: {{ '%0.2f'|format(query.executionMS * 1000) }} ms
|
||||
</small>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
{% endblock %}
|
Loading…
Reference in a new issue