comparison web/log.py @ 31:5e75e08d20ac

- 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 048143905092
children e18d7e89c17d
comparison
equal deleted inserted replaced
30:13fcf497f8b7 31:5e75e08d20ac
1 import rrdtool 1 import rrdtool
2 import os 2 import os
3 import os.path 3 import os.path
4 import sys 4 import sys
5 import glob 5 import glob
6 import hashlib
7 import tempfile
6 from colorsys import hls_to_rgb 8 from colorsys import hls_to_rgb
7 9
8 import config 10 import config
9 11
10 def sensor_rrd_path(s): 12 def sensor_rrd_path(s):
23 25
24 # stolen from viewmtn, stolen from monotone-viz 26 # stolen from viewmtn, stolen from monotone-viz
25 def colour_from_string(str): 27 def colour_from_string(str):
26 def f(off): 28 def f(off):
27 return ord(hashval[off]) / 256.0 29 return ord(hashval[off]) / 256.0
28 hashval = sha.new(str).digest() 30 hashval = hashlib.sha1(str).digest()
29 hue = f(5) 31 hue = f(5)
30 li = f(1) * 0.15 + 0.55 32 li = f(1) * 0.15 + 0.55
31 sat = f(2) * 0.5 + .5 33 sat = f(2) * 0.5 + .5
32 return ''.join(["%.2x" % int(x * 256) for x in hls_to_rgb(hue, li, sat)]) 34 return ''.join(["%.2x" % int(x * 256) for x in hls_to_rgb(hue, li, sat)])
33 35
38 for n, (rrdfile, sensor) in enumerate(rrds): 40 for n, (rrdfile, sensor) in enumerate(rrds):
39 vname = 'temp%d' % n 41 vname = 'temp%d' % n
40 graph_args.append('DEF:%(vname)s=%(rrdfile)s:temp:AVERAGE' % locals()) 42 graph_args.append('DEF:%(vname)s=%(rrdfile)s:temp:AVERAGE' % locals())
41 width = config.LINE_WIDTH 43 width = config.LINE_WIDTH
42 legend = config.SENSOR_NAMES.get(sensor, sensor) 44 legend = config.SENSOR_NAMES.get(sensor, sensor)
43 colour = config.SENSOR_COLOURS.get(legend, colour_from_string(r)) 45 colour = config.SENSOR_COLOURS.get(legend, colour_from_string(sensor))
44 graph_args.append('LINE%(width)f:%(vname)s#%(colour)s:%(legend)s' % locals()) 46 graph_args.append('LINE%(width)f:%(vname)s#%(colour)s:%(legend)s' % locals())
45 47
46 tempf = tempfile.NamedTemporaryFile() 48 tempf = tempfile.NamedTemporaryFile()
47 args = [tempf.name, '-s', str(start), 49 args = [tempf.name, '-s', str(int(start)),
48 '-e', str(start+length), 50 '-e', str(int(start+length)),
49 '-w', config.GRAPH_WIDTH, 51 '-w', str(config.GRAPH_WIDTH),
52 '-h', str(config.GRAPH_HEIGHT),
50 '--slope-mode', 53 '--slope-mode',
51 '--imgformat', 'PNG'] 54 '--border', '0',
55 '--color', 'BACK#ffffff',
56 '--imgformat', 'PNG'] \
52 + graph_args 57 + graph_args
58 if config.GRAPH_FONT:
59 args += ['--font', 'DEFAULT:0:%s' % config.GRAPH_FONT]
60 print>>sys.stderr, args
53 rrdtool.graph(*args) 61 rrdtool.graph(*args)
54 return tempf.read() 62 return tempf.read()
55 63
56 def sensor_update(sensor_id, measurements, first_real_time, time_step): 64 def sensor_update(sensor_id, measurements, first_real_time, time_step):
57 try: 65 try:
58 open(sensor_rrd_path(sensor_id)) 66 open(sensor_rrd_path(sensor_id))
59 except IOError, e: 67 except IOError, e:
60 create_rrd(sensor_id) 68 create_rrd(sensor_id)
61 69
62 value_text = ' '.join('%f:%f' % p for p in 70 values = ['%f:%f' % p for p in
63 zip(measurements, 71 zip((first_real_time + time_step*t for t in xrange(len(measurements))),
64 (first_real_time + time_step*t for t in xrange(len(measurements))))) 72 measurements)]
65 73
66 rrdtool.update(sensor_rrd_path(sensor_id), value_text) 74 rrdtool.update(sensor_rrd_path(sensor_id), *values)
67 75
68 def parse(lines): 76 def parse(lines):
69 entries = dict(l.split('=', 1) for l in lines) 77 entries = dict(l.split('=', 1) for l in lines)
70 if len(entries) != len(lines); 78 if len(entries) != len(lines):
71 raise Exception("Keys are not unique") 79 raise Exception("Keys are not unique")
72 80
73 num_sensors = int(entries['sensors']) 81 num_sensors = int(entries['sensors'])
74 num_measurements = int(entries['sensors']) 82 num_measurements = int(entries['sensors'])
75 83
89 raise Exception("Wrong number of sensors for measurement %d" % n) 97 raise Exception("Wrong number of sensors for measurement %d" % n)
90 # we make an array of values for each sensor 98 # we make an array of values for each sensor
91 for s in xrange(num_sensors): 99 for s in xrange(num_sensors):
92 meas[s].append(vals[s]) 100 meas[s].append(vals[s])
93 101
94 avr_now = float(entries['now') 102 avr_now = float(entries['now'])
95 avr_first_time = float(entries['first_time']) 103 avr_first_time = float(entries['first_time'])
96 time_step = float(entries['time_step']) 104 time_step = float(entries['time_step'])
97 105
98 first_real_time = time.time() - (avr_now - avr_first_time) 106 first_real_time = time.time() - (avr_now - avr_first_time)
99 107