fix(core): Optimize SharedWorkflow queries (#6297)

* optimize SharedWorkflow queries

* fix int to string ids
This commit is contained in:
Michael Auerswald 2023-05-23 09:40:38 +02:00 committed by GitHub
parent 4d9c8b07a9
commit ed7f3b845f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 24 deletions

View file

@ -67,6 +67,7 @@ import { whereClause } from './UserManagement/UserManagementHelper';
import { WorkflowsService } from './workflows/workflows.services'; import { WorkflowsService } from './workflows/workflows.services';
import { START_NODES } from './constants'; import { START_NODES } from './constants';
import { webhookNotFoundErrorMessage } from './utils'; import { webhookNotFoundErrorMessage } from './utils';
import { In } from 'typeorm';
const WEBHOOK_PROD_UNREGISTERED_HINT = 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)"; "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()); activeWorkflowIds.push.apply(activeWorkflowIds, this.activeWorkflows.allActiveWorkflows());
const activeWorkflows = await this.getActiveWorkflows(); const activeWorkflows = await this.getActiveWorkflows();
activeWorkflowIds = [ activeWorkflowIds = [...activeWorkflowIds, ...activeWorkflows];
...activeWorkflowIds,
...activeWorkflows.map((workflow) => workflow.id.toString()),
];
// Make sure IDs are unique // Make sure IDs are unique
activeWorkflowIds = Array.from(new Set(activeWorkflowIds)); activeWorkflowIds = Array.from(new Set(activeWorkflowIds));
@ -348,30 +345,31 @@ export class ActiveWorkflowRunner {
/** /**
* Returns the ids of the currently active workflows * Returns the ids of the currently active workflows
*/ */
async getActiveWorkflows(user?: User): Promise<IWorkflowDb[]> { async getActiveWorkflows(user?: User): Promise<string[]> {
let activeWorkflows: WorkflowEntity[] = []; let activeWorkflows: WorkflowEntity[] = [];
if (!user || user.globalRole.name === 'owner') { if (!user || user.globalRole.name === 'owner') {
activeWorkflows = await Db.collections.Workflow.find({ activeWorkflows = await Db.collections.Workflow.find({
select: ['id'], select: ['id'],
where: { active: true }, where: { active: true },
}); });
return activeWorkflows.map((workflow) => workflow.id.toString());
} else { } else {
const shared = await Db.collections.SharedWorkflow.find({ const active = await Db.collections.Workflow.find({
relations: ['workflow'], select: ['id'],
where: whereClause({ where: { active: true },
user,
entityType: 'workflow',
}),
}); });
const activeIds = active.map((workflow) => workflow.id);
activeWorkflows = shared.reduce<WorkflowEntity[]>((acc, cur) => { const where = whereClause({
if (cur.workflow.active) acc.push(cur.workflow); user,
return acc; entityType: 'workflow',
}, []); });
Object.assign(where, { workflowId: In(activeIds) });
const shared = await Db.collections.SharedWorkflow.find({
select: ['workflowId'],
where,
});
return shared.map((id) => id.workflowId.toString());
} }
return activeWorkflows.filter((workflow) => this.activationErrors[workflow.id] === undefined);
} }
/** /**

View file

@ -24,8 +24,6 @@ export async function getSharedWorkflowIds(user: User): Promise<string[]> {
select: ['workflowId'], select: ['workflowId'],
}); });
return sharedWorkflows.map(({ workflowId }) => workflowId); return sharedWorkflows.map(({ workflowId }) => workflowId);
return sharedWorkflows.map(({ workflowId }) => workflowId);
} }
export async function getSharedWorkflow( export async function getSharedWorkflow(

View file

@ -764,8 +764,7 @@ export class Server extends AbstractServer {
this.app.get( this.app.get(
`/${this.restEndpoint}/active`, `/${this.restEndpoint}/active`,
ResponseHelper.send(async (req: WorkflowRequest.GetAllActive) => { ResponseHelper.send(async (req: WorkflowRequest.GetAllActive) => {
const activeWorkflows = await this.activeWorkflowRunner.getActiveWorkflows(req.user); return this.activeWorkflowRunner.getActiveWorkflows(req.user);
return activeWorkflows.map(({ id }) => id);
}), }),
); );

View file

@ -44,6 +44,7 @@ export class PermissionChecker {
const workflowSharings = await Db.collections.SharedWorkflow.find({ const workflowSharings = await Db.collections.SharedWorkflow.find({
relations: ['workflow'], relations: ['workflow'],
where: { workflowId: workflow.id }, where: { workflowId: workflow.id },
select: ['userId'],
}); });
workflowUserIds = workflowSharings.map((s) => s.userId); workflowUserIds = workflowSharings.map((s) => s.userId);
} }