pvl.verkko.wlan: basic Table view
authorTero Marttila <terom@paivola.fi>
Fri, 05 Jul 2013 02:03:12 +0300
changeset 243 787c5f93f434
parent 242 088d68412650
child 244 fc9fb80e4ebd
pvl.verkko.wlan: basic Table view
bin/pvl.wlan-syslog
pvl/verkko/wlan.py
setup.py
static/wlan.css
--- a/bin/pvl.wlan-syslog	Fri Jul 05 02:02:33 2013 +0300
+++ b/bin/pvl.wlan-syslog	Fri Jul 05 02:03:12 2013 +0300
@@ -11,7 +11,9 @@
 
 import pvl.args
 import pvl.syslog.args
+import pvl.web.args
 import pvl.rrd.hosts
+import pvl.verkko.wlan
 
 import optparse
 import logging; log = logging.getLogger('main')
@@ -38,6 +40,7 @@
     parser.add_option_group(pvl.args.parser(parser))
     parser.add_option_group(pvl.syslog.args.parser(parser, prog=WLAN_STA_PROG))
     parser.add_option_group(pvl.verkko.db.parser(parser, table=db.wlan_sta))
+    parser.add_option_group(pvl.web.args.parser(parser))
 
     parser.add_option('--interfaces', metavar='PATH',
             help="Load interface/node names from mapping file")
@@ -201,15 +204,20 @@
 
     # syslog
     log.info("Open up syslog...")
-    syslog = pvl.syslog.args.apply(options)
+    syslog = pvl.syslog.args.apply(options, optional=True)
+        
+    if syslog :
+        # handler
+        handler = WlanStaDatabase(db, interface_map=interfaces)
 
-    # handler
-    handler = WlanStaDatabase(db, interface_map=interfaces)
-
-    log.info("Enter mainloop...")
-    for source in syslog.main() :
-        for item in source:
-            handler(item)
+        log.info("Enter mainloop...")
+        for source in syslog.main() :
+            for item in source:
+                handler(item)
+    else :
+        # run web
+        application = pvl.web.args.apply(options, pvl.verkko.wlan.Application, db)
+        return pvl.web.args.main(options, application)
 
     return 0
     
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pvl/verkko/wlan.py	Fri Jul 05 02:03:12 2013 +0300
@@ -0,0 +1,75 @@
+from pvl.verkko import web, db, table
+from pvl.web import html, urls
+
+import datetime
+import logging; log = logging.getLogger('pvl.verkko.wlan')
+
+class WLANSta (object) :
+    """
+        A WLAN STA (client) with ap/ssid/sta and starts/ends.
+    """
+
+    DATE_FMT = '%Y%m%d %H:%M'
+    TIME_FMT = '%H:%M:%S'
+
+    @classmethod
+    def format_datetime (cls, dt) :
+        if dt.date() == datetime.date.today() :
+            return dt.strftime(cls.TIME_FMT)
+        else :
+            return dt.strftime(cls.DATE_FMT)
+
+    def seen (self) :
+        return (
+                html.span(title=self.first_seen)(self.format_datetime(self.first_seen)),
+                '-',
+                html.span(title=self.last_seen)(self.format_datetime(self.last_seen))
+        )
+
+db.mapper(WLANSta, db.wlan_sta, properties=dict(
+
+))
+
+class WLANTable (table.Table) :
+    """
+        <table> of WLAN STA
+    """
+
+    ITEMS   = "WLAN Stations"
+    COLUMNS = (
+        table.Column('ap',          "Access Point",     WLANSta.ap,
+            rowfilter   = True,
+        ),
+        table.Column('wlan',        "SSID",             WLANSta.wlan,
+            rowfilter   = True,
+        ),
+        table.Column('sta',         "MAC",              WLANSta.sta,
+            rowfilter   = True,
+        ),
+        table.Column('seen',        "Seen",             WLANSta.last_seen,  WLANSta.seen),
+    )
+    
+    # XXX: have to set again
+    ATTRS = dict((col.attr, col) for col in COLUMNS)
+
+    # default
+    SORT = WLANSta.last_seen.desc()
+    PAGE = 20
+
+class WLANHandler (table.TableHandler, web.DatabaseHandler) :
+    """
+        Combined database + <table>
+    """
+
+    CSS = web.DatabaseHandler.CSS + table.TableHandler.CSS + (
+        "/static/wlan.css", 
+    )
+    
+    # view
+    TABLE = WLANTable
+    DB_CLASS = WLANSta
+
+class Application (web.Application) :
+    URLS = urls.Map((
+        urls.rule('/',                       WLANHandler),
+    ))
--- a/setup.py	Fri Jul 05 02:02:33 2013 +0300
+++ b/setup.py	Fri Jul 05 02:03:12 2013 +0300
@@ -45,13 +45,14 @@
     ],
     
     # bin
-    scripts     = globs('bin/pvl.dhcp-*', 'bin/pvl.rrd-*', 'bin/pvl.verkko-*', 'bin/pvl.dns-*'),
+    scripts     = globs('bin/pvl.*-*'),
     
     # etc, static
     data_files  = [
         ( 'etc/pvl/verkko', [  ] ),
         ( 'share/pvl/verkko/static/dhcp',   globs('static/dhcp/*.css', 'static/dhcp/*.js')),
         ( 'share/pvl/verkko/static/rrd',    globs('static/rrd/*.css', 'static/rrd/*.js')),
+        ( 'share/pvl/verkko/static',        globs('static/*.css')),
     ],
 )
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/static/wlan.css	Fri Jul 05 02:03:12 2013 +0300
@@ -0,0 +1,26 @@
+/*
+ * General
+ */
+a
+{
+    color: inherit;
+}
+
+/*
+ * Hosts
+ */
+.id
+{
+    font-weight: bold;
+    text-align: right;
+}
+
+.ap,
+.sta
+{
+    font-family: monospace;
+}
+
+.seen
+{
+}