Use an instance method to format the play time string.
authorAnt Zucaro <azucaro@gmail.com>
Wed, 8 Nov 2017 02:59:03 +0000 (21:59 -0500)
committerAnt Zucaro <azucaro@gmail.com>
Wed, 8 Nov 2017 02:59:03 +0000 (21:59 -0500)
xonstat/models/main.py
xonstat/templates/main_index.mako
xonstat/templates/top_servers_index.mako

index 0648623..2862a25 100644 (file)
@@ -2,6 +2,7 @@
 Models related to the main index page.
 """
 
+import datetime
 from xonstat.util import html_colors
 
 
@@ -44,6 +45,21 @@ class ActiveServer(object):
         self.server_name = server_name
         self.play_time = play_time
 
+    def play_time_str(self):
+        hour = 3600
+        day = hour * 24
+
+        if not self.play_time:
+            return "0m"
+
+        total_seconds = self.play_time.total_seconds()
+        if total_seconds >= day:
+            return "{}d".format(round(float(total_seconds) / day, 1))
+        elif day > total_seconds >= hour:
+            return "{}h".format(round(float(total_seconds) / hour, 1))
+        else:
+            return "{}m".format(round(float(total_seconds) / 60, 1))
+
     def __repr__(self):
         return "<ActiveServer({0.sort_order}, {0.server_id})>".format(self)
 
index 6641d96..75f7854 100644 (file)
         <tr>
           <th class="small-2">#</th>
           <th class="small-7">Server</th>
-          <th class="small-3">Games</th>
+          <th class="small-3">Play Time</th>
         </tr>
       </thead>
       <tbody>
         <tr>
           <td>${ts.sort_order}</td>
           <td class="no-stretch"><a href="${request.route_url('server_info', id=ts.server_id)}" title="Go to the server info page for ${ts.server_name}">${ts.server_name}</a></td>
-          <td>${ts.games}</td>
+          <td>${ts.play_time_str()}</td>
         </tr>
       % endfor
       </tbody>
index 4adba45..836e36f 100644 (file)
@@ -24,7 +24,7 @@
           <tr>
             <th class="small-2">#</th>
             <th class="small-7">Server</th>
-            <th class="small-3">Games</th>
+            <th class="small-3">Time</th>
           </tr>
         </thead>
         <tbody>
@@ -32,7 +32,7 @@
           <tr>
             <td>${ts.sort_order}</td>
             <td class="no-stretch"><a href="${request.route_url('server_info', id=ts.server_id)}" title="Go to the server info page for ${ts.server_name}">${ts.server_name}</a></td>
-            <td>${ts.games}</td>
+            <td>${ts.play_time_str()}</td>
           </tr>
         % endfor
         </tbody>