Skip to content

Commit 3d83ea6

Browse files
fix(utils): update failed workflows duration using finished time (reanahub#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 reanahub#386.
1 parent 5d232af commit 3d83ea6

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

reana-ui/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"eslint-config-react-app": "^7.0.1",
6767
"eslint-plugin-prettier": "^5.0.1",
6868
"eslint-plugin-react": "^7.23.2",
69+
"jest-date-mock": "^1.0.8",
6970
"npm-run-all": "^4.1.5",
7071
"prettier": "^3.0.3",
7172
"semantic-ui-less": "^2.4.1",

reana-ui/src/util.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,16 @@ export function getDuration(start, end) {
156156
/**
157157
* Parses workflows date info in a friendly way.
158158
*/
159-
function parseWorkflowDates(workflow) {
159+
export function parseWorkflowDates(workflow) {
160160
const createdMoment = moment.utc(workflow.created);
161161
const startedMoment = moment.utc(workflow.progress.run_started_at);
162162
const finishedMoment = moment.utc(workflow.progress.run_finished_at);
163163
const stoppedMoment = moment.utc(workflow.progress.run_stopped_at);
164164
// Mapping between workflow status and the end moment to use for calculating the duration
165+
// If the workflow has not terminated yet (running, queued, pending), the endMoment should not be
166+
// specified, and the current time will be used instead.
165167
const endMomentStatusMapping = {
168+
failed: finishedMoment,
166169
finished: finishedMoment,
167170
stopped: stoppedMoment,
168171
deleted: finishedMoment.isValid() ? finishedMoment : stoppedMoment,

reana-ui/src/util.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import {
44
formatSearch,
55
getDuration,
66
getMimeType,
7+
parseWorkflowDates,
78
} from "~/util";
9+
import { advanceTo, clear } from "jest-date-mock";
810

911
test.each([
1012
["path/to/test.txt", "text/plain"],
@@ -56,3 +58,31 @@ test.each([
5658
])("formatFileSize(%p) === %p", (fileSize, formattedFileSize) => {
5759
expect(formatFileSize(fileSize)).toEqual(formattedFileSize);
5860
});
61+
62+
test.each([
63+
["finished", "15 min 0 sec", { run_finished_at: "2024-01-18T08:45:00" }],
64+
["failed", "15 min 0 sec", { run_finished_at: "2024-01-18T08:45:00" }],
65+
["stopped", "10 min 0 sec", { run_stopped_at: "2024-01-18T08:40:00" }],
66+
["running", "20 min 0 sec", {}],
67+
["queued", "20 min 0 sec", {}],
68+
["pending", "20 min 0 sec", {}],
69+
["created", "20 min 0 sec", {}],
70+
])(
71+
`parseWorkflowDates [status: %p], duration === %p`,
72+
(status, duration, progress_override) => {
73+
const workflow = {
74+
status: status,
75+
created: "2024-01-18T08:25:00",
76+
progress: {
77+
run_started_at: "2024-01-18T08:30:00",
78+
run_stopped_at: null,
79+
run_finished_at: null,
80+
...progress_override,
81+
},
82+
};
83+
84+
advanceTo(new Date(2024, 0, 18, 8, 50, 0));
85+
expect(parseWorkflowDates(workflow).duration).toEqual(duration);
86+
clear();
87+
},
88+
);

reana-ui/yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6420,6 +6420,11 @@ jest-config@^27.5.1:
64206420
slash "^3.0.0"
64216421
strip-json-comments "^3.1.1"
64226422

6423+
jest-date-mock@^1.0.8:
6424+
version "1.0.8"
6425+
resolved "https://registry.yarnpkg.com/jest-date-mock/-/jest-date-mock-1.0.8.tgz#13468c0352c5a3614c6b356dbc6b88eb37d9e0b3"
6426+
integrity sha512-0Lyp+z9xvuNmLbK+5N6FOhSiBeux05Lp5bbveFBmYo40Aggl2wwxFoIrZ+rOWC8nDNcLeBoDd2miQdEDSf3iQw==
6427+
64236428
jest-diff@^27.5.1:
64246429
version "27.5.1"
64256430
resolved "https://registry.yarnpkg.com/jest-diff/-/jest-diff-27.5.1.tgz#a07f5011ac9e6643cf8a95a462b7b1ecf6680def"

0 commit comments

Comments
 (0)