From ed7f3b845fe9a7aa0f6e1ff57ae9197057cd8aa1 Mon Sep 17 00:00:00 2001 From: Michael Auerswald Date: Tue, 23 May 2023 09:40:38 +0200 Subject: [PATCH] fix(core): Optimize SharedWorkflow queries (#6297) * optimize SharedWorkflow queries * fix int to string ids --- packages/cli/src/ActiveWorkflowRunner.ts | 36 +++++++++---------- .../handlers/workflows/workflows.service.ts | 2 -- packages/cli/src/Server.ts | 3 +- .../src/UserManagement/PermissionChecker.ts | 1 + 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/packages/cli/src/ActiveWorkflowRunner.ts b/packages/cli/src/ActiveWorkflowRunner.ts index 1cbfca63073ee..1de34856d34f9 100644 --- a/packages/cli/src/ActiveWorkflowRunner.ts +++ b/packages/cli/src/ActiveWorkflowRunner.ts @@ -67,6 +67,7 @@ import { whereClause } from './UserManagement/UserManagementHelper'; import { WorkflowsService } from './workflows/workflows.services'; import { START_NODES } from './constants'; import { webhookNotFoundErrorMessage } from './utils'; +import { In } from 'typeorm'; const WEBHOOK_PROD_UNREGISTERED_HINT = "The workflow must be active for a production URL to run successfully. You can activate the workflow using the toggle in the top-right of the editor. Note that unlike test URL calls, production URL calls aren't shown on the canvas (only in the executions list)"; @@ -168,11 +169,7 @@ export class ActiveWorkflowRunner { activeWorkflowIds.push.apply(activeWorkflowIds, this.activeWorkflows.allActiveWorkflows()); const activeWorkflows = await this.getActiveWorkflows(); - activeWorkflowIds = [ - ...activeWorkflowIds, - ...activeWorkflows.map((workflow) => workflow.id.toString()), - ]; - + activeWorkflowIds = [...activeWorkflowIds, ...activeWorkflows]; // Make sure IDs are unique activeWorkflowIds = Array.from(new Set(activeWorkflowIds)); @@ -348,30 +345,31 @@ export class ActiveWorkflowRunner { /** * Returns the ids of the currently active workflows */ - async getActiveWorkflows(user?: User): Promise { + async getActiveWorkflows(user?: User): Promise { let activeWorkflows: WorkflowEntity[] = []; - if (!user || user.globalRole.name === 'owner') { activeWorkflows = await Db.collections.Workflow.find({ select: ['id'], where: { active: true }, }); + return activeWorkflows.map((workflow) => workflow.id.toString()); } else { + const active = await Db.collections.Workflow.find({ + select: ['id'], + where: { active: true }, + }); + const activeIds = active.map((workflow) => workflow.id); + const where = whereClause({ + user, + entityType: 'workflow', + }); + Object.assign(where, { workflowId: In(activeIds) }); const shared = await Db.collections.SharedWorkflow.find({ - relations: ['workflow'], - where: whereClause({ - user, - entityType: 'workflow', - }), + select: ['workflowId'], + where, }); - - activeWorkflows = shared.reduce((acc, cur) => { - if (cur.workflow.active) acc.push(cur.workflow); - return acc; - }, []); + return shared.map((id) => id.workflowId.toString()); } - - return activeWorkflows.filter((workflow) => this.activationErrors[workflow.id] === undefined); } /** diff --git a/packages/cli/src/PublicApi/v1/handlers/workflows/workflows.service.ts b/packages/cli/src/PublicApi/v1/handlers/workflows/workflows.service.ts index adb37dce57dd1..8ed5031628853 100644 --- a/packages/cli/src/PublicApi/v1/handlers/workflows/workflows.service.ts +++ b/packages/cli/src/PublicApi/v1/handlers/workflows/workflows.service.ts @@ -24,8 +24,6 @@ export async function getSharedWorkflowIds(user: User): Promise { select: ['workflowId'], }); return sharedWorkflows.map(({ workflowId }) => workflowId); - - return sharedWorkflows.map(({ workflowId }) => workflowId); } export async function getSharedWorkflow( diff --git a/packages/cli/src/Server.ts b/packages/cli/src/Server.ts index 8739fe9ed166d..6ad8ac7da2cf6 100644 --- a/packages/cli/src/Server.ts +++ b/packages/cli/src/Server.ts @@ -764,8 +764,7 @@ export class Server extends AbstractServer { this.app.get( `/${this.restEndpoint}/active`, ResponseHelper.send(async (req: WorkflowRequest.GetAllActive) => { - const activeWorkflows = await this.activeWorkflowRunner.getActiveWorkflows(req.user); - return activeWorkflows.map(({ id }) => id); + return this.activeWorkflowRunner.getActiveWorkflows(req.user); }), ); diff --git a/packages/cli/src/UserManagement/PermissionChecker.ts b/packages/cli/src/UserManagement/PermissionChecker.ts index d2327159d4b54..b8b851419aec0 100644 --- a/packages/cli/src/UserManagement/PermissionChecker.ts +++ b/packages/cli/src/UserManagement/PermissionChecker.ts @@ -44,6 +44,7 @@ export class PermissionChecker { const workflowSharings = await Db.collections.SharedWorkflow.find({ relations: ['workflow'], where: { workflowId: workflow.id }, + select: ['userId'], }); workflowUserIds = workflowSharings.map((s) => s.userId); }