Mercurial > templog
annotate web/templog.py @ 249:c490de0cf17e
scaled zooming works
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 27 May 2015 22:37:43 +0800 |
parents | 2071d939e4ff |
children | 141948a400a6 |
rev | line source |
---|---|
27 | 1 #!/usr/bin/env python2.7 |
2 | |
28 | 3 import binascii |
146
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
4 import json |
28 | 5 import hmac |
29 | 6 import zlib |
69 | 7 from datetime import datetime, timedelta |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
30
diff
changeset
|
8 import time |
69 | 9 import urllib |
10 import sys | |
182 | 11 import os |
185 | 12 import traceback |
13 import fcntl | |
240 | 14 import hashlib |
28 | 15 |
27 | 16 import bottle |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
30
diff
changeset
|
17 from bottle import route, request, response |
27 | 18 |
28 | 19 import config |
20 import log | |
185 | 21 import secure |
191 | 22 import atomicfile |
28 | 23 |
69 | 24 DATE_FORMAT = '%Y%m%d-%H.%M' |
25 ZOOM_SCALE = 2.0 | |
26 | |
237 | 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 | |
238
509a1be16456
gevent doesn't work well with subprocess
Matt Johnston <matt@ucc.asn.au>
parents:
237
diff
changeset
|
33 #bottle.default_app.push(TemplogBottle()) |
237 | 34 |
35 secure.setup_csrf() | |
36 | |
27 | 37 @route('/update', method='post') |
38 def update(): | |
146
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
39 js_enc = request.forms.data |
28 | 40 mac = request.forms.hmac |
41 | |
240 | 42 h = hmac.new(config.HMAC_KEY, js_enc.strip(), hashlib.sha256).hexdigest() |
43 if h != mac: | |
85 | 44 raise bottle.HTTPError(code = 403, output = "Bad key") |
28 | 45 |
146
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
46 js = zlib.decompress(binascii.a2b_base64(js_enc)) |
28 | 47 |
146
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
48 params = json.loads(js) |
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
49 |
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
50 log.parse(params) |
28 | 51 |
52 return "OK" | |
27 | 53 |
244 | 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 | |
27 | 66 @route('/graph.png') |
67 def graph(): | |
30
13fcf497f8b7
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
68 response.set_header('Content-Type', 'image/png') |
244 | 69 return make_graph(request.query.length, request.query.end) |
27 | 70 |
188 | 71 @route('/set/update', method='post') |
194 | 72 def set_update(): |
188 | 73 post_json = json.loads(request.forms.data) |
74 | |
75 csrf_blob = post_json['csrf_blob'] | |
76 | |
194 | 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" | |
188 | 87 |
182 | 88 @route('/set') |
89 def set(): | |
211
59379b2bd056
key fingerprints are case- and whitespace-insensitive.
Matt Johnston <matt@ucc.asn.au>
parents:
202
diff
changeset
|
90 allowed = ["false", "true"][secure.check_user_hash(config.ALLOWED_USERS)] |
202
6dd157a12035
Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents:
194
diff
changeset
|
91 response.set_header('Cache-Control', 'no-cache') |
185 | 92 return bottle.template('set', |
93 inline_data = log.get_params(), | |
189 | 94 csrf_blob = secure.get_csrf_blob(), |
95 allowed = allowed) | |
182 | 96 |
27 | 97 @route('/') |
98 def top(): | |
69 | 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 | |
249 | 110 scale = float(request.query.scaledwidth) / config.GRAPH_WIDTH |
111 xpos = int(request.query.x) / scale | |
77 | 112 xpos -= config.GRAPH_LEFT_MARGIN * config.ZOOM |
69 | 113 |
77 | 114 if xpos >= 0 and xpos < config.GRAPH_WIDTH * config.ZOOM: |
69 | 115 click_time = orig_start \ |
77 | 116 + timedelta(minutes=(float(xpos) / (config.GRAPH_WIDTH * config.ZOOM)) * minutes) |
69 | 117 minutes = int(minutes / ZOOM_SCALE) |
118 | |
119 end = click_time + timedelta(minutes=minutes/2) | |
120 else: | |
121 # zoom out | |
122 minutes = int(minutes*ZOOM_SCALE) | |
123 end += timedelta(minutes=minutes/2) | |
124 | |
125 if end > datetime.now(): | |
126 end = datetime.now() | |
127 | |
128 request.query.replace('length', minutes) | |
129 request.query.replace('end', end.strftime(DATE_FORMAT)) | |
130 | |
131 urlparams = urllib.urlencode(request.query) | |
244 | 132 graphdata = encode_data(make_graph(request.query.length, request.query.end), 'image/png') |
69 | 133 return bottle.template('top', urlparams=urlparams, |
134 end = end.strftime(DATE_FORMAT), | |
244 | 135 length = minutes, |
136 graphwidth = config.GRAPH_WIDTH, | |
137 graphdata = graphdata) | |
27 | 138 |
103 | 139 @route('/debug') |
140 def debuglog(): | |
141 response.set_header('Content-Type', 'text/plain') | |
142 return log.tail_debug_log() | |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
143 |
182 | 144 @route('/env') |
145 def env(): | |
146 response.set_header('Content-Type', 'text/plain') | |
189 | 147 #return '\n'.join(traceback.format_stack()) |
148 return '\n'.join(("%s %s" % k) for k in request.environ.items()) | |
182 | 149 #return str(request.environ) |
150 #yield "\n" | |
151 #var_lookup = environ['mod_ssl.var_lookup'] | |
152 #return var_lookup("SSL_SERVER_I_DN_O") | |
153 | |
154 @bottle.get('/<filename:re:.*\.js>') | |
155 def javascripts(filename): | |
185 | 156 response.set_header('Cache-Control', "public, max-age=1296000") |
182 | 157 return bottle.static_file(filename, root='static') |
158 | |
27 | 159 def main(): |
61 | 160 #bottle.debug(True) |
161 #bottle.run(reloader=True) | |
162 bottle.run(server='cgi', reloader=True) | |
40 | 163 #bottle.run(port=9999, reloader=True) |
27 | 164 |
165 if __name__ == '__main__': | |
166 main() | |
167 |