]> www.average.org Git - pulsecounter.git/blobdiff - web/index.html
fix total calculation for empty dataset
[pulsecounter.git] / web / index.html
index 9e459a20450ddcba8543270404b1fdbe25633d8f..a1745afe9684bb469b686fb7c6dad6832a852c42 100644 (file)
 <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 = 20, yzero = 20;
+  var xzero = 40, yzero = 48;
   var cold_d = [], hot_d = [];
 
+  function showdate(utime) {
+    var dt = new Date(utime*1000);
+    return dt.toLocaleDateString() + " " + dt.toLocaleTimeString();
+  }
+
   function px(x) {
     return xzero + ((x - tmin) * tfact);
   }
-  function py(y) {
-    return wh - yzero - (y * hfact);
+
+  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(lo, hi) {
+    var comb = [], lb = [];
+    var trange = tmax - tmin;
+    var inc, inc2, base, t, dt;
+    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 = 2592000;
+      inc2 = 2592000;
+      label = tomonth;
+    }
+
+    dt = new Date(tmin*1000);
+    base = (Math.floor(tmin / inc) + 1) * inc + 60 * dt.getTimezoneOffset();
+    for (t = base; t < tmax; t += inc)
+      comb.push(t);
+    base = (Math.floor(tmin / inc2) + 1) * inc2 + 60 * dt.getTimezoneOffset();
+    for (t = base; t < tmax; t += inc2)
+      lb.push([t, label(t)]);
+    //dbg.innerHTML = "ord=" + ord + "<br>inc=" + inc + "<br>"
+    //              + comb + "<br>" + lb;
+    return [comb, lb];
+  }
+
   function xaxis() {
+    var comb = getcombx(0, hmax);
+    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 scl = Math.floor(d / ord);
+    var inc, inc2, first, x, lb;
+
+    if (scl < 2) { 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;
+    first = (Math.floor(lo / inc) + 1) * inc;
+    for (x = 0; x < (d / inc) - 1.2; x++)
+      comb.push(first + inc * x);
+    first = (Math.floor(lo / inc2) + 1) * inc2;
+    for (x = 0; x < (d / inc2) - 1.2; x++)
+      lb.push(first + inc2 * x);
+    //dbg.innerHTML = "ord=" + ord + "<br>inc=" + inc + "<br>"
+    //              + comb + "<br>" + lb;
+    return [comb, lb];
   }
+
   function yaxis() {
+    var comb = getcomby(0, hmax);
+    var i;
+
+    ctx.beginPath();
+    for (i = 0; comb[0][i]; i++) {
+      ctx.moveTo(px(tmin) - 5, py(comb[0][i]));
+      ctx.lineTo(px(tmax), py(comb[0][i]));
+    }
+    ctx.strokeStyle = "lightgray";
+    ctx.stroke();
+
     ctx.beginPath();
     ctx.moveTo(px(tmin), py(0));
     ctx.lineTo(px(tmin), py(hmax));
     ctx.strokeStyle = "black";
     ctx.stroke();
+
+    ctx.fillStyle = "black";
+    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/min", px(tmin) + 4, py(hmax) + 12);
   }
+
   /* @ updates global var `hmax` */
   function differentiate(times) {
     var res = [];
       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]);
       }
     }
-    res.push([times[i][0], v]);
+    if (i) res.push([times[i][0], v]);
 
     return res;
   }
+
   function drawplot(data, color) {
     var i;
 
     ctx.strokeStyle = color;
     ctx.stroke();
   }
+
   function showloading() {
     ctx.fillStyle = "green";
     ctx.font = "bold 16px Courier";
-    ctx.fillText("loading", (ww / 2) - 40 , (wh / 2) + 8);
+    ctx.textAlign="center";
+    ctx.fillText("...loading...", (ww / 2) , (wh / 2) + 8);
+  }
+
+  function showempty() {
+    ctx.fillStyle = "red";
+    ctx.font = "bold 24px Courier";
+    ctx.textAlign="center";
+    ctx.fillText("No data for the requested time interval",
+                 (ww / 2) , (wh / 2) + 8);
   }
+
   function clearplot() {
     ctx.clearRect(0, 0, ww, wh);
   }
+
   function redraw() {
-    if (cold_d.length && hot_d.length) {
-      clearplot();
-      drawplot(cold_d, "blue");
-      drawplot(hot_d, "red");
+    errordiv.style.visibility = "hidden";
+    errordiv.innerHTML = "";
+    clearplot();
+    if (cold_d.length || hot_d.length) {
+      tfact = (ww - xzero) / (tmax - tmin);
+      hfact = (wh - yzero) / hmax;
       xaxis();
       yaxis();
+      drawplot(cold_d, "blue");
+      drawplot(hot_d, "red");
     } else {
-      showloading();
+      showempty();
     }
   }
 
   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;
-    tfact = (ww - xzero) / (tmax - tmin);
+    //dbg.innerHTML = "from " + tmin + " to " + tmax
+    //              + "<br>from " + showdate(tmin) + " to " + showdate(tmax);
     /* differetiate() updates hmax */
     hmax = 0;
     cold_d = differentiate(data.cold);
     hot_d = differentiate(data.hot);
-    hfact = (wh - yzero) / hmax;
     //dbg.innerHTML = "hmax=" + hmax + " hfact=" + hfact + "<br>"
     //              + cold_d + "<br>" + hot_d;
     redraw();
   }
 
