graph: Added support for days and steps
[ampy.git] / html / graph.php
index 4675620a0e5066fcd760aa1d9fd75ec001f70c17..34f73ba30a2b2ee2e36ffb040b7d7880d48e37d0 100644 (file)
@@ -1,25 +1,53 @@
 <?php
-$RRDFILE="/home/nirgal/ampy/power.rrd";
+
+require("config.php");
+
 $IMGFILE="/tmp/power.svg";
-$REFRESH=30;
 
-$result=rrd_graph("$IMGFILE", [
+$days = array_key_exists('d', $_REQUEST) ? (int)$_REQUEST['d'] : 0;
+$hours = array_key_exists('h', $_REQUEST) ? (int)$_REQUEST['h'] : 0;
+$minutes = array_key_exists('m', $_REQUEST) ? (int)$_REQUEST['m'] : 0;
+if (!$days && !$hours && !$minutes)
+       $hours = 1;
+$step = array_key_exists('step', $_REQUEST) ? (int)$_REQUEST['step'] : 0;
+$params = [
+       "--daemon", RRDSOCK,
        "--imgformat", "SVG",
-       "-s", "now-1h",
-       #"-e", "now-8h",
-       "--width", "1000",
-       "--height", "500",
+       "-s", "now-".$days."d".$hours."h".$minutes."m",
+       "--width", "".IMGWIDTH,
+       "--height", "".IMGHEIGHT,
        "--vertical-label", "watts",
        "--lower-limit", "0",
-       #"--logarithmic",
-       "DEF:watts=${RRDFILE}:watts:AVERAGE",
-       "LINE1:watts#FF0000"
-]);
+       "DEF:watts=".RRDFILE.":watts:AVERAGE",
+       #"DEF:maxwatts=".RRDFILE.":watts:MAX:step=".(60*(60*$hours)+$minutes),
+       #"DEF:maxwatts=".RRDFILE.":watts:MAX",
+       "CDEF:wh_raw=watts,".(float)($hours+$minutes/60.).",*",
+       "VDEF:watts_avg=watts,AVERAGE",
+       "VDEF:wh=wh_raw,AVERAGE",
+       "AREA:watts#FF0000",
+       "LINE:watts_avg#00FF00",
+       "GPRINT:watts_avg:Average %6.2lf %swatts",
+       "GPRINT:wh:Total %6.2lf %swh",
+       #"LINE:maxwatts#0000FF",
+       #"GPRINT:maxwatts:Max %6.2lf %sW",
+];
+if ($step) {
+       $params[] = '--step';
+       $params[] = "$step";
+}
+
+/*header("Content-Type: text/plain");
+foreach ($params as $param) echo(escapeshellarg($param)." ");
+return;*/
+
+$result=rrd_graph("$IMGFILE", $params);
 if ($result === FALSE) {
-       echo("Error in rrd_graph");
+       echo("Error in rrd_graph:<br>");
+       echo(rrd_error());
        die();
 }
 header("Content-Type: image/svg+xml");
-#header("Refresh: $REFRESH");
+header("Cache-Control: no-cache, max-age=0, must-revalidate");
+header("Refresh: ".(60*(60*(24*$days+$hours))+$minutes)/IMGWIDTH);
 readfile($IMGFILE);
 ?>