annotate web/templog.py @ 186:52b1ebcd00c0

get rid of button1.htm prototype
author Matt Johnston <matt@ucc.asn.au>
date Fri, 07 Feb 2014 08:00:03 +0800
parents 3b4277aaed3c
children e731c0d30b09
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
146
3b4277aaed3c update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 103
diff changeset
4 import json
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
5 import hmac
29
048143905092 work on web interface
Matt Johnston <matt@ucc.asn.au>
parents: 28
diff changeset
6 import zlib
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
7 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
8 import time
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
9 import urllib
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
10 import sys
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
11
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 import bottle
31
5e75e08d20ac - Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents: 30
diff changeset
13 from bottle import route, request, response
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
15 import config
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
16 import log
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
17
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
18 DATE_FORMAT = '%Y%m%d-%H.%M'
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
19 ZOOM_SCALE = 2.0
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
20
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 @route('/update', method='post')
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 def update():
146
3b4277aaed3c update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 103
diff changeset
23 js_enc = request.forms.data
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
24 mac = request.forms.hmac
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
25
146
3b4277aaed3c update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 103
diff changeset
26 if hmac.new(config.HMAC_KEY, js_enc).hexdigest() != mac:
85
3cd1ca6d0489 - Fix HTTPError
Matt Johnston <matt@ucc.asn.au>
parents: 77
diff changeset
27 raise bottle.HTTPError(code = 403, output = "Bad key")
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
28
146
3b4277aaed3c update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 103
diff changeset
29 js = zlib.decompress(binascii.a2b_base64(js_enc))
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
30
146
3b4277aaed3c update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 103
diff changeset
31 params = json.loads(js)
3b4277aaed3c update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 103
diff changeset
32
3b4277aaed3c update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 103
diff changeset
33 log.parse(params)
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
34
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
35 return "OK"
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 @route('/graph.png')
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 def graph():
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
39 length_minutes = int(request.query.length)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
40 end = datetime.strptime(request.query.end, DATE_FORMAT)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
41 start = end - timedelta(minutes=length_minutes)
30
13fcf497f8b7 parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
42
13fcf497f8b7 parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
43 response.set_header('Content-Type', 'image/png')
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
44 start_epoch = time.mktime(start.timetuple())
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
45 return log.graph_png(start_epoch, length_minutes * 60)
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 @route('/')
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 def top():
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
49
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
50 minutes = int(request.query.get('length', 26*60))
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
51
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
52 if 'end' in request.query:
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
53 end = datetime.strptime(request.query.end, DATE_FORMAT)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
54 else:
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
55 end = datetime.now()
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
56
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
57 if 'zoom' in request.query:
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
58 orig_start = end - timedelta(minutes=minutes)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
59 orig_end = end
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
60 xpos = int(request.query.x)
77
959e88c0bdfa ui tweaks
Matt Johnston <matt@ucc.asn.au>
parents: 69
diff changeset
61 xpos -= config.GRAPH_LEFT_MARGIN * config.ZOOM
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
62
77
959e88c0bdfa ui tweaks
Matt Johnston <matt@ucc.asn.au>
parents: 69
diff changeset
63 if xpos >= 0 and xpos < config.GRAPH_WIDTH * config.ZOOM:
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
64 click_time = orig_start \
77
959e88c0bdfa ui tweaks
Matt Johnston <matt@ucc.asn.au>
parents: 69
diff changeset
65 + timedelta(minutes=(float(xpos) / (config.GRAPH_WIDTH * config.ZOOM)) * minutes)
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
66 minutes = int(minutes / ZOOM_SCALE)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
67
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
68 end = click_time + timedelta(minutes=minutes/2)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
69 else:
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
70 # zoom out
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
71 minutes = int(minutes*ZOOM_SCALE)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
72 end += timedelta(minutes=minutes/2)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
73
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
74 if end > datetime.now():
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
75 end = datetime.now()
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
76
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
77 request.query.replace('length', minutes)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
78 request.query.replace('end', end.strftime(DATE_FORMAT))
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
79
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
80 urlparams = urllib.urlencode(request.query)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
81 return bottle.template('top', urlparams=urlparams,
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
82 end = end.strftime(DATE_FORMAT),
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
83 length = minutes)
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84
103
ef59da811986 add debug url
Matt Johnston <matt@ucc.asn.au>
parents: 85
diff changeset
85 @route('/debug')
ef59da811986 add debug url
Matt Johnston <matt@ucc.asn.au>
parents: 85
diff changeset
86 def debuglog():
ef59da811986 add debug url
Matt Johnston <matt@ucc.asn.au>
parents: 85
diff changeset
87 response.set_header('Content-Type', 'text/plain')
ef59da811986 add debug url
Matt Johnston <matt@ucc.asn.au>
parents: 85
diff changeset
88 return log.tail_debug_log()
37
8da0fdadc8d7 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 31
diff changeset
89
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 def main():
61
62112fc2af21 sort out voltage logging
Matt Johnston <matt@ucc.asn.au>
parents: 56
diff changeset
91 #bottle.debug(True)
62112fc2af21 sort out voltage logging
Matt Johnston <matt@ucc.asn.au>
parents: 56
diff changeset
92 #bottle.run(reloader=True)
62112fc2af21 sort out voltage logging
Matt Johnston <matt@ucc.asn.au>
parents: 56
diff changeset
93 bottle.run(server='cgi', reloader=True)
40
9b5b202129c3 main.c:
Matt Johnston <matt@ucc.asn.au>
parents: 37
diff changeset
94 #bottle.run(port=9999, reloader=True)
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 if __name__ == '__main__':
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 main()
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99