Mercurial > templog
comparison web/templog.py @ 543:77dba104dcda
merge
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 21 May 2015 00:00:54 +0800 |
parents | 30628aa50a10 |
children | c490de0cf17e |
comparison
equal
deleted
inserted
replaced
537:92632a6d1dc6 | 543:77dba104dcda |
---|---|
9 import urllib | 9 import urllib |
10 import sys | 10 import sys |
11 import os | 11 import os |
12 import traceback | 12 import traceback |
13 import fcntl | 13 import fcntl |
14 import hashlib | |
14 | 15 |
15 import bottle | 16 import bottle |
16 from bottle import route, request, response | 17 from bottle import route, request, response |
17 | 18 |
18 import config | 19 import config |
21 import atomicfile | 22 import atomicfile |
22 | 23 |
23 DATE_FORMAT = '%Y%m%d-%H.%M' | 24 DATE_FORMAT = '%Y%m%d-%H.%M' |
24 ZOOM_SCALE = 2.0 | 25 ZOOM_SCALE = 2.0 |
25 | 26 |
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 | |
33 #bottle.default_app.push(TemplogBottle()) | |
34 | |
35 secure.setup_csrf() | |
36 | |
26 @route('/update', method='post') | 37 @route('/update', method='post') |
27 def update(): | 38 def update(): |
28 js_enc = request.forms.data | 39 js_enc = request.forms.data |
29 mac = request.forms.hmac | 40 mac = request.forms.hmac |
30 | 41 |
31 if hmac.new(config.HMAC_KEY, js_enc).hexdigest() != mac: | 42 h = hmac.new(config.HMAC_KEY, js_enc.strip(), hashlib.sha256).hexdigest() |
43 if h != mac: | |
32 raise bottle.HTTPError(code = 403, output = "Bad key") | 44 raise bottle.HTTPError(code = 403, output = "Bad key") |
33 | 45 |
34 js = zlib.decompress(binascii.a2b_base64(js_enc)) | 46 js = zlib.decompress(binascii.a2b_base64(js_enc)) |
35 | 47 |
36 params = json.loads(js) | 48 params = json.loads(js) |
37 | 49 |
38 log.parse(params) | 50 log.parse(params) |
39 | 51 |
40 return "OK" | 52 return "OK" |
41 | 53 |
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 | |
42 @route('/graph.png') | 66 @route('/graph.png') |
43 def graph(): | 67 def graph(): |
44 length_minutes = int(request.query.length) | |
45 end = datetime.strptime(request.query.end, DATE_FORMAT) | |
46 start = end - timedelta(minutes=length_minutes) | |
47 | |
48 response.set_header('Content-Type', 'image/png') | 68 response.set_header('Content-Type', 'image/png') |
49 start_epoch = time.mktime(start.timetuple()) | 69 return make_graph(request.query.length, request.query.end) |
50 return log.graph_png(start_epoch, length_minutes * 60) | |
51 | 70 |
52 @route('/set/update', method='post') | 71 @route('/set/update', method='post') |
53 def set_update(): | 72 def set_update(): |
54 post_json = json.loads(request.forms.data) | 73 post_json = json.loads(request.forms.data) |
55 | 74 |
72 response.set_header('Cache-Control', 'no-cache') | 91 response.set_header('Cache-Control', 'no-cache') |
73 return bottle.template('set', | 92 return bottle.template('set', |
74 inline_data = log.get_params(), | 93 inline_data = log.get_params(), |
75 csrf_blob = secure.get_csrf_blob(), | 94 csrf_blob = secure.get_csrf_blob(), |
76 allowed = allowed) | 95 allowed = allowed) |
77 | |
78 @route('/set_current.json') | |
79 def set_fresh(): | |
80 response.set_header('Content-Type', 'application/javascript') | |
81 return log.get_current() | |
82 | 96 |
83 @route('/') | 97 @route('/') |
84 def top(): | 98 def top(): |
85 | 99 |
86 minutes = int(request.query.get('length', 26*60)) | 100 minutes = int(request.query.get('length', 26*60)) |
112 | 126 |
113 request.query.replace('length', minutes) | 127 request.query.replace('length', minutes) |
114 request.query.replace('end', end.strftime(DATE_FORMAT)) | 128 request.query.replace('end', end.strftime(DATE_FORMAT)) |
115 | 129 |
116 urlparams = urllib.urlencode(request.query) | 130 urlparams = urllib.urlencode(request.query) |
131 graphdata = encode_data(make_graph(request.query.length, request.query.end), 'image/png') | |
117 return bottle.template('top', urlparams=urlparams, | 132 return bottle.template('top', urlparams=urlparams, |
118 end = end.strftime(DATE_FORMAT), | 133 end = end.strftime(DATE_FORMAT), |
119 length = minutes) | 134 length = minutes, |
135 graphwidth = config.GRAPH_WIDTH, | |
136 graphdata = graphdata) | |
120 | 137 |
121 @route('/debug') | 138 @route('/debug') |
122 def debuglog(): | 139 def debuglog(): |
123 response.set_header('Content-Type', 'text/plain') | 140 response.set_header('Content-Type', 'text/plain') |
124 return log.tail_debug_log() | 141 return log.tail_debug_log() |
136 @bottle.get('/<filename:re:.*\.js>') | 153 @bottle.get('/<filename:re:.*\.js>') |
137 def javascripts(filename): | 154 def javascripts(filename): |
138 response.set_header('Cache-Control', "public, max-age=1296000") | 155 response.set_header('Cache-Control', "public, max-age=1296000") |
139 return bottle.static_file(filename, root='static') | 156 return bottle.static_file(filename, root='static') |
140 | 157 |
141 secure.setup_csrf() | |
142 | |
143 def main(): | 158 def main(): |
144 #bottle.debug(True) | 159 #bottle.debug(True) |
145 #bottle.run(reloader=True) | 160 #bottle.run(reloader=True) |
146 bottle.run(server='cgi', reloader=True) | 161 bottle.run(server='cgi', reloader=True) |
147 #bottle.run(port=9999, reloader=True) | 162 #bottle.run(port=9999, reloader=True) |