[PATCH 1/2] testbot/web: Centralize the conversion of durations to human readable strings.

Francois Gouget fgouget at codeweavers.com
Wed Mar 21 20:01:33 CDT 2018


Signed-off-by: Francois Gouget <fgouget at codeweavers.com>
---
 testbot/lib/WineTestBot/Utils.pm | 30 +++++++++++++++++++++++++++++-
 testbot/web/Activity.pl          | 12 ++----------
 testbot/web/Stats.pl             | 29 ++---------------------------
 3 files changed, 33 insertions(+), 38 deletions(-)

diff --git a/testbot/lib/WineTestBot/Utils.pm b/testbot/lib/WineTestBot/Utils.pm
index 5538ecd60..63b906242 100644
--- a/testbot/lib/WineTestBot/Utils.pm
+++ b/testbot/lib/WineTestBot/Utils.pm
@@ -35,7 +35,7 @@ require Exporter;
 @ISA = qw(Exporter);
 @EXPORT = qw(&MakeSecureURL &SecureConnection &GenerateRandomString
              &OpenNewFile &CreateNewFile &CreateNewLink &CreateNewDir
-             &BuildEMailRecipient);
+             &DurationToString &BuildEMailRecipient);
 
 sub MakeSecureURL($)
 {
@@ -121,6 +121,34 @@ sub CreateNewDir($$)
   }
 }
 
+sub DurationToString($;$)
+{
+  my ($Secs, $Raw) = @_;
+
+  return "n/a" if (!defined $Secs);
+
+  my @Parts;
+  if (!$Raw)
+  {
+    my $Mins = int($Secs / 60);
+    $Secs -= 60 * $Mins;
+    my $Hours = int($Mins / 60);
+    $Mins -= 60 * $Hours;
+    my $Days = int($Hours / 24);
+    $Hours -= 24 * $Days;
+    push @Parts, "${Days}d" if ($Days);
+    push @Parts, "${Hours}h" if ($Hours);
+    push @Parts, "${Mins}m" if ($Mins);
+  }
+  if (!@Parts or int($Secs) != 0)
+  {
+    push @Parts, (@Parts or int($Secs) == $Secs) ?
+                 int($Secs) ."s" :
+                 sprintf('%.1fs', $Secs);
+  }
+  return join(" ", @Parts);
+}
+
 sub BuildEMailRecipient($$)
 {
   my ($EMailAddress, $Name) = @_;
diff --git a/testbot/web/Activity.pl b/testbot/web/Activity.pl
index 9655e1492..08e0291ae 100644
--- a/testbot/web/Activity.pl
+++ b/testbot/web/Activity.pl
@@ -28,6 +28,7 @@ use ObjectModel::CGI::FreeFormPage;
 use WineTestBot::Config;
 use WineTestBot::Activity;
 use WineTestBot::Log;
+use WineTestBot::Utils;
 use WineTestBot::VMs;
 
 @ActivityPage::ISA = qw(ObjectModel::CGI::FreeFormPage);
@@ -72,16 +73,7 @@ sub _GetHtmlTime($)
 sub _GetHtmlDuration($)
 {
   my ($Secs) = @_;
-
-  return "" if ($Secs < 2);
-  my $Mins = int($Secs / 60);
-  my $Hours = int($Mins / 60);
-
-  my @Parts;
-  push @Parts, "${Hours}h" if ($Hours);
-  push @Parts, "${Mins}m" if ($Mins %= 60);
-  push @Parts, "${Secs}s" if ($Secs %= 60);
-  return "<span class='RecordDuration'>". join(" ", @Parts) ."</span>";
+  return ($Secs < 2) ? "" : "<span class='RecordDuration'>". DurationToString($Secs) ."</span>";
 }
 
 sub _CompareVMs()
diff --git a/testbot/web/Stats.pl b/testbot/web/Stats.pl
index 806a3666c..146740789 100644
--- a/testbot/web/Stats.pl
+++ b/testbot/web/Stats.pl
@@ -38,31 +38,6 @@ sub _initialize($$$)
   $self->SUPER::_initialize($Request, $RequiredRole);
 }
 
-sub _GetDuration($;$)
-{
-  my ($Secs, $Raw) = @_;
-
-  return "n/a" if (!defined $Secs);
-
-  my @Parts;
-  if (!$Raw)
-  {
-    my $Mins = int($Secs / 60);
-    my $Hours = int($Mins / 60);
-    my $Days = int($Hours / 24);
-    push @Parts, "${Days}d" if ($Days);
-    my $Part = $Hours - 24 * $Days;
-    push @Parts, "${Part}h" if ($Part);
-    $Part = $Mins - 60 * $Hours;
-    push @Parts, "${Part}m" if ($Part);
-    $Secs = $Secs - 60 * $Mins;
-  }
-  push @Parts, (@Parts or int($Secs) == $Secs) ?
-               int($Secs) ."s" :
-               sprintf('%.1fs', $Secs);
-  return join(" ", @Parts);
-}
-
 sub _CompareVMs()
 {
   my ($aHost, $bHost) = ($a->GetHost(), $b->GetHost());
@@ -105,7 +80,7 @@ sub _GetStatStr($$;$$)
       exists $Stats->{"$StatKey.count"})
   {
     my $Avg = _GetAverage($Stats, $StatKey);
-    return $Avg eq "n/a" ? "n/a" : _GetDuration($Avg, $Flags & $NO_TIME);
+    return $Avg eq "n/a" ? "n/a" : DurationToString($Avg, $Flags & $NO_TIME);
   }
 
   if ($StatKey =~ /\.size$/ and !($Flags & $NO_AVERAGE) and
@@ -124,7 +99,7 @@ sub _GetStatStr($$;$$)
   }
   if ($StatKey =~ /(?:\belapsed|\.time(?!\.count))/)
   {
-    return _GetDuration($Value, $Flags & $NO_TIME);
+    return DurationToString($Value, $Flags & $NO_TIME);
   }
   if ($StatKey =~ /\.rate$/)
   {
-- 
2.16.2




More information about the wine-devel mailing list