Skip to content

Commit e2f4cf7

Browse files
committed
Use 'intersect' new method from Set
This allow to do intersection more performant
1 parent d700c59 commit e2f4cf7

File tree

6 files changed

+138
-128
lines changed

6 files changed

+138
-128
lines changed

src/domain-services/external-reference/external-reference-service.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import { type FlowId } from '@unocha/hpc-api-core/src/db/models/flow';
33
import { Op } from '@unocha/hpc-api-core/src/db/util/conditions';
44
import { type InstanceDataOfModel } from '@unocha/hpc-api-core/src/db/util/raw-model';
55
import { type InstanceOfModel } from '@unocha/hpc-api-core/src/db/util/types';
6-
import { createBrandedValue } from '@unocha/hpc-api-core/src/util/types';
76
import { Service } from 'typedi';
87
import { type FlowExternalReference } from '../flows/graphql/types';
9-
import { type UniqueFlowEntity } from '../flows/model';
108
import { type SystemID } from '../report-details/graphql/types';
119

1210
@Service()
@@ -51,7 +49,7 @@ export class ExternalReferenceService {
5149
async getUniqueFlowIDsBySystemID(
5250
models: Database,
5351
systemID: SystemID
54-
): Promise<UniqueFlowEntity[]> {
52+
): Promise<Set<string>> {
5553
const externalRefences: Array<
5654
InstanceDataOfModel<Database['externalReference']>
5755
> = await models.externalReference.find({
@@ -61,13 +59,11 @@ export class ExternalReferenceService {
6159
skipValidation: true,
6260
});
6361

64-
const flowIDs: UniqueFlowEntity[] = [];
65-
66-
for (const reference of externalRefences) {
67-
flowIDs.push(this.mapExternalDataToUniqueFlowEntity(reference));
68-
}
69-
70-
return flowIDs;
62+
return new Set(
63+
externalRefences.map((externalReference) => {
64+
return `${externalReference.flowID}:${externalReference.versionID}`;
65+
})
66+
);
7167
}
7268

7369
private mapExternalReferenceToExternalReferenceFlows(
@@ -83,13 +79,4 @@ export class ExternalReferenceService {
8379
versionID: externalReference.versionID ?? 0,
8480
};
8581
}
86-
87-
private mapExternalDataToUniqueFlowEntity(
88-
external: InstanceDataOfModel<Database['externalReference']>
89-
): UniqueFlowEntity {
90-
return {
91-
id: createBrandedValue(external.flowID),
92-
versionID: external.versionID,
93-
};
94-
}
9582
}

src/domain-services/flows/strategy/impl/get-flowIds-flow-from-nested-flow-filters-strategy-impl.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import { ExternalReferenceService } from '../../../external-reference/external-r
33
import { LegacyService } from '../../../legacy/legacy-service';
44
import { ReportDetailService } from '../../../report-details/report-detail-service';
55
import { FlowService } from '../../flow-service';
6-
import type { UniqueFlowEntity } from '../../model';
76
import {
87
type FlowIDSearchStrategy,
98
type FlowIdSearchStrategyArgs,
109
type FlowIdSearchStrategyResponse,
1110
} from '../flowID-search-strategy';
12-
import { intersectUniqueFlowEntities } from './utils';
11+
import { intersectSets, parseFlowIdVersionSet } from './utils';
1312

1413
@Service()
1514
export class GetFlowIdsFromNestedFlowFiltersStrategyImpl
@@ -27,10 +26,10 @@ export class GetFlowIdsFromNestedFlowFiltersStrategyImpl
2726
): Promise<FlowIdSearchStrategyResponse> {
2827
const { models, nestedFlowFilters } = args;
2928

30-
let flowsReporterReferenceCode: UniqueFlowEntity[] = [];
31-
let flowsSourceSystemId: UniqueFlowEntity[] = [];
32-
let flowsSystemId: UniqueFlowEntity[] = [];
33-
const flowsLegacyId: UniqueFlowEntity[] = [];
29+
let flowsReporterReferenceCode: Set<string> = new Set<string>();
30+
let flowsSourceSystemId: Set<string> = new Set<string>();
31+
let flowsSystemId: Set<string> = new Set<string>();
32+
const flowsLegacyId: Set<string> = new Set<string>();
3433

3534
// Get the flowIDs using 'reporterReferenceCode'
3635
if (nestedFlowFilters?.reporterRefCode) {
@@ -67,30 +66,26 @@ export class GetFlowIdsFromNestedFlowFiltersStrategyImpl
6766
);
6867

6968
if (flowID) {
70-
flowsLegacyId.push({
71-
id: flowID,
72-
versionID: 1,
73-
});
69+
flowsLegacyId.add(`${flowID}:1`);
7470
}
7571
}
7672

