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 --- ssh.py | 11 ++++++----- termcast.py | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/ssh.py b/ssh.py index 166022c..a14dda2 100644 --- a/ssh.py +++ b/ssh.py @@ -81,8 +81,8 @@ class Connection(object): def _display_streamer_screen(self, streamers): self.chan.send("\033[2J\033[HWelcome to Termcast!") self.chan.send( - "\033[3H %-20s %-15s %-15s" % ( - "User", "Terminal size", "Idle time", + "\033[3H %-20s %-15s %-15s %-15s" % ( + "User", "Terminal size", "Idle time", "Total time" ) ) row = 4 @@ -95,10 +95,11 @@ class Connection(object): if streamer["cols"] > self.server.cols or streamer["rows"] > self.server.rows: size_pre = "\033[31m" size_post = "\033[m" - idle = streamer["idle"] + idle = streamer["idle_time"] + total = streamer["total_time"] self.chan.send( - "\033[%dH%s) %-20s %s%-15s%s %-15s" % ( - row, key, name, size_pre, size, size_post, idle + "\033[%dH%s) %-20s %s%-15s%s %-15s %-15s" % ( + row, key, name, size_pre, size, size_post, idle, total ) ) row += 1 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