Skip to content

Commit

Permalink
Add 'usageYear' filter to reduce computation time
Browse files Browse the repository at this point in the history
  • Loading branch information
manelcecs committed Apr 2, 2024
1 parent b802bf3 commit 7a22ae2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
47 changes: 47 additions & 0 deletions src/domain-services/flows/flow-search-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,53 @@ export class FlowSearchService {
flowFilters.activeStatus = true;
}

if (flowObjectFilters) {
const usageYearFilter = flowObjectFilters.find(
(filter) => filter.objectType === 'usageYear'
);

if (!usageYearFilter) {
// Find the flowObjectFilters since 2021 until currentYear
let startYear = 2021;
const currentYear = new Date().getFullYear();

const usageYearsArrayFilter: string[] = [];
while (startYear <= currentYear) {
usageYearsArrayFilter.push(startYear.toString());
startYear++;
}
const usageYears = await models.usageYear.find({
where: {
year: {
[Op.IN]: usageYearsArrayFilter,
},
},
});

for (const usageYear of usageYears) {
// Map the usageYear filters to the flowObjectFilters
const sourceUsageYearFilter: FlowObjectFilters = {
objectType: 'usageYear',
direction: 'source',
objectID: usageYear.id.valueOf(),
inclusive: true,
};

const destinationUsageYearFilter: FlowObjectFilters = {
objectType: 'usageYear',
direction: 'destination',
objectID: usageYear.id.valueOf(),
inclusive: true,
};

flowObjectFilters.push(
sourceUsageYearFilter,
destinationUsageYearFilter
);
}
}
}

// This filter MUST apply always
flowFilters.restricted = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,22 +247,18 @@ export class SearchFlowByFiltersStrategy implements FlowSearchStrategy {
// We need to paginate over the searchConditions
// Then collect the flows

// 1. Generate an array of arrays with the chunkSize
const chunkedFlows = [];
// 1. Generate an array with the chunkSize
for (let i = 0; i < reducedFlows.length; i += chunkSize) {
chunkedFlows.push(reducedFlows.slice(i, i + chunkSize));
}

// 2. Iterate over the array of arrays to generate the searchConditions
// And collect the flows
for (const chunk of chunkedFlows) {
const chunk = reducedFlows.slice(i, i + chunkSize);
// 2. Generate the searchConditions
// And collect the flows
const chunkSearchConditions = this.buildConditions(chunk, flowFilters);
const flowsChunk = await this.flowService.getFlows({
models,
conditions: chunkSearchConditions,
orderBy: orderByForFlow,
});
flows = [...flows, ...flowsChunk];
flows.push(...flowsChunk);
}
} else {
// Once the list of elements is reduced, we need to build the conditions
Expand Down

0 comments on commit 7a22ae2

Please sign in to comment.