-  function sendquery(lo, hi) {
-    var url = "query.cgi";
+  function sendreq(qstr) {
+    var url = "query.cgi" + qstr;
+    var xmlhttp = new XMLHttpRequest();
 
-    if (lo && hi) url += "?lo=" + lo + "&" + hi;
-    else url = "query.cgi?lo=2015-12-19+00:00:00&hi=2015-12-20+00:00:00"; //FIX
+    //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();
+    showloading();
     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;
     redraw();
   }
 
+  function daystart(date) {
+    date.setMilliseconds(0);
+    date.setSeconds(0);
+    date.setMinutes(0);
+    date.setHours(0);
+    return date;
+  }
+
+  function prevweek() {
+    var tdy = daystart(new Date());
+    var dow = tdy.getDay();
+    var wstart, wend;
+
+    wstart = new Date(1*tdy - 86400000 * (dow + 7));
+    wend = new Date(1*wstart + 86400000 * 7);
+    sendquery(wstart.toISOString(), wend.toISOString());
+  }
+
+  function thisweek() {
+    var tdy = daystart(new Date());
+    var dow = tdy.getDay();
+    var wstart, wend;
+
+    wstart = new Date(1*tdy - 86400000 * dow);
+    wend = new Date(1*wstart + 86400000 * 7);
+    sendquery(wstart.toISOString(), wend.toISOString());
+  }
+
+  function beforeyesterday() {
+    var tdy = daystart(new Date());
+    var ytd = new Date(1*tdy - 86400000);
+    var byd = new Date(1*ytd - 86400000);
+    sendquery(byd.toISOString(), ytd.toISOString());
+  }
+
+  function yesterday() {
+    var tdy = daystart(new Date());
+    var ytd = new Date(1*tdy - 86400000);
+    sendquery(ytd.toISOString(), tdy.toISOString());
+  }
+
+  function today() {
+    var tdy = daystart(new Date());
+    var tmr = new Date(1*tdy + 86400000);
+    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");
-    xmlhttp = new XMLHttpRequest();
     resize();
-    sendquery();
+    if (qstr) sendreq(qstr);
+    else today();
+    
+    document.getElementById("today").onclick = today;
+    document.getElementById("yesterday").onclick = yesterday;
+    document.getElementById("beforeyesterday").onclick = beforeyesterday;
+    document.getElementById("thisweek").onclick = thisweek;
+    document.getElementById("prevweek").onclick = prevweek;
   }
 
   /* 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 {
+  font-family: PipeDream;
+  src: url('PIPED.TTF') format('truetype');
+  /* Free to use font from http://www.mlink.net/~paterson/jpfonts.htm */
+}
 h1 {
+  margin-top: 5px;
   text-align: center;
+  font-family: PipeDream;
+  font-size: 64px;
+  font-weight: normal;
+  background-color: lightgray;
 }
 br {
   clear: both;
@@ -161,17 +399,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;
 }
@@ -181,7 +427,35 @@ canvas#plot {
   display: block;
   width: 640px;
   height: 320px;
+  /* border: solid 1px black; */
+}
+div#errormsg {
+  visibility: hidden;
+  color: red;
+  text-align: center;
+}
+div#queries {
+  margin-left: auto;
+  margin-right: auto;
+  margin-bottom: 10px;
+  text-align: center;
+}
+div.query {
+  display: inline-block;
+  position: relative;
+  width: 8em;
+  height: 8em;
   border: solid 1px black;
+  border-radius: 1em;
+  background-color: lightgray;
+  cursor: pointer;
+}
+div.label {
+  display: block;
+  width: 100%;
+  position: absolute;
+  top: 50%;
+  transform: translate(0, -50%);
 }
 body {
   margin: 0px;
@@ -189,15 +463,31 @@ body {
 </style>
 <title>Water Meters</title>
 </head><body>
-<h1>Water Meters</h1>
+<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=debug>
+<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
+   BEFORE YESTERDAY</div></div>
+ <div class="query" id="yesterday"><div class="label">YESTERDAY</div></div>
+ <div class="query" id="today"><div class="label">TODAY</div></div>
+ <div class="query" id="thisweek"><div class="label">THIS WEEK</div></div>
+</div>
+<br />
+<div id="debug"></div>
 </body>