Skip to content
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hawk.api",
"version": "1.5.0",
"version": "1.5.1",
"main": "index.ts",
"license": "BUSL-1.1",
"scripts": {
Expand Down
22 changes: 16 additions & 6 deletions src/dataLoaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,36 @@ export default class DataLoaders {
* Batching function for resolving entities from their ids
* @param collectionName - collection name to get entities
* @param ids - ids for resolving
* @param projection - optional mongo projection to limit returned fields
*/
private async batchByIds<T extends { _id: ObjectId }>(
collectionName: string,
ids: ReadonlyArray<string>
ids: ReadonlyArray<string>,
projection?: Record<string, 0 | 1>
): Promise<(WithId<T> | null)[]> {
return this.batchByField<T, '_id'>(collectionName, '_id', ids.map(id => new ObjectId(id)));
return this.batchByField<T, '_id'>(
collectionName,
'_id',
ids.map(id => new ObjectId(id)),
projection
);
}

/**
* Batching function for resolving entities by certain field
* @param collectionName - collection name to get entities
* @param fieldName - field name to resolve
* @param values - values for resolving
* @param projection - optional mongo projection to limit returned fields
*/
private async batchByField<
T extends Record<string, any>,
FieldType extends keyof T
>(
collectionName: string,
fieldName: FieldType,
values: ReadonlyArray<T[FieldType]>
values: ReadonlyArray<T[FieldType]>,
projection?: Record<string, 0 | 1>
): Promise<(WithId<T> | null)[]> {
type Doc = WithId<T>;
const valuesMap = new Map<string, FieldType>();
Expand All @@ -99,9 +108,10 @@ export default class DataLoaders {

const queryResult = await this.dbConnection
.collection<T>(collectionName)
.find({
[fieldName]: { $in: Array.from(valuesMap.values()) },
} as any)
.find(
{ [fieldName]: { $in: Array.from(valuesMap.values()) } } as any,
projection ? { projection } : {}
)
.toArray();

/**
Expand Down
39 changes: 27 additions & 12 deletions src/models/eventsFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,17 @@ class EventsFactory extends Factory {
return { 'event.assignee': String(assignee) };
})();

/** When false, $limit can move before the $lookups. */
const hasContentFilters =
escapedSearch.length > 0 ||
Boolean(release) ||
Boolean(assignee) ||
Object.keys(matchFilter).length > 0;

if (!hasContentFilters) {
pipeline.push({ $limit: limit + 1 });
}

pipeline.push(
/**
* Left outer join original event on groupHash field
Expand Down Expand Up @@ -434,21 +445,25 @@ class EventsFactory extends Factory {
path: '$repetition',
preserveNullAndEmptyArrays: true,
},
},
{
$match: {
...matchFilter,
...searchFilter,
...releaseFilter,
...assigneeFilter,
},
},
{ $limit: limit + 1 },
{
$unset: 'groupHash',
}
);

if (hasContentFilters) {
pipeline.push(
{
$match: {
...matchFilter,
...searchFilter,
...releaseFilter,
...assigneeFilter,
},
},
{ $limit: limit + 1 }
);
}

pipeline.push({ $unset: 'groupHash' });

const cursor = this.getCollection(this.TYPES.DAILY_EVENTS).aggregate(pipeline);
const result = await cursor.toArray();

Expand Down