7773
// Intersect the flowIDs from the nestedFlowFilters
78-
const flowIDsFromNestedFlowFilters: UniqueFlowEntity[] =
79-
intersectUniqueFlowEntities(
80-
flowsReporterReferenceCode,
81-
flowsSourceSystemId,
82-
flowsSystemId,
83-
flowsLegacyId
84-
);
74+
const flowIDsFromNestedFlowFilters: Set<string> = intersectSets(
75+
flowsReporterReferenceCode,
76+
flowsSourceSystemId,
77+
flowsSystemId,
78+
flowsLegacyId
79+
);
8580

86-
if (flowIDsFromNestedFlowFilters.length === 0) {
81+
if (flowIDsFromNestedFlowFilters.size === 0) {
8782
return { flows: [] };
8883
}
8984
// Once gathered and disjoined the flowIDs from the nestedFlowFilters
9085
// Look after this uniqueFlows in the flow table
9186
const flows = await this.flowService.progresiveSearch(
9287
models,
93-
flowIDsFromNestedFlowFilters,
88+
parseFlowIdVersionSet(flowIDsFromNestedFlowFilters),
9489
1000,
9590
0,
9691
false, // Stop when we have the limit

src/domain-services/flows/strategy/impl/get-flowIds-flow-object-conditions-strategy-impl.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { Op } from '@unocha/hpc-api-core/src/db/util/conditions';
22
import { Service } from 'typedi';
3-
import { type UniqueFlowEntity } from '../../model';
43
import {
54
type FlowIDSearchStrategy,
65
type FlowIdSearchStrategyArgs,
76
type FlowIdSearchStrategyResponse,
87
} from '../flowID-search-strategy';
9-
import { intersectUniqueFlowEntities } from './utils';
8+
import { intersectSets, parseFlowIdVersionSet } from './utils';
109

1110
@Service()
1211
export class GetFlowIdsFromObjectConditionsStrategyImpl
@@ -23,7 +22,7 @@ export class GetFlowIdsFromObjectConditionsStrategyImpl
2322
return { flows: [] };
2423
}
2524

26-
let intersectedFlows: UniqueFlowEntity[] = [];
25+
let intersectedFlows: Set<string> = new Set<string>();
2726

2827
for (const [flowObjectType, group] of flowObjectFilterGrouped.entries()) {
2928
for (const [direction, ids] of group.entries()) {
@@ -36,22 +35,19 @@ export class GetFlowIdsFromObjectConditionsStrategyImpl
3635
where: condition,
3736
});
3837

39-
const uniqueFlowObjectsEntities: UniqueFlowEntity[] =
38+
const uniqueFlowObjectsEntities: Set<string> = new Set<string>(
4039
flowObjectsFound.map(
41-
(flowObject) =>
42-
({
43-
id: flowObject.flowID,
44-
versionID: flowObject.versionID,
45-
}) satisfies UniqueFlowEntity
46-
);
47-
48-
intersectedFlows = intersectUniqueFlowEntities(
40+
(flowObject) => `${flowObject.flowID}:${flowObject.versionID}`
41+
)
42+
);
43+
44+
intersectedFlows = intersectSets(
4945
intersectedFlows,
5046
uniqueFlowObjectsEntities
5147
);
5248
}
5349
}
5450

55-
return { flows: intersectedFlows };
51+
return { flows: parseFlowIdVersionSet(intersectedFlows) };
5652
}
5753
}

0 commit comments

Comments
 (0)