Mercurial > templog
annotate web/templog.py @ 252:3e6f82347eab
uwsgi bits
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 06 Jun 2015 15:34:03 +0800 |
parents | 141948a400a6 |
children | 8ef52f27cf95 |
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') |
250 | 69 minutes, endstr = get_request_zoom() |
70 return make_graph(minutes, endstr) | |
27 | 71 |
188 | 72 @route('/set/update', method='post') |
194 | 73 def set_update(): |
188 | 74 post_json = json.loads(request.forms.data) |
75 | |
76 csrf_blob = post_json['csrf_blob'] | |
77 | |
194 | 78 if not secure.check_csrf_blob(csrf_blob): |
79 bottle.response.status = 403 | |
80 return "Bad csrf" | |
81 | |
82 ret = log.update_params(post_json['params']) | |
83 if not ret is True: | |
84 bottle.response.status = 403 | |
85 return ret | |
86 | |
87 return "Good" | |
188 | 88 |
182 | 89 @route('/set') |
90 def set(): | |
211
59379b2bd056
key fingerprints are case- and whitespace-insensitive.
Matt Johnston <matt@ucc.asn.au>
parents:
202
diff
changeset
|
91 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
|
92 response.set_header('Cache-Control', 'no-cache') |
185 | 93 return bottle.template('set', |
94 inline_data = log.get_params(), | |
189 | 95 csrf_blob = secure.get_csrf_blob(), |
96 allowed = allowed) | |
182 | 97 |
250 | 98 def get_request_zoom(): |
99 """ returns (length, end) tuple. | |
100 length is in minutes, end is a DATE_FORMAT string """ | |
69 | 101 minutes = int(request.query.get('length', 26*60)) |
102 | |
103 if 'end' in request.query: | |
104 end = datetime.strptime(request.query.end, DATE_FORMAT) | |
105 else: | |
106 end = datetime.now() | |
107 | |
108 if 'zoom' in request.query: | |
109 orig_start = end - timedelta(minutes=minutes) | |
110 orig_end = end | |
249 | 111 scale = float(request.query.scaledwidth) / config.GRAPH_WIDTH |
112 xpos = int(request.query.x) / scale | |
77 | 113 xpos -= config.GRAPH_LEFT_MARGIN * config.ZOOM |
69 | 114 |
77 | 115 if xpos >= 0 and xpos < config.GRAPH_WIDTH * config.ZOOM: |
69 | 116 click_time = orig_start \ |
77 | 117 + timedelta(minutes=(float(xpos) / (config.GRAPH_WIDTH * config.ZOOM)) * minutes) |
69 | 118 minutes = int(minutes / ZOOM_SCALE) |
119 | |
120 end = click_time + timedelta(minutes=minutes/2) | |
121 else: | |
122 # zoom out | |
123 minutes = int(minutes*ZOOM_SCALE) | |
124 end += timedelta(minutes=minutes/2) | |
125 | |
126 if end > datetime.now(): | |
127 end = datetime.now() | |
250 | 128 |
129 endstr = end.strftime(DATE_FORMAT) | |
130 return (minutes, endstr) | |
131 | |
132 @route('/') | |
133 def top(): | |
134 minutes, endstr = get_request_zoom() | |
135 | |
69 | 136 request.query.replace('length', minutes) |
250 | 137 request.query.replace('end', endstr) |
69 | 138 |
139 urlparams = urllib.urlencode(request.query) | |
250 | 140 graphdata = encode_data(make_graph(minutes, endstr), 'image/png') |
69 | 141 return bottle.template('top', urlparams=urlparams, |
250 | 142 end = endstr, |
244 | 143 length = minutes, |
144 graphwidth = config.GRAPH_WIDTH, | |
145 graphdata = graphdata) | |
27 | 146 |
103 | 147 @route('/debug') |
148 def debuglog(): | |
149 response.set_header('Content-Type', 'text/plain') | |
150 return log.tail_debug_log() | |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
151 |
182 | 152 @route('/env') |
153 def env(): | |
154 response.set_header('Content-Type', 'text/plain') | |
189 | 155 #return '\n'.join(traceback.format_stack()) |
156 return '\n'.join(("%s %s" % k) for k in request.environ.items()) | |
182 | 157 #return str(request.environ) |
158 #yield "\n" | |
159 #var_lookup = environ['mod_ssl.var_lookup'] | |
160 #return var_lookup("SSL_SERVER_I_DN_O") | |
161 | |
252 | 162 @route('/wait') |
163 def wait(): | |
164 response.set_header('Content-Type', 'text/plain') | |
165 yield 'done' | |
166 | |
182 | 167 @bottle.get('/<filename:re:.*\.js>') |
168 def javascripts(filename): | |
185 | 169 response.set_header('Cache-Control', "public, max-age=1296000") |
182 | 170 return bottle.static_file(filename, root='static') |
171 | |
27 | 172 def main(): |
252 | 173 """ for standalone testing """ |
61 | 174 #bottle.debug(True) |
175 #bottle.run(reloader=True) | |
176 bottle.run(server='cgi', reloader=True) | |
40 | 177 #bottle.run(port=9999, reloader=True) |
27 | 178 |
179 if __name__ == '__main__': | |
180 main() | |
181 |