Repository: reanahub/reana-client

Issue:
cli: duration of stopped workflows is not displayed correctly
Similarly to what was happening in reanahub/reana-ui#386, when running `reana-client status --include-duration` for a stopped workflow, the displayed duration is not the time between the moment in which the run was started and the moment in which the run was stopped, but the total time passed from the beginning of the run until the current moment.

```console
❯ reana-client status -w recast --include-duration --json | jq '.[0].status'
"stopped"
❯ reana-client status -w recast --include-duration --json | jq '.[0].duration'
2598
# After a few seconds...
❯ reana-client status -w recast --include-duration --json | jq '.[0].duration'
2604
```

This is very likely because in [`reana_client.utils.get_workflow_duration`](https://github.com/reanahub/reana-client/blob/5b278f131b59d3ecfd3c7f129040a126cd01b60a/reana_client/utils.py#L134) we only look at `run_finished_at`, but not at `run_stopped_at`.


Discussion:
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/reanahub/reana-client/pull/701?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=reanahub) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`fa5b7c7`)](https://app.codecov.io/gh/reanahub/reana-client/commit/fa5b7c76eb25bfb1591e6fae4a142d975e14b937?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=reanahub) 58.57% compared to head [(`d3925f2`)](https://app.codecov.io/gh/reanahub/reana-client/pull/701?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=reanahub) 58.62%.

<details><summary>Additional details and impacted files</summary>


[![Impacted file tree graph](https://app.codecov.io/gh/reanahub/reana-client/pull/701/graphs/tree.svg?width=650&height=150&src=pr&token=b85cSEiOhs&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=reanahub)](https://app.codecov.io/gh/reanahub/reana-client/pull/701?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=reanahub)

```diff
@@            Coverage Diff             @@
##           master     #701      +/-   ##
==========================================
+ Coverage   58.57%   58.62%   +0.05%     
==========================================
  Files          24       24              
  Lines        2390     2393       +3     
==========================================
+ Hits         1400     1403       +3     
  Misses        990      990              
```


| [Files](https://app.codecov.io/gh/reanahub/reana-client/pull/701?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=reanahub) | Coverage Δ | |
|---|---|---|
| [reana\_client/api/client.py](https://app.codecov.io/gh/reanahub/reana-client/pull/701?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=reanahub#diff-cmVhbmFfY2xpZW50L2FwaS9jbGllbnQucHk=) | `45.54% <100.00%> (ø)` | |
| [reana\_client/utils.py](https://app.codecov.io/gh/reanahub/reana-client/pull/701?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=reanahub#diff-cmVhbmFfY2xpZW50L3V0aWxzLnB5) | `76.47% <100.00%> (+0.60%)` | :arrow_up: |
| [reana\_client/validation/environments.py](https://app.codecov.io/gh/reanahub/reana-client/pull/701?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=reanahub#diff-cmVhbmFfY2xpZW50L3ZhbGlkYXRpb24vZW52aXJvbm1lbnRzLnB5) | `32.50% <ø> (ø)` | |

</details>
giuseppe-steduto: Note for the reviewer: I chose `status` as the scope of the commit because I think that the commands affected by the PR are a nice user-facing scope for changes to the client. For example, this PR fixes `status`, `list`, and in general the commands that use the `--include-duration` flag, I chose `status` as it is the one most evidently impacted. WDYT?
mdonadoni: Regarding the scope, I think `fix(status)` can be a good choice. WDYT @tiborsimko ?

Patch:
diff --git a/reana_client/utils.py b/reana_client/utils.py
index b8596bf..16e2ac1 100644
--- a/reana_client/utils.py
+++ b/reana_client/utils.py
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 #
 # This file is part of REANA.
-# Copyright (C) 2017, 2018, 2019, 2020, 2021, 2022 CERN.
+# Copyright (C) 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024 CERN.
 #
 # REANA is free software; you can redistribute it and/or modify it
 # under the terms of the MIT License; see LICENSE file for more details.
@@ -146,12 +146,15 @@ def get_workflow_duration(workflow: Dict) -> Optional[int]:
     progress = workflow.get("progress", {})
     run_started_at = progress.get("run_started_at")
     run_finished_at = progress.get("run_finished_at")
+    run_stopped_at = progress.get("run_stopped_at")
 
     duration = None
     if run_started_at:
         start_time = fromisoformat(run_started_at)
         if run_finished_at:
             end_time = fromisoformat(run_finished_at)
+        elif run_stopped_at:
+            end_time = fromisoformat(run_stopped_at)
         else:
             end_time = datetime.utcnow()
         duration = round((end_time - start_time).total_seconds())


Test patch:
diff --git a/tests/test_utils.py b/tests/test_utils.py
index cbef171..b982e9e 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -34,6 +34,17 @@ def test_duration_finished_workflow():
     assert get_workflow_duration(workflow) == 60 + 11
 
 
+def test_duration_stopped_workflow():
+    workflow = {
+        "progress": {
+            "run_started_at": "2022-06-16T14:42:11",
+            "run_stopped_at": "2022-06-16T14:43:22",
+            "run_finished_at": None,
+        }
+    }
+    assert get_workflow_duration(workflow) == 60 + 11
+
+
 def test_duration_running_workflow():
     workflow = {
         "progress": {