From 8f82c3b280e726fd749c0f7ee2a2ea344d4e7af4 Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Apr 29 2019 06:53:48 +0000
Subject: [PATCH 1/3] display task durations in webui


Fixes: https://pagure.io/koji/issue/1382

---

diff --git a/www/kojiweb/taskinfo.chtml b/www/kojiweb/taskinfo.chtml
index 69c2bbd..acec1bf 100644
--- a/www/kojiweb/taskinfo.chtml
+++ b/www/kojiweb/taskinfo.chtml
@@ -2,15 +2,16 @@
 #from kojiweb import util
 #from six.moves.urllib.parse import quote
 #import cgi
+#from datetime import datetime
 
 #def printValue($key, $value, $sep=', ')
   #if $key in ('brootid', 'buildroot_id')
 <a href="buildrootinfo?buildrootID=$value">$value</a>
-  #elif $isinstance($value, list) 
-$sep.join([$str($val) for $val in $value]) 
+  #elif $isinstance($value, list)
+$sep.join([$str($val) for $val in $value])
   #elif $isinstance($value, dict)
 $sep.join(['%s=%s' % (($n == '' and "''" or $n), $v) for $n, $v in $value.items()])
-  #else 
+  #else
 $value
   #end if
 #end def
@@ -26,7 +27,7 @@ $value
   #elif $key != '__starstar'
   $prefix$key&nbsp;=&nbsp;$printValue($key, $value)<br/>
   #end if
-  #end for  
+  #end for
 #end def
 
 #def printOpts($opts)
@@ -130,26 +131,26 @@ $value
         <strong>SCM URL:</strong> $params[0]<br/>
         <strong>Build Target:</strong> <a href="buildtargetinfo?name=$params[1]">$params[1]</a><br/>
         $printOpts($params[2])
-	#elif $task.method == 'buildMaven'
-	<strong>SCM URL:</strong> $params[0]<br/>
-	<strong>Build Tag:</strong> <a href="taginfo?tagID=$buildTag.id">$buildTag.name</a><br/>
-	#if $len($params) > 2
+        #elif $task.method == 'buildMaven'
+        <strong>SCM URL:</strong> $params[0]<br/>
+        <strong>Build Tag:</strong> <a href="taginfo?tagID=$buildTag.id">$buildTag.name</a><br/>
+        #if $len($params) > 2
         $printOpts($params[2])
         #end if
-	#elif $task.method == 'wrapperRPM'
-	<strong>Spec File URL:</strong> $params[0]<br/>
-	#if 'locked' in $buildTarget
-	#set $buildTag = $buildTarget
-	<strong>Build Tag:</strong> <a href="taginfo?tagID=$buildTag.id">$buildTag.name</a><br/>
-	#else
-	<strong>Build Target:</strong> <a href="buildtargetinfo?targetID=$buildTarget.id">$buildTarget.name</a><br/>
-	#end if
-	#if $params[2]
-	<strong>Build:</strong> <a href="buildinfo?buildID=$params[2].id">$koji.buildLabel($params[2])</a><br/>
-	#end if
-	#if $params[3]
-	<strong>Task:</strong> <a href="taskinfo?taskID=$wrapTask.id">$koji.taskLabel($wrapTask)</a><br/>
-	#end if
+        #elif $task.method == 'wrapperRPM'
+        <strong>Spec File URL:</strong> $params[0]<br/>
+        #if 'locked' in $buildTarget
+        #set $buildTag = $buildTarget
+        <strong>Build Tag:</strong> <a href="taginfo?tagID=$buildTag.id">$buildTag.name</a><br/>
+        #else
+        <strong>Build Target:</strong> <a href="buildtargetinfo?targetID=$buildTarget.id">$buildTarget.name</a><br/>
+        #end if
+        #if $params[2]
+        <strong>Build:</strong> <a href="buildinfo?buildID=$params[2].id">$koji.buildLabel($params[2])</a><br/>
+        #end if
+        #if $params[3]
+        <strong>Task:</strong> <a href="taskinfo?taskID=$wrapTask.id">$koji.taskLabel($wrapTask)</a><br/>
+        #end if
         #if $len($params) > 4
         $printOpts($params[4])
         #end if
