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