File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44import json
55from typing import Any , Dict
66
7- import humanize
87from dateutil .parser import isoparse
98
109
@@ -50,14 +49,20 @@ def completion_time(self) -> datetime.datetime:
5049 return isoparse (self .obj ["status" ]["completionTime" ])
5150
5251 @property
53- def duration (self ) -> Any :
52+ def duration (self ) -> str :
5453 """
5554 Get the duration of the TaskRun.
5655
5756 Returns:
58- Any: A human readable duration of the TaskRun
59- """
60- return humanize .naturaldelta (self .completion_time - self .start_time )
57+ str: A human readable duration of the TaskRun.
58+ """
59+ # Avoid humanize.naturaldelta() which rounds e.g. 67 min to "an hour"
60+ delta = self .completion_time - self .start_time
61+ total_seconds = int (delta .total_seconds ())
62+ if total_seconds >= 60 :
63+ minutes = round (total_seconds / 60 )
64+ return f"{ minutes } minute{ 's' if minutes != 1 else '' } "
65+ return f"{ total_seconds } second{ 's' if total_seconds != 1 else '' } "
6166
6267 @property
6368 def status (self ) -> str :
Original file line number Diff line number Diff line change 1+ {
2+ "apiVersion" : " tekton.dev/v1" ,
3+ "kind" : " PipelineRun" ,
4+ "metadata" : {
5+ "name" : " long-running-pipeline-run" ,
6+ "labels" : {
7+ "tekton.dev/pipeline" : " test-pipeline"
8+ }
9+ },
10+ "status" : {
11+ "startTime" : " 2021-11-10T18:00:00Z" ,
12+ "pipelineSpec" : {
13+ "tasks" : [
14+ {"name" : " build-fbc-index-images" }
15+ ],
16+ "finally" : []
17+ }
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ [
2+ {
3+ "apiVersion" : " tekton.dev/v1" ,
4+ "kind" : " TaskRun" ,
5+ "metadata" : {
6+ "labels" : {
7+ "tekton.dev/pipelineTask" : " build-fbc-index-images"
8+ }
9+ },
10+ "status" : {
11+ "startTime" : " 2021-11-10T18:00:00Z" ,
12+ "completionTime" : " 2021-11-10T19:07:12Z" ,
13+ "conditions" : [
14+ {
15+ "type" : " Succeeded" ,
16+ "reason" : " Succeeded"
17+ }
18+ ]
19+ }
20+ }
21+ ]
Original file line number Diff line number Diff line change 66PARENT_DIR = pathlib .Path (__file__ ).parent .resolve ()
77PIPELINERUN_PATH = PARENT_DIR .joinpath ("data/pipelinerun.json" )
88TASKRUNS_PATH = PARENT_DIR .joinpath ("data/taskruns.json" )
9+ PIPELINERUN_LONG_PATH = PARENT_DIR .joinpath ("data/pipelinerun_long.json" )
10+ TASKRUNS_LONG_PATH = PARENT_DIR .joinpath ("data/taskruns_long.json" )
911
1012
1113def test_taskrun () -> None :
@@ -32,6 +34,17 @@ def test_taskrun() -> None:
3234 assert tr .status == TaskRun .FAILED
3335
3436
37+ def test_taskrun_long_duration () -> None :
38+ pr = PipelineRun .from_files (str (PIPELINERUN_LONG_PATH ), str (TASKRUNS_LONG_PATH ))
39+
40+ tr = pr .taskruns [0 ]
41+ assert tr .pipelinetask == "build-fbc-index-images"
42+ assert str (tr .start_time ) == "2021-11-10 18:00:00+00:00"
43+ assert str (tr .completion_time ) == "2021-11-10 19:07:12+00:00"
44+ assert tr .duration == "67 minutes"
45+ assert tr .status == TaskRun .SUCCEEDED
46+
47+
3548def test_pipelinerun () -> None :
3649 pr = PipelineRun .from_files (str (PIPELINERUN_PATH ), str (TASKRUNS_PATH ))
3750
You can’t perform that action at this time.
0 commit comments