annotate web/templog.py @ 224:01bbb8b93f77

title
author Matt Johnston <matt@ucc.asn.au>
date Thu, 29 Jan 2015 22:52:26 +0800
parents 59379b2bd056
children 88dfac30d065
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
182
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
11 import os
185
adbf70d1449f working
Matt Johnston <matt@ucc.asn.au>
parents: 182
diff changeset
12 import traceback
adbf70d1449f working
Matt Johnston <matt@ucc.asn.au>
parents: 182
diff changeset
13 import fcntl
28
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 import bottle
31
5e75e08d20ac - Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents: 30
diff changeset
16 from bottle import route, request, response
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
18 import config
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
19 import log
185
adbf70d1449f working
Matt Johnston <matt@ucc.asn.au>
parents: 182
diff changeset
20 import secure
191
8318d50d766d gets current params
Matt Johnston <matt@ucc.asn.au>
parents: 189
diff changeset
21 import atomicfile
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
22
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
23 DATE_FORMAT = '%Y%m%d-%H.%M'
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
24 ZOOM_SCALE = 2.0
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
25
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 @route('/update', method='post')
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 def update():
146
3b4277aaed3c update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 103
diff changeset
28 js_enc = request.forms.data
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
29 mac = request.forms.hmac
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 if hmac.new(config.HMAC_KEY, js_enc).hexdigest() != mac:
85
3cd1ca6d0489 - Fix HTTPError
Matt Johnston <matt@ucc.asn.au>
parents: 77
diff changeset
32 raise bottle.HTTPError(code = 403, output = "Bad key")
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
33
146
3b4277aaed3c update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 103
diff changeset
34 js = zlib.decompress(binascii.a2b_base64(js_enc))
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
35
146
3b4277aaed3c update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 103
diff changeset
36 params = json.loads(js)
3b4277aaed3c update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 103
diff changeset
37
3b4277aaed3c update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 103
diff changeset
38 log.parse(params)
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
39
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
40 return "OK"
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 @route('/graph.png')
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 def graph():
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
44 length_minutes = int(request.query.length)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
45 end = datetime.strptime(request.query.end, DATE_FORMAT)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
46 start = end - timedelta(minutes=length_minutes)
30
13fcf497f8b7 parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
47
13fcf497f8b7 parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
48 response.set_header('Content-Type', 'image/png')
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
49 start_epoch = time.mktime(start.timetuple())
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
50 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
51
188
ae5efca89001 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 185
diff changeset
52 @route('/set/update', method='post')
194
4fa8cbf31065 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
53 def set_update():
188
ae5efca89001 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 185
diff changeset
54 post_json = json.loads(request.forms.data)
ae5efca89001 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 185
diff changeset
55
ae5efca89001 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 185
diff changeset
56 csrf_blob = post_json['csrf_blob']
ae5efca89001 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 185
diff changeset
57
194
4fa8cbf31065 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
58 if not secure.check_csrf_blob(csrf_blob):
4fa8cbf31065 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
59 bottle.response.status = 403
4fa8cbf31065 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
60 return "Bad csrf"
4fa8cbf31065 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
61
4fa8cbf31065 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
62 ret = log.update_params(post_json['params'])
4fa8cbf31065 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
63 if not ret is True:
4fa8cbf31065 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
64 bottle.response.status = 403
4fa8cbf31065 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
65 return ret
4fa8cbf31065 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
66
4fa8cbf31065 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
67 return "Good"
188
ae5efca89001 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 185
diff changeset
68
182
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
69 @route('/set')
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
70 def set():
211
59379b2bd056 key fingerprints are case- and whitespace-insensitive.
Matt Johnston <matt@ucc.asn.au>
parents: 202
diff changeset
71 allowed = ["false", "true"][secure.check_user_hash(config.ALLOWED_USERS)]
202
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 194
diff changeset
72 response.set_header('Cache-Control', 'no-cache')
185
adbf70d1449f working
Matt Johnston <matt@ucc.asn.au>
parents: 182
diff changeset
73 return bottle.template('set',
adbf70d1449f working
Matt Johnston <matt@ucc.asn.au>
parents: 182
diff changeset
74 inline_data = log.get_params(),
189
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents: 188
diff changeset
75 csrf_blob = secure.get_csrf_blob(),
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents: 188
diff changeset
76 allowed = allowed)
182
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
77
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
78 @route('/set_current.json')
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
79 def set_fresh():
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
80 response.set_header('Content-Type', 'application/javascript')
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
81 return log.get_current()
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
82
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 @route('/')
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 def top():
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
85
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
86 minutes = int(request.query.get('length', 26*60))
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
87
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
88 if 'end' in request.query:
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
89 end = datetime.strptime(request.query.end, DATE_FORMAT)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
90 else:
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
91 end = datetime.now()
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
92
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
93 if 'zoom' in request.query:
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
94 orig_start = end - timedelta(minutes=minutes)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
95 orig_end = end
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
96 xpos = int(request.query.x)
77
959e88c0bdfa ui tweaks
Matt Johnston <matt@ucc.asn.au>
parents: 69
diff changeset
97 xpos -= config.GRAPH_LEFT_MARGIN * config.ZOOM
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
98
77
959e88c0bdfa ui tweaks
Matt Johnston <matt@ucc.asn.au>
parents: 69
diff changeset
99 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
100 click_time = orig_start \
77
959e88c0bdfa ui tweaks
Matt Johnston <matt@ucc.asn.au>
parents: 69
diff changeset
101 + 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
102 minutes = int(minutes / ZOOM_SCALE)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
103
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
104 end = click_time + timedelta(minutes=minutes/2)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
105 else:
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
106 # zoom out
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
107 minutes = int(minutes*ZOOM_SCALE)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
108 end += timedelta(minutes=minutes/2)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
109
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
110 if end > datetime.now():
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
111 end = datetime.now()
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
112
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
113 request.query.replace('length', minutes)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
114 request.query.replace('end', end.strftime(DATE_FORMAT))
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
115
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
116 urlparams = urllib.urlencode(request.query)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
117 return bottle.template('top', urlparams=urlparams,
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
118 end = end.strftime(DATE_FORMAT),
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
119 length = minutes)
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120
103
ef59da811986 add debug url
Matt Johnston <matt@ucc.asn.au>
parents: 85
diff changeset
121 @route('/debug')
ef59da811986 add debug url
Matt Johnston <matt@ucc.asn.au>
parents: 85
diff changeset
122 def debuglog():
ef59da811986 add debug url
Matt Johnston <matt@ucc.asn.au>
parents: 85
diff changeset
123 response.set_header('Content-Type', 'text/plain')
ef59da811986 add debug url
Matt Johnston <matt@ucc.asn.au>
parents: 85
diff changeset
124 return log.tail_debug_log()
37
8da0fdadc8d7 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 31
diff changeset
125
182
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
126 @route('/env')
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
127 def env():
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
128 response.set_header('Content-Type', 'text/plain')
189
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents: 188
diff changeset
129 #return '\n'.join(traceback.format_stack())
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents: 188
diff changeset
130 return '\n'.join(("%s %s" % k) for k in request.environ.items())
182
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
131 #return str(request.environ)
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
132 #yield "\n"
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
133 #var_lookup = environ['mod_ssl.var_lookup']
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
134 #return var_lookup("SSL_SERVER_I_DN_O")
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
135
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
136 @bottle.get('/<filename:re:.*\.js>')
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
137 def javascripts(filename):
185
adbf70d1449f working
Matt Johnston <matt@ucc.asn.au>
parents: 182
diff changeset
138 response.set_header('Cache-Control', "public, max-age=1296000")
182
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
139 return bottle.static_file(filename, root='static')
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
140
185
adbf70d1449f working
Matt Johnston <matt@ucc.asn.au>
parents: 182
diff changeset
141 secure.setup_csrf()
182
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
142
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 def main():
61
62112fc2af21 sort out voltage logging
Matt Johnston <matt@ucc.asn.au>
parents: 56
diff changeset
144 #bottle.debug(True)
62112fc2af21 sort out voltage logging
Matt Johnston <matt@ucc.asn.au>
parents: 56
diff changeset
145 #bottle.run(reloader=True)
62112fc2af21 sort out voltage logging
Matt Johnston <matt@ucc.asn.au>
parents: 56
diff changeset
146 bottle.run(server='cgi', reloader=True)
40
9b5b202129c3 main.c:
Matt Johnston <matt@ucc.asn.au>
parents: 37
diff changeset
147 #bottle.run(port=9999, reloader=True)
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 if __name__ == '__main__':
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 main()
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151