Mercurial > templog
annotate web/templog.py @ 480:6d464ac4ba3c
prototype
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 03 Feb 2014 00:08:59 +0800 |
parents | 5b9dc87c988f |
children | e731c0d30b09 |
rev | line source |
---|---|
333 | 1 #!/usr/bin/env python2.7 |
2 | |
334 | 3 import binascii |
445
5b9dc87c988f
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
409
diff
changeset
|
4 import json |
334 | 5 import hmac |
335 | 6 import zlib |
375 | 7 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
|
8 import time |
375 | 9 import urllib |
10 import sys | |
334 | 11 |
333 | 12 import bottle |
337
f575ef538f5d
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
336
diff
changeset
|
13 from bottle import route, request, response |
333 | 14 |
334 | 15 import config |
16 import log | |
17 | |
375 | 18 DATE_FORMAT = '%Y%m%d-%H.%M' |
19 ZOOM_SCALE = 2.0 | |
20 | |
333 | 21 @route('/update', method='post') |
22 def update(): | |
445
5b9dc87c988f
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
409
diff
changeset
|
23 js_enc = request.forms.data |
334 | 24 mac = request.forms.hmac |
25 | |
445
5b9dc87c988f
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
409
diff
changeset
|
26 if hmac.new(config.HMAC_KEY, js_enc).hexdigest() != mac: |
391 | 27 raise bottle.HTTPError(code = 403, output = "Bad key") |
334 | 28 |
445
5b9dc87c988f
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
409
diff
changeset
|
29 js = zlib.decompress(binascii.a2b_base64(js_enc)) |
334 | 30 |
445
5b9dc87c988f
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
409
diff
changeset
|
31 params = json.loads(js) |
5b9dc87c988f
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
409
diff
changeset
|
32 |
5b9dc87c988f
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
409
diff
changeset
|
33 log.parse(params) |
334 | 34 |
35 return "OK" | |
333 | 36 |
37 @route('/graph.png') | |
38 def graph(): | |
375 | 39 length_minutes = int(request.query.length) |
40 end = datetime.strptime(request.query.end, DATE_FORMAT) | |
41 start = end - timedelta(minutes=length_minutes) | |
336
ba4c4df13487
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
42 |
ba4c4df13487
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
43 response.set_header('Content-Type', 'image/png') |
375 | 44 start_epoch = time.mktime(start.timetuple()) |
45 return log.graph_png(start_epoch, length_minutes * 60) | |
333 | 46 |
47 @route('/') | |
48 def top(): | |
375 | 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) | |
383 | 61 xpos -= config.GRAPH_LEFT_MARGIN * config.ZOOM |
375 | 62 |
383 | 63 if xpos >= 0 and xpos < config.GRAPH_WIDTH * config.ZOOM: |
375 | 64 click_time = orig_start \ |
383 | 65 + timedelta(minutes=(float(xpos) / (config.GRAPH_WIDTH * config.ZOOM)) * minutes) |
375 | 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) | |
333 | 84 |
409 | 85 @route('/debug') |
86 def debuglog(): | |
87 response.set_header('Content-Type', 'text/plain') | |
88 return log.tail_debug_log() | |
344
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
89 |
333 | 90 def main(): |
367 | 91 #bottle.debug(True) |
92 #bottle.run(reloader=True) | |
93 bottle.run(server='cgi', reloader=True) | |
346 | 94 #bottle.run(port=9999, reloader=True) |
333 | 95 |
96 if __name__ == '__main__': | |
97 main() | |
98 | |
99 |