449 return ( |
448 return ( |
450 self.render_hosts(self.hosts, filters=self.filters, page=self.page), |
449 self.render_hosts(self.hosts, filters=self.filters, page=self.page), |
451 |
450 |
452 html.a(href=self.url())(html('«'), 'Back') if self.filters else None, |
451 html.a(href=self.url())(html('«'), 'Back') if self.filters else None, |
453 ) |
452 ) |
|
453 |
|
454 import json |
|
455 import time |
|
456 |
|
457 def dt2ts (dt) : |
|
458 return int(time.mktime(dt.timetuple())) |
|
459 |
|
460 def ts2dt (ts) : |
|
461 return datetime.datetime.fromtimestamp(ts) |
|
462 |
|
463 class RealtimeHandler (web.Handler) : |
|
464 TITLE = "Pseudo-Realtime hosts.." |
|
465 JS = ( |
|
466 #"/static/jquery/jquery.js", |
|
467 'http://code.jquery.com/jquery-1.8.2.js', |
|
468 '/static/hosts.js', |
|
469 ) |
|
470 |
|
471 def process (self) : |
|
472 hosts = self.db.query(Host).order_by(Host.last_seen.desc()) |
|
473 t = self.request.args.get('t') |
|
474 |
|
475 if t : |
|
476 t = ts2dt(int(t)) |
|
477 |
|
478 # update |
|
479 hosts = hosts.filter(Host.last_seen > t) |
|
480 hosts = list(hosts) |
|
481 hosts.reverse() |
|
482 |
|
483 if hosts : |
|
484 t = hosts[-1].last_seen |
|
485 hosts = [{'id': host.id, 't': dt2ts(host.last_seen), 'ip': host.ip, 'mac': host.mac} for host in hosts] |
|
486 |
|
487 else : |
|
488 hosts = [] |
|
489 |
|
490 data = dict( |
|
491 t = dt2ts(t), |
|
492 hosts = hosts, |
|
493 ) |
|
494 |
|
495 return web.Response(json.dumps(data), mimetype='text/json') |
|
496 |
|
497 else : |
|
498 self.hosts = hosts.limit(10) |
|
499 |
|
500 # XXX: testing |
|
501 self.hosts = self.hosts.offset(1) |
|
502 |
|
503 self.t = self.hosts[0].last_seen |
|
504 |
|
505 def render (self) : |
|
506 params = dict( |
|
507 url = self.url(), |
|
508 t = dt2ts(self.t), |
|
509 ) |
|
510 params = json.dumps(params) |
|
511 |
|
512 return ( |
|
513 html.input(type='submit', id='refresh', value="Refresh", _selfclosing=False), |
|
514 html.ul(id='hosts')( |
|
515 html.li(id=host.id)("{host.ip} / {host.mac}".format(host=host)) for host in self.hosts |
|
516 ), |
|
517 html.script(type='text/javascript')(""" |
|
518 $(document).ready(hosts_realtime({params})); |
|
519 """.format(params=params) |
|
520 ) |
|
521 ) |
|
522 |