Skip to content

Commit

Permalink
Merge pull request #57 from venyii/data-collector-toolbar
Browse files Browse the repository at this point in the history
Add a DataCollector with debug toolbar integration
  • Loading branch information
julienbourdeau authored Aug 28, 2018
2 parents 8c24f4f + 12211cc commit 217ce21
Show file tree
Hide file tree
Showing 12 changed files with 439 additions and 13 deletions.
85 changes: 85 additions & 0 deletions DataCollector/ClientDataCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Algolia\AlgoliaSearchBundle\DataCollector;

use Algolia\AlgoliaSearchBundle\Debug\DebugClientInterface;
use AlgoliaSearch\Client;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;

class ClientDataCollector extends DataCollector
{
/**
* @var Client
*/
private $client;

/**
* @param Client $client
*/
public function __construct(Client $client)
{
$this->client = $client;
}

/**
* {@inheritdoc}
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
{
$this->data = [
'transactions' => [],
'transactionCount' => [],
];

if (! $this->client instanceof DebugClientInterface) {
return;
}

$transactions = $this->client->getTransactions();

$this->data = [
'transactions' => $transactions,
'transactionCount' => count($transactions),
];
}

/**
* @return array
*/
public function getTransactions()
{
return $this->data['transactions'];
}

/**
* @return int
*/
public function getTransactionCount()
{
return $this->data['transactionCount'];
}

/**
* @return int
*/
public function getTotalTime()
{
$time = 0;

foreach ($this->data['transactions'] as $query) {
$time += $query['ms'];
}

return (int) $time;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'algolia.client_data_collector';
}
}
109 changes: 109 additions & 0 deletions Debug/DebugClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Algolia\AlgoliaSearchBundle\Debug;

use AlgoliaSearch\Client;
use Symfony\Component\Stopwatch\Stopwatch;

class DebugClient extends Client implements DebugClientInterface
{
/**
* @var array
*/
private $transactions = [];

/**
* @var bool
*/
private $disableRequests = false;

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

/**
* @var Stopwatch
*/
private $stopwatch;

/**
* @param bool $disable
*/
public function disableRequests($disable = false)
{
$this->disableRequests = $disable;
}

/**
* @param array $response
*/
public function pushResponse(array $response)
{
$this->responseStack[] = $response;
}

/**
* @param Stopwatch $stopwatch
*/
public function setStopwatch(Stopwatch $stopwatch)
{
$this->stopwatch = $stopwatch;
}

/**
* {@inheritdoc}
*/
public function doRequest($context, $method, $host, $path, $params, $data, $connectTimeout, $readTimeout)
{
$transactionId = md5(microtime() . uniqid());

$request = [
'context' => $context,
'method' => $method,
'host' => $host,
'path' => $path,
'params' => $params,
'data' => $data,
'connect_timeout' => $connectTimeout,
'read_timeout' => $readTimeout,
];

if ($this->stopwatch) {
$this->stopwatch->start('algolia_transaction');
}
$start = microtime(true);

$response = [];

if ($this->disableRequests) {
if ($this->responseStack !== []) {
$response = array_shift($this->responseStack);
}
} else {
$response = parent::doRequest($context, $method, $host, $path, $params, $data, $connectTimeout, $readTimeout);
}

$time = microtime(true) - $start;
if ($this->stopwatch) {
$this->stopwatch->stop('algolia_transaction');
}

$this->transactions[$transactionId] = [
'mocked' => $this->disableRequests,
'request' => $request,
'response' => $response,
'ms' => round($time * 1000),
];

return $response;
}

/**
* @return array
*/
public function getTransactions()
{
return $this->transactions;
}
}
11 changes: 11 additions & 0 deletions Debug/DebugClientInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Algolia\AlgoliaSearchBundle\Debug;

interface DebugClientInterface
{
/**
* @return array
*/
public function getTransactions();
}
14 changes: 14 additions & 0 deletions DependencyInjection/AlgoliaAlgoliaSearchExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,27 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('algolia.api_key', $config['api_key']);
}

if (isset($config['disable_requests'])) {
$container->setParameter('algolia.disable_requests', $config['disable_requests']);
}

$container->setParameter('algolia.catch_log_exceptions', $config['catch_log_exceptions']);
$container->setParameter('algolia.index_name_prefix', $config['index_name_prefix']);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');

if ($container->getParameter('kernel.debug')) {
// Replace the regular client service with the debug one
$definition = $container->findDefinition('algolia.client');
$definition->setPublic(false);
$container->setAlias('algolia.client', 'algolia.debug_client');
}
}

/**
* {@inheritdoc}
*/
public function getAlias()
{
return 'algolia';
Expand Down
21 changes: 8 additions & 13 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,16 @@ public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('algolia');
$rootNode->children()
->scalarNode('application_id')
$rootNode
->children()
->scalarNode('application_id')->end()
->scalarNode('api_key')->end()
->scalarNode('index_name_prefix')->defaultValue('')->end()
->booleanNode('catch_log_exceptions')->defaultFalse()->end()
->booleanNode('disable_requests')->defaultFalse()->end()
->end()
->scalarNode('api_key')
->end()
->scalarNode('index_name_prefix')
->defaultValue('')
->end()
->booleanNode('catch_log_exceptions')
->defaultFalse()
->end();
;

// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
return $treeBuilder;
}
}
12 changes: 12 additions & 0 deletions Indexer/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,18 @@ public function getClient()
return $this->client;
}

/**
* @param \AlgoliaSearch\Client $client
*
* @return $this
*/
public function setClient(\AlgoliaSearch\Client $client)
{
$this->client = $client;

return $this;
}

/**
* Returns an object used to communicate with the Algolia indexes
* and caches it.
Expand Down
24 changes: 24 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,36 @@ parameters:
algolia.indexer_subscriber.mongodb_odm.class: Algolia\AlgoliaSearchBundle\EventListener\AlgoliaSearchDoctrineMongoDBODMEventSubscriber

services:
algolia.client:
class: AlgoliaSearch\Client
arguments:
- "%algolia.application_id%"
- "%algolia.api_key%"

algolia.debug_client:
class: Algolia\AlgoliaSearchBundle\Debug\DebugClient
arguments:
- "%algolia.application_id%"
- "%algolia.api_key%"
calls:
- [disableRequests, ["%algolia.disable_requests%"]]
- [setStopwatch, ["@debug.stopwatch"]]

algolia.client_data_collector:
class: Algolia\AlgoliaSearchBundle\DataCollector\ClientDataCollector
public: false
arguments:
- "@algolia.client"
tags:
- {name: data_collector, template: "@AlgoliaAlgoliaSearch/Collector/algolia.html.twig", id: "algolia.client_data_collector"}

algolia.indexer:
class: "%algolia.indexer.class%"
calls:
- [setEnvironment, ["%kernel.environment%"]]
- [setIndexNamePrefix, ["%algolia.index_name_prefix%"]]
- [setApiSettings, ["%algolia.application_id%", "%algolia.api_key%", "%algolia.connection_timeout%"]]
- [setClient, ["@algolia.client"]]

algolia.indexer_subscriber.abstract:
abstract: true
Expand Down
9 changes: 9 additions & 0 deletions Resources/views/Collector/algolia.css.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.sf-toolbar-algolia-requests-info td {
border-bottom-color: #f7f7f7;
}

.sf-toolbar-algolia-requests .metadata pre {
white-space: pre-wrap;
padding: 5px 10px;
background: #f7f7f7;
}
Loading

0 comments on commit 217ce21

Please sign in to comment.