483 'http://code.jquery.com/ui/1.9.0/jquery-ui.js', |
483 'http://code.jquery.com/ui/1.9.0/jquery-ui.js', |
484 '/static/js/spin.js', |
484 '/static/js/spin.js', |
485 '/static/hosts.js', |
485 '/static/hosts.js', |
486 ) |
486 ) |
487 |
487 |
|
488 COLUMNS = ( |
|
489 ( 'ip', 'IP', lambda host: host.ip ), |
|
490 ( 'mac', 'MAC', lambda host: host.mac ), |
|
491 ( 'name', 'Hostname', lambda host: host.name ), |
|
492 ( 'gw', 'Network', lambda host: host.gw ), |
|
493 ( 'seen', 'Seen', Host.seen, ), |
|
494 ( 'state', 'State', lambda host: host.state ), |
|
495 ) |
|
496 |
|
497 |
488 def process (self) : |
498 def process (self) : |
489 hosts = self.db.query(Host).order_by(Host.last_seen.desc()) |
499 hosts = self.db.query(Host).order_by(Host.last_seen.desc()) |
490 t = self.request.args.get('t') |
500 t = self.request.args.get('t') |
|
501 |
|
502 def host_params (host) : |
|
503 yield 'id', host.id |
|
504 |
|
505 for name, title, fvalue in self.COLUMNS : |
|
506 value = fvalue(host) |
|
507 |
|
508 if name == 'seen' : |
|
509 # XXX: hackfix html() rendering |
|
510 value = unicode(html.div(value)) |
|
511 |
|
512 yield name, value |
|
513 |
|
514 # special |
|
515 yield 'state_class', host.state_class() |
491 |
516 |
492 if t : |
517 if t : |
|
518 # return json |
493 t = ts2dt(int(t)) |
519 t = ts2dt(int(t)) |
494 |
520 |
495 # update |
|
496 hosts = hosts.filter(Host.last_seen > t) |
521 hosts = hosts.filter(Host.last_seen > t) |
497 hosts = list(hosts) |
522 hosts = list(hosts) |
498 hosts.reverse() |
523 hosts.reverse() |
499 |
524 |
500 if hosts : |
525 if hosts : |
501 t = hosts[-1].last_seen |
526 t = hosts[-1].last_seen |
502 hosts = [dict( |
527 hosts = [dict(host_params(host)) for host in hosts] |
503 id = host.id, |
528 |
504 ip = host.ip, |
|
505 mac = host.mac, |
|
506 name = host.name, |
|
507 gw = host.gw, |
|
508 seen = unicode(html.div(host.seen())), |
|
509 state = host.state, |
|
510 state_class = host.state_class(), |
|
511 |
|
512 t = dt2ts(host.last_seen), |
|
513 ) for host in hosts] |
|
514 |
|
515 else : |
529 else : |
516 hosts = [] |
530 hosts = [] |
517 |
531 |
518 data = dict( |
532 data = dict( |
519 t = dt2ts(t), |
533 t = dt2ts(t), |
521 ) |
535 ) |
522 |
536 |
523 return web.Response(json.dumps(data), mimetype='text/json') |
537 return web.Response(json.dumps(data), mimetype='text/json') |
524 |
538 |
525 else : |
539 else : |
|
540 # render html |
526 self.hosts = hosts.limit(10) |
541 self.hosts = hosts.limit(10) |
527 |
542 |
528 # XXX: testing |
543 # XXX: testing |
529 self.hosts = self.hosts.offset(1) |
544 self.hosts = self.hosts.offset(1) |
530 |
545 |
531 self.t = self.hosts[0].last_seen |
546 self.t = self.hosts[0].last_seen |
532 |
547 |
533 def render (self) : |
548 def render (self) : |
|
549 def column (name, title, fvalue, host) : |
|
550 cls = name |
|
551 |
|
552 if name == 'state' : |
|
553 cls = host.state_class() |
|
554 |
|
555 return html.td(class_=cls)(fvalue(host)) |
|
556 |
534 params = dict( |
557 params = dict( |
535 url = self.url(), |
558 url = self.url(), |
536 t = dt2ts(self.t), |
559 t = dt2ts(self.t), |
537 host = self.url(ItemHandler, id='0'), |
560 host = self.url(ItemHandler, id='0'), |
|
561 columns = [name for name, title, fvalue in self.COLUMNS] |
538 ) |
562 ) |
539 params = json.dumps(params) |
563 params = json.dumps(params) |
540 |
564 |
541 COLUMNS = ( |
|
542 '#', 'IP', 'MAC', 'Hostname', 'Network', 'Seen', 'State' |
|
543 ) |
|
544 |
|
545 return html.div(id='wrapper')( |
565 return html.div(id='wrapper')( |
546 html.input(type='submit', id='refresh', value="Refresh"), |
566 html.input(type='submit', id='refresh', value="Refresh"), |
547 html.table(id='hosts')( |
567 html.table(id='hosts')( |
548 html.thead( |
568 html.thead( |
549 html.tr( |
569 html.tr( |
550 html.th(title) for title in COLUMNS |
570 html.th('#'), |
|
571 ( |
|
572 html.th(class_=name)(title) for name, title, fvalue in self.COLUMNS |
|
573 ) |
551 ), |
574 ), |
552 ), |
575 ), |
553 html.tbody( |
576 html.tbody( |
554 html.tr(id=host.id)( |
577 html.tr(id=host.id)( |
555 html.td(html.a(href=self.url(ItemHandler, id=host.id))('#')), |
578 html.td(html.a(href=self.url(ItemHandler, id=host.id))('#')), |
556 ( |
579 ( |
557 html.td(value) for value in ( |
580 column(name, title, fvalue, host) for name, title, fvalue in self.COLUMNS |
558 host.ip, host.mac, host.name, host.gw, host.seen(), |
|
559 ) |
|
560 ), |
581 ), |
561 html.td(class_=host.state_class())(host.state), |
|
562 ) for host in self.hosts |
582 ) for host in self.hosts |
563 ) |
583 ) |
564 ), |
584 ), |
565 html.script(type='text/javascript')(""" |
585 html.script(type='text/javascript')(""" |
566 $(document).ready(hosts_realtime({params})); |
586 $(document).ready(hosts_realtime({params})); |