hosts.RealtimeHandler: refactor COLUMNS handling
authorTero Marttila <terom@paivola.fi>
Wed, 24 Oct 2012 20:27:39 +0300
changeset 29 38265b7d8f62
parent 28 9940bc6c0a34
child 30 841d856293a1
hosts.RealtimeHandler: refactor COLUMNS handling
pvl/verkko/hosts.py
static/hosts.js
--- a/pvl/verkko/hosts.py	Wed Oct 24 20:00:12 2012 +0300
+++ b/pvl/verkko/hosts.py	Wed Oct 24 20:27:39 2012 +0300
@@ -485,33 +485,47 @@
         '/static/hosts.js',
     )
 
+    COLUMNS = (
+        ( 'ip',     'IP',           lambda host: host.ip    ),
+        ( 'mac',    'MAC',          lambda host: host.mac   ),
+        ( 'name',   'Hostname',     lambda host: host.name  ),
+        ( 'gw',     'Network',      lambda host: host.gw    ),
+        ( 'seen',   'Seen',         Host.seen,              ),
+        ( 'state',  'State',        lambda host: host.state ),
+    )
+
+
     def process (self) :
         hosts = self.db.query(Host).order_by(Host.last_seen.desc())
         t = self.request.args.get('t')
+
+        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))
 
-            # update
             hosts = hosts.filter(Host.last_seen > t)
             hosts = list(hosts)
             hosts.reverse()
             
             if hosts :
                 t = hosts[-1].last_seen
-                hosts = [dict(
-                    id      = host.id, 
-                    ip      = host.ip, 
-                    mac     = host.mac,
-                    name    = host.name,
-                    gw      = host.gw,
-                    seen    = unicode(html.div(host.seen())),
-                    state       = host.state,
-                    state_class = host.state_class(),
+                hosts = [dict(host_params(host)) for host in hosts]
 
-                    t       = dt2ts(host.last_seen), 
-                ) for host in hosts]
-            
             else :
                 hosts = []
 
@@ -523,6 +537,7 @@
             return web.Response(json.dumps(data), mimetype='text/json')
 
         else :
+            # render html
             self.hosts = hosts.limit(10)
 
             # XXX: testing
@@ -531,34 +546,39 @@
             self.t = self.hosts[0].last_seen
 
     def render (self) :
+        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(),
             t       = dt2ts(self.t),
             host    = self.url(ItemHandler, id='0'),
+            columns = [name for name, title, fvalue in self.COLUMNS]
         )
         params = json.dumps(params)
         
-        COLUMNS = (
-            '#', 'IP', 'MAC', 'Hostname', 'Network', 'Seen', 'State'
-        )
-
         return html.div(id='wrapper')(
             html.input(type='submit', id='refresh', value="Refresh"),
             html.table(id='hosts')(
                 html.thead(
                     html.tr(
-                        html.th(title) for title in COLUMNS
+                        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))('#')),
                         (
-                            html.td(value) for value in (
-                                host.ip, host.mac, host.name, host.gw, host.seen(),
-                            )
+                            column(name, title, fvalue, host) for name, title, fvalue in self.COLUMNS
                         ),
-                        html.td(class_=host.state_class())(host.state),
                     ) for host in self.hosts
                 )
             ),
--- a/static/hosts.js	Wed Oct 24 20:00:12 2012 +0300
+++ b/static/hosts.js	Wed Oct 24 20:27:39 2012 +0300
@@ -53,17 +53,22 @@
     var animate = false; // true;
 
     function render_host (host) {
-        return html("tr", {id: host.id}, [
+        var columns = [
             html("td", {}, 
                 html("a", { href: params.host.replace('0', host.id) }, "#" )
             ),
-            html("td", {}, host.ip),
-            html("td", {}, host.mac),
-            html("td", {}, host.name),
-            html("td", {}, host.gw),
-            html("td", {}, host.seen),
-            html("td", { 'class': host.state_class }, host.state),
-        ]);
+        ];
+
+        $.each(params.columns, function (i, name) {
+            column = html("td", { class: name }, host[name]);
+
+            if (name == 'state')
+                column.addClass(host['state_class']);
+
+            columns.push(column);
+        });
+
+        return html("tr", {id: host.id}, columns);
     }
 
     function refresh () {