<?php
/*
* oh lord.. i think it's getting bigger
*
*/
include ("jpgraph/src/jpgraph.php");
include ("jpgraph/src/jpgraph_log.php");
include ("jpgraph/src/jpgraph_line.php");
//how i can pass the data array.. still try to figure it out
//finally i am using get method
$avg = $_GET["avg"];
$title = $_GET["title"];
$zoomin = $_GET['zoomin'];
$zoomout = $_GET['zoomout'];
$interval = $_GET["interval"];
//QUERY START and FINISH
if(isset($_GET['st'])){
$q_start_time = " AND posttime >= \"".$_GET['st']."\"";
}
if(isset($_GET['ft'])){
$q_finish_time = " AND posttime <= \"".$_GET['ft']."\"";
}
$agent_id = $_GET['agent_id'];
$db = new PDO('mysql:host=localhost;dbname=monyet', 'monyet', 'nyitnyit',array(
PDO::ATTR_PERSISTENT => true));
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//get boundary
$sql = "SELECT green, red, starttime, finishtime FROM pingmondef WHERE agent_id = ".$agent_id;
$stmt = $db->prepare($sql);
$stmt->execute();
$row_bound = $stmt->fetch(PDO::FETCH_OBJ);
$green = $row_bound->green;
$red = $row_bound->red;
$peak = $red;
$valley = $green;
$starttime = $row_bound->starttime;
$finishtime = $row_bound->finishtime;
$sql = "SELECT DATE_FORMAT(posttime, \"%k:%i\") as posttime, rtt FROM p_".$agent_id." WHERE rtt is not NULL ".$q_start_time." ".$q_finish_time."";
$stmt = $db->prepare($sql);
$stmt->execute();
$posttime = array();
$performance = array();
$greenline = array();
$redline = array();
$inactive = array();
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
if($avg==0){
foreach($result as $key => $value){
$posttime[] = $value->posttime;
$performance[] = $value->rtt;
if((strtotime($value->posttime)>strtotime($starttime))&&(strtotime($value->posttime)<strtotime($finishtime))){
$inactive[] = NULL;
}else{
$inactive[] = $value->rtt;
}
if($peak < $value->rtt){
$peak = $value->rtt;
}
if($valley > $value->rtt){
$valley = $value->rtt;
}
$greenline[] = $green;
$redline[] = $red;
}
}
else{
$total = count($result);
for($i=0;$i<$total;$i=$i+$interval){
if($i+$interval<$total){
$temp = 0;
for($z=$i;$z<($i+$interval);$z++){
$temp += $result[$z]->rtt;
}
$value = $temp/$interval;
$performance[] = $value;
$posttime[] = $result[$i]->posttime;
$greenline[] = $green;
$redline[] = $red;
if((strtotime($result[$i]->posttime)>strtotime($starttime))&&(strtotime($result[$i]->posttime)<strtotime($finishtime))){
$inactive[] = NULL;
}else{
$inactive[] = $value;
}
if($peak < $value){
$peak = $value;
}
if($valley > $value){
$valley = $value;
}
}
else{
$temp = 0;
$count = 0;
for($z=$i;$z<$total;$z++){
$temp += $result[$z]->rtt;
$count++;
}
$value = $temp/$count;
if((strtotime($result[$i]->posttime)>strtotime($starttime))&&(strtotime($result[$i]->posttime)<strtotime($finishtime))){
$inactive[] = NULL;
}else{
$inactive[] = $value;
}
$posttime[] = $result[$i]->posttime;
$performance[] = $value;
$greenline[] = $green;
$redline[] = $red;
if($peak < $value){
$peak = $value;
}
if($valley > $value){
$valley = $value;
}
}
}
$interval = 1;
}
$peak += ($peak-$valley)*0.15;
$valley -= ($peak-$valley)*0.05;
if($valley < 0){
$valley = 0;
}
// Setup the graph
$graph = new Graph(510,300);
$graph->SetMarginColor('white');
$graph->SetScale("textlin",$valley,$peak);
$graph->SetFrame(false);
$graph->SetMargin(30,50,30,30);
$graph->title->Set($title);
$graph->yaxis->HideZeroLabel();
//$graph->ygrid->SetFill(true,'#hide@address.com','#hide@address.com');
$graph->xgrid->Show();
$graph->xaxis->SetTickLabels($posttime);
$graph->xaxis->SetTextTickInterval($interval);
$txt=new Text("[-]");
$txt->SetPos(492,25);
$txt->SetFont(FF_FONT1,FS_NORMAL);
if($zoomout){
$txt->SetColor("black");
}
else{
$txt->SetColor("gray");
}
$graph->AddText($txt);
$txt=new Text("[+]");
$txt->SetPos(492,45);
$txt->SetFont(FF_FONT1,FS_NORMAL);
if($zoomin){
$txt->SetColor("black");
}
else{
$txt->SetColor("gray");
}
$graph->AddText($txt);
// Create the first line
$p1 = new LinePlot($performance);
$p1->SetColor("navy");
$p1->SetLegend('performance');
$graph->Add($p1);
$p2 = new LinePlot($greenline);
$p2->SetColor("yellow");
//$p2->SetLegend('lower bound LAN');
$p2->SetStyle("dashed");
$graph->Add($p2);
$txt=new Text("[A]");
$txt->SetPos(492,85);
$txt->SetFont(FF_FONT1,FS_NORMAL);
if($avg==0){
$txt->SetColor("black");
}
else{
$txt->SetColor("gray");
}
$graph->AddText($txt);
$txt=new Text("[D]");
$txt->SetPos(492,105);
$txt->SetFont(FF_FONT1,FS_NORMAL);
if($avg){
$txt->SetColor("black");
}
else{
$txt->SetColor("gray");
}
$graph->AddText($txt);
$p5 = new LinePlot($inactive);
$p5->SetColor("gray");
$graph->Add($p5);
// Create the third line
$p3 = new LinePlot($redline);
$p3->SetColor("red");
//$p3->SetLegend('upper bound LAN');
$p3->SetStyle("dashed");
$graph->Add($p3);
$graph->legend->SetShadow('hide@address.com',5);
$graph->legend->SetPos(0.1,0.1,'right','top');
// Output line
$graph->Stroke();
?>