Skip to content

Commit 75e09a3

Browse files
committed
[Test] red.dev deployment
1 parent b48d57c commit 75e09a3

File tree

3 files changed

+72
-41
lines changed

3 files changed

+72
-41
lines changed

src/domain-services/flows/strategy/flowID-search-strategy.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { type Database } from '@unocha/hpc-api-core/src/db';
2+
import { type FlowId } from '@unocha/hpc-api-core/src/db/models/flow';
23
import { type ShortcutCategoryFilter } from '../../categories/model';
34
import { type FlowObjectFilterGrouped } from '../../flow-object/model';
45
import { type FlowCategory, type NestedFlowFilters } from '../graphql/args';
@@ -14,6 +15,7 @@ export interface FlowIdSearchStrategyArgs {
1415
flowCategoryConditions?: FlowCategory[];
1516
nestedFlowFilters?: NestedFlowFilters;
1617
shortcutFilters?: ShortcutCategoryFilter[] | null;
18+
candidates?: Set<FlowId>;
1719
}
1820

1921
export interface FlowIDSearchStrategy {

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import { type FlowId } from '@unocha/hpc-api-core/src/db/models/flow';
12
import { Op } from '@unocha/hpc-api-core/src/db/util/conditions';
3+
import { splitIntoChunks } from '@unocha/hpc-api-core/src/util';
4+
import { PG_MAX_QUERY_PARAMS } from '@unocha/hpc-api-core/src/util/consts';
25
import { Service } from 'typedi';
36
import {
47
type FlowIDSearchStrategy,
@@ -16,12 +19,12 @@ export class GetFlowIdsFromObjectConditionsStrategyImpl
1619
async search(
1720
args: FlowIdSearchStrategyArgs
1821
): Promise<FlowIdSearchStrategyResponse> {
19-
const { flowObjectFilterGrouped, models } = args;
22+
const { flowObjectFilterGrouped, models, candidates } = args;
2023

2124
if (!flowObjectFilterGrouped) {
2225
return { flows: [] };
2326
}
24-
27+
let flowCandidates = candidates ?? new Set<FlowId>();
2528
let intersectedFlows = new Set<string>();
2629

2730
for (const [flowObjectType, group] of flowObjectFilterGrouped.entries()) {
@@ -31,16 +34,28 @@ export class GetFlowIdsFromObjectConditionsStrategyImpl
3134
refDirection: direction,
3235
objectID: { [Op.IN]: ids },
3336
};
34-
const flowObjectsFound = await models.flowObject.find({
35-
where: condition,
36-
});
37+
38+
const flowObjectsFound = (
39+
await Promise.all(
40+
splitIntoChunks([...flowCandidates], PG_MAX_QUERY_PARAMS - 10).map(
41+
(entityIds) =>
42+
models.flowObject.find({
43+
where: { ...condition, flowID: { [Op.IN]: entityIds } },
44+
distinct: ['flowID', 'versionID'],
45+
})
46+
)
47+
)
48+
).flat();
3749

3850
const uniqueFlowObjectsEntities = new Set<string>(
3951
flowObjectsFound.map(
4052
(flowObject) => `${flowObject.flowID}:${flowObject.versionID}`
4153
)
4254
);
43-
55+
flowCandidates = intersectSets(
56+
flowCandidates,
57+
new Set(flowObjectsFound.map((flowObject) => flowObject.flowID))
58+
);
4459
intersectedFlows = intersectSets(
4560
intersectedFlows,
4661
uniqueFlowObjectsEntities

src/domain-services/flows/strategy/impl/search-flow-by-filters-strategy-impl.ts

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,6 @@ export class SearchFlowByFiltersStrategy implements FlowSearchStrategy {
5252
// to be able to sort the flows using the entity
5353
const isSortByEntity = orderBy && orderBy.entity !== 'flow';
5454
let sortByFlowIDsPromise: Promise<UniqueFlowEntity[]> = Promise.resolve([]);
55-
const orderByForFlow = mapFlowOrderBy(orderBy);
56-
57-
// Fetch sorted flow IDs only for the filtered subset instead of the whole table
58-
if (isSortByEntity) {
59-
// Get entity-sorted IDs then intersect with filtered subset
60-
sortByFlowIDsPromise = this.flowService.getFlowIDsFromEntity(
61-
models,
62-
orderBy
63-
);
64-
} else {
65-
// Let the DB sort only the filtered IDs
66-
sortByFlowIDsPromise = this.flowService.getFlows({
67-
models,
68-
orderBy: orderByForFlow,
69-
});
70-
}
7155
// We need to fetch the flowIDs by the nestedFlowFilters
7256
// if there are any
7357
const isFilterByNestedFilters = nestedFlowFilters !== undefined;
@@ -118,21 +102,6 @@ export class SearchFlowByFiltersStrategy implements FlowSearchStrategy {
118102
let didFlowsFromObjectFiltersPromiseCreated = false;
119103
let flowObjectFiltersGrouped: FlowObjectFilterGrouped | null = null;
120104

121-
if (isFilterByFlowObjects) {
122-
// First step is to map the filters to the FlowObjectFiltersGrouped
123-
// To allow doing inclusive filtering between filters of the same type+direction
124-
// But exclusive filtering between filters of different type+direction
125-
flowObjectFiltersGrouped =
126-
mapFlowFiltersToFlowObjectFiltersGrouped(flowObjectFilters);
127-
128-
flowsFromObjectFiltersPromise =
129-
this.getFlowIdsFromObjectConditions.search({
130-
models,
131-
flowObjectFilterGrouped: flowObjectFiltersGrouped,
132-
});
133-
didFlowsFromObjectFiltersPromiseCreated = true;
134-
}
135-
136105
// Lastly, we need to check if we need to filter by flow
137106
// And if we didn't did it before when sorting by entity
138107
// if so, we need to obtain the flowIDs from the flowFilters
@@ -163,17 +132,62 @@ export class SearchFlowByFiltersStrategy implements FlowSearchStrategy {
163132
const [
164133
flowsFromCategoryFilters,
165134
flowsFromNestedFilters,
166-
flowsFromObjectFilters,
167135
flowsFromFlowFilters,
168-
sortByFlowIDs,
169136
] = await Promise.all([
170137
flowsFromCategoryFiltersPromise,
171138
flowsFromNestedFiltersPromise,
172-
flowsFromObjectFiltersPromise,
173139
flowsFromFlowFiltersPromise,
174-
sortByFlowIDsPromise,
175140
]);
176141

142+
const intersectCandidates = intersectSets(
143+
new Set(flowsFromCategoryFilters.flows.map((f) => f.id)),
144+
new Set(flowsFromNestedFilters.flows.map((f) => f.id)),
145+
new Set(flowsFromFlowFilters.map((f) => f.id))
146+
);
147+
148+
if (isFilterByFlowObjects) {
149+
// First step is to map the filters to the FlowObjectFiltersGrouped
150+
// To allow doing inclusive filtering between filters of the same type+direction
151+
// But exclusive filtering between filters of different type+direction
152+
flowObjectFiltersGrouped =
153+
mapFlowFiltersToFlowObjectFiltersGrouped(flowObjectFilters);
154+
155+
flowsFromObjectFiltersPromise =
156+
this.getFlowIdsFromObjectConditions.search({
157+
models,
158+
flowObjectFilterGrouped: flowObjectFiltersGrouped,
159+
candidates: intersectCandidates,
160+
});
161+
didFlowsFromObjectFiltersPromiseCreated = true;
162+
}
163+
const flowsFromObjectFilters = await flowsFromObjectFiltersPromise;
164+
165+
const orderByForFlow = mapFlowOrderBy(orderBy);
166+
167+
// Fetch sorted flow IDs only for the filtered subset instead of the whole table
168+
if (isSortByEntity) {
169+
// Get entity-sorted IDs then intersect with filtered subset
170+
sortByFlowIDsPromise = this.flowService.getFlowIDsFromEntity(
171+
models,
172+
orderBy
173+
);
174+
} else {
175+
// Let the DB sort only the filtered IDs
176+
sortByFlowIDsPromise = this.flowService.getFlows({
177+
models,
178+
conditions: {
179+
id: {
180+
[models.Op.IN]: intersectSets(
181+
intersectCandidates,
182+
new Set(flowsFromObjectFilters.flows.map((f) => f.id))
183+
),
184+
},
185+
},
186+
orderBy: orderByForFlow,
187+
});
188+
}
189+
const sortByFlowIDs = await sortByFlowIDsPromise;
190+
177191
// First check if we have created the promises
178192
// and if so, check if the flows are empty
179193
// If they are empty, we can return an empty array

0 commit comments

Comments
 (0)