Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format aggregation queries in data collector #406

Merged
merged 1 commit into from
Apr 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion DataCollector/PrettyDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,15 @@ public function collect(Request $request, Response $response, \Exception $except
}

// format the method call
if (isset($log['authenticate'])) {
if (isset($log['aggregate'])) {
$query .= '.aggregate(' . $this->bsonEncode($log['pipeline']);

if ($log['options']) {
$query .= ', ' . $this->bsonEncode($log['options']);
}

$query .= ')';
} elseif (isset($log['authenticate'])) {
$query .= '.authenticate()';
} elseif (isset($log['batchInsert'])) {
if (1 === $log['num']) {
Expand Down
50 changes: 50 additions & 0 deletions Tests/DataCollector/PrettyDataCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,54 @@ public function testCollectLimitAndSort()
$this->assertEquals(1, $collector->getQueryCount());
$this->assertEquals($formatted, $collector->getQueries());
}

public function testCollectAggregate()
{
$queries = [
[
'aggregate' => true,
'pipeline' => [
[
'$group' => [
'_id' => '$verified',
'count' => ['$sum' => 1],
],
],
],
'options' => [],
'db' => 'foo',
'collection' => 'User',
],
[
'aggregate' => true,
'pipeline' => [
[
'$group' => [
'_id' => '$verified',
'count' => ['$sum' => 1],
],
],
],
'options' => ['group' => true],
'db' => 'foo',
'collection' => 'User',
],
];

$formatted = [
'use foo;',
'db.User.aggregate([ { "$group": { "_id": "$verified", "count": { "$sum": 1 } } } ]);',
'db.User.aggregate([ { "$group": { "_id": "$verified", "count": { "$sum": 1 } } } ], { "group": true });',
];


$collector = new PrettyDataCollector();
foreach ($queries as $query) {
$collector->logQuery($query);
}
$collector->collect(new Request(), new Response());

$this->assertEquals(2, $collector->getQueryCount());
$this->assertEquals($formatted, $collector->getQueries());
}
}