Skip to content

Commit 100f69b

Browse files
authored
Merge pull request #651 from codex-team/perf/api-mongo-projections
Perf/api mongo projections
2 parents 6352808 + 997ff35 commit 100f69b

2 files changed

Lines changed: 33 additions & 13 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hawk.api",
3-
"version": "1.5.2",
3+
"version": "1.5.3",
44
"main": "index.ts",
55
"license": "BUSL-1.1",
66
"scripts": {

src/models/eventsFactory.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,22 @@ class EventsFactory extends Factory {
403403
return { 'event.assignee': String(assignee) };
404404
})();
405405

406+
/**
407+
* These filters match joined event.* fields, so their $match must run
408+
* after the lookups. With none set, $limit can move before the lookups
409+
* and skip joining rows we'd drop anyway (~8.8s).
410+
* Trim search to match searchFilter's own condition.
411+
*/
412+
const hasContentFilters =
413+
search.trim().length > 0 ||
414+
Boolean(release) ||
415+
Boolean(assignee) ||
416+
Object.keys(matchFilter).length > 0;
417+
418+
if (!hasContentFilters) {
419+
pipeline.push({ $limit: limit + 1 });
420+
}
421+
406422
pipeline.push(
407423
/**
408424
* Left outer join original event on groupHash field
@@ -434,21 +450,25 @@ class EventsFactory extends Factory {
434450
path: '$repetition',
435451
preserveNullAndEmptyArrays: true,
436452
},
437-
},
438-
{
439-
$match: {
440-
...matchFilter,
441-
...searchFilter,
442-
...releaseFilter,
443-
...assigneeFilter,
444-
},
445-
},
446-
{ $limit: limit + 1 },
447-
{
448-
$unset: 'groupHash',
449453
}
450454
);
451455

456+
if (hasContentFilters) {
457+
pipeline.push(
458+
{
459+
$match: {
460+
...matchFilter,
461+
...searchFilter,
462+
...releaseFilter,
463+
...assigneeFilter,
464+
},
465+
},
466+
{ $limit: limit + 1 }
467+
);
468+
}
469+
470+
pipeline.push({ $unset: 'groupHash' });
471+
452472
const cursor = this.getCollection(this.TYPES.DAILY_EVENTS).aggregate(pipeline);
453473
const result = await cursor.toArray();
454474

0 commit comments

Comments
 (0)