changeset 337:f575ef538f5d

- Various fixes for web server, kind of works
author Matt Johnston <matt@ucc.asn.au>
date Wed, 13 Jun 2012 23:41:05 +0800
parents ba4c4df13487
children 12123e390169
files web/config.py web/index.py web/log.py
diffstat 3 files changed, 29 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/web/config.py	Tue Jun 12 23:43:49 2012 +0800
+++ b/web/config.py	Wed Jun 13 23:41:05 2012 +0800
@@ -4,9 +4,12 @@
 HMAC_KEY = 'a hmac key'
 
 GRAPH_WIDTH = 800
+GRAPH_HEIGHT = 400
 
 LINE_WIDTH = 2
 
-SENSOR_NAMES = {}
+SENSOR_NAMES = {'sensor_test1': "First Test"}
 
 SENSOR_COLOURS = {}
+
+GRAPH_FONT = "Courier"
--- a/web/index.py	Tue Jun 12 23:43:49 2012 +0800
+++ b/web/index.py	Wed Jun 13 23:41:05 2012 +0800
@@ -3,9 +3,11 @@
 import binascii
 import hmac
 import zlib
+import datetime
+import time
 
 import bottle
-from bottle import route, request
+from bottle import route, request, response
 
 import config
 import log
@@ -26,14 +28,13 @@
 
 @route('/graph.png')
 def graph():
-    start_secs = int(request.query.start)
     # url takes time in hours or days
     if 'day' in request.query:
-        start_day = datetime.strptime(request.query.day, '%Y%m%d')
+        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.strptime(request.query.hour, '%Y%m%d%H')
+        start_hour = datetime.datetime.strptime(request.query.hour, '%Y%m%d%H')
         start = time.mktime(start_hour.timetuple())
         length = int(request.query.length) * 3600
 
--- a/web/log.py	Tue Jun 12 23:43:49 2012 +0800
+++ b/web/log.py	Wed Jun 13 23:41:05 2012 +0800
@@ -3,6 +3,8 @@
 import os.path
 import sys
 import glob
+import hashlib
+import tempfile
 from colorsys import hls_to_rgb
 
 import config
@@ -25,7 +27,7 @@
 def colour_from_string(str):
     def f(off):
         return ord(hashval[off]) / 256.0
-    hashval = sha.new(str).digest()
+    hashval = hashlib.sha1(str).digest()
     hue = f(5)
     li = f(1) * 0.15 + 0.55
     sat = f(2) * 0.5 + .5
@@ -40,16 +42,22 @@
         graph_args.append('DEF:%(vname)s=%(rrdfile)s:temp:AVERAGE' % locals())
         width = config.LINE_WIDTH
         legend = config.SENSOR_NAMES.get(sensor, sensor)
-        colour = config.SENSOR_COLOURS.get(legend, colour_from_string(r))
+        colour = config.SENSOR_COLOURS.get(legend, colour_from_string(sensor))
         graph_args.append('LINE%(width)f:%(vname)s#%(colour)s:%(legend)s' % locals())
 
     tempf = tempfile.NamedTemporaryFile()
-    args = [tempf.name, '-s', str(start),
-        '-e', str(start+length),
-        '-w', config.GRAPH_WIDTH,
+    args = [tempf.name, '-s', str(int(start)),
+        '-e', str(int(start+length)),
+        '-w', str(config.GRAPH_WIDTH),
+        '-h', str(config.GRAPH_HEIGHT),
         '--slope-mode',
-        '--imgformat', 'PNG']
+        '--border', '0',
+        '--color', 'BACK#ffffff',
+        '--imgformat', 'PNG'] \
         + graph_args
+    if config.GRAPH_FONT:
+        args += ['--font', 'DEFAULT:0:%s' % config.GRAPH_FONT]
+    print>>sys.stderr, args
     rrdtool.graph(*args)
     return tempf.read()
 
@@ -59,15 +67,15 @@
     except IOError, e:
         create_rrd(sensor_id)
 
-    value_text = ' '.join('%f:%f' % p for p in 
-        zip(measurements, 
-        (first_real_time + time_step*t for t in xrange(len(measurements)))))
+    values = ['%f:%f' % p for p in 
+        zip((first_real_time + time_step*t for t in xrange(len(measurements))),
+            measurements)]
 
-    rrdtool.update(sensor_rrd_path(sensor_id), value_text)
+    rrdtool.update(sensor_rrd_path(sensor_id), *values)
 
 def parse(lines):
     entries = dict(l.split('=', 1) for l in lines)
-    if len(entries) != len(lines);
+    if len(entries) != len(lines):
         raise Exception("Keys are not unique")
 
     num_sensors = int(entries['sensors'])
@@ -91,7 +99,7 @@
         for s in xrange(num_sensors):
             meas[s].append(vals[s])
 
-    avr_now = float(entries['now')
+    avr_now = float(entries['now'])
     avr_first_time = float(entries['first_time'])
     time_step = float(entries['time_step'])