Skip to content

Commit 6dbd303

Browse files
committed
Format aggregation queries in data collector
1 parent ee1cdeb commit 6dbd303

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

DataCollector/PrettyDataCollector.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ public function collect(Request $request, Response $response, \Exception $except
8484
}
8585

8686
// format the method call
87-
if (isset($log['authenticate'])) {
87+
if (isset($log['aggregate'])) {
88+
$query .= '.aggregate('.$this->bsonEncode($log['pipeline']).', '.$this->bsonEncode($log['options']).')';
89+
} elseif (isset($log['authenticate'])) {
8890
$query .= '.authenticate()';
8991
} elseif (isset($log['batchInsert'])) {
9092
if (1 === $log['num']) {

Tests/DataCollector/PrettyDataCollectorTest.php

+35
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,41 @@ public function testCollectLimitAndSort()
303303
'db.User.find({ "_id": "foo" }).limit(10).sort({ "name": 1, "city": -1 });',
304304
];
305305

306+
$collector = new PrettyDataCollector();
307+
foreach ($queries as $query) {
308+
$collector->logQuery($query);
309+
}
310+
$collector->collect(new Request(), new Response());
311+
312+
$this->assertEquals(1, $collector->getQueryCount());
313+
$this->assertEquals($formatted, $collector->getQueries());
314+
}
315+
316+
public function testCollectAggregate()
317+
{
318+
$queries = [
319+
[
320+
'aggregate' => true,
321+
'pipeline' => [
322+
[
323+
'$group' => [
324+
'_id' => '$verified',
325+
'count' => ['$sum' => 1],
326+
],
327+
],
328+
],
329+
'options' => [],
330+
'db' => 'foo',
331+
'collection' => 'User',
332+
],
333+
];
334+
335+
$formatted = [
336+
'use foo;',
337+
'db.User.aggregate([ { "$group": { "_id": "$verified", "count": { "$sum": 1 } } } ], [ ]);'
338+
];
339+
340+
306341
$collector = new PrettyDataCollector();
307342
foreach ($queries as $query) {
308343
$collector->logQuery($query);

0 commit comments

Comments
 (0)