pvl/verkko/hosts.py
changeset 26 589249097230
parent 25 47faf2ac32d0
child 27 38d050c657da
equal deleted inserted replaced
25:47faf2ac32d0 26:589249097230
    85         if self.error :
    85         if self.error :
    86             return "{self.state}: {self.error}".format(self=self)
    86             return "{self.state}: {self.error}".format(self=self)
    87         else :
    87         else :
    88             return self.state
    88             return self.state
    89 
    89 
    90     def when (self) :
    90     def seen (self) :
    91         return (
    91         return (
    92                 html.span(title=self.first_seen)(self.first_seen.strftime(self.DATE_FMT)),
    92                 html.span(title=self.first_seen)(self.first_seen.strftime(self.DATE_FMT)),
    93                 '-',
    93                 '-',
    94                 html.span(title=self.last_seen)(self.last_seen.strftime(self.DATE_FMT)),
    94                 html.span(title=self.last_seen)(self.last_seen.strftime(self.DATE_FMT)),
    95         )
    95         )
   118 db.mapper(Host, db.dhcp_hosts, properties=dict(
   118 db.mapper(Host, db.dhcp_hosts, properties=dict(
   119     #id      = db.dhcp_hosts.c.rowid,
   119     #id      = db.dhcp_hosts.c.rowid,
   120     #state   = db.dhcp_hosts.c.,
   120     #state   = db.dhcp_hosts.c.,
   121 ))
   121 ))
   122 
   122 
       
   123 
       
   124    
       
   125  
   123 class BaseHandler (web.Handler) :
   126 class BaseHandler (web.Handler) :
   124     HOST_ATTRS = {
   127     HOST_ATTRS = {
   125         'id':       Host.id,
   128         'id':       Host.id,
   126         'net':      Host.gw,
   129         'net':      Host.gw,
   127         'ip':       Host.ip,
   130         'ip':       Host.ip,
   252                     render_cell('ip', host.ip, filter=True),
   255                     render_cell('ip', host.ip, filter=True),
   253                     render_cell('mac', host.mac, filter=True, htmlvalue=host.render_mac()),
   256                     render_cell('mac', host.mac, filter=True, htmlvalue=host.render_mac()),
   254                     render_cell('name', host.name, htmlvalue=host.render_name()),
   257                     render_cell('name', host.name, htmlvalue=host.render_name()),
   255                     render_cell('gw', host.gw),
   258                     render_cell('gw', host.gw),
   256 
   259 
   257                     html.td(host.when()),
   260                     html.td(host.seen()),
   258                     html.td(class_=host.state_class(), title=host.state_title())(host.state),
   261                     html.td(class_=host.state_class(), title=host.state_title())(host.state),
   259                 ) for i, host in enumerate(hosts)
   262                 ) for i, host in enumerate(hosts)
   260             ),
   263             ),
   261             html.tfoot(
   264             html.tfoot(
   262                 html.tr(
   265                 html.tr(
   463 class RealtimeHandler (web.Handler) :
   466 class RealtimeHandler (web.Handler) :
   464     TITLE = "Pseudo-Realtime hosts.."
   467     TITLE = "Pseudo-Realtime hosts.."
   465     JS = (
   468     JS = (
   466         #"/static/jquery/jquery.js",
   469         #"/static/jquery/jquery.js",
   467         'http://code.jquery.com/jquery-1.8.2.js',
   470         'http://code.jquery.com/jquery-1.8.2.js',
       
   471         '/static/js/spin.js',
   468         '/static/hosts.js',
   472         '/static/hosts.js',
   469     )
   473     )
   470 
   474 
   471     def process (self) :
   475     def process (self) :
   472         hosts = self.db.query(Host).order_by(Host.last_seen.desc())
   476         hosts = self.db.query(Host).order_by(Host.last_seen.desc())
   480             hosts = list(hosts)
   484             hosts = list(hosts)
   481             hosts.reverse()
   485             hosts.reverse()
   482             
   486             
   483             if hosts :
   487             if hosts :
   484                 t = hosts[-1].last_seen
   488                 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]
   489                 hosts = [dict(
       
   490                     id      = host.id, 
       
   491                     ip      = host.ip, 
       
   492                     mac     = host.mac,
       
   493                     name    = host.name,
       
   494                     gw      = host.gw,
       
   495                     seen    = unicode(html.div(host.seen())),
       
   496                     state       = host.state,
       
   497                     state_class = host.state_class(),
       
   498 
       
   499                     t       = dt2ts(host.last_seen), 
       
   500                 ) for host in hosts]
   486             
   501             
   487             else :
   502             else :
   488                 hosts = []
   503                 hosts = []
   489 
   504 
   490             data = dict(
   505             data = dict(
   504 
   519 
   505     def render (self) :
   520     def render (self) :
   506         params = dict(
   521         params = dict(
   507             url     = self.url(),
   522             url     = self.url(),
   508             t       = dt2ts(self.t),
   523             t       = dt2ts(self.t),
       
   524             host    = self.url(ItemHandler, id='0'),
   509         )
   525         )
   510         params = json.dumps(params)
   526         params = json.dumps(params)
   511 
   527         
   512         return (
   528         COLUMNS = (
   513             html.input(type='submit', id='refresh', value="Refresh", _selfclosing=False),
   529             '#', 'IP', 'MAC', 'Hostname', 'Network', 'Seen', 'State'
   514             html.ul(id='hosts')(
   530         )
   515                 html.li(id=host.id)("{host.ip} / {host.mac}".format(host=host)) for host in self.hosts
   531 
       
   532         return html.div(id='wrapper')(
       
   533             html.input(type='submit', id='refresh', value="Refresh"),
       
   534             html.table(id='hosts')(
       
   535                 html.thead(
       
   536                     html.tr(
       
   537                         html.th(title) for title in COLUMNS
       
   538                     ),
       
   539                 ),
       
   540                 html.tbody(
       
   541                     html.tr(id=host.id)(
       
   542                         html.td(html.a(href=self.url(ItemHandler, id=host.id))('#')),
       
   543                         (
       
   544                             html.td(value) for value in (
       
   545                                 host.ip, host.mac, host.name, host.gw, host.seen(),
       
   546                             )
       
   547                         ),
       
   548                         html.td(class_=host.state_class())(host.state),
       
   549                     ) for host in self.hosts
       
   550                 )
   516             ),
   551             ),
   517             html.script(type='text/javascript')("""
   552             html.script(type='text/javascript')("""
   518 $(document).ready(hosts_realtime({params}));
   553 $(document).ready(hosts_realtime({params}));
   519 """.format(params=params)
   554 """.format(params=params)
   520             )
   555             )