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); + }, +); diff --git a/run-tests.sh b/run-tests.sh index 27f829cc..efc8cbeb 100755 --- a/run-tests.sh +++ b/run-tests.sh @@ -29,7 +29,7 @@ check_commitlint () { } check_shellcheck () { - find . -name "*.sh" -exec shellcheck {} \+ + find . -name "*.sh" ! -path "./reana-ui/node_modules/*" -exec shellcheck {} \+ } check_sphinx () {