Mercurial > templog
annotate web/templog.py @ 143:0895f5ad7731
copied fridge logic from main.c
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 20 Nov 2012 22:03:10 +0800 |
parents | ef59da811986 |
children | 3b4277aaed3c |
rev | line source |
---|---|
27 | 1 #!/usr/bin/env python2.7 |
2 | |
28 | 3 import binascii |
4 import hmac | |
29 | 5 import zlib |
69 | 6 from datetime import datetime, timedelta |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
30
diff
changeset
|
7 import time |
69 | 8 import urllib |
9 import sys | |
28 | 10 |
27 | 11 import bottle |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
30
diff
changeset
|
12 from bottle import route, request, response |
27 | 13 |
28 | 14 import config |
15 import log | |
16 | |
69 | 17 DATE_FORMAT = '%Y%m%d-%H.%M' |
18 ZOOM_SCALE = 2.0 | |
19 | |
27 | 20 @route('/update', method='post') |
21 def update(): | |
28 | 22 enc_lines = request.forms.lines |
23 mac = request.forms.hmac | |
24 | |
25 if hmac.new(config.HMAC_KEY, enc_lines).hexdigest() != mac: | |
85 | 26 raise bottle.HTTPError(code = 403, output = "Bad key") |
28 | 27 |
29 | 28 lines = zlib.decompress(binascii.a2b_base64(enc_lines)).split('\n') |
28 | 29 |
30 log.parse(lines) | |
31 | |
32 return "OK" | |
27 | 33 |
34 @route('/graph.png') | |
35 def graph(): | |
69 | 36 length_minutes = int(request.query.length) |
37 end = datetime.strptime(request.query.end, DATE_FORMAT) | |
38 start = end - timedelta(minutes=length_minutes) | |
30
13fcf497f8b7
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
39 |
13fcf497f8b7
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
40 response.set_header('Content-Type', 'image/png') |
69 | 41 start_epoch = time.mktime(start.timetuple()) |
42 return log.graph_png(start_epoch, length_minutes * 60) | |
27 | 43 |
44 @route('/') | |
45 def top(): | |
69 | 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) | |
77 | 58 xpos -= config.GRAPH_LEFT_MARGIN * config.ZOOM |
69 | 59 |
77 | 60 if xpos >= 0 and xpos < config.GRAPH_WIDTH * config.ZOOM: |
69 | 61 click_time = orig_start \ |
77 | 62 + timedelta(minutes=(float(xpos) / (config.GRAPH_WIDTH * config.ZOOM)) * minutes) |
69 | 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) | |
27 | 81 |
103 | 82 @route('/debug') |
83 def debuglog(): | |
84 response.set_header('Content-Type', 'text/plain') | |
85 return log.tail_debug_log() | |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
86 |
27 | 87 def main(): |
61 | 88 #bottle.debug(True) |
89 #bottle.run(reloader=True) | |
90 bottle.run(server='cgi', reloader=True) | |
40 | 91 #bottle.run(port=9999, reloader=True) |
27 | 92 |
93 if __name__ == '__main__': | |
94 main() | |
95 | |
96 |