Gaël Le Mignot avatar Gaël Le Mignot committed d257038

Better ETA handling in sesqlasyncreindex

Comments (0)

Files changed (2)

sesql/management/commands/sesqlasyncreindex.py

         done = self.state['done']
         percent = total and (done * 100.0 / total) or 100.0
         cumulated = self.state['cumulated']
-        if percent == 100.0 or percent == 0.0:
-            eta = 0
-        else:
-            eta = cumulated / percent * (100.0 - percent)
 
-        print "%.2f %% done in %.2f seconds; ETA : %.2f seconds" % (percent, cumulated, eta)
-        
+        utils.print_eta(percent, cumulated)
+
         if drift:
             print " -> %d done, drift rate : %.2f, estimated total: %d, actual remaining: %d, estimated remaining: %d" % (done, drift_rate, total, remaining, drift_estimated)
                                                                              
         while True:
             self.state['start_time'] = time.time()
             nb = self.iteration()
+            time.sleep(self.options['delay'])
             self.state['end_time'] = time.time()
             self.display_status()
             if self.options['state']:
                 cPickle.dump(self.state, open(self.options['state'], 'w'), 0)
-            time.sleep(self.options['delay'])
             if nb < (self.options['step'] - 1) and not self.options['forever']:
                 break
             
 
     value = ''.join([ charmap.get(c,c) for c in value ])    
     return value
+
+def format_time(timedelta, keep = 3):
+    """
+    Pretty format a timedelta
+    """
+    units = [ 'ms', 's', 'm', 'h', 'd' ]
+    factors = [ 1000, 60, 60, 24, None ]
+    formats = [ '%03d', '%02d', '%02d', '%02d', '%d' ]
+
+    res = []
+    value = int(timedelta * factors[0])
+    for factor, unit, fmt in zip(factors, units, formats):
+        if factor:
+            nextval = value / float(factor)
+            val = nextval - int(nextval)
+            val = int(val * factor)
+            value = int(nextval)
+        else:
+            val = value
+        res.append((val, fmt, unit))
+
+    while res and res[-1][0] == 0:
+        res.pop()
+
+    res = res[-3:]
+    res = [ r for r in res if r[0] ]
+    res = res[::-1]
+    if res:
+        res = ', '.join([ fmt % val + ' ' + unit for val, fmt, unit in res ])
+    else:
+        res = '0'
+
+    res = '%.2f seconds (%s)' % (timedelta, res)
+    return res
+        
     
+def print_eta(percent, timedelta):
+    """
+    Print an ETA line, given we did percent of total in timedelta
+    """
+    if percent == 100.0 or percent == 0.0:
+        eta = 0
+    else:
+        eta = timedelta / percent * (100.0 - percent)
+
+    print "%05.2f %% done in %s; ETA : %s" % (percent,
+                                              format_time(timedelta),
+                                              format_time(eta))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.