changeset 345:a3473b5ea50e

merge
author Matt Johnston <matt@ucc.asn.au>
date Sat, 16 Jun 2012 09:08:47 +0800
parents aa247177837a (current diff) ea1779d27641 (diff)
children d6219df77c41
files web/index.py
diffstat 4 files changed, 89 insertions(+), 71 deletions(-) [+]
line wrap: on
line diff
--- a/web/config.py	Sat Jun 16 09:08:40 2012 +0800
+++ b/web/config.py	Sat Jun 16 09:08:47 2012 +0800
@@ -1,5 +1,5 @@
 
-DATA_PATH = 'data'
+DATA_PATH = '/home/matt/templog/web/data'
 
 HMAC_KEY = 'a hmac key'
 
--- a/web/index.py	Sat Jun 16 09:08:40 2012 +0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,55 +0,0 @@
-#!/usr/bin/env python2.7
-
-import binascii
-import hmac
-import zlib
-import datetime
-import time
-
-import bottle
-from bottle import route, request, response
-
-import config
-import log
-
-@route('/update', method='post')
-def update():
-    enc_lines = request.forms.lines
-    mac = request.forms.hmac
-
-    if hmac.new(config.HMAC_KEY, enc_lines).hexdigest() != mac:
-        raise HTTPError(code = 403, output = "Bad key")
-
-    lines = zlib.decompress(binascii.a2b_base64(enc_lines)).split('\n')
-
-    log.parse(lines)
-
-    return "OK"
-
-@route('/graph.png')
-def graph():
-    # url takes time in hours or days
-    if 'day' in request.query:
-        start_day = datetime.datetime.strptime(request.query.day, '%Y%m%d')
-        start = time.mktime(start_day.timetuple())
-        length = int(request.query.length) * 3600 * 24
-    else:
-        start_hour = datetime.datetime.strptime(request.query.hour, '%Y%m%d%H')
-        start = time.mktime(start_hour.timetuple())
-        length = int(request.query.length) * 3600
-
-    response.set_header('Content-Type', 'image/png')
-    return log.graph_png(start, length)
-
-@route('/')
-def top():
-    return bottle.template('top', urlparams=request.query_string)
-
-def main():
-    bottle.debug(True)
-    bottle.run(port=9999, reloader=True)
-
-if __name__ == '__main__':
-    main()
-    
-
--- a/web/log.py	Sat Jun 16 09:08:40 2012 +0800
+++ b/web/log.py	Sat Jun 16 09:08:47 2012 +0800
@@ -1,3 +1,4 @@
+#:vim:et:ts=4:sts=4:sw=4:
 import rrdtool
 import os
 import os.path
@@ -5,6 +6,8 @@
 import glob
 import hashlib
 import tempfile
+import time
+import syslog
 from colorsys import hls_to_rgb
 
 import config
@@ -67,36 +70,40 @@
     except IOError, e:
         create_rrd(sensor_id)
 
-    values = ['%f:%f' % p for p in 
-        zip((first_real_time + time_step*t for t in xrange(len(measurements))),
-            measurements)]
+    print>>sys.stderr, sensor_id, measurements, first_real_time, time_step
+
+    if measurements:
+        values = ['%d:%f' % p for p in 
+            zip((first_real_time + time_step*t for t in xrange(len(measurements))),
+                measurements)]
 
-    rrdfile = sensor_rrd_path(sensor_id)
-    rrdtool.update(rrdfile, *values)
+        rrdfile = sensor_rrd_path(sensor_id)
+        print>>sys.stderr, values
+        rrdtool.update(rrdfile, *values)
 
-    # be paranois
-    f = file(rrdfile)
-    os.fsync(f.fileno())
+        # be paranoid
+        f = file(rrdfile)
+        os.fsync(f.fileno())
 
 def record_debug(lines):
-    f = open('%s/debug.log', config.DATA_PATH, 'a+')
-    f.write('===== %s =====' % time.strftime('%a, %d %b %Y %H:%M:%S')
+    f = open('%s/debug.log' % config.DATA_PATH, 'a+')
+    f.write('===== %s =====\n' % time.strftime('%a, %d %b %Y %H:%M:%S'))
     f.writelines(('%s\n' % s for s in lines))
     f.flush()
     return f
 
 def parse(lines):
    
-    debugf = record_debug(lines):
+    debugf = record_debug(lines)
 
     entries = dict(l.split('=', 1) for l in lines)
     if len(entries) != len(lines):
         raise Exception("Keys are not unique")
 
     num_sensors = int(entries['sensors'])
-    num_measurements = int(entries['sensors'])
+    num_measurements = int(entries['measurements'])
 
-    sensor_ids = [entries['sensor_id%d' % n] for n in xrange(num_sensors)]
+    sensors = [entries['sensor_id%d' % n] for n in xrange(num_sensors)]
 
     meas = []
     for s in sensors:
@@ -107,7 +114,7 @@
         return 0.1 * v
 
     for n in xrange(num_measurements):
-        vals = [val_scale(int(entries["meas%d" % n].strip().split()))]
+        vals = [val_scale(int(x)) for x in entries["meas%d" % n].strip().split()]
         if len(vals) != num_sensors:
             raise Exception("Wrong number of sensors for measurement %d" % n)
         # we make an array of values for each sensor
@@ -123,5 +130,5 @@
     for sensor_id, measurements in zip(sensors, meas):
         sensor_update(sensor_id, measurements, first_real_time, time_step)
 
-    debugf.write("Updated %d sensors\n" % len(sensors)
+    debugf.write("Updated %d sensors\n" % len(sensors))
     debugf.flush()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/templog.py	Sat Jun 16 09:08:47 2012 +0800
@@ -0,0 +1,66 @@
+#!/usr/bin/env python2.7
+
+import binascii
+import hmac
+import zlib
+import datetime
+import time
+
+import bottle
+from bottle import route, request, response
+
+import config
+import log
+
+@route('/update', method='post')
+def update():
+    enc_lines = request.forms.lines
+    mac = request.forms.hmac
+
+    if hmac.new(config.HMAC_KEY, enc_lines).hexdigest() != mac:
+        raise HTTPError(code = 403, output = "Bad key")
+
+    lines = zlib.decompress(binascii.a2b_base64(enc_lines)).split('\n')
+
+    log.parse(lines)
+
+    return "OK"
+
+@route('/graph.png')
+def graph():
+    # url takes time in hours or days
+    if 'day' in request.query:
+        start_day = datetime.datetime.strptime(request.query.day, '%Y%m%d')
+        start = time.mktime(start_day.timetuple())
+        length = int(request.query.length) * 3600 * 24
+    else:
+        start_hour = datetime.datetime.strptime(request.query.hour, '%Y%m%d%H')
+        start = time.mktime(start_hour.timetuple())
+        length = int(request.query.length) * 3600
+
+    response.set_header('Content-Type', 'image/png')
+    return log.graph_png(start, length)
+
+@route('/')
+def top():
+    return bottle.template('top', urlparams=request.query_string)
+
+@route('/test')
+def test():
+    import config
+    import os
+    f = open('%s/testout' % config.DATA_PATH, 'a+')
+    f.write("more")
+    f.flush()
+    f.close()
+
+    return 'done'
+
+def main():
+    bottle.debug(True)
+    bottle.run(port=9999, reloader=True)
+
+if __name__ == '__main__':
+    main()
+    
+