<?php
//
// A PHP file for PHPMOTIONDETECT, to plot data from the database
//
//
if (!function_exists("imagecreate")) {
echo "<H1>This version of PHP was compiled with no GD support, so no graphs can be shown.</H1>";
return -1;
}
if (!isset($ndays)) {$ndays=1;}
if (!isset($nentries)) {$nentries=500;}
echo " <FORM METHOD=\"GET\" ACTION=\"$PHP_SELF\">\n";
echo "<TABLE WIDTH=\"800\" BORDER=\"0\" CELLSPACING=\"1\" CELLPADDING=\"5\" ALIGN=\"center\">\n";
echo "<TR><TH>";
echo "Show <SELECT style=\"width:80px;\" name=nentries size=1>\n";
for ($i = 1; $i <= 50; $i++) {
$show=$i*100;
if ($show==$nentries) {
echo "<OPTION SELECTED>$show";
} else {
echo "<OPTION>$show";
}
}
echo "</SELECT> entries \n";
echo " -- ";
echo "Show <SELECT style=\"width:80px;\" name=days size=1>\n";
for ($i = 1; $i <= 50; $i++) {
$show=$i*1;
if ($show==$ndays) {
echo "<OPTION SELECTED>$show";
} else {
echo "<OPTION>$show";
}
}
echo "</SELECT> days \n";
echo " <INPUT TYPE=\"submit\" NAME=\"show\" VALUE=\"Update\" >\n";
echo "</TH></TR>\n";
echo "</TABLE>\n";
echo "</FORM>\n";
// SHOW SELECTED GRAPHS
BrightnessGraph_entries($nentries,$db);
BrightnessGraph_days($ndays,$db);
function BrightnessGraph_days($limit_days,$db) {
$picWidth=720;
$picHeight=200;
$now = time();
$limit_seconds=$limit_hour*3600;
//$limit=15000; // ENTRIES TO PLOT
//$result = mysql_query("SELECT id,diff,bright,bright_ref,UNIX_TIMESTAMP(timestamp) as time FROM log ORDER BY time DESC LIMIT 0,20000",$db);
//$result = mysql_query("SELECT id,diff,bright,bright_ref,UNIX_TIMESTAMP(timestamp) as time, UNIX_TIMESTAMP(timestamp) AS logtime, UNIX_TIMESTAMP() AS nowtime FROM log WHERE (TO_DAYS(NOW()) - TO_DAYS(timestamp)) <= $limit_days ORDER BY time LIMIT 0,15000",$db);
$result = mysql_query("SELECT id,stored,diff,bright,bright_ref,timestamp,UNIX_TIMESTAMP(timestamp) as time FROM log WHERE (TO_DAYS(NOW()) - TO_DAYS(timestamp)) <= $limit_days ORDER BY time LIMIT 0,115000",$db);
$nresults= mysql_affected_rows();
if ($nresults>1000) {
$jump=round($nresults/1000);
} else {
$jump=1;
}
$i=0;$ientry=0;
if ($myrow = mysql_fetch_array($result)) {
$i=0;$ientry=0;$istored=0;
do {
$stored=$myrow["stored"];
if ($stored=="Stored") {
$time_stored[$istored]=$myrow["time"];
//echo "$istored, $stored, $time_stored[$istored]<BR>";
$istored++;
}
if (($i/$jump)==round($i/$jump)) {
//printf("%3.2f %3.2f -- %3.2f %3.2f %3.2f<BR>\n",$nresults,$jump,$i/$jump,round($i/$jump),$i);
$id=$myrow["id"];
$time[$ientry]=$myrow["time"];
$diff[$ientry]=$myrow["diff"];
$bright[$ientry]=$myrow["bright"];
$bright_ref[$ientry]=$myrow["bright_ref"];
// printf("%s %s - %s -- %s <BR>",$bright_ref[$i],$bright[$i],$diff[$i],$nowtime[$i]-$logtime[$i]);
$ientry++;
}
$i++;
} while ($myrow = mysql_fetch_array($result));
} else {
echo "No records found";
}
$npoints=$ientry-1;
$min_time=min($time);$max_time=max($time);$d_time=$max_time-$min_time;
$min_bright=min($bright);$max_bright=max($bright);$d_bright=($max_bright-$min_bright);
//$min_diff=min($diff);$max_diff=max($diff);$d_diff=$max_diff-$min_diff;
$min_diff=-10;$max_diff=10;$d_diff=$max_diff-$min_diff;
if ($d_bright==0) {$d_bright=1;}
if ($d_time==0) {$d_time=1;}
// CREATE FIG.
$pic=imagecreate($picWidth+1,$picHeight+1);
$cWhite=ImageColorAllocate($pic,255,255,255);
ImageFilledRectangle($pic,0,0,$picWidth+1,$picHeight+1,$cWhite);
$cRed=ImageColorAllocate($pic,255,0,0);
$cGreen=ImageColorAllocate($pic,0,255,0);
$cBlue=ImageColorAllocate($pic,0,0,255);
$cBlack=ImageColorAllocate($pic,0,0,0);
//$curX1=0;
//$curY1=$picHeight;
$nmean=20; // NUMBER OF DATA TO USE FOR MEAN
for($pt=0;$pt<$npoints;$pt++){
$newX1=$picWidth*($time[$pt]-$min_time)/$d_time;
$newY1=$picHeight-($picHeight*($bright[$pt]-$min_bright)/$d_bright);
$newX2=$picWidth*($time[$pt]-$min_time)/$d_time;
$newY2=$picHeight-($picHeight*($bright_ref[$pt]-$min_bright)/$d_bright);
$newX3=$picWidth*($time[$pt]-$min_time)/$d_time;
$newY3=$picHeight-($picHeight*($diff[$pt]-$min_diff)/$d_diff);
// CALCUALTE SMOOTH MEAN OF DIFF
$meandiff=0;
if ($pt>$nmean) {
for($i=0;$i<$nmean;$i++){
$meandiff=$meandiff+abs($diff[$pt-$i]);
}
$meandiff=$meandiff/$nmean;
$Ymean=$picHeight-($picHeight*($meandiff-$min_diff)/$d_diff);
ImageLine($pic,$curX3,$curYmean,$newX3,$Ymean,$cGreen);
}
if ($pt>0) {
ImageLine($pic,$curX1,$curY1,$newX1,$newY1,$cRed);
ImageLine($pic,$curX2,$curY2,$newX2,$newY2,$cBlue);
ImageLine($pic,$curX3,$curY3,$newX3,$newY3,$cBlack);
}
// UNCOMMENT THE NEXT FOUR LINES FOR A DOTTED PLOT INSTEAD OF A LINE PLOT
$curX1=$newX1; $curY1=$newY1;
$curX2=$newX2; $curY2=$newY2;
$curX3=$newX3; $curY3=$newY3;
$curYmean=$Ymean;
}
// NOW DRAW POINTS FOR EACH TAKEN SNAPSHOT
for($pt=0;$pt<($istored-1);$pt++){
$X=$picWidth*($time_stored[$pt]-$min_time)/$d_time;
ImageLine($pic,$X,0,$X,5,$cGreen);
}
// FINALLY DRAW THRESHOLD ON TO
$threshold=2; // THIS SHOULD BE READ FROM DB OR FROM CONF FILE !
$Top=$picHeight-($picHeight*(-$threshold-$min_diff)/$d_diff);
$Base=$picHeight-($picHeight*($threshold-$min_diff)/$d_diff);
ImageLine($pic,0,$Top,$picWidth,$Top,$cBlack);
ImageLine($pic,0,$Base,$picWidth,$Base,$cBlack);
ImagePNG($pic,"./statimages/graph_$limit_days.png");
ImageDestroy($pic);
?>
<TABLE ALIGN="CENTER" CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TH CLASS="top" ALIGN="CENTER">Brightness the last <? echo $limit_days; ?> days. every <? echo $jump; ?> entry is plotted</TH>
</TR>
<TR>
<TH ALIGN="CENTER"><IMG SRC="./statimages/graph_<? echo $limit_days; ?>.png"></TH>
</TR>
</TABLE>
<P></P>
<?
} // END OF FUNCTION
function BrightnessGraph_entries($limit,$db) {
$picWidth=720;
$picHeight=200;
$now = time();
$result = mysql_query("SELECT id,stored,diff,bright,bright_ref,UNIX_TIMESTAMP(timestamp) as time FROM log ORDER BY time DESC LIMIT 0,$limit",$db);
$nresults= mysql_affected_rows();
if ($nresults>1000) {
$jump=round($nresults/1000);
} else {
$jump=1;
}
if ($myrow = mysql_fetch_array($result)) {
$i=0;$ientry=0;$istored=0;
do {
$stored=$myrow["stored"];
if ($stored=="Stored") {
$time_stored[$istored]=$myrow["time"];
//echo "$istored, $stored, $time_stored[$istored]<BR>";
$istored++;
}
if (($i/$jump)==round($i/$jump)) {
$id=$myrow["id"];
$time[$ientry]=$myrow["time"];
//$logtime[$i]=$myrow["logtime"];
//$nowtime[$i]=$myrow["nowtime"];
$diff[$ientry]=$myrow["diff"];
$bright[$ientry]=$myrow["bright"];
$bright_ref[$ientry]=$myrow["bright_ref"];
//printf("%s ---- %s %s - %s -- %s <BR>",$i,$bright_ref[$i],$bright[$i],$diff[$i],$nowtime[$i]-$logtime[$i]);
$ientry++;
}
$i++;
} while ($myrow = mysql_fetch_array($result));
} else {
echo "No records found";
}
$npoints=$ientry-1;
$min_time=min($time);$max_time=max($time);$d_time=$max_time-$min_time;
$min_bright=min($bright);$max_bright=max($bright);$d_bright=($max_bright-$min_bright);
$min_diff=min($diff);$max_diff=max($diff);$d_diff=$max_diff-$min_diff;
$min_diff=-10;$max_diff=10;$d_diff=$max_diff-$min_diff;
if ($d_bright==0) {$d_bright=1;}
if ($d_time==0) {$d_time=1;}
// CREATE FIG.
$pic=imagecreate($picWidth+1,$picHeight+1);
$cWhite=ImageColorAllocate($pic,255,255,255);
ImageFilledRectangle($pic,0,0,$picWidth+1,$picHeight+1,$cWhite);
$cRed=ImageColorAllocate($pic,255,0,0);
$cGreen=ImageColorAllocate($pic,0,255,0);
$cBlue=ImageColorAllocate($pic,0,0,255);
$cBlack=ImageColorAllocate($pic,0,0,0);
//$curX1=0;
//$curY1=$picHeight;
$nmean=20;
for($pt=0;$pt<$npoints;$pt++){
$newX1=$picWidth*($time[$pt]-$min_time)/$d_time;
$newY1=$picHeight-($picHeight*($bright[$pt]-$min_bright)/$d_bright);
$newX2=$picWidth*($time[$pt]-$min_time)/$d_time;
$newY2=$picHeight-($picHeight*($bright_ref[$pt]-$min_bright)/$d_bright);
$newX3=$picWidth*($time[$pt]-$min_time)/$d_time;
$newY3=$picHeight-($picHeight*($diff[$pt]-$min_diff)/$d_diff);
// CALCUALTE SMOOTH MEAN OF DIFF
$meandiff=0;
if (($pt>($nmean/2))&($pt<($npoints-($nmean/2)))) {
for($i=-($nmean/2);$i<($nmean/2);$i++){
$meandiff=$meandiff+abs($diff[$pt-$i]);
}
$meandiff=$meandiff/$nmean;
$Ymean=$picHeight-($picHeight*($meandiff-$min_diff)/$d_diff);
ImageLine($pic,$curX3,$curYmean,$newX3,$Ymean,$cGreen);
}
if ($pt>0) {
ImageLine($pic,$curX1,$curY1,$newX1,$newY1,$cRed);
ImageLine($pic,$curX2,$curY2,$newX2,$newY2,$cBlue);
ImageLine($pic,$curX3,$curY3,$newX3,$newY3,$cBlack);
}
// UNCOMMENT THE NEXT FOUR LINES FOR A DOTTED PLOT INSTEAD OF A LINE PLOT
$curX1=$newX1; $curY1=$newY1;
$curX2=$newX2; $curY2=$newY2;
$curX3=$newX3; $curY3=$newY3;
$curYmean=$Ymean;
}
// NOW DRAW POINTS FOR EACH TAKEN SNAPSHOT
for($pt=0;$pt<($istored-1);$pt++){
$X=$picWidth*($time_stored[$pt]-$min_time)/$d_time;
ImageLine($pic,$X,0,$X,5,$cGreen);
}
// FINALLY DRAW THRESHOLD ON TOP
$threshold=2; // THIS SHOULD BE READ FROM DB OR FROM CONF FILE !
$Top=$picHeight-($picHeight*(-$threshold-$min_diff)/$d_diff);
$Base=$picHeight-($picHeight*($threshold-$min_diff)/$d_diff);
ImageLine($pic,0,$Top,$picWidth,$Top,$cBlack);
ImageLine($pic,0,$Base,$picWidth,$Base,$cBlack);
ImagePNG($pic,"./statimages/graph_entries_$limit.png");
ImageDestroy($pic);
?>
<TABLE ALIGN="CENTER" CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TH CLASS="top" ALIGN="CENTER">Last <? echo $limit; ?> entries</TH>
</TR>
<TR>
<TH ALIGN="CENTER"><IMG SRC="./statimages/graph_entries_<? echo $limit; ?>.png"></TH>
</TR>
</TABLE>
<P></P>
<?
} // END OF FUNCTION
?>