# HG changeset patch # User Matt Johnston # Date 1339602065 -28800 # Node ID f575ef538f5d6e0abe0f1d81d222bb6229ab6d08 # Parent ba4c4df13487c46642b01c5e8af46797ebb27a82 - Various fixes for web server, kind of works diff -r ba4c4df13487 -r f575ef538f5d web/config.py --- 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" diff -r ba4c4df13487 -r f575ef538f5d web/index.py --- 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 diff -r ba4c4df13487 -r f575ef538f5d web/log.py --- 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'])