diff --git a/lib/util.py b/lib/util.py index 1fdf1f7..e5c8674 100644 --- a/lib/util.py +++ b/lib/util.py @@ -57,12 +57,20 @@ class cachedproperty(object): return value -def formatted_time(t): +def formatted_time(t, sep=' '): '''Return a number of seconds as a string in days, hours, mins and - secs.''' + maybe secs.''' t = int(t) - return '{:d}d {:02d}h {:02d}m {:02d}s'.format( - t // 86400, (t % 86400) // 3600, (t % 3600) // 60, t % 60) + fmts = (('{:d}d', 86400), ('{:02d}h', 3600), ('{:02d}m', 60)) + parts = [] + for fmt, n in fmts: + val = t // n + if parts or val: + parts.append(fmt.format(val)) + t %= n + if len(parts) < 3: + parts.append('{:02d}s'.format(t)) + return sep.join(parts) def deep_getsizeof(obj): diff --git a/server/controller.py b/server/controller.py index f814e6f..db161c8 100644 --- a/server/controller.py +++ b/server/controller.py @@ -464,6 +464,7 @@ class Controller(util.LoggedClass): 'sessions': self.session_count(), 'subs': self.sub_count(), 'txs_sent': self.txs_sent, + 'uptime': util.formatted_time(time.time() - self.start_time), } def sub_count(self): @@ -515,12 +516,6 @@ class Controller(util.LoggedClass): '''A generator returning lines for a list of sessions. data is the return value of rpc_sessions().''' - - def time_fmt(t): - t = int(t) - return ('{:3d}:{:02d}:{:02d}' - .format(t // 3600, (t % 3600) // 60, t % 60)) - fmt = ('{:<6} {:<5} {:>17} {:>5} {:>5} ' '{:>7} {:>7} {:>7} {:>7} {:>7} {:>9} {:>21}') yield fmt.format('ID', 'Flags', 'Client', 'Reqs', 'Txs', 'Subs', @@ -535,7 +530,7 @@ class Controller(util.LoggedClass): '{:,d}'.format(recv_size // 1024), '{:,d}'.format(send_count), '{:,d}'.format(send_size // 1024), - time_fmt(time), peer) + util.formatted_time(time, sep=''), peer) def session_data(self, for_log): '''Returned to the RPC 'sessions' call.'''