Mercurial > templog
annotate web/templog.py @ 146:3b4277aaed3c
update web to handle new style params
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 05 Dec 2012 22:44:13 +0800 |
parents | ef59da811986 |
children | e731c0d30b09 |
rev | line source |
---|---|
27 | 1 #!/usr/bin/env python2.7 |
2 | |
28 | 3 import binascii |
146
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
4 import json |
28 | 5 import hmac |
29 | 6 import zlib |
69 | 7 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
|
8 import time |
69 | 9 import urllib |
10 import sys | |
28 | 11 |
27 | 12 import bottle |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
30
diff
changeset
|
13 from bottle import route, request, response |
27 | 14 |
28 | 15 import config |
16 import log | |
17 | |
69 | 18 DATE_FORMAT = '%Y%m%d-%H.%M' |
19 ZOOM_SCALE = 2.0 | |
20 | |
27 | 21 @route('/update', method='post') |
22 def update(): | |
146
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
23 js_enc = request.forms.data |
28 | 24 mac = request.forms.hmac |
25 | |
146
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
26 if hmac.new(config.HMAC_KEY, js_enc).hexdigest() != mac: |
85 | 27 raise bottle.HTTPError(code = 403, output = "Bad key") |
28 | 28 |
146
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
29 js = zlib.decompress(binascii.a2b_base64(js_enc)) |
28 | 30 |
146
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
31 params = json.loads(js) |
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
32 |
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
33 log.parse(params) |
28 | 34 |
35 return "OK" | |
27 | 36 |
37 @route('/graph.png') | |
38 def graph(): | |
69 | 39 length_minutes = int(request.query.length) |
40 end = datetime.strptime(request.query.end, DATE_FORMAT) | |
41 start = end - timedelta(minutes=length_minutes) | |
30
13fcf497f8b7
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
42 |
13fcf497f8b7
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
43 response.set_header('Content-Type', 'image/png') |
69 | 44 start_epoch = time.mktime(start.timetuple()) |
45 return log.graph_png(start_epoch, length_minutes * 60) | |
27 | 46 |
47 @route('/') | |
48 def top(): | |
69 | 49 |
50 minutes = int(request.query.get('length', 26*60)) | |
51 | |
52 if 'end' in request.query: | |
53 end = datetime.strptime(request.query.end, DATE_FORMAT) | |
54 else: | |
55 end = datetime.now() | |
56 | |
57 if 'zoom' in request.query: | |
58 orig_start = end - timedelta(minutes=minutes) | |
59 orig_end = end | |
60 xpos = int(request.query.x) | |
77 | 61 xpos -= config.GRAPH_LEFT_MARGIN * config.ZOOM |
69 | 62 |
77 | 63 if xpos >= 0 and xpos < config.GRAPH_WIDTH * config.ZOOM: |
69 | 64 click_time = orig_start \ |
77 | 65 + timedelta(minutes=(float(xpos) / (config.GRAPH_WIDTH * config.ZOOM)) * minutes) |
69 | 66 minutes = int(minutes / ZOOM_SCALE) |
67 | |
68 end = click_time + timedelta(minutes=minutes/2) | |
69 else: | |
70 # zoom out | |
71 minutes = int(minutes*ZOOM_SCALE) | |
72 end += timedelta(minutes=minutes/2) | |
73 | |
74 if end > datetime.now(): | |
75 end = datetime.now() | |
76 | |
77 request.query.replace('length', minutes) | |
78 request.query.replace('end', end.strftime(DATE_FORMAT)) | |
79 | |
80 urlparams = urllib.urlencode(request.query) | |
81 return bottle.template('top', urlparams=urlparams, | |
82 end = end.strftime(DATE_FORMAT), | |
83 length = minutes) | |
27 | 84 |
103 | 85 @route('/debug') |
86 def debuglog(): | |
87 response.set_header('Content-Type', 'text/plain') | |
88 return log.tail_debug_log() | |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
89 |
27 | 90 def main(): |
61 | 91 #bottle.debug(True) |
92 #bottle.run(reloader=True) | |
93 bottle.run(server='cgi', reloader=True) | |
40 | 94 #bottle.run(port=9999, reloader=True) |
27 | 95 |
96 if __name__ == '__main__': | |
97 main() | |
98 | |
99 |