annotate web/templog.py @ 405:d9b78a1bdd1d

fix off-by-one in remainder code
author Matt Johnston <matt@ucc.asn.au>
date Tue, 17 Jul 2012 21:58:49 +0800
parents 5026780c2dc7
children ef59da811986
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 #!/usr/bin/env python2.7
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
3 import binascii
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
4 import hmac
335
1e22eaf93620 work on web interface
Matt Johnston <matt@ucc.asn.au>
parents: 334
diff changeset
5 import zlib
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
6 from datetime import datetime, timedelta
337
f575ef538f5d - Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents: 336
diff changeset
7 import time
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
8 import urllib
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
9 import sys
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
10
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 import bottle
337
f575ef538f5d - Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents: 336
diff changeset
12 from bottle import route, request, response
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
14 import config
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
15 import log
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
16
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
17 DATE_FORMAT = '%Y%m%d-%H.%M'
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
18 ZOOM_SCALE = 2.0
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
19
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 @route('/update', method='post')
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 def update():
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
22 enc_lines = request.forms.lines
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
23 mac = request.forms.hmac
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
24
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
25 if hmac.new(config.HMAC_KEY, enc_lines).hexdigest() != mac:
391
5026780c2dc7 - Fix HTTPError
Matt Johnston <matt@ucc.asn.au>
parents: 383
diff changeset
26 raise bottle.HTTPError(code = 403, output = "Bad key")
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
27
335
1e22eaf93620 work on web interface
Matt Johnston <matt@ucc.asn.au>
parents: 334
diff changeset
28 lines = zlib.decompress(binascii.a2b_base64(enc_lines)).split('\n')
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
29
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
30 log.parse(lines)
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
31
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
32 return "OK"
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 @route('/graph.png')
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 def graph():
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
36 length_minutes = int(request.query.length)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
37 end = datetime.strptime(request.query.end, DATE_FORMAT)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
38 start = end - timedelta(minutes=length_minutes)
336
ba4c4df13487 parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
39
ba4c4df13487 parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
40 response.set_header('Content-Type', 'image/png')
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
41 start_epoch = time.mktime(start.timetuple())
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
42 return log.graph_png(start_epoch, length_minutes * 60)
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 @route('/')
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 def top():
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
46
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
47 minutes = int(request.query.get('length', 26*60))
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
48
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
49 if 'end' in request.query:
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
50 end = datetime.strptime(request.query.end, DATE_FORMAT)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
51 else:
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
52 end = datetime.now()
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
53
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
54 if 'zoom' in request.query:
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
55 orig_start = end - timedelta(minutes=minutes)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
56 orig_end = end
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
57 xpos = int(request.query.x)
383
a032051475fe ui tweaks
Matt Johnston <matt@ucc.asn.au>
parents: 375
diff changeset
58 xpos -= config.GRAPH_LEFT_MARGIN * config.ZOOM
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
59
383
a032051475fe ui tweaks
Matt Johnston <matt@ucc.asn.au>
parents: 375
diff changeset
60 if xpos >= 0 and xpos < config.GRAPH_WIDTH * config.ZOOM:
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
61 click_time = orig_start \
383
a032051475fe ui tweaks
Matt Johnston <matt@ucc.asn.au>
parents: 375
diff changeset
62 + timedelta(minutes=(float(xpos) / (config.GRAPH_WIDTH * config.ZOOM)) * minutes)
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
63 minutes = int(minutes / ZOOM_SCALE)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
64
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
65 end = click_time + timedelta(minutes=minutes/2)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
66 else:
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
67 # zoom out
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
68 minutes = int(minutes*ZOOM_SCALE)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
69 end += timedelta(minutes=minutes/2)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
70
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
71 if end > datetime.now():
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
72 end = datetime.now()
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
73
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
74 request.query.replace('length', minutes)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
75 request.query.replace('end', end.strftime(DATE_FORMAT))
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
76
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
77 urlparams = urllib.urlencode(request.query)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
78 return bottle.template('top', urlparams=urlparams,
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
79 end = end.strftime(DATE_FORMAT),
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
80 length = minutes)
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81
344
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
82 @route('/test')
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
83 def test():
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
84 import config
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
85 import os
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
86 f = open('%s/testout' % config.DATA_PATH, 'a+')
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
87 f.write("more")
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
88 f.flush()
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
89 f.close()
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
90
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
91 return 'done'
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
92
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 def main():
367
3db118498b97 sort out voltage logging
Matt Johnston <matt@ucc.asn.au>
parents: 362
diff changeset
94 #bottle.debug(True)
3db118498b97 sort out voltage logging
Matt Johnston <matt@ucc.asn.au>
parents: 362
diff changeset
95 #bottle.run(reloader=True)
3db118498b97 sort out voltage logging
Matt Johnston <matt@ucc.asn.au>
parents: 362
diff changeset
96 bottle.run(server='cgi', reloader=True)
346
d6219df77c41 main.c:
Matt Johnston <matt@ucc.asn.au>
parents: 344
diff changeset
97 #bottle.run(port=9999, reloader=True)
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 if __name__ == '__main__':
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 main()
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102