Skip to content

Commit

Permalink
Temp: fix MaxStackSize when filtering categoryFlows
Browse files Browse the repository at this point in the history
  • Loading branch information
manelcecs committed Apr 16, 2024
1 parent 95d03f1 commit f5bea6c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 32 deletions.
55 changes: 29 additions & 26 deletions src/domain-services/flows/flow-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,40 @@ export class FlowService {
whereClauses,
} = args;

let query = databaseConnection!
.queryBuilder()
.distinct('id', 'versionID', orderBy.column) // Include orderBy.column in the distinct selection
.from('flow')
.whereNull('deletedAt')
.orderBy(orderBy.column, orderBy.order);
if (databaseConnection) {
let query = databaseConnection
.queryBuilder()
.distinct('id', 'versionID', orderBy.column) // Include orderBy.column in the distinct selection
.from('flow')
.whereNull('deletedAt')
.orderBy(orderBy.column, orderBy.order);

if (conditions) {
query = applySearchFilters(query, conditions);
}
if (whereClauses) {
query = query.andWhere(prepareCondition(whereClauses));
}
if (conditions) {
query = applySearchFilters(query, conditions);
}
if (whereClauses) {
query = query.andWhere(prepareCondition(whereClauses));
}

// Because this list can be really large (+330K entries), we need to split it in chunks
// Using limit and offset as cursors
const databaseParsingLimit = 10_000;
if (limit && offset) {
query = query.limit(databaseParsingLimit + offset).offset(offset);
} else if (limit) {
query = query.limit(limit + databaseParsingLimit);
}
// Because this list can be really large (+330K entries), we need to split it in chunks
// Using limit and offset as cursors
const databaseParsingLimit = 10_000;
if (limit && offset) {
query = query.limit(databaseParsingLimit + offset).offset(offset);
} else if (limit) {
query = query.limit(limit + databaseParsingLimit);
}

const flows = await query;
const flows = await query;

const mapFlowsToUniqueFlowEntities = flows.map((flow) => ({
id: flow.id,
versionID: flow.versionID,
}));
const mapFlowsToUniqueFlowEntities = flows.map((flow) => ({
id: flow.id,
versionID: flow.versionID,
}));

return mapFlowsToUniqueFlowEntities;
return mapFlowsToUniqueFlowEntities;
}
return [];
}

async getFlowsCount(databaseConnection: Knex, conditions: any) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { type UniqueFlowEntity } from '../model';

export interface FlowIdSearchStrategyResponse {
flows: UniqueFlowEntity[];
count?: number | null;
}

export interface FlowIdSearchStrategyArgs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,19 @@ export class GetFlowIdsFromCategoryConditionsStrategyImpl
// Once we have the flowIDs from the category conditions
// Look after this uniqueFlows in the flow table
// to check if they are not 'deleted' (by the key: 'deletedAt')

// TEMPORARY FIX
const reducedFlows = mapFlows.slice(0, 1000);
const searchFlowArgs = {
databaseConnection,
orderBy: defaultFlowOrderBy(),
whereClauses: buildSearchFlowsConditions(mapFlows),
whereClauses: buildSearchFlowsConditions(reducedFlows),
};
const uniqueEntitiesFlow =
await this.flowService.getFlowsAsUniqueFlowEntity(searchFlowArgs);

return { flows: uniqueEntitiesFlow };
const count =
reducedFlows.length < mapFlows.length ? mapFlows.length : null;
return { flows: uniqueEntitiesFlow, count };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class GetFlowIdsFromObjectConditionsStrategyImpl
orderBy: defaultFlowOrderBy(),
whereClauses: buildSearchFlowsConditions(flowsFromFilteredFlowObjects),
};

const uniqueFlowsNotDeleted =
await this.flowService.getFlowsAsUniqueFlowEntity(searchFlowArgs);
return { flows: uniqueFlowsNotDeleted };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ export class SearchFlowByFiltersStrategy implements FlowSearchStrategy {
isSearchByCategoryShotcut || flowCategoryFilters?.length > 0;

const flowsFromCategoryFilters: UniqueFlowEntity[] = [];

// TEMPORARY FIX
let categoryFlowsCount: number | null = null;
if (isFilterByCategory) {
const { flows }: FlowIdSearchStrategyResponse =
const { flows, count }: FlowIdSearchStrategyResponse =
await this.getFlowIdsFromCategoryConditions.search({
databaseConnection,
models,
Expand All @@ -149,8 +150,8 @@ export class SearchFlowByFiltersStrategy implements FlowSearchStrategy {
for (const flow of flows) {
flowsFromCategoryFilters.push(flow);
}
categoryFlowsCount = count ?? null;
}

// After that, if we need to filter by flowObjects
// Obtain the flowIDs from the flowObjects
const isFilterByFlowObjects = flowObjectFilters?.length > 0;
Expand Down Expand Up @@ -234,7 +235,8 @@ export class SearchFlowByFiltersStrategy implements FlowSearchStrategy {
sortedFlows = deduplicatedFlows;
}

const count = sortedFlows.length;
// TEMPORARY FIX
const count = categoryFlowsCount ?? sortedFlows.length;

const flowEntityOrderBy = defaultFlowOrderBy();
// Store the flows promise
Expand Down

0 comments on commit f5bea6c

Please sign in to comment.