From 3b7da0541ca73ff9fb81b94bbe159c6d9299dab1 Mon Sep 17 00:00:00 2001 From: Giuseppe Steduto Date: Mon, 29 Jan 2024 17:03:51 +0100 Subject: [PATCH] fix(status): correctly display duration of stopped workflows (#156) Closes reanahub/reana-client#699 Co-authored-by: Marco Donadoni --- .../get_workflow_status_responses.go | 3 +++ client/operations/get_workflows_responses.go | 3 +++ cmd/list.go | 1 + cmd/status.go | 1 + pkg/workflows/utils.go | 7 ++++- pkg/workflows/utils_test.go | 27 +++++++++++++++---- 6 files changed, 36 insertions(+), 6 deletions(-) diff --git a/client/operations/get_workflow_status_responses.go b/client/operations/get_workflow_status_responses.go index c3bf639..a75a429 100644 --- a/client/operations/get_workflow_status_responses.go +++ b/client/operations/get_workflow_status_responses.go @@ -689,6 +689,9 @@ type GetWorkflowStatusOKBodyProgress struct { // run started at RunStartedAt *string `json:"run_started_at,omitempty"` + // run stopped at + RunStoppedAt *string `json:"run_stopped_at,omitempty"` + // running Running *GetWorkflowStatusOKBodyProgressRunning `json:"running,omitempty"` diff --git a/client/operations/get_workflows_responses.go b/client/operations/get_workflows_responses.go index 3ecf930..ea0c7f7 100644 --- a/client/operations/get_workflows_responses.go +++ b/client/operations/get_workflows_responses.go @@ -860,6 +860,9 @@ type GetWorkflowsOKBodyItemsItems0Progress struct { // run started at RunStartedAt *string `json:"run_started_at,omitempty"` + // run stopped at + RunStoppedAt *string `json:"run_stopped_at,omitempty"` + // running Running *GetWorkflowsOKBodyItemsItems0ProgressRunning `json:"running,omitempty"` diff --git a/cmd/list.go b/cmd/list.go index 4025302..5fb7625 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -255,6 +255,7 @@ func displayListPayload( value, err = workflows.GetDuration( workflow.Progress.RunStartedAt, workflow.Progress.RunFinishedAt, + workflow.Progress.RunStoppedAt, ) if err != nil { return err diff --git a/cmd/status.go b/cmd/status.go index 2ad27a3..1856fb2 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -153,6 +153,7 @@ func displayStatusPayload( value, err = workflows.GetDuration( p.Progress.RunStartedAt, p.Progress.RunFinishedAt, + p.Progress.RunStoppedAt, ) if err != nil { return err diff --git a/pkg/workflows/utils.go b/pkg/workflows/utils.go index df3a973..2696021 100644 --- a/pkg/workflows/utils.go +++ b/pkg/workflows/utils.go @@ -27,7 +27,7 @@ func GetNameAndRunNumber(workflowName string) (string, string) { } // GetDuration calculates and returns the duration the workflow, based on the given timestamps. -func GetDuration(runStartedAt, runFinishedAt *string) (any, error) { +func GetDuration(runStartedAt, runFinishedAt, runStoppedAt *string) (any, error) { if runStartedAt == nil { return nil, nil } @@ -43,6 +43,11 @@ func GetDuration(runStartedAt, runFinishedAt *string) (any, error) { if err != nil { return nil, err } + } else if runStoppedAt != nil { + endTime, err = datautils.FromIsoToTimestamp(*runStoppedAt) + if err != nil { + return nil, err + } } else { endTime = time.Now() } diff --git a/pkg/workflows/utils_test.go b/pkg/workflows/utils_test.go index 1fe4660..ea70a21 100644 --- a/pkg/workflows/utils_test.go +++ b/pkg/workflows/utils_test.go @@ -45,16 +45,33 @@ func TestGetDuration(t *testing.T) { tests := map[string]struct { runStartedAt *string runFinishedAt *string + runStoppedAt *string want any wantError bool }{ "finished instantly": {runStartedAt: &curTime, runFinishedAt: &curTime, want: 0.0}, "finished in 1 second": {runStartedAt: &curTime, runFinishedAt: &future, want: 1.0}, "finished before start": {runStartedAt: &curTime, runFinishedAt: &past, want: -1.0}, - "nil arguments": {runStartedAt: nil, runFinishedAt: nil, want: nil}, - "nil start": {runStartedAt: nil, runFinishedAt: &curTime, want: nil}, - "nil finish": {runStartedAt: &curTime, runFinishedAt: nil}, - "bad start format": {runStartedAt: &badFormat, wantError: true}, + "stopped in 1 second": { + runStartedAt: &curTime, + runFinishedAt: nil, + runStoppedAt: &future, + want: 1.0, + }, + "nil arguments": { + runStartedAt: nil, + runFinishedAt: nil, + runStoppedAt: nil, + want: nil, + }, + "nil start": {runStartedAt: nil, runFinishedAt: &curTime, want: nil}, + "nil finish": {runStartedAt: &curTime, runFinishedAt: nil}, + "bad start format": {runStartedAt: &badFormat, wantError: true}, + "bad stop format": { + runStartedAt: &curTime, + runStoppedAt: &badFormat, + wantError: true, + }, "bad finish format": { runStartedAt: &curTime, runFinishedAt: &badFormat, @@ -63,7 +80,7 @@ func TestGetDuration(t *testing.T) { } for name, test := range tests { t.Run(name, func(t *testing.T) { - got, err := GetDuration(test.runStartedAt, test.runFinishedAt) + got, err := GetDuration(test.runStartedAt, test.runFinishedAt, test.runStoppedAt) if test.wantError { if err == nil { t.Errorf("Expected error, got nil")