Mercurial > templog
annotate web/templog.py @ 421:5055caf46064
fridge range is decicelcius
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 10 Oct 2012 22:48:24 +0800 |
parents | 1854abee5a59 |
children | 3b4277aaed3c |
rev | line source |
---|---|
333 | 1 #!/usr/bin/env python2.7 |
2 | |
334 | 3 import binascii |
4 import hmac | |
335 | 5 import zlib |
375 | 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 | 8 import urllib |
9 import sys | |
334 | 10 |
333 | 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 | 13 |
334 | 14 import config |
15 import log | |
16 | |
375 | 17 DATE_FORMAT = '%Y%m%d-%H.%M' |
18 ZOOM_SCALE = 2.0 | |
19 | |
333 | 20 @route('/update', method='post') |
21 def update(): | |
334 | 22 enc_lines = request.forms.lines |
23 mac = request.forms.hmac | |
24 | |
25 if hmac.new(config.HMAC_KEY, enc_lines).hexdigest() != mac: | |
391 | 26 raise bottle.HTTPError(code = 403, output = "Bad key") |
334 | 27 |
335 | 28 lines = zlib.decompress(binascii.a2b_base64(enc_lines)).split('\n') |
334 | 29 |
30 log.parse(lines) | |
31 | |
32 return "OK" | |
333 | 33 |
34 @route('/graph.png') | |
35 def graph(): | |
375 | 36 length_minutes = int(request.query.length) |
37 end = datetime.strptime(request.query.end, DATE_FORMAT) | |
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 | 41 start_epoch = time.mktime(start.timetuple()) |
42 return log.graph_png(start_epoch, length_minutes * 60) | |
333 | 43 |
44 @route('/') | |
45 def top(): | |
375 | 46 |
47 minutes = int(request.query.get('length', 26*60)) | |
48 | |
49 if 'end' in request.query: | |
50 end = datetime.strptime(request.query.end, DATE_FORMAT) | |
51 else: | |
52 end = datetime.now() | |
53 | |
54 if 'zoom' in request.query: | |
55 orig_start = end - timedelta(minutes=minutes) | |
56 orig_end = end | |
57 xpos = int(request.query.x) | |
383 | 58 xpos -= config.GRAPH_LEFT_MARGIN * config.ZOOM |
375 | 59 |
383 | 60 if xpos >= 0 and xpos < config.GRAPH_WIDTH * config.ZOOM: |
375 | 61 click_time = orig_start \ |
383 | 62 + timedelta(minutes=(float(xpos) / (config.GRAPH_WIDTH * config.ZOOM)) * minutes) |
375 | 63 minutes = int(minutes / ZOOM_SCALE) |
64 | |
65 end = click_time + timedelta(minutes=minutes/2) | |
66 else: | |
67 # zoom out | |
68 minutes = int(minutes*ZOOM_SCALE) | |
69 end += timedelta(minutes=minutes/2) | |
70 | |
71 if end > datetime.now(): | |
72 end = datetime.now() | |
73 | |
74 request.query.replace('length', minutes) | |
75 request.query.replace('end', end.strftime(DATE_FORMAT)) | |
76 | |
77 urlparams = urllib.urlencode(request.query) | |
78 return bottle.template('top', urlparams=urlparams, | |
79 end = end.strftime(DATE_FORMAT), | |
80 length = minutes) | |
333 | 81 |
409 | 82 @route('/debug') |
83 def debuglog(): | |
84 response.set_header('Content-Type', 'text/plain') | |
85 return log.tail_debug_log() | |
344
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
86 |
333 | 87 def main(): |
367 | 88 #bottle.debug(True) |
89 #bottle.run(reloader=True) | |
90 bottle.run(server='cgi', reloader=True) | |
346 | 91 #bottle.run(port=9999, reloader=True) |
333 | 92 |
93 if __name__ == '__main__': | |
94 main() | |
95 | |
96 |