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