Mercurial > templog
annotate web/templog.py @ 188:ae5efca89001
working OK
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 07 Feb 2014 23:32:08 +0800 |
parents | adbf70d1449f |
children | 101c66da848d |
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 | |
28 | 14 |
27 | 15 import bottle |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
30
diff
changeset
|
16 from bottle import route, request, response |
27 | 17 |
28 | 18 import config |
19 import log | |
185 | 20 import secure |
28 | 21 |
69 | 22 DATE_FORMAT = '%Y%m%d-%H.%M' |
23 ZOOM_SCALE = 2.0 | |
24 | |
27 | 25 @route('/update', method='post') |
26 def update(): | |
146
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
27 js_enc = request.forms.data |
28 | 28 mac = request.forms.hmac |
29 | |
146
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
30 if hmac.new(config.HMAC_KEY, js_enc).hexdigest() != mac: |
85 | 31 raise bottle.HTTPError(code = 403, output = "Bad key") |
28 | 32 |
146
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
33 js = zlib.decompress(binascii.a2b_base64(js_enc)) |
28 | 34 |
146
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
35 params = json.loads(js) |
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
36 |
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
37 log.parse(params) |
28 | 38 |
39 return "OK" | |
27 | 40 |
41 @route('/graph.png') | |
42 def graph(): | |
69 | 43 length_minutes = int(request.query.length) |
44 end = datetime.strptime(request.query.end, DATE_FORMAT) | |
45 start = end - timedelta(minutes=length_minutes) | |
30
13fcf497f8b7
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
46 |
13fcf497f8b7
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
47 response.set_header('Content-Type', 'image/png') |
69 | 48 start_epoch = time.mktime(start.timetuple()) |
49 return log.graph_png(start_epoch, length_minutes * 60) | |
27 | 50 |
188 | 51 @route('/set/update', method='post') |
52 def update(): | |
53 post_json = json.loads(request.forms.data) | |
54 | |
55 csrf_blob = post_json['csrf_blob'] | |
56 | |
57 return str(post_json['params']) | |
58 | |
182 | 59 @route('/set') |
60 def set(): | |
185 | 61 return bottle.template('set', |
62 inline_data = log.get_params(), | |
63 csrf_blob = secure.get_csrf_blob()) | |
182 | 64 |
65 @route('/set_current.json') | |
66 def set_fresh(): | |
67 response.set_header('Content-Type', 'application/javascript') | |
68 return log.get_current() | |
69 | |
27 | 70 @route('/') |
71 def top(): | |
69 | 72 |
73 minutes = int(request.query.get('length', 26*60)) | |
74 | |
75 if 'end' in request.query: | |
76 end = datetime.strptime(request.query.end, DATE_FORMAT) | |
77 else: | |
78 end = datetime.now() | |
79 | |
80 if 'zoom' in request.query: | |
81 orig_start = end - timedelta(minutes=minutes) | |
82 orig_end = end | |
83 xpos = int(request.query.x) | |
77 | 84 xpos -= config.GRAPH_LEFT_MARGIN * config.ZOOM |
69 | 85 |
77 | 86 if xpos >= 0 and xpos < config.GRAPH_WIDTH * config.ZOOM: |
69 | 87 click_time = orig_start \ |
77 | 88 + timedelta(minutes=(float(xpos) / (config.GRAPH_WIDTH * config.ZOOM)) * minutes) |
69 | 89 minutes = int(minutes / ZOOM_SCALE) |
90 | |
91 end = click_time + timedelta(minutes=minutes/2) | |
92 else: | |
93 # zoom out | |
94 minutes = int(minutes*ZOOM_SCALE) | |
95 end += timedelta(minutes=minutes/2) | |
96 | |
97 if end > datetime.now(): | |
98 end = datetime.now() | |
99 | |
100 request.query.replace('length', minutes) | |
101 request.query.replace('end', end.strftime(DATE_FORMAT)) | |
102 | |
103 urlparams = urllib.urlencode(request.query) | |
104 return bottle.template('top', urlparams=urlparams, | |
105 end = end.strftime(DATE_FORMAT), | |
106 length = minutes) | |
27 | 107 |
103 | 108 @route('/debug') |
109 def debuglog(): | |
110 response.set_header('Content-Type', 'text/plain') | |
111 return log.tail_debug_log() | |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
112 |
182 | 113 @route('/env') |
114 def env(): | |
115 response.set_header('Content-Type', 'text/plain') | |
185 | 116 return '\n'.join(traceback.format_stack()) |
117 #return '\n'.join(("%s %s" % k) for k in request.environ.items()) | |
182 | 118 #return str(request.environ) |
119 #yield "\n" | |
120 #var_lookup = environ['mod_ssl.var_lookup'] | |
121 #return var_lookup("SSL_SERVER_I_DN_O") | |
122 | |
123 @bottle.get('/<filename:re:.*\.js>') | |
124 def javascripts(filename): | |
185 | 125 response.set_header('Cache-Control', "public, max-age=1296000") |
182 | 126 return bottle.static_file(filename, root='static') |
127 | |
185 | 128 secure.setup_csrf() |
182 | 129 |
27 | 130 def main(): |
61 | 131 #bottle.debug(True) |
132 #bottle.run(reloader=True) | |
133 bottle.run(server='cgi', reloader=True) | |
40 | 134 #bottle.run(port=9999, reloader=True) |
27 | 135 |
136 if __name__ == '__main__': | |
137 main() | |
138 | |
139 |