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