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