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