comparison web/log.py @ 335:1e22eaf93620

work on web interface
author Matt Johnston <matt@ucc.asn.au>
date Tue, 12 Jun 2012 23:27:53 +0800
parents 3b821541657d
children 5e75e08d20ac
comparison
equal deleted inserted replaced
334:3b821541657d 335:1e22eaf93620
1 import rrdtool 1 import rrdtool
2 import os 2 import os
3 import os.path
3 import sys 4 import sys
5 import glob
6 from colorsys import hls_to_rgb
4 7
5 import config 8 import config
6 9
7 def sensor_rrd_path(s): 10 def sensor_rrd_path(s):
8 return '%s/sensor_%s.rrd' % (config.DATA_PATH, s) 11 return '%s/sensor_%s.rrd' % (config.DATA_PATH, s)
9 12
13 # returns (path, sensor_name) tuples
14 def all_sensors():
15 return [(r, os.path.basename(r[:-4]))
16 for r in glob.glob('%s/*.rrd' % config.DATA_PATH)]
17
10 def create_rrd(sensor_id): 18 def create_rrd(sensor_id):
11 rrdtool.create(sensor_rrd_path(sensor_id), '-s', '300', 19 rrdtool.create(sensor_rrd_path(sensor_id), '-s', '300',
12 'DS:temp:GAUGE:600:-10:100', 20 'DS:temp:GAUGE:600:-10:100',
13 'RRA:AVERAGE:0.5:1:1051200') 21 'RRA:AVERAGE:0.5:1:1051200')
22
23
24 # stolen from viewmtn, stolen from monotone-viz
25 def colour_from_string(str):
26 def f(off):
27 return ord(hashval[off]) / 256.0
28 hashval = sha.new(str).digest()
29 hue = f(5)
30 li = f(1) * 0.15 + 0.55
31 sat = f(2) * 0.5 + .5
32 return ''.join(["%.2x" % int(x * 256) for x in hls_to_rgb(hue, li, sat)])
33
34 def graph_png(start, length):
35 rrds = all_sensors()
36
37 graph_args = []
38 for n, (rrdfile, sensor) in enumerate(rrds):
39 vname = 'temp%d' % n
40 graph_args.append('DEF:%(vname)s=%(rrdfile)s:temp:AVERAGE' % locals())
41 width = config.LINE_WIDTH
42 legend = config.SENSOR_NAMES.get(sensor, sensor)
43 colour = config.SENSOR_COLOURS.get(legend, colour_from_string(r))
44 graph_args.append('LINE%(width)f:%(vname)s#%(colour)s:%(legend)s' % locals())
45
46 tempf = tempfile.NamedTemporaryFile()
47 args = [tempf.name, '-s', str(start),
48 '-e', str(start+length),
49 '-w', config.GRAPH_WIDTH,
50 '--slope-mode',
51 '--imgformat', 'PNG']
52 + graph_args
53 rrdtool.graph(*args)
54 return tempf.read()
14 55
15 def sensor_update(sensor_id, measurements, first_real_time, time_step): 56 def sensor_update(sensor_id, measurements, first_real_time, time_step):
16 try: 57 try:
17 open(sensor_rrd_path(sensor_id)) 58 open(sensor_rrd_path(sensor_id))
18 except IOError, e: 59 except IOError, e: