comparison web/templog.py @ 375:f22427bcfda8

a bunch of web fiddli
author Matt Johnston <matt@ucc.asn.au>
date Wed, 27 Jun 2012 23:41:50 +0800
parents 3db118498b97
children 959e88c0bdfa
comparison
equal deleted inserted replaced
373:1f530c97c053 375:f22427bcfda8
1 #!/usr/bin/env python2.7 1 #!/usr/bin/env python2.7
2 2
3 import binascii 3 import binascii
4 import hmac 4 import hmac
5 import zlib 5 import zlib
6 import datetime 6 from datetime import datetime, timedelta
7 import time 7 import time
8 import urllib
9 import sys
8 10
9 import bottle 11 import bottle
10 from bottle import route, request, response 12 from bottle import route, request, response
11 13
12 import config 14 import config
13 import log 15 import log
16
17 DATE_FORMAT = '%Y%m%d-%H.%M'
18 ZOOM_SCALE = 2.0
14 19
15 @route('/update', method='post') 20 @route('/update', method='post')
16 def update(): 21 def update():
17 enc_lines = request.forms.lines 22 enc_lines = request.forms.lines
18 mac = request.forms.hmac 23 mac = request.forms.hmac
26 31
27 return "OK" 32 return "OK"
28 33
29 @route('/graph.png') 34 @route('/graph.png')
30 def graph(): 35 def graph():
31 # url takes time in hours or days 36 length_minutes = int(request.query.length)
32 if 'day' in request.query: 37 end = datetime.strptime(request.query.end, DATE_FORMAT)
33 start_day = datetime.datetime.strptime(request.query.day, '%Y%m%d') 38 start = end - timedelta(minutes=length_minutes)
34 start = time.mktime(start_day.timetuple())
35 length = int(request.query.get('length', 5)) * 3600 * 24
36 else:
37 if 'hour' in request.query:
38 start_hour = datetime.datetime.strptime(request.query.hour, '%Y%m%d%H')
39 else:
40 start_hour = datetime.datetime.now() - datetime.timedelta(days=1)
41
42 start = time.mktime(start_hour.timetuple())
43 length = int(request.query.get('length', 26)) * 3600
44 39
45 response.set_header('Content-Type', 'image/png') 40 response.set_header('Content-Type', 'image/png')
46 return log.graph_png(start, length) 41 start_epoch = time.mktime(start.timetuple())
42 return log.graph_png(start_epoch, length_minutes * 60)
47 43
48 @route('/') 44 @route('/')
49 def top(): 45 def top():
50 return bottle.template('top', urlparams=request.query_string) 46
47 minutes = int(request.query.get('length', 26*60))
48
49 if 'end' in request.query:
50 end = datetime.strptime(request.query.end, DATE_FORMAT)
51 else:
52 end = datetime.now()
53
54 if 'zoom' in request.query:
55 orig_start = end - timedelta(minutes=minutes)
56 orig_end = end
57 xpos = int(request.query.x)
58 xpos -= config.GRAPH_LEFT_MARGIN
59
60 if xpos >= 0 and xpos < config.GRAPH_WIDTH:
61 click_time = orig_start \
62 + timedelta(minutes=(float(xpos) / config.GRAPH_WIDTH) * minutes)
63 minutes = int(minutes / ZOOM_SCALE)
64
65 end = click_time + timedelta(minutes=minutes/2)
66 else:
67 # zoom out
68 minutes = int(minutes*ZOOM_SCALE)
69 end += timedelta(minutes=minutes/2)
70
71 if end > datetime.now():
72 end = datetime.now()
73
74 request.query.replace('length', minutes)
75 request.query.replace('end', end.strftime(DATE_FORMAT))
76
77 urlparams = urllib.urlencode(request.query)
78 return bottle.template('top', urlparams=urlparams,
79 end = end.strftime(DATE_FORMAT),
80 length = minutes)
51 81
52 @route('/test') 82 @route('/test')
53 def test(): 83 def test():
54 import config 84 import config
55 import os 85 import os