9f6f4e70e9472e6ef806b59b3b37dba970c7de1c
[ampy.git] / html / graph.php
1 <?php
2
3 require("config.php");
4
5 $IMGFILE="/tmp/power.svg";
6
7 $hours = array_key_exists('h', $_REQUEST) ? (int)$_REQUEST['h'] : 0;
8 $minutes = array_key_exists('m', $_REQUEST) ? (int)$_REQUEST['m'] : 0;
9 if (!$hours && !$minutes)
10         $hours = 1;
11 $params = [
12         "--daemon", RRDSOCK,
13         "--imgformat", "SVG",
14         "-s", "now-".$hours."h".$minutes."m",
15         "--width", "".IMGWIDTH,
16         "--height", "".IMGHEIGHT,
17         "--vertical-label", "watts",
18         "--lower-limit", "0",
19         "DEF:watts=".RRDFILE.":watts:AVERAGE",
20         #"DEF:maxwatts=".RRDFILE.":watts:MAX:step=".(60*(60*$hours)+$minutes),
21         #"DEF:maxwatts=".RRDFILE.":watts:MAX",
22         "CDEF:wh_raw=watts,".(float)($hours+$minutes/60.).",*",
23         "VDEF:watts_avg=watts,AVERAGE",
24         "VDEF:wh=wh_raw,AVERAGE",
25         "AREA:watts#FF0000",
26         "LINE:watts_avg#00FF00",
27         "GPRINT:watts_avg:Average %6.2lf %swatts",
28         "GPRINT:wh:Total %6.2lf %swh",
29         #"LINE:maxwatts#0000FF",
30         #"GPRINT:maxwatts:Max %6.2lf %sW",
31 ];
32
33 /*header("Content-Type: text/plain");
34 foreach ($params as $param) echo(escapeshellarg($param)." ");
35 return;*/
36
37 $result=rrd_graph("$IMGFILE", $params);
38 if ($result === FALSE) {
39         echo("Error in rrd_graph:<br>");
40         echo(rrd_error());
41         die();
42 }
43 header("Content-Type: image/svg+xml");
44 header("Cache-Control: no-cache, max-age=0, must-revalidate");
45 header("Refresh: ".(60*(60*$hours)+$minutes)/IMGWIDTH);
46 readfile($IMGFILE);
47 ?>