From 817f91ca28eaeba4e5a2e202ae2495a2cd04843a Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 15 Sep 2014 17:38:50 -0400 Subject: display idle time and total time for streamers --- termcast.py | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) (limited to 'termcast.py') diff --git a/termcast.py b/termcast.py index 2750869..51fb592 100644 --- a/termcast.py +++ b/termcast.py @@ -1,3 +1,4 @@ +import time import json import re @@ -5,6 +6,8 @@ import vt100 class Handler(object): def __init__(self, rows, cols): + self.created_at = time.time() + self.idle_since = time.time() self.rows = rows self.cols = cols self.buf = b'' @@ -16,6 +19,7 @@ class Handler(object): if clear != -1: self.buf = self.buf[clear + 4:] self.vt.process(data) + self.idle_since = time.time() def get_term(self): term = '' @@ -26,6 +30,39 @@ class Handler(object): return term[:-1] + def total_time(self): + return self._human_readable_duration(time.time() - self.created_at) + + def idle_time(self): + return self._human_readable_duration(time.time() - self.idle_since) + + def _human_readable_duration(self, duration): + days = 0 + hours = 0 + minutes = 0 + seconds = 0 + + if duration > 60*60*24: + days = duration // (60*60*24) + duration -= days * 60*60*24 + if duration > 60*60: + hours = duration // (60*60) + duration -= hours * 60*60 + if duration > 60: + minutes = duration // 60 + duration -= minutes * 60 + seconds = duration + + ret = "%02ds" % seconds + if minutes > 0 or hours > 0 or days > 0: + ret = ("%02dm" % minutes) + ret + if hours > 0 or days > 0: + ret = ("%02dh" % hours) + ret + if days > 0: + ret = ("%dd" % days) + ret + + return ret + class Connection(object): def __init__(self, client, connection_id, publisher): self.client = client @@ -95,5 +132,6 @@ class Connection(object): "id": self.connection_id, "rows": self.handler.rows, "cols": self.handler.cols, - "idle": "0s", # XXX + "idle_time": self.handler.idle_time(), + "total_time": self.handler.total_time(), } -- cgit v1.2.3-54-g00ecf