Mercurial > templog
annotate web/templog.py @ 367:3db118498b97
sort out voltage logging
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 26 Jun 2012 08:08:48 +0800 |
parents | 8fea6144951b |
children | a8ff20f15734 |
rev | line source |
---|---|
333 | 1 #!/usr/bin/env python2.7 |
2 | |
334 | 3 import binascii |
4 import hmac | |
335 | 5 import zlib |
337
f575ef538f5d
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
336
diff
changeset
|
6 import datetime |
f575ef538f5d
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
336
diff
changeset
|
7 import time |
334 | 8 |
333 | 9 import bottle |
337
f575ef538f5d
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
336
diff
changeset
|
10 from bottle import route, request, response |
333 | 11 |
334 | 12 import config |
13 import log | |
14 | |
333 | 15 @route('/update', method='post') |
16 def update(): | |
334 | 17 enc_lines = request.forms.lines |
18 mac = request.forms.hmac | |
19 | |
20 if hmac.new(config.HMAC_KEY, enc_lines).hexdigest() != mac: | |
21 raise HTTPError(code = 403, output = "Bad key") | |
22 | |
335 | 23 lines = zlib.decompress(binascii.a2b_base64(enc_lines)).split('\n') |
334 | 24 |
25 log.parse(lines) | |
26 | |
27 return "OK" | |
333 | 28 |
29 @route('/graph.png') | |
30 def graph(): | |
336
ba4c4df13487
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
31 # url takes time in hours or days |
ba4c4df13487
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
32 if 'day' in request.query: |
337
f575ef538f5d
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
336
diff
changeset
|
33 start_day = datetime.datetime.strptime(request.query.day, '%Y%m%d') |
336
ba4c4df13487
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
34 start = time.mktime(start_day.timetuple()) |
362 | 35 length = int(request.query.get('length', 5)) * 3600 * 24 |
336
ba4c4df13487
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
36 else: |
362 | 37 if 'hour' in request.query: |
38 start_hour = datetime.datetime.strptime(request.query.hour, '%Y%m%d%H') | |
39 else: | |
40 start_hour = datetime.datetime.now() - datetime.timedelta(days=1) | |
41 | |
336
ba4c4df13487
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
42 start = time.mktime(start_hour.timetuple()) |
367 | 43 length = int(request.query.get('length', 26)) * 3600 |
336
ba4c4df13487
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
44 |
ba4c4df13487
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
45 response.set_header('Content-Type', 'image/png') |
ba4c4df13487
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
46 return log.graph_png(start, length) |
333 | 47 |
48 @route('/') | |
49 def top(): | |
50 return bottle.template('top', urlparams=request.query_string) | |
51 | |
344
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
52 @route('/test') |
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
53 def test(): |
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
54 import config |
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
55 import os |
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
56 f = open('%s/testout' % config.DATA_PATH, 'a+') |
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
57 f.write("more") |
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
58 f.flush() |
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
59 f.close() |
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
60 |
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
61 return 'done' |
ea1779d27641
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
337
diff
changeset
|
62 |
333 | 63 def main(): |
367 | 64 #bottle.debug(True) |
65 #bottle.run(reloader=True) | |
66 bottle.run(server='cgi', reloader=True) | |
346 | 67 #bottle.run(port=9999, reloader=True) |
333 | 68 |
69 if __name__ == '__main__': | |
70 main() | |
71 | |
72 |