]> www.average.org Git - pulsecounter.git/blobdiff - web/index.html
don't draw plot w/o data
[pulsecounter.git] / web / index.html
index f70f51b05686131df6ff06c82a605a3fdcebf1f8..7c0b4a4af3c9554e27d316fd47732226cd6eb8b3 100644 (file)
@@ -4,13 +4,12 @@
 <html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <script>
-  var dbg;
-  var xmlhttp;
+  var dbg, errordiv;
   var canvas, ctx;
   var ww, wh;
   var hmax, hfact;
   var tmin, tmax, tfact;
-  var xzero = 36, yzero = 24;
+  var xzero = 60, yzero = 48;
   var cold_d = [], hot_d = [];
 
   function showdate(utime) {
     return dt.toLocaleDateString() + " " + dt.toLocaleTimeString();
   }
 
-  function getcomb(lo, hi) {
+  function px(x) {
+    return xzero + ((x - tmin) * tfact);
+  }
+
+  function py(y) { return wh - yzero - (y * hfact);
+  }
+
+  const dow = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
+  const mn = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
+              "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
+
+  function getcombx(tmin, tmax) {
+    var comb = [], lb = [];
+    var trange = tmax - tmin;
+    var inc, inc2, base, base2, t, dt, zoff;
+    var label = function(t) { return "<<" + t.toFixed(1) + ">>"; }
+    function tohour(t) { var dt = new Date(t*1000); return dt.getHours(); }
+    function todow(t) { var dt = new Date(t*1000); return dow[dt.getDay()]; }
+    function todom(t) { var dt = new Date(t*1000); return dt.getDate(); }
+    function tomonth(t) { var dt = new Date(t*1000); return mn[dt.getMonth()]; }
+
+    if (trange < 172800) { /* two days -> one hour */
+      inc = 3600;
+      inc2 = 21600;
+      label = tohour;
+    }
+    else if (trange < 864000) { /* 10 days -> six hours */
+      inc = 21600;
+      inc2 = 86400;
+      label = todow;
+    }
+    else if (trange < 2678400) { /* 31 days -> 1 day */
+      inc = 86400;
+      inc2 = 86400;
+      label = todom;
+    }
+    else { /* ~ one month */
+      /* TODO: make this a separate case with loop over months rather than
+         fixed number of seconds. */
+      inc = 86400;
+      inc2 = 2592000;
+      label = tomonth;
+    }
+
+    dt = new Date(tmin*1000);
+    zoff = 60 * dt.getTimezoneOffset();
+    base = (Math.floor((tmin - zoff - 1) / inc) + 1) * inc + zoff;
+    for (t = base; t < tmax; t += inc)
+      comb.push(t);
+    base2 = (Math.floor((tmin - zoff - 1) / inc2) + 1) * inc2 + zoff;
+    for (t = base2; t < tmax; t += inc2)
+      lb.push([t, label(t)]);
+    //dbg.innerHTML = "inc=" + inc + "<br>inc2=" + inc2 + "<br>"
+    //              + "tmin=" + tmin + " tmax=" + tmax + "<br>"
+    //              + "base=" + base + " base2=" + base2 + "<br>"
+    //              + comb + "<br>" + lb;
+    return [comb, lb];
+  }
+
+  function xaxis() {
+    var comb = getcombx(tmin, tmax);
+    var i;
+
+    ctx.beginPath();
+    for (i = 0; comb[0][i]; i++) {
+      ctx.moveTo(px(comb[0][i]), py(0) + 5);
+      ctx.lineTo(px(comb[0][i]), py(0));
+    }
+    ctx.strokeStyle = "gray";
+    ctx.stroke();
+
+    ctx.beginPath();
+    ctx.moveTo(px(tmin), py(0));
+    ctx.lineTo(px(tmax), py(0));
+    ctx.strokeStyle = "black";
+    ctx.stroke();
+
+    ctx.fillStyle = "black";
+    ctx.font = "bold 16px Courier";
+    ctx.textAlign = "center";
+    ctx.beginPath();
+    for (i = 0; comb[1][i]; i++) {
+      ctx.fillText(comb[1][i][1], px(comb[1][i][0]), py(0) + 20);
+      ctx.moveTo(px(comb[1][i][0]), py(0) + 5);
+      ctx.lineTo(px(comb[1][i][0]), py(0));
+    }
+    ctx.strokeStyle = "black";
+    ctx.stroke();
+
+    ctx.fillStyle = "black";
+    ctx.textAlign = "left";
+    ctx.fillText(showdate(tmin), px(tmin), py(0) + 40);
+    ctx.textAlign = "right";
+    ctx.fillText(showdate(tmax), px(tmax), py(0) + 40);
+  }
+
+  function getcomby(lo, hi) {
     var comb = [], lb = [];
     var d = hi - lo;
     var ord = Math.pow(10, Math.floor(Math.log10(d)));
     var inc, inc2, first, x, lb;
 
     if (scl < 2) { inc = 0.1; inc2 = 0.2; }
-    else if (scl < 5) { inc = 0.1; inc2 = 0.2; }
+    else if (scl < 5) { inc = 0.1; inc2 = 0.5; }
     else { inc = 0.5; inc2 = 1; }
     inc *= ord;
     inc2 *= ord;
     return [comb, lb];
   }
 
-  function px(x) {
-    return xzero + ((x - tmin) * tfact);
-  }
-
-  function py(y) {
-    return wh - yzero - (y * hfact);
-  }
-
-  function xaxis() {
-    ctx.beginPath();
-    ctx.moveTo(px(tmin), py(0));
-    ctx.lineTo(px(tmax), py(0));
-    ctx.strokeStyle = "black";
-    ctx.stroke();
-
-    ctx.fillStyle = "black";
-    ctx.font = "bold 12px Courier";
-    ctx.textAlign = "left";
-    ctx.fillText(showdate(tmin), px(tmin), py(0) + 20);
-    ctx.textAlign = "right";
-    ctx.fillText(showdate(tmax), px(tmax), py(0) + 20);
-  }
-
   function yaxis() {
-    var comb = getcomb(0, hmax);
+    var comb = getcomby(0, hmax);
     var i;
 
     ctx.beginPath();
     ctx.stroke();
 
     ctx.fillStyle = "black";
-    ctx.font = "bold 12px Courier";
+    ctx.font = "bold 16px Courier";
     ctx.textAlign = "right";
     ctx.fillText(0, px(tmin) - 6, py(0));
+    ctx.beginPath();
     for (i = 0; comb[1][i]; i++) {
       ctx.fillText(comb[1][i].toFixed(1), px(tmin) - 6, py(comb[1][i]));
+      ctx.moveTo(px(tmin) - 5, py(comb[1][i]));
+      ctx.lineTo(px(tmax), py(comb[1][i]));
     }
+    ctx.strokeStyle = "gray";
+    ctx.stroke();
+
     ctx.textAlign = "left";
-    ctx.fillText("l/h", px(tmin) + 4, py(hmax) + 8);
+    ctx.fillText("l/min", px(tmin) + 4, py(hmax) + 12);
   }
 
   /* @ updates global var `hmax` */
       dv = times[i+1][1] - times[i][1];
       dt = times[i+1][0] - times[i][0];
       if (dt != 0 && dv != 0) {
-        v = (dv / dt) * 360 ; /* Litres per hour */
+        v = (dv / dt) * 600 ; /* Litres per min */
         if (hmax < v) hmax = v;
         res.push([times[i][0], v]);
       }
   function drawplot(data, color) {
     var i;
 
-    ctx.beginPath();
-    ctx.moveTo(px(data[0][0]), py(data[0][1]));
-    for (i = 1; i < data.length; i++) {
-      ctx.lineTo(px(data[i][0]), py(data[i - 1][1]));
-      ctx.lineTo(px(data[i][0]), py(data[i][1]));
+    if (data.length) {
+      ctx.beginPath();
+      ctx.moveTo(px(data[0][0]), py(data[0][1]));
+      for (i = 1; i < data.length; i++) {
+        ctx.lineTo(px(data[i][0]), py(data[i - 1][1]));
+        ctx.lineTo(px(data[i][0]), py(data[i][1]));
+      }
+      ctx.strokeStyle = color;
+      ctx.stroke();
     }
-    ctx.strokeStyle = color;
-    ctx.stroke();
   }
 
   function showloading() {
   }
 
   function redraw() {
+    errordiv.style.visibility = "hidden";
+    errordiv.innerHTML = "";
     clearplot();
     if (cold_d.length || hot_d.length) {
       tfact = (ww - xzero) / (tmax - tmin);
   }
 
   function gotdata(data) {
-    document.getElementById("cold").innerHTML =
+    document.getElementById("curcold").innerHTML =
       (data.current.cold / 100).toFixed(2);
-    document.getElementById("hot").innerHTML =
+    document.getElementById("curhot").innerHTML =
       (data.current.hot / 100).toFixed(2);
+
+    if (data.cold.length)
+      document.getElementById("totcold").innerHTML =
+        ((data.cold[data.cold.length - 1][1] - data.cold[0][1]) * 10);
+    else document.getElementById("totcold").innerHTML = "0";
+    if (data.hot.length)
+      document.getElementById("tothot").innerHTML =
+        ((data.hot[data.hot.length - 1][1] - data.hot[0][1]) * 10);
+    else document.getElementById("tothot").innerHTML = "0";
+
     tmin = data.range.lo;
     tmax = data.range.hi;
     //dbg.innerHTML = "from " + tmin + " to " + tmax
     redraw();
   }
 
-  function iso2qu(idate) {
-    return idate.replace("T", "+").replace("0Z", "");
-  }
-
-  function sendquery(lo, hi) {
-    var url = "query.cgi" + "?lo=" + iso2qu(lo) + "&hi=" + iso2qu(hi);
+  function sendreq(qstr) {
+    var url = "query.cgi" + qstr;
+    var xmlhttp = new XMLHttpRequest();
 
     //dbg.innerHTML = url;
     xmlhttp.onreadystatechange = function() {
-      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
-        // dbg.innerHTML = xmlhttp.responseText;
-        var myData = JSON.parse(xmlhttp.responseText);
-        gotdata(myData);
-      }
+      if (xmlhttp.readyState == 4)
+        if (xmlhttp.status == 200) {
+          // dbg.innerHTML = xmlhttp.responseText;
+          var myData = JSON.parse(xmlhttp.responseText);
+          gotdata(myData);
+        } else {
+          errordiv.style.visibility = "visible";
+          errordiv.style.display = "block";
+          errordiv.innerHTML = xmlhttp.responseText;
+        }
     }
     xmlhttp.open("GET", url, true);
     clearplot();
     xmlhttp.send();
   }
 
+  function iso2qu(idate) {
+    return idate.replace("T", "+").replace("0Z", "");
+  }
+
+  function sendquery(lo, hi) {
+    return sendreq("?lo=" + iso2qu(lo) + "&hi=" + iso2qu(hi));
+  }
+
   function resize() {
     ww = window.innerWidth - 4;
     if (ww > window.innerHeight) ww = window.innerHeight;
 
     wstart = new Date(1*tdy - 86400000 * (dow + 7));
     wend = new Date(1*wstart + 86400000 * 7);
-    xmlhttp = new XMLHttpRequest();
     sendquery(wstart.toISOString(), wend.toISOString());
   }
 
 
     wstart = new Date(1*tdy - 86400000 * dow);
     wend = new Date(1*wstart + 86400000 * 7);
-    xmlhttp = new XMLHttpRequest();
     sendquery(wstart.toISOString(), wend.toISOString());
   }
 
     var tdy = daystart(new Date());
     var ytd = new Date(1*tdy - 86400000);
     var byd = new Date(1*ytd - 86400000);
-    xmlhttp = new XMLHttpRequest();
     sendquery(byd.toISOString(), ytd.toISOString());
   }
 
   function yesterday() {
     var tdy = daystart(new Date());
     var ytd = new Date(1*tdy - 86400000);
-    xmlhttp = new XMLHttpRequest();
     sendquery(ytd.toISOString(), tdy.toISOString());
   }
 
   function today() {
     var tdy = daystart(new Date());
     var tmr = new Date(1*tdy + 86400000);
-    xmlhttp = new XMLHttpRequest();
     sendquery(tdy.toISOString(), tmr.toISOString());
   }
 
   function initialize() {
+    var qstr = window.location.search;
+
     dbg = document.getElementById("debug");
+    errordiv = document.getElementById("errormsg");
     canvas = document.getElementById("plot");
     ctx = canvas.getContext("2d");
     resize();
-    today();
+    if (qstr) sendreq(qstr);
+    else today();
     
     document.getElementById("today").onclick = today;
     document.getElementById("yesterday").onclick = yesterday;
   }
 
   /* Set up */
-  if(window.attachEvent) {
-    window.attachEvent('onload', initialize);
-    window.attachEvent('onresize', resize);
-  } else {
-    window.onload = initialize;
-    window.onresize = resize;
-  }
+  window.onload = initialize;
+  window.onresize = resize;
 </script>
 <style>
 @font-face {
@@ -308,17 +404,25 @@ div#currentvals {
   text-align: center;
   font-size: 150%;
 }
+div#totalvals {
+  width: 18em;
+  margin-left: auto;
+  margin-right: auto;
+  margin-bottom: 10px;
+  text-align: center;
+  font-size: 100%;
+}
 div.current {
   position: relative;
   padding: 0.2em;
   border: solid 1px black;
   margin: 0.2em;
 }
