diff web/templog.py @ 244:2071d939e4ff

use base64 data, faster
author Matt Johnston <matt@ucc.asn.au>
date Thu, 21 May 2015 00:00:10 +0800
parents c90190a380c6
children c490de0cf17e
line wrap: on
line diff
--- a/web/templog.py	Tue May 19 23:56:56 2015 +0800
+++ b/web/templog.py	Thu May 21 00:00:10 2015 +0800
@@ -51,15 +51,22 @@
 
     return "OK"
 
+def make_graph(length, end):
+    length_minutes = int(length)
+    end = datetime.strptime(end, DATE_FORMAT)
+    start = end - timedelta(minutes=length_minutes)
+
+    start_epoch = time.mktime(start.timetuple())
+    return log.graph_png(start_epoch, length_minutes * 60)
+
+def encode_data(data, mimetype):
+    return 'data:%s;base64,%s' % (mimetype, binascii.b2a_base64(data).rstrip())
+
+
 @route('/graph.png')
 def graph():
-    length_minutes = int(request.query.length)
-    end = datetime.strptime(request.query.end, DATE_FORMAT)
-    start = end - timedelta(minutes=length_minutes)
-
     response.set_header('Content-Type', 'image/png')
-    start_epoch = time.mktime(start.timetuple())
-    return log.graph_png(start_epoch, length_minutes * 60)
+    return make_graph(request.query.length, request.query.end)
 
 @route('/set/update', method='post')
 def set_update():
@@ -121,9 +128,12 @@
     request.query.replace('end', end.strftime(DATE_FORMAT))
 
     urlparams = urllib.urlencode(request.query)
+    graphdata = encode_data(make_graph(request.query.length, request.query.end), 'image/png')
     return bottle.template('top', urlparams=urlparams,
                     end = end.strftime(DATE_FORMAT),
-                    length = minutes)
+                    length = minutes,
+                    graphwidth = config.GRAPH_WIDTH,
+                    graphdata = graphdata)
 
 @route('/debug')
 def debuglog():