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