comparison web/log.py @ 367:3db118498b97

sort out voltage logging
author Matt Johnston <matt@ucc.asn.au>
date Tue, 26 Jun 2012 08:08:48 +0800
parents 8fea6144951b
children 43ec670f1b75
comparison
equal deleted inserted replaced
364:8645fdcacad1 367:3db118498b97
23 for r in glob.glob('%s/*.rrd' % config.DATA_PATH)] 23 for r in glob.glob('%s/*.rrd' % config.DATA_PATH)]
24 24
25 def create_rrd(sensor_id): 25 def create_rrd(sensor_id):
26 # start date of 10 seconds into 1970 is used so that we can 26 # start date of 10 seconds into 1970 is used so that we can
27 # update with prior values straight away. 27 # update with prior values straight away.
28 args = [sensor_rrd_path(sensor_id), 28 if 'voltage' in sensor_id:
29 '--start', '10', 29 args = [ 'DS:temp:GAUGE:7200:-1:10',
30 '--step', '300', 30 'RRA:AVERAGE:0.9999:1:1051200']
31 'DS:temp:GAUGE:600:-10:100', 31 else:
32 'RRA:AVERAGE:0.5:1:1051200'] 32 args = [ 'DS:temp:GAUGE:600:-10:100',
33 'RRA:AVERAGE:0.5:1:1051200']
33 34
34 rrdtool.create(*args) 35 rrdtool.create(sensor_rrd_path(sensor_id),
36 '--start', '10',
37 '--step', '300',
38 *args)
35 39
36 # stolen from viewmtn, stolen from monotone-viz 40 # stolen from viewmtn, stolen from monotone-viz
37 def colour_from_string(str): 41 def colour_from_string(str):
38 def f(off): 42 def f(off):
39 return ord(hashval[off]) / 256.0 43 return ord(hashval[off]) / 256.0
45 49
46 def graph_png(start, length): 50 def graph_png(start, length):
47 rrds = all_sensors() 51 rrds = all_sensors()
48 52
49 graph_args = [] 53 graph_args = []
54 have_volts = False
50 for n, (rrdfile, sensor) in enumerate(rrds): 55 for n, (rrdfile, sensor) in enumerate(rrds):
51 vname = 'temp%d' % n 56 if 'avrtemp' in sensor:
52 graph_args.append('DEF:%(vname)s=%(rrdfile)s:temp:AVERAGE' % locals()) 57 continue
58 if 'voltage' in sensor:
59 have_volts = True
60 vname = 'scalevolts'
61 graph_args = ['DEF:rawvolts=%(rrdfile)s:temp:AVERAGE:step=3600' % locals(),
62 'CDEF:scalevolts=rawvolts,0.2,/'] + graph_args
63 else:
64 vname = 'temp%d' % n
65 graph_args.append('DEF:%(vname)s=%(rrdfile)s:temp:AVERAGE' % locals())
53 width = config.LINE_WIDTH 66 width = config.LINE_WIDTH
54 legend = config.SENSOR_NAMES.get(sensor, sensor) 67 legend = config.SENSOR_NAMES.get(sensor, sensor)
55 colour = config.SENSOR_COLOURS.get(legend, colour_from_string(sensor)) 68 colour = config.SENSOR_COLOURS.get(legend, colour_from_string(sensor))
56 graph_args.append('LINE%(width)f:%(vname)s#%(colour)s:%(legend)s' % locals()) 69 graph_args.append('LINE%(width)f:%(vname)s#%(colour)s:%(legend)s' % locals())
57 70
60 '-e', str(int(start+length)), 73 '-e', str(int(start+length)),
61 '-w', str(config.GRAPH_WIDTH), 74 '-w', str(config.GRAPH_WIDTH),
62 '-h', str(config.GRAPH_HEIGHT), 75 '-h', str(config.GRAPH_HEIGHT),
63 '--slope-mode', 76 '--slope-mode',
64 '--border', '0', 77 '--border', '0',
78 '--vertical-label', 'Temperature',
65 '--y-grid', '1:1', 79 '--y-grid', '1:1',
66 '--grid-dash', '1:0', 80 '--grid-dash', '1:0',
67 '--color', 'GRID#00000000', 81 '--color', 'GRID#00000000',
68 '--color', 'MGRID#aaaaaa', 82 '--color', 'MGRID#aaaaaa',
69 '--color', 'BACK#ffffff', 83 '--color', 'BACK#ffffff',
84 'VRULE:%d#ee0000' % time.time(),
70 '--imgformat', 'PNG'] \ 85 '--imgformat', 'PNG'] \
71 + graph_args 86 + graph_args
72 if config.GRAPH_FONT: 87 if config.GRAPH_FONT:
73 args += ['--font', 'DEFAULT:11:%s' % config.GRAPH_FONT] 88 args += ['--font', 'DEFAULT:11:%s' % config.GRAPH_FONT]
74 print>>sys.stderr, args 89 if have_volts:
90 args += ['--right-axis', '0.2:0', # matches the scalevolts CDEF above
91 '--right-axis-format', '%.2lf',
92 '--right-axis-label', 'Voltage']
93
75 rrdtool.graph(*args) 94 rrdtool.graph(*args)
76 return tempf.read() 95 return tempf.read()
77 96
78 def sensor_update(sensor_id, measurements, first_real_time, time_step): 97 def sensor_update(sensor_id, measurements, first_real_time, time_step):
79 try: 98 try:
80 open(sensor_rrd_path(sensor_id)) 99 open(sensor_rrd_path(sensor_id))
81 except IOError, e: 100 except IOError, e:
82 create_rrd(sensor_id) 101 create_rrd(sensor_id)
83 102
84 print>>sys.stderr, sensor_id, measurements, first_real_time, time_step
85
86 if measurements: 103 if measurements:
87 values = ['%d:%f' % p for p in 104 values = ['%d:%f' % p for p in
88 zip((first_real_time + time_step*t for t in xrange(len(measurements))), 105 zip((first_real_time + time_step*t for t in xrange(len(measurements))),
89 measurements)] 106 measurements)]
90 107
91 rrdfile = sensor_rrd_path(sensor_id) 108 rrdfile = sensor_rrd_path(sensor_id)
92 print>>sys.stderr, values
93 # XXX what to do here when it fails... 109 # XXX what to do here when it fails...
94 for v in values: 110 for v in values:
95 try: 111 try:
96 rrdtool.update(rrdfile, v) 112 rrdtool.update(rrdfile, v)
97 except Exception, e: 113 except Exception, e:
140 156
141 avr_now = float(entries['now']) 157 avr_now = float(entries['now'])
142 avr_first_time = float(entries['first_time']) 158 avr_first_time = float(entries['first_time'])
143 time_step = float(entries['time_step']) 159 time_step = float(entries['time_step'])
144 160
161 if 'avrtemp' in entries:
162 avrtemp = val_scale(int(entries['avrtemp']))
163 sensor_update('avrtemp', [avrtemp], time.time(), 1)
164
165 if 'voltage' in entries:
166 voltage = 0.001 * int(entries['voltage'])
167 sensor_update('voltage', [voltage], time.time(), 1)
168
145 #sqlite 169 #sqlite
146 # - time 170 # - time
147 # - voltage 171 # - voltage
148 # - boot time 172 # - boot time
149 173