changeset 542:30628aa50a10

use base64 data, faster
author Matt Johnston <matt@ucc.asn.au>
date Thu, 21 May 2015 00:00:10 +0800
parents bde53484df3c
children 77dba104dcda 2fa26745e537
files web/config.py web/templog.py web/views/top.tpl
diffstat 3 files changed, 23 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/web/config.py	Tue May 19 23:56:56 2015 +0800
+++ b/web/config.py	Thu May 21 00:00:10 2015 +0800
@@ -16,9 +16,11 @@
 
 UPDATE_URL = 'http://evil.ucc.asn.au/~matt/templog/update'
 
-GRAPH_WIDTH = 1200
-GRAPH_HEIGHT = 600
+GRAPH_WIDTH = 600
+GRAPH_HEIGHT = 700
 ZOOM = 1
+# determine by viewing the image
+GRAPH_LEFT_MARGIN = 65
 
 LINE_WIDTH = 2
 
@@ -50,8 +52,6 @@
 GRAPH_FONT = "Prociono"
 #GRAPH_FONT = "URW Gothic L"
 
-# determine by zooming in an image viewer
-GRAPH_LEFT_MARGIN = 63
 
 # 1 hour
 CSRF_TIMEOUT = 3600
--- 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():
--- a/web/views/top.tpl	Tue May 19 23:56:56 2015 +0800
+++ b/web/views/top.tpl	Thu May 21 00:00:10 2015 +0800
@@ -2,6 +2,7 @@
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
 <head>
 <title>Wort Temperature Log</title>
+<meta name="viewport" content="width=device-width">
 <style type="text/css"><!--
 span.no_selection {
     -webkit-user-select: none; // webkit (safari, chrome) browsers
@@ -19,7 +20,7 @@
 </head>
 <body>
 <form action="" method="get">
-<span class="no_selection"><input type="image" style="width: 1200px" src="graph.png?{{urlparams}}"/></span>
+<span class="no_selection"><input type="image" style="width: {{graphwidth}}" src="{{graphdata}}"/></span>
 <input type="hidden" name="length" value="{{length}}"/>
 <input type="hidden" name="end" value="{{end}}"/>
 <input type="hidden" name="zoom" value="yeah"/>