Skip to content

Commit

Permalink
Added explain query service
Browse files Browse the repository at this point in the history
  • Loading branch information
mario authored and Alessandro Galli committed Aug 25, 2017
1 parent 617a2d9 commit 7860dc6
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
85 changes: 85 additions & 0 deletions src/Services/ExplainQueryService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Facile\MongoDbBundle\Services;

use Facile\MongoDbBundle\Models\Query;
use InvalidArgumentException;
use MongoDB\Driver\Command;

class ExplainQueryService
{
const VERBOSITY_QUERY_PLANNER = 'queryPlanner';
const VERBOSITY_EXECUTION_STATS = 'executionStats';
const VERBOSITY_ALL_PLAN_EXECUTION = 'allPlansExecution';

public static $acceptedMethod = ['count', 'distinct', 'group', 'find', 'findAndModify', 'delete', 'update'];

/** @var ClientRegistry */
private $clientRegistry;

/**
* Constructs a explain command.
*
* Supported options:
* verbosity : queryPlanner | executionStats Mode | allPlansExecution (default)
* The explain command provides information on the execution of the following commands:
* count, distinct, group, find, findAndModify, delete, and update.
*
* @param ClientRegistry $clientRegistry
*/
public function __construct(ClientRegistry $clientRegistry)
{

$this->clientRegistry = $clientRegistry;
}

/**
* Execute the operation.
*
* @param string $connection
* @param Query $query
* @param string $verbosity
* @return array
* @throws \MongoDB\Driver\Exception\InvalidArgumentException
* @throws \MongoDB\Driver\Exception\WriteException
* @throws \MongoDB\Driver\Exception\WriteConcernException
* @throws \MongoDB\Driver\Exception\RuntimeException
* @throws \MongoDB\Driver\Exception\Exception
* @throws \MongoDB\Driver\Exception\DuplicateKeyException
* @throws \MongoDB\Driver\Exception\ConnectionException
* @throws \MongoDB\Driver\Exception\AuthenticationException
*/
public function execute(string $connection, Query $query, string $verbosity = self::VERBOSITY_ALL_PLAN_EXECUTION)
{
if (!in_array($query->getMethod(), self::$acceptedMethod)) {
throw new InvalidArgumentException(
'Cannot explain the method'.$query->getMethod().'. Allowed method '.self::$acceptedMethod
);
};

$manager = $this->clientRegistry->getClient('test_client')->getManager();

return $manager->executeCommand('collaboratori', $this->createCommand($query, $verbosity))->toArray();
}

/**
* Create the explain command.
*
* @return Command
* @throws \MongoDB\Driver\Exception\InvalidArgumentException
*/
private function createCommand(Query $query, string $verbosity)
{
$args = [
$query->getMethod() => $query->getCollection(),
'query' => $query->getFilters(),
];

$cmd = [
'explain' => $args,
'verbosity' => $verbosity,
];

return new Command($cmd);
}
}
16 changes: 15 additions & 1 deletion tests/Functional/Capsule/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use Facile\MongoDbBundle\Capsule\Collection;
use Facile\MongoDbBundle\Event\QueryEvent;
use Facile\MongoDbBundle\Models\Query;
use Facile\MongoDbBundle\Services\ExplainQueryService;
use Facile\MongoDbBundle\Tests\Functional\AppTestCase;
use MongoDB\Driver\Manager;
use MongoDB\Driver\Server;
Expand All @@ -16,7 +18,7 @@ private function getManager(): Manager
$reg = $this->getContainer()->get('mongo.client_registry');
/** @var \MongoDB\Client $client */
$client = $reg->getClient('test_client');
/** @var Server[] $servers */

return $client->__debugInfo()['manager'];
}

Expand Down Expand Up @@ -182,6 +184,18 @@ public function test_distinct()
$coll->distinct('field');
}

public function test_explainQuery()
{
$query = new Query();
$query->setCollection('agenda_task');
$query->setMethod('count');
$query->setFilters([
"target" => null
]);

$service = new ExplainQueryService($this->getContainer()->get('mongo.client_registry'));
$service->execute('mongo.connection.test_db', $query);
}
/**
* @param $ev
*/
Expand Down

0 comments on commit 7860dc6

Please sign in to comment.