From cd5ad65e90a3be4d67b51521772e0fceb7f4abc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20G=C3=B3mez=20Morales?= Date: Wed, 20 Nov 2024 15:33:16 +0100 Subject: [PATCH] fix(editor): Fix executions sorting (#11808) --- packages/editor-ui/src/Interface.ts | 1 + .../global/GlobalExecutionsList.test.ts | 1 + .../WorkflowExecutionsPreview.test.ts | 1 + .../src/composables/useRunWorkflow.ts | 1 + .../src/stores/executions.store.test.ts | 47 ++++++++++++++++++- .../editor-ui/src/stores/executions.store.ts | 2 +- .../src/stores/workflows.store.test.ts | 1 + packages/workflow/src/Interfaces.ts | 2 +- 8 files changed, 52 insertions(+), 4 deletions(-) diff --git a/packages/editor-ui/src/Interface.ts b/packages/editor-ui/src/Interface.ts index 45a09849b4..625e6e7a87 100644 --- a/packages/editor-ui/src/Interface.ts +++ b/packages/editor-ui/src/Interface.ts @@ -369,6 +369,7 @@ export interface IExecutionBase { retryOf?: string; retrySuccessId?: string; startedAt: Date; + createdAt: Date; stoppedAt?: Date; workflowId?: string; // To be able to filter executions easily // } diff --git a/packages/editor-ui/src/components/executions/global/GlobalExecutionsList.test.ts b/packages/editor-ui/src/components/executions/global/GlobalExecutionsList.test.ts index 5b2c9c7486..f1aa96ebaa 100644 --- a/packages/editor-ui/src/components/executions/global/GlobalExecutionsList.test.ts +++ b/packages/editor-ui/src/components/executions/global/GlobalExecutionsList.test.ts @@ -39,6 +39,7 @@ const executionDataFactory = (): ExecutionSummary => ({ id: faker.string.uuid(), finished: faker.datatype.boolean(), mode: faker.helpers.arrayElement(['manual', 'trigger']), + createdAt: faker.date.past(), startedAt: faker.date.past(), stoppedAt: faker.date.past(), workflowId: faker.number.int().toString(), diff --git a/packages/editor-ui/src/components/executions/workflow/WorkflowExecutionsPreview.test.ts b/packages/editor-ui/src/components/executions/workflow/WorkflowExecutionsPreview.test.ts index 98add737fe..e332203192 100644 --- a/packages/editor-ui/src/components/executions/workflow/WorkflowExecutionsPreview.test.ts +++ b/packages/editor-ui/src/components/executions/workflow/WorkflowExecutionsPreview.test.ts @@ -50,6 +50,7 @@ const executionDataFactory = (): ExecutionSummaryWithScopes => ({ id: faker.string.uuid(), finished: faker.datatype.boolean(), mode: faker.helpers.arrayElement(['manual', 'trigger']), + createdAt: faker.date.past(), startedAt: faker.date.past(), stoppedAt: faker.date.past(), workflowId: faker.number.int().toString(), diff --git a/packages/editor-ui/src/composables/useRunWorkflow.ts b/packages/editor-ui/src/composables/useRunWorkflow.ts index f527a2340b..eaa91a6228 100644 --- a/packages/editor-ui/src/composables/useRunWorkflow.ts +++ b/packages/editor-ui/src/composables/useRunWorkflow.ts @@ -252,6 +252,7 @@ export function useRunWorkflow(useRunWorkflowOpts: { router: ReturnType { id: '3', mode: 'manual', status: 'success', + createdAt: new Date('2021-01-01T00:00:00Z'), startedAt: new Date('2021-01-03T00:00:00Z'), workflowId: '1', scopes: [], @@ -30,6 +31,7 @@ describe('executions.store', () => { id: '2', mode: 'manual', status: 'success', + createdAt: new Date('2021-01-02T00:00:00Z'), startedAt: new Date('2021-01-02T00:00:00Z'), workflowId: '1', scopes: [], @@ -38,6 +40,7 @@ describe('executions.store', () => { id: '1', mode: 'manual', status: 'success', + createdAt: new Date('2021-01-03T00:00:00Z'), startedAt: new Date('2021-01-01T00:00:00Z'), workflowId: '1', scopes: [], @@ -55,9 +58,13 @@ describe('executions.store', () => { }); it('should delete executions started before given date', async () => { - await executionsStore.deleteExecutions({ deleteBefore: mockExecutions[1].startedAt }); + const deleteBefore = mockExecutions[1].startedAt; + await executionsStore.deleteExecutions({ deleteBefore }); - expect(executionsStore.executions).toEqual([mockExecutions[0], mockExecutions[1]]); + expect(executionsStore.executions.length).toBe(2); + executionsStore.executions.forEach(({ startedAt }) => + expect(startedAt.getTime()).toBeGreaterThanOrEqual(deleteBefore.getTime()), + ); }); it('should delete all executions if given date is now', async () => { @@ -66,4 +73,40 @@ describe('executions.store', () => { expect(executionsStore.executions).toEqual([]); }); }); + + it('should sort execution by createdAt', () => { + const mockExecutions: ExecutionSummaryWithScopes[] = [ + { + id: '1', + mode: 'manual', + status: 'success', + createdAt: new Date('2021-01-01T00:00:00Z'), + startedAt: new Date('2021-02-03T00:00:00Z'), + workflowId: '1', + scopes: [], + }, + { + id: '2', + mode: 'manual', + status: 'success', + createdAt: new Date('2021-01-02T00:00:00Z'), + startedAt: new Date('2021-02-02T00:00:00Z'), + workflowId: '1', + scopes: [], + }, + { + id: '3', + mode: 'manual', + status: 'success', + createdAt: new Date('2021-01-03T00:00:00Z'), + startedAt: new Date('2021-02-01T00:00:00Z'), + workflowId: '1', + scopes: [], + }, + ]; + + mockExecutions.forEach(executionsStore.addExecution); + + expect(executionsStore.executions.at(-1)).toEqual(expect.objectContaining({ id: '1' })); + }); }); diff --git a/packages/editor-ui/src/stores/executions.store.ts b/packages/editor-ui/src/stores/executions.store.ts index b7f160a950..b74a63325d 100644 --- a/packages/editor-ui/src/stores/executions.store.ts +++ b/packages/editor-ui/src/stores/executions.store.ts @@ -50,7 +50,7 @@ export const useExecutionsStore = defineStore('executions', () => { const data = Object.values(executionsById.value); data.sort((a, b) => { - return new Date(b.startedAt).getTime() - new Date(a.startedAt).getTime(); + return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime(); }); return data; diff --git a/packages/editor-ui/src/stores/workflows.store.test.ts b/packages/editor-ui/src/stores/workflows.store.test.ts index d269acf3ac..1ed19aae24 100644 --- a/packages/editor-ui/src/stores/workflows.store.test.ts +++ b/packages/editor-ui/src/stores/workflows.store.test.ts @@ -656,6 +656,7 @@ function generateMockExecutionEvents() { finished: false, mode: 'cli', startedAt: new Date(), + createdAt: new Date(), status: 'new', data: { resultData: { diff --git a/packages/workflow/src/Interfaces.ts b/packages/workflow/src/Interfaces.ts index b36897071b..10dc222d2b 100644 --- a/packages/workflow/src/Interfaces.ts +++ b/packages/workflow/src/Interfaces.ts @@ -2591,7 +2591,7 @@ export interface ExecutionSummary { retryOf?: string | null; retrySuccessId?: string | null; waitTill?: Date; - createdAt?: Date; + createdAt: Date; startedAt: Date; stoppedAt?: Date; workflowId: string;