Mercurial > templog
annotate web/templog.py @ 485:d68af9e84485
working
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 06 Feb 2014 22:45:16 +0800 |
parents | 9950ffa9a79b |
children | ae5efca89001 |
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 | |
482 | 11 import os |
485 | 12 import traceback |
13 import fcntl | |
334 | 14 |
333 | 15 import bottle |
337
f575ef538f5d
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
336
diff
changeset
|
16 from bottle import route, request, response |
333 | 17 |
334 | 18 import config |
19 import log | |
485 | 20 import secure |
334 | 21 |
375 | 22 DATE_FORMAT = '%Y%m%d-%H.%M' |
23 ZOOM_SCALE = 2.0 | |
24 | |
333 | 25 @route('/update', method='post') |
26 def update(): | |
445
5b9dc87c988f
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
409
diff
changeset
|
27 js_enc = request.forms.data |
334 | 28 mac = request.forms.hmac |
29 | |
445
5b9dc87c988f
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
409
diff
changeset
|
30 if hmac.new(config.HMAC_KEY, js_enc).hexdigest() != mac: |
391 | 31 raise bottle.HTTPError(code = 403, output = "Bad key") |
334 | 32 |
445
5b9dc87c988f
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
409
diff
changeset
|
33 js = zlib.decompress(binascii.a2b_base64(js_enc)) |
334 | 34 |
445
5b9dc87c988f
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
409
diff
changeset
|
35 params = json.loads(js) |
5b9dc87c988f
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
409
diff
changeset
|
36 |
5b9dc87c988f
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
409
diff
changeset
|
37 log.parse(params) |
334 | 38 |
39 return "OK" | |
333 | 40 |
41 @route('/graph.png') | |
42 def graph(): | |
375 | 43 length_minutes = int(request.query.length) |
44 end = datetime.strptime(request.query.end, DATE_FORMAT) | |
45 start = end - timedelta(minutes=length_minutes) | |
336
ba4c4df13487
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
46 |
ba4c4df13487
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
47 response.set_header('Content-Type', 'image/png') |
375 | 48 start_epoch = time.mktime(start.timetuple()) |
49 return log.graph_png(start_epoch, length_minutes * 60) | |
333 | 50 |
482 | 51 @route('/set') |
52 def set(): | |
485 | 53 return bottle.template('set', |
54 inline_data = log.get_params(), | |
55 csrf_blob = secure.get_csrf_blob()) | |
482 | 56 |
57 @route('/set_current.json') | |
58 def set_fresh(): | |
59 response.set_header('Content-Type', 'application/javascript') | |
60 return log.get_current() | |
61 | |
333 | 62 @route('/') |
63 def top(): | |
375 | 64 |
65 minutes = int(request.query.get('length', 26*60)) | |
66 | |
67 if 'end' in request.query: | |
68 end = datetime.strptime(request.query.end, DATE_FORMAT) | |
69 else: | |
70 end = datetime.now() | |
71 | |
72 if 'zoom' in request.query: | |
73 orig_start = end - timedelta(minutes=minutes) | |
74 orig_end = end | |
75 xpos = int(request.query.x) | |
383 | 76 xpos -= config.GRAPH_LEFT_MARGIN * config.ZOOM |
375 | 77 |
383 | 78 if xpos >= 0 and xpos < config.GRAPH_WIDTH * config.ZOOM: |
375 | 79 click_time = orig_start \ |
383 | 80 + timedelta(minutes=(float(xpos) / (config.GRAPH_WIDTH * config.ZOOM)) * minutes) |
375 | 81 minutes = int(minutes / ZOOM_SCALE) |
82 | |
83 end = click_time + timedelta(minutes=minutes/2) | |
84 else: | |
85 # zoom out | |
86 minutes = int(minutes*ZOOM_SCALE) | |
87 end += timedelta(minutes=minutes/2) | |
88 | |
89 if end > datetime.now(): | |
90 end = datetime.now() | |
91 | |
92 request.query.replace('length', minutes) | |
93 request.query.replace('end', end.strftime(DATE_FORMAT)) | |
94 | |
95 urlparams = urllib.urlencode(request.query) | |
96 return bottle.template('top', urlparams=urlparams, | |
97 end = end.strftime(DATE_FORMAT), | |
98 length = minutes) | |
333 | 99 |
409 | 100 @route('/debug') |
101 def debuglog(): | |
102 response.set_header('Content-Type', 'text/plain') | |
103 return log.tail_debug_log() | |
344
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
104 |
482 | 105 @route('/env') |
106 def env(): | |
107 response.set_header('Content-Type', 'text/plain') | |
485 | 108 return '\n'.join(traceback.format_stack()) |
109 #return '\n'.join(("%s %s" % k) for k in request.environ.items()) | |
482 | 110 #return str(request.environ) |
111 #yield "\n" | |
112 #var_lookup = environ['mod_ssl.var_lookup'] | |
113 #return var_lookup("SSL_SERVER_I_DN_O") | |
114 | |
115 @bottle.get('/<filename:re:.*\.js>') | |
116 def javascripts(filename): | |
485 | 117 response.set_header('Cache-Control', "public, max-age=1296000") |
482 | 118 return bottle.static_file(filename, root='static') |
119 | |
485 | 120 @route('/setparams', method='post') |
121 def update(): | |
122 post_json = json.loads(request.forms.data) | |
123 | |
124 csrf_blob = post_json['csrf_blob'] | |
125 | |
126 return str(post_json['params']) | |
127 | |
128 secure.setup_csrf() | |
482 | 129 |
333 | 130 def main(): |
367 | 131 #bottle.debug(True) |
132 #bottle.run(reloader=True) | |
133 bottle.run(server='cgi', reloader=True) | |
346 | 134 #bottle.run(port=9999, reloader=True) |
333 | 135 |
136 if __name__ == '__main__': | |
137 main() | |
138 | |
139 |