comparison web/log.py @ 37:8da0fdadc8d7

- Getting there, update has problems
author Matt Johnston <matt@ucc.asn.au>
date Sat, 16 Jun 2012 09:08:07 +0800
parents 024f5571df8c
children 9b5b202129c3
comparison
equal deleted inserted replaced
34:79124d7d3f79 37:8da0fdadc8d7
1 #:vim:et:ts=4:sts=4:sw=4:
1 import rrdtool 2 import rrdtool
2 import os 3 import os
3 import os.path 4 import os.path
4 import sys 5 import sys
5 import glob 6 import glob
6 import hashlib 7 import hashlib
7 import tempfile 8 import tempfile
9 import time
10 import syslog
8 from colorsys import hls_to_rgb 11 from colorsys import hls_to_rgb
9 12
10 import config 13 import config
11 14
12 def sensor_rrd_path(s): 15 def sensor_rrd_path(s):
65 try: 68 try:
66 open(sensor_rrd_path(sensor_id)) 69 open(sensor_rrd_path(sensor_id))
67 except IOError, e: 70 except IOError, e:
68 create_rrd(sensor_id) 71 create_rrd(sensor_id)
69 72
70 values = ['%f:%f' % p for p in 73 print>>sys.stderr, sensor_id, measurements, first_real_time, time_step
71 zip((first_real_time + time_step*t for t in xrange(len(measurements))),
72 measurements)]
73 74
74 rrdfile = sensor_rrd_path(sensor_id) 75 if measurements:
75 rrdtool.update(rrdfile, *values) 76 values = ['%d:%f' % p for p in
77 zip((first_real_time + time_step*t for t in xrange(len(measurements))),
78 measurements)]
76 79
77 # be paranois 80 rrdfile = sensor_rrd_path(sensor_id)
78 f = file(rrdfile) 81 print>>sys.stderr, values
79 os.fsync(f.fileno()) 82 rrdtool.update(rrdfile, *values)
83
84 # be paranoid
85 f = file(rrdfile)
86 os.fsync(f.fileno())
80 87
81 def record_debug(lines): 88 def record_debug(lines):
82 f = open('%s/debug.log', config.DATA_PATH, 'a+') 89 f = open('%s/debug.log' % config.DATA_PATH, 'a+')
83 f.write('===== %s =====' % time.strftime('%a, %d %b %Y %H:%M:%S') 90 f.write('===== %s =====\n' % time.strftime('%a, %d %b %Y %H:%M:%S'))
84 f.writelines(('%s\n' % s for s in lines)) 91 f.writelines(('%s\n' % s for s in lines))
85 f.flush() 92 f.flush()
86 return f 93 return f
87 94
88 def parse(lines): 95 def parse(lines):
89 96
90 debugf = record_debug(lines): 97 debugf = record_debug(lines)
91 98
92 entries = dict(l.split('=', 1) for l in lines) 99 entries = dict(l.split('=', 1) for l in lines)
93 if len(entries) != len(lines): 100 if len(entries) != len(lines):
94 raise Exception("Keys are not unique") 101 raise Exception("Keys are not unique")
95 102
96 num_sensors = int(entries['sensors']) 103 num_sensors = int(entries['sensors'])
97 num_measurements = int(entries['sensors']) 104 num_measurements = int(entries['measurements'])
98 105
99 sensor_ids = [entries['sensor_id%d' % n] for n in xrange(num_sensors)] 106 sensors = [entries['sensor_id%d' % n] for n in xrange(num_sensors)]
100 107
101 meas = [] 108 meas = []
102 for s in sensors: 109 for s in sensors:
103 meas.append([]) 110 meas.append([])
104 111
105 def val_scale(v): 112 def val_scale(v):
106 # convert decidegrees to degrees 113 # convert decidegrees to degrees
107 return 0.1 * v 114 return 0.1 * v
108 115
109 for n in xrange(num_measurements): 116 for n in xrange(num_measurements):
110 vals = [val_scale(int(entries["meas%d" % n].strip().split()))] 117 vals = [val_scale(int(x)) for x in entries["meas%d" % n].strip().split()]
111 if len(vals) != num_sensors: 118 if len(vals) != num_sensors:
112 raise Exception("Wrong number of sensors for measurement %d" % n) 119 raise Exception("Wrong number of sensors for measurement %d" % n)
113 # we make an array of values for each sensor 120 # we make an array of values for each sensor
114 for s in xrange(num_sensors): 121 for s in xrange(num_sensors):
115 meas[s].append(vals[s]) 122 meas[s].append(vals[s])
121 first_real_time = time.time() - (avr_now - avr_first_time) 128 first_real_time = time.time() - (avr_now - avr_first_time)
122 129
123 for sensor_id, measurements in zip(sensors, meas): 130 for sensor_id, measurements in zip(sensors, meas):
124 sensor_update(sensor_id, measurements, first_real_time, time_step) 131 sensor_update(sensor_id, measurements, first_real_time, time_step)
125 132
126 debugf.write("Updated %d sensors\n" % len(sensors) 133 debugf.write("Updated %d sensors\n" % len(sensors))
127 debugf.flush() 134 debugf.flush()