Skip to content

Commit

Permalink
Merge branch '6.4' into 2019-08-01-es7
Browse files Browse the repository at this point in the history
  • Loading branch information
pmishev committed Apr 30, 2020
2 parents 40653af + cf3d53f commit 9162e8a
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
/phpunit.xml
/composer.lock
/var/

.idea
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
language: php
php:
- '7.1'
- '7.2'
- '7.3'

env:
global:
- SYMFONY_VERSION="~3.4" ES_FILENAME_SUFFIX="-linux-x86_64"
- SYMFONY_VERSION="~4.4" ES_FILENAME_SUFFIX="-linux-x86_64"
matrix:
- ES_VERSION="7.2.1"
- ES_VERSION="7.2.1" SYMFONY_VERSION="~4.0"
- ES_VERSION="7.2.1" SYMFONY_VERSION="~3.4"
- ES_VERSION="7.3.0"
- ES_VERSION="7.3.0" SYMFONY_VERSION="~4.0"
- ES_VERSION="7.3.0" SYMFONY_VERSION="~3.4"

install:
- composer require --no-update symfony/symfony:${SYMFONY_VERSION}
Expand Down
49 changes: 22 additions & 27 deletions Profiler/ElasticsearchProfiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
use Sineflow\ElasticsearchBundle\Profiler\Handler\CollectionHandler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;

