# HG changeset patch # User Matt Johnston # Date 1339514873 -28800 # Node ID 1e22eaf93620a80409b933be28f1430202d73cb3 # Parent 3b821541657d7524880ce9fcf95c29e6601194f5 work on web interface diff -r 3b821541657d -r 1e22eaf93620 server/ts.py --- a/server/ts.py Tue Jun 12 00:35:23 2012 +0800 +++ b/server/ts.py Tue Jun 12 23:27:53 2012 +0800 @@ -110,7 +110,7 @@ return int(next_wake) def send_results(lines): - enc_lines = binascii.b2a_base64('\n'.join(lines)) + enc_lines = binascii.b2a_base64(zlib.compress('\n'.join(lines))) hmac.new(config.HMAC_KEY, enc_lines).hexdigest() url_data = urllib.url_encode( ('lines', enc_lines), ('hmac', mac) ) diff -r 3b821541657d -r 1e22eaf93620 web/config.py --- a/web/config.py Tue Jun 12 00:35:23 2012 +0800 +++ b/web/config.py Tue Jun 12 23:27:53 2012 +0800 @@ -3,3 +3,10 @@ HMAC_KEY = 'a hmac key' +GRAPH_WIDTH = 800 + +LINE_WIDTH = 2 + +SENSOR_NAMES = {} + +SENSOR_COLOURS = {} diff -r 3b821541657d -r 1e22eaf93620 web/index.py --- a/web/index.py Tue Jun 12 00:35:23 2012 +0800 +++ b/web/index.py Tue Jun 12 23:27:53 2012 +0800 @@ -2,6 +2,7 @@ import binascii import hmac +import zlib import bottle from bottle import route, request @@ -17,7 +18,7 @@ if hmac.new(config.HMAC_KEY, enc_lines).hexdigest() != mac: raise HTTPError(code = 403, output = "Bad key") - lines = binascii.a2b_base64(enc_lines).split('\n') + lines = zlib.decompress(binascii.a2b_base64(enc_lines)).split('\n') log.parse(lines) diff -r 3b821541657d -r 1e22eaf93620 web/log.py --- a/web/log.py Tue Jun 12 00:35:23 2012 +0800 +++ b/web/log.py Tue Jun 12 23:27:53 2012 +0800 @@ -1,17 +1,58 @@ import rrdtool import os +import os.path import sys +import glob +from colorsys import hls_to_rgb import config def sensor_rrd_path(s): return '%s/sensor_%s.rrd' % (config.DATA_PATH, s) +# returns (path, sensor_name) tuples +def all_sensors(): + return [(r, os.path.basename(r[:-4])) + for r in glob.glob('%s/*.rrd' % config.DATA_PATH)] + def create_rrd(sensor_id): rrdtool.create(sensor_rrd_path(sensor_id), '-s', '300', 'DS:temp:GAUGE:600:-10:100', 'RRA:AVERAGE:0.5:1:1051200') + +# stolen from viewmtn, stolen from monotone-viz +def colour_from_string(str): + def f(off): + return ord(hashval[off]) / 256.0 + hashval = sha.new(str).digest() + hue = f(5) + li = f(1) * 0.15 + 0.55 + sat = f(2) * 0.5 + .5 + return ''.join(["%.2x" % int(x * 256) for x in hls_to_rgb(hue, li, sat)]) + +def graph_png(start, length): + rrds = all_sensors() + + graph_args = [] + for n, (rrdfile, sensor) in enumerate(rrds): + vname = 'temp%d' % n + 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)) + 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, + '--slope-mode', + '--imgformat', 'PNG'] + + graph_args + rrdtool.graph(*args) + return tempf.read() + def sensor_update(sensor_id, measurements, first_real_time, time_step): try: open(sensor_rrd_path(sensor_id)) diff -r 3b821541657d -r 1e22eaf93620 web/test.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/test.py Tue Jun 12 23:27:53 2012 +0800 @@ -0,0 +1,5 @@ +#!/usr/bin/env python +import log +import time + +log.sensor_update("test1", [22,22,22.1,22.4,22.5], time.time() - 10, 300)