@@ -320,16 +321,32 @@ $value
     <tr>
       <th>Started</th><td>$util.formatTimeLong($task.start_time)</td>
     #end if
+    #set $end_ts = None
     #if $task.state == $koji.TASK_STATES.OPEN
     #if $estCompletion
     <tr>
-      <th>Est. Completion</th><td>$util.formatTimeLong($estCompletion)</td>
+      <th title="Estimation based on previous builds of same package">Est. Completion</th>
+      <td>$util.formatTimeLong($estCompletion)</td>
     </tr>
     #end if
     #elif $task.completion_time
     <tr>
       <th>Completed</th><td>$util.formatTimeLong($task.completion_time)</td>
     </tr>
+    #set $end_ts = $task.completion_ts
+    #end if
+    #if not $end_ts
+    #set $end_ts = datetime.now().timestamp()
+    #end if
+    <tr>
+      <th title="From task's creation">Total time</th>
+      <td>$util.formatTimestampDifference($task.create_ts, $end_ts)</td>
+    </tr>
+    #if $task.start_time
+    <tr>
+      <th title="From task's start">Task time</th>
+      <td>$util.formatTimestampDifference($task.start_ts, $end_ts)</td>
+    </tr>
     #end if
     <tr>
       <th>Owner</th>
diff --git a/www/lib/kojiweb/util.py b/www/lib/kojiweb/util.py
index f96b131..abca32d 100644
--- a/www/lib/kojiweb/util.py
+++ b/www/lib/kojiweb/util.py
@@ -30,6 +30,7 @@ import os
 import six
 import ssl
 import stat
+import time
 
 from six.moves import range
 #a bunch of exception classes that explainError needs
@@ -415,6 +416,15 @@ formatTime = koji.formatTime
 formatTimeRSS = koji.formatTimeLong
 formatTimeLong = koji.formatTimeLong
 
+def formatTimestampDifference(start_ts, end_ts):
+    diff = end_ts - start_ts
+    seconds = diff % 60
+    diff = diff // 60
+    minutes = diff % 60
+    diff = diff // 60
+    hours = diff
+    return "%d:%02d:%02d" % (hours, minutes, seconds)
+
 def formatDep(name, version, flags):
     """Format dependency information into
     a human-readable format.  Copied from

From 76aad4395e0be92f6f27b4fef766bddb6c28a60b Mon Sep 17 00:00:00 2001
From: Mike McLean <mikem@redhat.com>
Date: May 06 2019 12:28:11 +0000
Subject: [PATCH 2/3] use time.time() for current timestamp


---

diff --git a/www/kojiweb/taskinfo.chtml b/www/kojiweb/taskinfo.chtml
index acec1bf..adc0c43 100644
--- a/www/kojiweb/taskinfo.chtml
+++ b/www/kojiweb/taskinfo.chtml
@@ -3,6 +3,7 @@
 #from six.moves.urllib.parse import quote
 #import cgi
 #from datetime import datetime
+#import time
 
 #def printValue($key, $value, $sep=', ')
   #if $key in ('brootid', 'buildroot_id')
@@ -336,7 +337,7 @@ $value
     #set $end_ts = $task.completion_ts
     #end if
     #if not $end_ts
-    #set $end_ts = datetime.now().timestamp()
+    #set $end_ts = time.time()
     #end if
     <tr>
       <th title="From task's creation">Total time</th>

From e7e228022259f7d44c5c085c8fa3ac076a9b9d3b Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: May 06 2019 12:29:32 +0000
Subject: [PATCH 3/3] remove unneeded import


---

diff --git a/www/kojiweb/taskinfo.chtml b/www/kojiweb/taskinfo.chtml
index adc0c43..2363742 100644
--- a/www/kojiweb/taskinfo.chtml
+++ b/www/kojiweb/taskinfo.chtml
@@ -2,7 +2,6 @@
 #from kojiweb import util
 #from six.moves.urllib.parse import quote
 #import cgi
-#from datetime import datetime
 #import time
 
 #def printValue($key, $value, $sep=', ')

