-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from venyii/data-collector-toolbar
Add a DataCollector with debug toolbar integration
- Loading branch information
Showing
12 changed files
with
439 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.