--- a/pvl/verkko/hosts.py Wed Oct 24 18:10:42 2012 +0300
+++ b/pvl/verkko/hosts.py Wed Oct 24 18:13:14 2012 +0300
@@ -203,7 +203,6 @@
return html.input(type='text', name=filter, value=value)
def render_cell (attr, value, cssclass=True, filter=None, htmlvalue=None) :
-
if htmlvalue :
cell = htmlvalue
else :
@@ -451,3 +450,73 @@
html.a(href=self.url())(html('«'), 'Back') if self.filters else None,
)
+
+import json
+import time
+
+def dt2ts (dt) :
+ return int(time.mktime(dt.timetuple()))
+
+def ts2dt (ts) :
+ return datetime.datetime.fromtimestamp(ts)
+
+class RealtimeHandler (web.Handler) :
+ TITLE = "Pseudo-Realtime hosts.."
+ JS = (
+ #"/static/jquery/jquery.js",
+ 'http://code.jquery.com/jquery-1.8.2.js',
+ '/static/hosts.js',
+ )
+
+ def process (self) :
+ hosts = self.db.query(Host).order_by(Host.last_seen.desc())
+ t = self.request.args.get('t')
+
+ if t :
+ t = ts2dt(int(t))
+
+ # update
+ hosts = hosts.filter(Host.last_seen > t)
+ hosts = list(hosts)
+ hosts.reverse()
+
+ if hosts :
+ t = hosts[-1].last_seen
+ hosts = [{'id': host.id, 't': dt2ts(host.last_seen), 'ip': host.ip, 'mac': host.mac} for host in hosts]
+
+ else :
+ hosts = []
+
+ data = dict(
+ t = dt2ts(t),
+ hosts = hosts,
+ )
+
+ return web.Response(json.dumps(data), mimetype='text/json')
+
+ else :
+ self.hosts = hosts.limit(10)
+
+ # XXX: testing
+ self.hosts = self.hosts.offset(1)
+
+ self.t = self.hosts[0].last_seen
+
+ def render (self) :
+ params = dict(
+ url = self.url(),
+ t = dt2ts(self.t),
+ )
+ params = json.dumps(params)
+
+ return (
+ html.input(type='submit', id='refresh', value="Refresh", _selfclosing=False),
+ html.ul(id='hosts')(
+ html.li(id=host.id)("{host.ip} / {host.mac}".format(host=host)) for host in self.hosts
+ ),
+ html.script(type='text/javascript')("""
+$(document).ready(hosts_realtime({params}));
+""".format(params=params)
+ )
+ )
+