annotate web/templog.py @ 509:e45649618a5e

improve README
author Matt Johnston <matt@ucc.asn.au>
date Thu, 26 Jun 2014 21:35:23 +0800
parents 236e5d131b3e
children 59379b2bd056
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
445
5b9dc87c988f update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 409
diff changeset
4 import json
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
5 import hmac
335
1e22eaf93620 work on web interface
Matt Johnston <matt@ucc.asn.au>
parents: 334
diff changeset
6 import zlib
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
7 from datetime import datetime, timedelta
337
f575ef538f5d - Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents: 336
diff changeset
8 import time
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
9 import urllib
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
10 import sys
482
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
11 import os
485
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
12 import traceback
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
13 import fcntl
334
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 import bottle
337
f575ef538f5d - Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents: 336
diff changeset
16 from bottle import route, request, response
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
18 import config
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
19 import log
485
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
20 import secure
489
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents: 488
diff changeset
21 import atomicfile
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
22
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
23 DATE_FORMAT = '%Y%m%d-%H.%M'
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
24 ZOOM_SCALE = 2.0
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
25
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 @route('/update', method='post')
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 def update():
445
5b9dc87c988f update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 409
diff changeset
28 js_enc = request.forms.data
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
29 mac = request.forms.hmac
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
30
445
5b9dc87c988f update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 409
diff changeset
31 if hmac.new(config.HMAC_KEY, js_enc).hexdigest() != mac:
391
5026780c2dc7 - Fix HTTPError
Matt Johnston <matt@ucc.asn.au>
parents: 383
diff changeset
32 raise bottle.HTTPError(code = 403, output = "Bad key")
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
33
445
5b9dc87c988f update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 409
diff changeset
34 js = zlib.decompress(binascii.a2b_base64(js_enc))
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
35
445
5b9dc87c988f update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 409
diff changeset
36 params = json.loads(js)
5b9dc87c988f update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 409
diff changeset
37
5b9dc87c988f update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 409
diff changeset
38 log.parse(params)
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
39
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
40 return "OK"
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 @route('/graph.png')
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 def graph():
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
44 length_minutes = int(request.query.length)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
45 end = datetime.strptime(request.query.end, DATE_FORMAT)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
46 start = end - timedelta(minutes=length_minutes)
336
ba4c4df13487 parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
47
ba4c4df13487 parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
48 response.set_header('Content-Type', 'image/png')
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
49 start_epoch = time.mktime(start.timetuple())
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
50 return log.graph_png(start_epoch, length_minutes * 60)
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51
487
931408ce71d9 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 485
diff changeset
52 @route('/set/update', method='post')
492
23c6cf01d237 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
53 def set_update():
487
931408ce71d9 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 485
diff changeset
54 post_json = json.loads(request.forms.data)
931408ce71d9 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 485
diff changeset
55
931408ce71d9 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 485
diff changeset
56 csrf_blob = post_json['csrf_blob']
931408ce71d9 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 485
diff changeset
57
492
23c6cf01d237 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
58 if not secure.check_csrf_blob(csrf_blob):
23c6cf01d237 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
59 bottle.response.status = 403
23c6cf01d237 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
60 return "Bad csrf"
23c6cf01d237 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
61
23c6cf01d237 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
62 ret = log.update_params(post_json['params'])
23c6cf01d237 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
63 if not ret is True:
23c6cf01d237 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
64 bottle.response.status = 403
23c6cf01d237 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
65 return ret
23c6cf01d237 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
66
23c6cf01d237 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
67 return "Good"
487
931408ce71d9 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 485
diff changeset
68
482
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
69 @route('/set')
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
70 def set():
488
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents: 487
diff changeset
71 allowed = ["false", "true"][secure.get_user_hash() in config.ALLOWED_USERS]
501
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 492
diff changeset
72 response.set_header('Cache-Control', 'no-cache')
485
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
73 return bottle.template('set',
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
74 inline_data = log.get_params(),
488
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents: 487
diff changeset
75 csrf_blob = secure.get_csrf_blob(),
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents: 487
diff changeset
76 allowed = allowed)
482
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
77
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
78 @route('/set_current.json')
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
79 def set_fresh():
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
80 response.set_header('Content-Type', 'application/javascript')
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
81 return log.get_current()
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
82
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 @route('/')
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 def top():
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
85
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
86 minutes = int(request.query.get('length', 26*60))
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
87
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
88 if 'end' in request.query:
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
89 end = datetime.strptime(request.query.end, DATE_FORMAT)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
90 else:
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
91 end = datetime.now()
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
92
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
93 if 'zoom' in request.query:
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
94 orig_start = end - timedelta(minutes=minutes)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
95 orig_end = end
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
96 xpos = int(request.query.x)
383
a032051475fe ui tweaks
Matt Johnston <matt@ucc.asn.au>
parents: 375
diff changeset
97 xpos -= config.GRAPH_LEFT_MARGIN * config.ZOOM
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
98
383
a032051475fe ui tweaks
Matt Johnston <matt@ucc.asn.au>
parents: 375
diff changeset
99 if xpos >= 0 and xpos < config.GRAPH_WIDTH * config.ZOOM:
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
100 click_time = orig_start \
383
a032051475fe ui tweaks
Matt Johnston <matt@ucc.asn.au>
parents: 375
diff changeset
101 + timedelta(minutes=(float(xpos) / (config.GRAPH_WIDTH * config.ZOOM)) * minutes)
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
102 minutes = int(minutes / ZOOM_SCALE)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
103
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
104 end = click_time + timedelta(minutes=minutes/2)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
105 else:
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
106 # zoom out
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
107 minutes = int(minutes*ZOOM_SCALE)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
108 end += timedelta(minutes=minutes/2)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
109
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
110 if end > datetime.now():
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
111 end = datetime.now()
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
112
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
113 request.query.replace('length', minutes)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
114 request.query.replace('end', end.strftime(DATE_FORMAT))
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
115
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
116 urlparams = urllib.urlencode(request.query)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
117 return bottle.template('top', urlparams=urlparams,
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
118 end = end.strftime(DATE_FORMAT),
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
119 length = minutes)
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120
409
1854abee5a59 add debug url
Matt Johnston <matt@ucc.asn.au>
parents: 391
diff changeset
121 @route('/debug')
1854abee5a59 add debug url
Matt Johnston <matt@ucc.asn.au>
parents: 391
diff changeset
122 def debuglog():
1854abee5a59 add debug url
Matt Johnston <matt@ucc.asn.au>
parents: 391
diff changeset
123 response.set_header('Content-Type', 'text/plain')
1854abee5a59 add debug url
Matt Johnston <matt@ucc.asn.au>
parents: 391
diff changeset
124 return log.tail_debug_log()
344
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
125
482
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
126 @route('/env')
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
127 def env():
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
128 response.set_header('Content-Type', 'text/plain')
488
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents: 487
diff changeset
129 #return '\n'.join(traceback.format_stack())
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents: 487
diff changeset
130 return '\n'.join(("%s %s" % k) for k in request.environ.items())
482
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
131 #return str(request.environ)
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
132 #yield "\n"
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
133 #var_lookup = environ['mod_ssl.var_lookup']
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
134 #return var_lookup("SSL_SERVER_I_DN_O")
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
135
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
136 @bottle.get('/<filename:re:.*\.js>')
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
137 def javascripts(filename):
485
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
138 response.set_header('Cache-Control', "public, max-age=1296000")
482
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
139 return bottle.static_file(filename, root='static')
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
140
485
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
141 secure.setup_csrf()
482
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
142
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 def main():
367
3db118498b97 sort out voltage logging
Matt Johnston <matt@ucc.asn.au>
parents: 362
diff changeset
144 #bottle.debug(True)
3db118498b97 sort out voltage logging
Matt Johnston <matt@ucc.asn.au>
parents: 362
diff changeset
145 #bottle.run(reloader=True)
3db118498b97 sort out voltage logging
Matt Johnston <matt@ucc.asn.au>
parents: 362
diff changeset
146 bottle.run(server='cgi', reloader=True)
346
d6219df77c41 main.c:
Matt Johnston <matt@ucc.asn.au>
parents: 344
diff changeset
147 #bottle.run(port=9999, reloader=True)
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 if __name__ == '__main__':
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 main()
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151