Skip to content

Commit f2f399f

Browse files
committed
🏃 Increased performance when obtaining 'Parked Parents Flows'
1 parent 6e2294b commit f2f399f

File tree

3 files changed

+36
-26
lines changed

3 files changed

+36
-26
lines changed

src/domain-services/flow-link/flow-link-service.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type FlowId } from '@unocha/hpc-api-core/src/db/models/flow';
22
import { type Database } from '@unocha/hpc-api-core/src/db/type';
3-
import { Op } from '@unocha/hpc-api-core/src/db/util/conditions';
3+
import { Cond, Op } from '@unocha/hpc-api-core/src/db/util/conditions';
44
import { type InstanceOfModel } from '@unocha/hpc-api-core/src/db/util/types';
55
import { getOrCreate } from '@unocha/hpc-api-core/src/util';
66
import { Service } from 'typedi';
@@ -14,9 +14,25 @@ export class FlowLinkService {
1414
// Fetch all flow links in one go
1515
const flowLinks = await models.flowLink.find({
1616
where: {
17-
parentID: {
18-
[Op.IN]: flowIds,
19-
},
17+
[Cond.AND]: [
18+
{
19+
[Cond.OR]: [
20+
{
21+
parentID: {
22+
[Op.IN]: flowIds,
23+
},
24+
},
25+
{
26+
childID: {
27+
[Op.IN]: flowIds,
28+
},
29+
},
30+
],
31+
},
32+
{
33+
depth: { [Op.GTE]: 1 },
34+
},
35+
],
2036
},
2137
});
2238

@@ -28,12 +44,10 @@ export class FlowLinkService {
2844

2945
// Group flow links by parentID in one pass
3046
for (const link of flowLinks) {
31-
const flowLinksForFlow = getOrCreate(
32-
flowLinksMap,
33-
link.parentID,
34-
() => []
35-
);
36-
flowLinksForFlow.push(link);
47+
const childFlowLink = getOrCreate(flowLinksMap, link.childID, () => []);
48+
const parentFlowLink = getOrCreate(flowLinksMap, link.parentID, () => []);
49+
childFlowLink.push(link);
50+
parentFlowLink.push(link);
3751
}
3852

3953
return flowLinksMap;

src/domain-services/flows/flow-search-service.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,6 @@ export class FlowSearchService {
223223
]);
224224

225225
const promises = flows.map(async (flow) => {
226-
const flowLink = getOrCreate(flowLinksMap, flow.id, () => []);
227-
228226
// Categories Map follows the structure:
229227
// flowID: { versionID: [categories]}
230228
// So we need to get the categories for the flow version
@@ -247,16 +245,6 @@ export class FlowSearchService {
247245
}
248246

249247
let parkedParentSource: FlowParkedParentSource | null = null;
250-
const shouldLookAfterParentSource =
251-
flowLink.length > 0 && shouldIncludeChildrenOfParkedFlows;
252-
253-
if (shouldLookAfterParentSource) {
254-
parkedParentSource = await this.flowService.getParketParents(
255-
flow,
256-
flowLink,
257-
models
258-
);
259-
}
260248

261249
const childIDs: number[] =
262250
flowLinksMap
@@ -266,14 +254,23 @@ export class FlowSearchService {
266254
)
267255
.map((flowLink) => flowLink.childID.valueOf()) ?? [];
268256

269-
const parentIDs: number[] =
257+
const parentLinks =
270258
flowLinksMap
271259
.get(flow.id)
272260
?.filter(
273261
(flowLink) => flowLink.childID === flow.id && flowLink.depth > 0
274-
)
275-
.map((flowLink) => flowLink.parentID.valueOf()) ?? [];
262+
) ?? [];
263+
const parentIDs: number[] = parentLinks.map((flowLink) =>
264+
flowLink.parentID.valueOf()
265+
);
276266

267+
if (shouldIncludeChildrenOfParkedFlows) {
268+
parkedParentSource = await this.flowService.getParketParents(
269+
flow,
270+
parentLinks,
271+
models
272+
);
273+
}
277274
const parsedFlow: Flow = this.buildFlowDTO(
278275
flow,
279276
categoriesByVersion,

src/domain-services/flows/flow-service.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,6 @@ export class FlowService {
386386
if (flowLinksParentsIDs.length === 0) {
387387
return null;
388388
}
389-
390389
const parkedCategory = await models.category.findOne({
391390
where: {
392391
group: 'flowType',

0 commit comments

Comments
 (0)