Skip to content

Commit

Permalink
feat(public api): Expand /executions public endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tkasriel-numberly committed Jul 7, 2023
1 parent 2e8dfb8 commit e41ba27
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 142 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/PublicApi/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export declare namespace ExecutionRequest {
{},
{},
{
status?: ExecutionStatus;
status?: ExecutionStatus | ExecutionStatus[];
limit?: number;
cursor?: string;
offset?: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
deleteExecution,
getExecutionsCount,
} from './executions.service';
import { ActiveExecutions } from '@/ActiveExecutions';
import { authorize, validCursor } from '../../shared/middlewares/global.middleware';
import type { ExecutionRequest } from '../../../types';
import { getSharedWorkflowIds } from '../workflows/workflows.service';
Expand Down Expand Up @@ -95,18 +94,12 @@ export = {
return res.status(200).json({ data: [], nextCursor: null });
}

// get running workflows so we exclude them from the result
const runningExecutionsIds = Container.get(ActiveExecutions)
.getActiveExecutions()
.map(({ id }) => id);

const filters = {
status,
limit,
lastId,
includeData,
workflowIds: workflowId ? [workflowId] : sharedWorkflowsIds,
excludedExecutionsIds: runningExecutionsIds,
};

const executions = await getExecutions(filters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ import type { ExecutionStatus } from 'n8n-workflow';
import Container from 'typedi';
import { ExecutionRepository } from '@/databases/repositories';

function getStatusCondition(status: ExecutionStatus) {
function getStatusCondition(status: ExecutionStatus | ExecutionStatus[]) {
const condition: Pick<FindOptionsWhere<IExecutionFlattedDb>, 'status'> = {};

if (status === 'success') {
condition.status = 'success';
} else if (status === 'waiting') {
condition.status = 'waiting';
} else if (status === 'error') {
condition.status = In(['error', 'crashed', 'failed']);
if (typeof status === 'string') {
status = [status];
}

// Prevent breaking change
const output = new Set<string>();
for (const currFilter of status) {
if (currFilter === 'error') {
output.add('error');
output.add('crashed');
output.add('failed');
} else {
output.add(currFilter);
}
}
condition.status = In(Array.from(output));
return condition;
}

Expand All @@ -26,7 +32,7 @@ export async function getExecutions(params: {
includeData?: boolean;
lastId?: string;
workflowIds?: string[];
status?: ExecutionStatus;
status?: ExecutionStatus | ExecutionStatus[];
excludedExecutionsIds?: string[];
}): Promise<IExecutionBase[]> {
let where: FindOptionsWhere<IExecutionFlattedDb> = {};
Expand Down Expand Up @@ -79,10 +85,11 @@ export async function getExecutionsCount(data: {
limit: number;
lastId?: string;
workflowIds?: string[];
status?: ExecutionStatus;
status?: ExecutionStatus | ExecutionStatus[];
excludedWorkflowIds?: string[];
}): Promise<number> {
// TODO: Consider moving this to the repository as well

const executions = await Db.collections.Execution.count({
where: {
...(data.lastId && { id: LessThan(data.lastId) }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ get:
description: Status to filter the executions by.
required: false
schema:
type: string
enum: ['error', 'success', 'waiting']
oneOf:
- type: string
enum: ['canceled', 'crashed', 'error', 'failed', 'new', 'running', 'success', 'unknown', 'waiting']
- type: array
minItems: 1
items:
type: string
enum: ['canceled', 'crashed', 'error', 'failed', 'new', 'running', 'success', 'unknown', 'waiting']
- name: workflowId
in: query
description: Workflow to filter the executions by.
Expand Down
Loading

0 comments on commit e41ba27

Please sign in to comment.