annotate web/templog.py @ 352:99f8b97a9449

Use the PRR etc, set value to proper ones
author Matt Johnston <matt@ucc.asn.au>
date Sun, 24 Jun 2012 00:44:28 +0800
parents d6219df77c41
children 79761ee67134
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 #!/usr/bin/env python2.7
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
3 import binascii
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
4 import hmac
335
1e22eaf93620 work on web interface
Matt Johnston <matt@ucc.asn.au>
parents: 334
diff changeset
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
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
8
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
12 import config
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
13 import log
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
14
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 @route('/update', method='post')
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 def update():
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
17 enc_lines = request.forms.lines
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
18 mac = request.forms.hmac
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
19
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
20 if hmac.new(config.HMAC_KEY, enc_lines).hexdigest() != mac:
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
21 raise HTTPError(code = 403, output = "Bad key")
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
22
335
1e22eaf93620 work on web interface
Matt Johnston <matt@ucc.asn.au>
parents: 334
diff changeset
23 lines = zlib.decompress(binascii.a2b_base64(enc_lines)).split('\n')
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
24
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
25 log.parse(lines)
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
26
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
27 return "OK"
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 @route('/graph.png')
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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())
ba4c4df13487 parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
35 length = int(request.query.length) * 3600 * 24
ba4c4df13487 parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
36 else:
337
f575ef538f5d - Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents: 336
diff changeset
37 start_hour = datetime.datetime.strptime(request.query.hour, '%Y%m%d%H')
336
ba4c4df13487 parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
38 start = time.mktime(start_hour.timetuple())
ba4c4df13487 parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
39 length = int(request.query.length) * 3600
ba4c4df13487 parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
40
ba4c4df13487 parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
41 response.set_header('Content-Type', 'image/png')
ba4c4df13487 parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
42 return log.graph_png(start, length)
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 @route('/')
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 def top():
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 return bottle.template('top', urlparams=request.query_string)
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47
344
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
48 @route('/test')
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
49 def test():
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
50 import config
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
51 import os
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
52 f = open('%s/testout' % config.DATA_PATH, 'a+')
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
53 f.write("more")
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
54 f.flush()
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
55 f.close()
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
56
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
57 return 'done'
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
58
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 def main():
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 bottle.debug(True)
346
d6219df77c41 main.c:
Matt Johnston <matt@ucc.asn.au>
parents: 344
diff changeset
61 bottle.run(server='cgi')
d6219df77c41 main.c:
Matt Johnston <matt@ucc.asn.au>
parents: 344
diff changeset
62 #bottle.run(port=9999, reloader=True)
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 if __name__ == '__main__':
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 main()
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67