Skip to content

Commit

Permalink
fix(core): Optimize SharedWorkflow queries (#6297)
Browse files Browse the repository at this point in the history
* optimize SharedWorkflow queries

* fix int to string ids
  • Loading branch information
flipswitchingmonkey authored May 23, 2023
1 parent 4d9c8b0 commit ed7f3b8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
36 changes: 17 additions & 19 deletions packages/cli/src/ActiveWorkflowRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)";
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -348,30 +345,31 @@ export class ActiveWorkflowRunner {
/**
* Returns the ids of the currently active workflows
*/
async getActiveWorkflows(user?: User): Promise<IWorkflowDb[]> {
async getActiveWorkflows(user?: User): Promise<string[]> {
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<WorkflowEntity[]>((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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ export async function getSharedWorkflowIds(user: User): Promise<string[]> {
select: ['workflowId'],
});
return sharedWorkflows.map(({ workflowId }) => workflowId);

return sharedWorkflows.map(({ workflowId }) => workflowId);
}

export async function getSharedWorkflow(
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}),
);

Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/UserManagement/PermissionChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit ed7f3b8

Please sign in to comment.