mirror of
https://github.com/n8n-io/n8n.git
synced 2025-02-21 02:56:40 -08:00
perf(core): Optimize executions filtering by metadata (#9477)
This commit is contained in:
parent
09a5867707
commit
9bdc83a399
|
@ -728,12 +728,17 @@ export class ExecutionRepository extends Repository<ExecutionEntity> {
|
||||||
if (startedBefore) qb.andWhere({ startedAt: lessThanOrEqual(startedBefore) });
|
if (startedBefore) qb.andWhere({ startedAt: lessThanOrEqual(startedBefore) });
|
||||||
if (startedAfter) qb.andWhere({ startedAt: moreThanOrEqual(startedAfter) });
|
if (startedAfter) qb.andWhere({ startedAt: moreThanOrEqual(startedAfter) });
|
||||||
|
|
||||||
if (metadata) {
|
if (metadata?.length === 1) {
|
||||||
qb.leftJoin(ExecutionMetadata, 'md', 'md.executionId = execution.id');
|
const [{ key, value }] = metadata;
|
||||||
|
|
||||||
for (const item of metadata) {
|
qb.innerJoin(
|
||||||
qb.andWhere('md.key = :key AND md.value = :value', item);
|
ExecutionMetadata,
|
||||||
}
|
'md',
|
||||||
|
'md.executionId = execution.id AND md.key = :key AND md.value = :value',
|
||||||
|
);
|
||||||
|
|
||||||
|
qb.setParameter('key', key);
|
||||||
|
qb.setParameter('value', value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return qb;
|
return qb;
|
||||||
|
|
|
@ -257,6 +257,32 @@ describe('ExecutionService', () => {
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should filter executions by `metadata`', async () => {
|
||||||
|
const workflow = await createWorkflow();
|
||||||
|
|
||||||
|
const metadata = [{ key: 'myKey', value: 'myValue' }];
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
createExecution({ status: 'success', metadata }, workflow),
|
||||||
|
createExecution({ status: 'error' }, workflow),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const query: ExecutionSummaries.RangeQuery = {
|
||||||
|
kind: 'range',
|
||||||
|
range: { limit: 20 },
|
||||||
|
accessibleWorkflowIds: [workflow.id],
|
||||||
|
metadata,
|
||||||
|
};
|
||||||
|
|
||||||
|
const output = await executionService.findRangeWithCount(query);
|
||||||
|
|
||||||
|
expect(output).toEqual({
|
||||||
|
count: 1,
|
||||||
|
estimated: false,
|
||||||
|
results: [expect.objectContaining({ status: 'success' })],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('should exclude executions by inaccessible `workflowId`', async () => {
|
test('should exclude executions by inaccessible `workflowId`', async () => {
|
||||||
const accessibleWorkflow = await createWorkflow();
|
const accessibleWorkflow = await createWorkflow();
|
||||||
const inaccessibleWorkflow = await createWorkflow();
|
const inaccessibleWorkflow = await createWorkflow();
|
||||||
|
|
|
@ -4,6 +4,7 @@ import type { ExecutionEntity } from '@db/entities/ExecutionEntity';
|
||||||
import type { WorkflowEntity } from '@db/entities/WorkflowEntity';
|
import type { WorkflowEntity } from '@db/entities/WorkflowEntity';
|
||||||
import { ExecutionRepository } from '@db/repositories/execution.repository';
|
import { ExecutionRepository } from '@db/repositories/execution.repository';
|
||||||
import { ExecutionDataRepository } from '@db/repositories/executionData.repository';
|
import { ExecutionDataRepository } from '@db/repositories/executionData.repository';
|
||||||
|
import { ExecutionMetadataRepository } from '@/databases/repositories/executionMetadata.repository';
|
||||||
|
|
||||||
export async function createManyExecutions(
|
export async function createManyExecutions(
|
||||||
amount: number,
|
amount: number,
|
||||||
|
@ -18,10 +19,14 @@ export async function createManyExecutions(
|
||||||
* Store a execution in the DB and assign it to a workflow.
|
* Store a execution in the DB and assign it to a workflow.
|
||||||
*/
|
*/
|
||||||
export async function createExecution(
|
export async function createExecution(
|
||||||
attributes: Partial<ExecutionEntity & ExecutionData>,
|
attributes: Partial<
|
||||||
|
Omit<ExecutionEntity, 'metadata'> &
|
||||||
|
ExecutionData & { metadata: Array<{ key: string; value: string }> }
|
||||||
|
>,
|
||||||
workflow: WorkflowEntity,
|
workflow: WorkflowEntity,
|
||||||
) {
|
) {
|
||||||
const { data, finished, mode, startedAt, stoppedAt, waitTill, status, deletedAt } = attributes;
|
const { data, finished, mode, startedAt, stoppedAt, waitTill, status, deletedAt, metadata } =
|
||||||
|
attributes;
|
||||||
|
|
||||||
const execution = await Container.get(ExecutionRepository).save({
|
const execution = await Container.get(ExecutionRepository).save({
|
||||||
finished: finished ?? true,
|
finished: finished ?? true,
|
||||||
|
@ -34,6 +39,16 @@ export async function createExecution(
|
||||||
deletedAt,
|
deletedAt,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (metadata?.length) {
|
||||||
|
const metadataToSave = metadata.map(({ key, value }) => ({
|
||||||
|
key,
|
||||||
|
value,
|
||||||
|
execution: { id: execution.id },
|
||||||
|
}));
|
||||||
|
|
||||||
|
await Container.get(ExecutionMetadataRepository).save(metadataToSave);
|
||||||
|
}
|
||||||
|
|
||||||
await Container.get(ExecutionDataRepository).save({
|
await Container.get(ExecutionDataRepository).save({
|
||||||
data: data ?? '[]',
|
data: data ?? '[]',
|
||||||
workflowData: workflow ?? {},
|
workflowData: workflow ?? {},
|
||||||
|
|
Loading…
Reference in a new issue