/**
* Data collector for profiling elasticsearch bundle.
*/
class ElasticsearchProfiler implements DataCollectorInterface
class ElasticsearchProfiler extends DataCollector
{
const UNDEFINED_ROUTE = 'undefined_route';

Expand All @@ -20,26 +20,16 @@ class ElasticsearchProfiler implements DataCollectorInterface
*/
private $loggers = [];

/**
* @var array Queries array.
*/
private $queries = [];

/**
* @var int Query count.
*/
private $count = 0;

/**
* @var float Time all queries took in ms.
*/
private $time = .0;

/**
* @var array Registered index managers.
*/
private $indexManagers = [];

public function __construct()
{
$this->reset();
}

/**
* Adds logger to look for collector handler.
*
Expand All @@ -55,6 +45,8 @@ public function addLogger(Logger $logger)
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
{
$this->data['indexManagers'] = $this->cloneVar($this->indexManagers);

/** @var Logger $logger */
foreach ($this->loggers as $logger) {
foreach ($logger->getHandlers() as $handler) {
Expand All @@ -71,9 +63,12 @@ public function collect(Request $request, Response $response, \Exception $except
*/
public function reset()
{
$this->queries = [];
$this->count = 0;
$this->time = 0;
$this->data = [
'indexManagers' => [], // The list of all defined index managers
'queryCount' => 0, // The queries count
'time' => .0, // Total time for all queries in ms.
'queries' => [], // Array with all the queries
];
}

/**
Expand All @@ -83,7 +78,7 @@ public function reset()
*/
public function getTime()
{
return round($this->time * 1000, 2);
return round($this->data['time'] * 1000, 2);
}

/**
Expand All @@ -93,7 +88,7 @@ public function getTime()
*/
public function getQueryCount()
{
return $this->count;
return $this->data['queryCount'];
}

/**
Expand All @@ -109,15 +104,15 @@ public function getQueryCount()
*/
public function getQueries()
{
return $this->queries;
return $this->data['queries'];
}

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

/**
Expand Down Expand Up @@ -145,13 +140,13 @@ public function getName()
*/
private function handleRecords($records)
{
$this->count += count($records) / 2;
$this->data['queryCount'] += count($records) / 2;
$queryBody = '';
$rawRequest = '';
foreach ($records as $record) {
// First record will never have context.
if (!empty($record['context'])) {
$this->time += $record['context']['duration'];
$this->data['time'] += $record['context']['duration'];
$route = !empty($record['extra']['route']) ? $record['extra']['route'] : self::UNDEFINED_ROUTE;
$this->addQuery($route, $record, $queryBody, $rawRequest);
} else {
Expand Down Expand Up @@ -188,7 +183,7 @@ private function addQuery(string $route, array $record, string $queryBody, strin
$senseRequest .= "\n".trim($queryBody, " '");
}

$this->queries[$route][] = array_merge(
$this->data['queries'][$route][] = array_merge(
[
'time' => $record['context']['duration'] * 1000,
'curlRequest' => $rawRequest,
Expand Down
6 changes: 3 additions & 3 deletions Resources/views/Profiler/profiler.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% block toolbar %}
{% set profiler_markup_version = profiler_markup_version|default(1) %}

{% if collector.querycount > 0 %}
{% if collector.queryCount > 0 %}

{% set icon %}
{% if profiler_markup_version == 1 %}
Expand Down Expand Up @@ -81,7 +81,7 @@

{% else %}

<span class="label {{ collector.querycount == 0 ? 'disabled' }}">
<span class="label {{ collector.queryCount == 0 ? 'disabled' }}">
<span class="icon">
{{ include('@SineflowElasticsearch/Profiler/elasticsearch.svg') }}
</span>
Expand Down Expand Up @@ -186,7 +186,7 @@ To enable it, set 'sfes.profiler_backtrace_enabled' to true.</pre>

<h2>Index managers</h2>
{% if collector.indexManagers %}
{% include '@WebProfiler/Profiler/table.html.twig' with {data: collector.indexManagers} only %}
{{ include('@WebProfiler/Profiler/table.html.twig', { data: collector.indexManagers }, with_context = false) }}
{% else %}
<p>
<em>No managers.</em>
Expand Down
18 changes: 12 additions & 6 deletions Subscriber/KnpPaginateQuerySubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,19 @@ protected function getSorting(ItemsEvent $event)
$sortField = null;
$sortDirection = 'asc';

if (isset($_GET[$event->options['sortFieldParameterName']])) {
$sortField = $_GET[$event->options['sortFieldParameterName']];
$sortDirection = isset($_GET[$event->options['sortDirectionParameterName']]) && strtolower($_GET[$event->options['sortDirectionParameterName']]) === 'desc' ? 'desc' : 'asc';
$request = $this->requestStack->getCurrentRequest();
if ($request instanceof Request) {
$sortField = $request->get($event->options['sortFieldParameterName']);
$sortDirection = $request->get($event->options['sortDirectionParameterName'], 'desc');
$sortDirection = strtolower($sortDirection) === 'desc' ? 'desc' : 'asc';

// check if the requested sort field is in the sort whitelist
if (isset($event->options['sortFieldWhitelist']) && !in_array($sortField, $event->options['sortFieldWhitelist'])) {
throw new \UnexpectedValueException(sprintf('Cannot sort by [%s] as it is not in the whitelist', $sortField));
if ($sortField) {
// check if the requested sort field is in the sort whitelist
if (isset($event->options['sortFieldWhitelist']) && !in_array($sortField, $event->options['sortFieldWhitelist'])) {
throw new \UnexpectedValueException(
sprintf('Cannot sort by [%s] as it is not in the whitelist', $sortField)
);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
],
"require": {
"php": "^7.1",
"php": "^7.2",
"psr/log": "^1.0",

"symfony/framework-bundle": "^3.4 || ^4.0",
Expand All @@ -34,7 +34,7 @@
"symfony/browser-kit": "^3.4 || ^4.0",

"monolog/monolog": "^1.0",
"knplabs/knp-paginator-bundle": "^2.7",
"knplabs/knp-paginator-bundle": "^4.0 || ^5.0",
"squizlabs/php_codesniffer": "^3.0",
"php-coveralls/php-coveralls": "^2.1",
"escapestudios/symfony2-coding-standard": "^3.0",
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

<php>
<server name="KERNEL_CLASS" value="Sineflow\ElasticsearchBundle\Tests\App\AppKernel" />
<server name="SHELL_VERBOSITY" value="-1" />
<env name="SYMFONY_PHPUNIT_VERSION" value="7.5" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak" />
</php>
Expand Down

0 comments on commit 9162e8a

Please sign in to comment.