Mercurial > templog
annotate web/templog.py @ 383:a032051475fe
ui tweaks
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 04 Jul 2012 22:36:52 +0800 |
parents | f22427bcfda8 |
children | 3cd1ca6d0489 |
rev | line source |
---|---|
333 | 1 #!/usr/bin/env python2.7 |
2 | |
334 | 3 import binascii |
4 import hmac | |
335 | 5 import zlib |
375 | 6 from datetime import datetime, timedelta |
337
f575ef538f5d
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
336
diff
changeset
|
7 import time |
375 | 8 import urllib |
9 import sys | |
334 | 10 |
333 | 11 import bottle |
337
f575ef538f5d
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
336
diff
changeset
|
12 from bottle import route, request, response |
333 | 13 |
334 | 14 import config |
15 import log | |
16 | |
375 | 17 DATE_FORMAT = '%Y%m%d-%H.%M' |
18 ZOOM_SCALE = 2.0 | |
19 | |
333 | 20 @route('/update', method='post') |
21 def update(): | |
334 | 22 enc_lines = request.forms.lines |
23 mac = request.forms.hmac | |
24 | |
25 if hmac.new(config.HMAC_KEY, enc_lines).hexdigest() != mac: | |
26 raise HTTPError(code = 403, output = "Bad key") | |
27 | |
335 | 28 lines = zlib.decompress(binascii.a2b_base64(enc_lines)).split('\n') |
334 | 29 |
30 log.parse(lines) | |
31 | |
32 return "OK" | |
333 | 33 |
34 @route('/graph.png') | |
35 def graph(): | |
375 | 36 length_minutes = int(request.query.length) |
37 end = datetime.strptime(request.query.end, DATE_FORMAT) | |
38 start = end - timedelta(minutes=length_minutes) | |
336
ba4c4df13487
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
39 |
ba4c4df13487
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
40 response.set_header('Content-Type', 'image/png') |
375 | 41 start_epoch = time.mktime(start.timetuple()) |
42 return log.graph_png(start_epoch, length_minutes * 60) | |
333 | 43 |
44 @route('/') | |
45 def top(): | |
375 | 46 |
47 minutes = int(request.query.get('length', 26*60)) | |
48 | |
49 if 'end' in request.query: | |
50 end = datetime.strptime(request.query.end, DATE_FORMAT) | |
51 else: | |
52 end = datetime.now() | |
53 | |
54 if 'zoom' in request.query: | |
55 orig_start = end - timedelta(minutes=minutes) | |
56 orig_end = end | |
57 xpos = int(request.query.x) | |
383 | 58 xpos -= config.GRAPH_LEFT_MARGIN * config.ZOOM |
375 | 59 |
383 | 60 if xpos >= 0 and xpos < config.GRAPH_WIDTH * config.ZOOM: |
375 | 61 click_time = orig_start \ |
383 | 62 + timedelta(minutes=(float(xpos) / (config.GRAPH_WIDTH * config.ZOOM)) * minutes) |
375 | 63 minutes = int(minutes / ZOOM_SCALE) |
64 | |
65 end = click_time + timedelta(minutes=minutes/2) | |
66 else: | |
67 # zoom out | |
68 minutes = int(minutes*ZOOM_SCALE) | |
69 end += timedelta(minutes=minutes/2) | |
70 | |
71 if end > datetime.now(): | |
72 end = datetime.now() | |
73 | |
74 request.query.replace('length', minutes) | |
75 request.query.replace('end', end.strftime(DATE_FORMAT)) | |
76 | |
77 urlparams = urllib.urlencode(request.query) | |
78 return bottle.template('top', urlparams=urlparams, | |
79 end = end.strftime(DATE_FORMAT), | |
80 length = minutes) | |
333 | 81 |
344
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
82 @route('/test') |
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
83 def test(): |
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
84 import config |
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
85 import os |
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
86 f = open('%s/testout' % config.DATA_PATH, 'a+') |
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
87 f.write("more") |
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
88 f.flush() |
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
89 f.close() |
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
90 |
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
91 return 'done' |
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
92 |
333 | 93 def main(): |
367 | 94 #bottle.debug(True) |
95 #bottle.run(reloader=True) | |
96 bottle.run(server='cgi', reloader=True) | |
346 | 97 #bottle.run(port=9999, reloader=True) |
333 | 98 |
99 if __name__ == '__main__': | |
100 main() | |
101 | |
102 |