From 12a35634a6d75a70e9257802f896a86a4eb4033b Mon Sep 17 00:00:00 2001 From: Giuseppe Steduto Date: Thu, 18 Jan 2024 11:09:45 +0100 Subject: [PATCH] fix(utils): update failed workflows duration using finished time (#387) Fix the way in which the duration of failed workflows is calculated, to use the time at which the run was finished rather than the current time. Closes #386. --- reana-ui/src/util.js | 5 ++++- reana-ui/src/util.test.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/reana-ui/src/util.js b/reana-ui/src/util.js index 2400a44b..619b5ef3 100644 --- a/reana-ui/src/util.js +++ b/reana-ui/src/util.js @@ -156,13 +156,16 @@ export function getDuration(start, end) { /** * Parses workflows date info in a friendly way. */ -function parseWorkflowDates(workflow) { +export function parseWorkflowDates(workflow) { const createdMoment = moment.utc(workflow.created); const startedMoment = moment.utc(workflow.progress.run_started_at); const finishedMoment = moment.utc(workflow.progress.run_finished_at); const stoppedMoment = moment.utc(workflow.progress.run_stopped_at); // Mapping between workflow status and the end moment to use for calculating the duration + // If the workflow has not terminated yet (running, queued, pending), the endMoment should not be + // specified, and the current time will be used instead. const endMomentStatusMapping = { + failed: finishedMoment, finished: finishedMoment, stopped: stoppedMoment, deleted: finishedMoment.isValid() ? finishedMoment : stoppedMoment, diff --git a/reana-ui/src/util.test.js b/reana-ui/src/util.test.js index b899655a..00aa1223 100644 --- a/reana-ui/src/util.test.js +++ b/reana-ui/src/util.test.js @@ -4,6 +4,7 @@ import { formatSearch, getDuration, getMimeType, + parseWorkflowDates, } from "~/util"; test.each([ @@ -56,3 +57,31 @@ test.each([ ])("formatFileSize(%p) === %p", (fileSize, formattedFileSize) => { expect(formatFileSize(fileSize)).toEqual(formattedFileSize); }); + +test.each([ + ["finished", "15 min 0 sec", { run_finished_at: "2024-01-18T08:45:00" }], + ["failed", "15 min 0 sec", { run_finished_at: "2024-01-18T08:45:00" }], + ["stopped", "10 min 0 sec", { run_stopped_at: "2024-01-18T08:40:00" }], + ["running", "20 min 0 sec", {}], + ["queued", "20 min 0 sec", {}], + ["pending", "20 min 0 sec", {}], + ["created", "20 min 0 sec", {}], +])( + `parseWorkflowDates [status: %p], duration === %p`, + (status, duration, progress_override) => { + const workflow = { + status: status, + created: "2024-01-18T08:25:00", + progress: { + run_started_at: "2024-01-18T08:30:00", + run_stopped_at: null, + run_finished_at: null, + ...progress_override, + }, + }; + + jest.useFakeTimers(); + jest.setSystemTime(new Date(2024, 0, 18, 8, 50, 0)); + expect(parseWorkflowDates(workflow).duration).toEqual(duration); + }, +);