-div#cold {
+div.cold {
   float: left;
   background-color: #d0e0ff;
 }
-div#hot {
+div.hot {
   float: right;
   background-color: #ffd0e0;
 }
@@ -330,6 +434,11 @@ canvas#plot {
   height: 320px;
   /* border: solid 1px black; */
 }
+div#errormsg {
+  visibility: hidden;
+  color: red;
+  text-align: center;
+}
 div#queries {
   margin-left: auto;
   margin-right: auto;
@@ -342,6 +451,7 @@ div.query {
   width: 8em;
   height: 8em;
   border: solid 1px black;
+  border-radius: 1em;
   background-color: lightgray;
   cursor: pointer;
 }
@@ -360,13 +470,21 @@ body {
 </head><body>
 <h1>WATER METERS</h1>
 <div id="currentvals">
-  Current Values
-  <div class="current" id="cold">cold</div>
-  <div class="current" id="hot">hot</div>
+  Current Readings (m<sup>3</sup>)
+  <div class="current cold" id="curcold">cold</div>
+  <div class="current hot" id="curhot">hot</div>
 </div>
 <br />
 <canvas id="plot" width="640" height = "320"></canvas>
 <br />
+<div id="errormsg"></div>
+<br />
+<div id="totalvals">
+  Total for the period (l)
+  <div class="current cold" id="totcold">cold</div>
+  <div class="current hot" id="tothot">hot</div>
+</div>
+<br />
 <div id="queries">
  <div class="query" id="prevweek"><div class="label">PREVIOUS WEEK</div></div>
  <div class="query" id="beforeyesterday"><div class="label">DAY