Mercurial > templog
annotate web/templog.py @ 81:4a2a82d6302c
try and be a bit more frugal with stack
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 11 Jul 2012 00:22:31 +0800 |
parents | 959e88c0bdfa |
children | 3cd1ca6d0489 |
rev | line source |
---|---|
27 | 1 #!/usr/bin/env python2.7 |
2 | |
28 | 3 import binascii |
4 import hmac | |
29 | 5 import zlib |
69 | 6 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
|
7 import time |
69 | 8 import urllib |
9 import sys | |
28 | 10 |
27 | 11 import bottle |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
30
diff
changeset
|
12 from bottle import route, request, response |
27 | 13 |
28 | 14 import config |
15 import log | |
16 | |
69 | 17 DATE_FORMAT = '%Y%m%d-%H.%M' |
18 ZOOM_SCALE = 2.0 | |
19 | |
27 | 20 @route('/update', method='post') |
21 def update(): | |
28 | 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 | |
29 | 28 lines = zlib.decompress(binascii.a2b_base64(enc_lines)).split('\n') |
28 | 29 |
30 log.parse(lines) | |
31 | |
32 return "OK" | |
27 | 33 |
34 @route('/graph.png') | |
35 def graph(): | |
69 | 36 length_minutes = int(request.query.length) |
37 end = datetime.strptime(request.query.end, DATE_FORMAT) | |
38 start = end - timedelta(minutes=length_minutes) | |
30
13fcf497f8b7
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
39 |
13fcf497f8b7
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
40 response.set_header('Content-Type', 'image/png') |
69 | 41 start_epoch = time.mktime(start.timetuple()) |
42 return log.graph_png(start_epoch, length_minutes * 60) | |
27 | 43 |
44 @route('/') | |
45 def top(): | |
69 | 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) | |
77 | 58 xpos -= config.GRAPH_LEFT_MARGIN * config.ZOOM |
69 | 59 |
77 | 60 if xpos >= 0 and xpos < config.GRAPH_WIDTH * config.ZOOM: |
69 | 61 click_time = orig_start \ |
77 | 62 + timedelta(minutes=(float(xpos) / (config.GRAPH_WIDTH * config.ZOOM)) * minutes) |
69 | 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) | |
27 | 81 |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
82 @route('/test') |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
83 def test(): |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
84 import config |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
85 import os |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
86 f = open('%s/testout' % config.DATA_PATH, 'a+') |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
87 f.write("more") |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
88 f.flush() |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
89 f.close() |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
90 |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
91 return 'done' |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
92 |
27 | 93 def main(): |
61 | 94 #bottle.debug(True) |
95 #bottle.run(reloader=True) | |
96 bottle.run(server='cgi', reloader=True) | |
40 | 97 #bottle.run(port=9999, reloader=True) |
27 | 98 |
99 if __name__ == '__main__': | |
100 main() | |
101 | |
102 |