diff -r 72304d66ecd1 -r f7658198c224 pvl/verkko/hosts.py --- a/pvl/verkko/hosts.py Sun Feb 10 13:18:21 2013 +0200 +++ b/pvl/verkko/hosts.py Sun Feb 10 13:20:29 2013 +0200 @@ -305,6 +305,7 @@ 'http://code.jquery.com/jquery-1.8.2.js', 'http://code.jquery.com/ui/1.9.0/jquery-ui.js', '/static/dhcp/spin.js', + '/static/dhcp/table.js', '/static/dhcp/hosts.js', ) @@ -322,8 +323,14 @@ Either return JSON (if ?t=...), or fetch hosts/t for rendering. """ - hosts = self.db.query(Host) t = self.request.args.get('t') + + if t : + # return json + t = ts2dt(int(t)) + + # query + hosts = self.query() # always sorted by last_seen hosts = hosts.order_by(Host.last_seen.desc()) @@ -331,56 +338,43 @@ # filter self.filters, hosts = self.filter(hosts) - def host_params (host) : - yield 'id', host.id - - for name, title, fvalue in self.COLUMNS : - value = fvalue(host) - - if name == 'seen' : - # XXX: hackfix html() rendering - value = unicode(html.div(value)) - - yield name, value - - # special - yield 'state_class', host.state_class() - if t : # return json - t = ts2dt(int(t)) - hosts = hosts.filter(Host.last_seen > t) hosts = list(hosts) hosts.reverse() if hosts : + # update timestamp to most recent t = hosts[-1].last_seen - hosts = [dict(host_params(host)) for host in hosts] - else : - hosts = [] - + # json data = dict( t = dt2ts(t), - hosts = hosts, + hosts = [dict(self.table.json(host)) for host in hosts], ) return response.json(data) else : # render html - hosts = hosts.limit(10) + hosts = hosts.limit(self.table.PAGE) # XXX: testing hosts = hosts.offset(1) - self.hosts = list(hosts) + # extract timestamp + for host in hosts : + self.t = host.last_seen + + break + + else : + # no hosts :< + self.t = datetime.datetime.now() - if self.hosts : - self.t = self.hosts[0].last_seen - else : - self.t = datetime.datetime.now() + # store + self.hosts = hosts def title (self) : if self.filters : @@ -392,48 +386,27 @@ """ Render page HTML and initial , along with bootstrap JS (t0, configuration). """ - - def column (name, title, fvalue, host) : - cls = name - - if name == 'state' : - cls = host.state_class() - - return html.td(class_=cls)(fvalue(host)) - - params = dict( - url = self.url(), - filters = self.filters, - t = dt2ts(self.t), - host = self.url(ItemHandler, id='0'), - columns = [name for name, title, fvalue in self.COLUMNS] - ) - params = json.dumps(params) - + return html.div(id='wrapper')( html.input(type='submit', id='refresh', value="Refresh"), html.input(type='reset', id='pause', value="Pause"), - html.table(id='hosts')( - html.thead( - html.tr( - html.th('#'), - ( - html.th(class_=name)(title) for name, title, fvalue in self.COLUMNS - ) - ), - ), - html.tbody( - html.tr(id=host.id)( - html.td(html.a(href=self.url(ItemHandler, id=host.id))('#')), - ( - column(name, title, fvalue, host) for name, title, fvalue in self.COLUMNS - ), - ) for host in self.hosts + + self.table.render(self.hosts)(id='hosts-realtime'), + + html.script(type='text/javascript')( + """ +$(document).ready(HostsRealtime(Table($('#hosts-realtime'), {table_params}), {params})); + """.format( + table_params = json.dumps(dict( + item_url = self.url(ItemHandler, id='0'), + columns = [column.attr for column in self.table.columns], + )), + params = json.dumps(dict( + url = self.url(), + filters = self.filters, + t = dt2ts(self.t), + )), ) - ), - html.script(type='text/javascript')(""" -$(document).ready(hosts_realtime({params})); -""".format(params=params) ) )