Skip to content

Commit e453f3b

Browse files
committed
perf(api): apply $limit before $lookup in dailyEvents pipeline when no content filters
1 parent 53f46d4 commit e453f3b

1 file changed

Lines changed: 27 additions & 12 deletions

File tree

src/models/eventsFactory.js

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

406+
/** When false, $limit can move before the $lookups. */
407+
const hasContentFilters =
408+
escapedSearch.length > 0 ||
409+
Boolean(release) ||
410+
Boolean(assignee) ||
411+
Object.keys(matchFilter).length > 0;
412+
413+
if (!hasContentFilters) {
414+
pipeline.push({ $limit: limit + 1 });
415+
}
416+
406417
pipeline.push(
407418
/**
408419
* Left outer join original event on groupHash field
@@ -434,21 +445,25 @@ class EventsFactory extends Factory {
434445
path: '$repetition',
435446
preserveNullAndEmptyArrays: true,
436447
},
437-
},
438-
{
439-
$match: {
440-
...matchFilter,
441-
...searchFilter,
442-
...releaseFilter,
443-
...assigneeFilter,
444-
},
445-
},
446-
{ $limit: limit + 1 },
447-
{
448-
$unset: 'groupHash',
449448
}
450449
);
451450

451+
if (hasContentFilters) {
452+
pipeline.push(
453+
{
454+
$match: {
455+
...matchFilter,
456+
...searchFilter,
457+
...releaseFilter,
458+
...assigneeFilter,
459+
},
460+
},
461+
{ $limit: limit + 1 }
462+
);
463+
}
464+
465+
pipeline.push({ $unset: 'groupHash' });
466+
452467
const cursor = this.getCollection(this.TYPES.DAILY_EVENTS).aggregate(pipeline);
453468
const result = await cursor.toArray();
454469

0 commit comments

Comments
 (0)