annotate web/templog.py @ 61:62112fc2af21

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