<?php
require("../../inc/header.inc");
?>
<body leftmargin="15" rightmargin="10" onload="javascript:adjust_frame('down');" onresize="javascript:adjust_frame('down');" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;">

<?php
adjust_head_frame("down");
adjust_title_head_frame("down");
adjust_title_tail_frame("down");

adjust_content_head_frame("down");	
echo "<br>";
	$DEBUG=0;

	
	if(!isset($sel_date_from))
		$sel_date_from=0;
	
	if(!isset($sel_date_to))
		$sel_date_to=0;
	
	if($DEBUG)
		echo "sel_date_from=$sel_date_from,sel_date_to=$sel_date_to<br>";
		
	$fieldNameAttr=array("Attempt",
		                   "Success",
		                   "Operation fail",
		                   "Create <br>VIM fail",
		                   "Connect <br>local fail",
		                   "Connect <br>VIM fail",
		                   "Notification <br>request fail",
		                   "Exception <br>retrieve fail",
		                   "Notify retrieve fail");	
	
	$fieldAttr=array("Attempt",
									 "Success",
									 "oper_fail",
									 "cre_vim_fail",
									 "conn_local_fail",
									 "conn_vim_fail",
									 "noti_req_fail",
									 "excep_retr_fail",
									 "noti_retr_fail");         
	for($fi=0;$fi<sizeof($fieldNameAttr);$fi++)
	{
		$tmpV=$fieldAttr[$fi];
		$csta_data[1][$tmpV]=0;
		$csta_data[2][$tmpV]=0;
	}
		                    
	$slot=$sel_date_from-$sel_date_to+1;
	for($day=0;$day<$slot;$day++)
	{            
		$db="CSTA_DB";
		$table_name="irlsDetailData";
		$temp=date("d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day,date("Y")));
		$table_name=$table_name;
		
		//echo "table_name:$table_name<br>";	
		$from_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day,date("Y")));
		$to_time=date("Y-m-d H:i:s",mktime(23,0,0,date("m"),date("d")-$sel_date_from+$day,date("Y")));
		//echo "from:$from_time--to:$to_time<br>";
		
		//get data from database			
		$select="select key_type,sum(Attempt) as Attempt,
						 								 sum(Success) as Success,
														 sum(oper_fail) as oper_fail,
														 sum(cre_vim_fail) as cre_vim_fail,
														 sum(conn_local_fail) as conn_local_fail,
														 sum(conn_vim_fail) as conn_vim_fail,
														 sum(noti_req_fail) as noti_req_fail,
														 sum(excep_retr_fail) as excep_retr_fail,
														 sum(noti_retr_fail) as noti_retr_fail ";
		$from="from $table_name ";
		$where="where csta_datetime >='$from_time' and csta_datetime <'$to_time' ";
		$group="group by key_type ";
		$sql=$select.$from.$where.$group;
		if($DEBUG)echo "$sql<br>";
		$result=@mysqli_query($pubConn,$sql);
		$rows=@mysqli_fetch_array($result);
						
		if($rows)
		{
			do{
					$key_type=$rows[key_type];
					
					for($fi=0;$fi<sizeof($fieldAttr);$fi++)
					{
						$tmpVar=$fieldAttr[$fi];
						$csta_data[$key_type][$tmpVar]+=$rows[$tmpVar]+0;
					}
			}while($rows=mysqli_fetch_array($result));	
		}
		
		//get the 0-hour
		$table_name="irlsDetailData";
		$temp=date("d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day-1,date("Y")));
		$table_name=$table_name;
		
		//echo "table_name:$table_name<br>";	
		$from_time=date("Y-m-d H:i:s",mktime(23,0,0,date("m"),date("d")-$sel_date_from+$day-1,date("Y")));
		$to_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day,date("Y")));
		//echo "from:$from_time--to:$to_time<br>";
		
		$from="from $table_name ";
		$where="where csta_datetime >='$from_time' and csta_datetime <'$to_time' ";
		$group="group by key_type ";
		$sql=$select.$from.$where.$group;
		if($DEBUG)echo "$sql<br>";
		$result=@mysqli_query($pubConn,$sql);
		$rows=@mysqli_fetch_array($result);
						
		if($rows)
		{
			do{
					$key_type=$rows[key_type];
					
					for($fi=0;$fi<sizeof($fieldAttr);$fi++)
					{
						$tmpVar=$fieldAttr[$fi];
						$csta_data[$key_type][$tmpVar]+=$rows[$tmpVar]+0;
					}
			}while($rows=mysqli_fetch_array($result));	
		}
	}
	if($DEBUG)
	{
		echo "<pre>";
		print_r($csta_data);
		echo "</pre>";
	}
	
	//table title
	$bgn_time=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from,date("Y")));
	$end_time=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_to,date("Y")));
	if($slot==1)
		$table_title="iRLS Statistic Date:".$bgn_time;
	else
		$table_title="iRLS Statistic Date:".$bgn_time." to ".$end_time;	
	echo "<center><b>$table_title</b></center>";
	
	//show the data
	echo "<table border=\"1\" width=\"100%\" bordercolordark=\"#FFFFFF\" bordercolor=\"#808080\" cellspacing=\"0\">";
	echo "<tr><td bgcolor=cecece align=center>Type</td>";
	
	for($fi=0;$fi<sizeof($fieldNameAttr);$fi++)
	{
			echo "<td bgcolor=cecece align=center>{$fieldNameAttr[$fi]}</td>";
	}
	echo "</tr>";
	
	echo "<tr><td bgcolor=cecece align=center>MO</td>";
	for($fi=0;$fi<sizeof($fieldNameAttr);$fi++)
	{
		$tmpVar=$fieldAttr[$fi];
		echo "<td>{$csta_data[1][$tmpVar]}</td>";
	}
	echo "</tr>";
	
	echo "<tr><td bgcolor=cecece align=center>MT</td>";
	for($fi=0;$fi<sizeof($fieldNameAttr);$fi++)
	{
		$tmpVar=$fieldAttr[$fi];
		echo "<td>{$csta_data[2][$tmpVar]}</td>";
	}
	echo "</tr>";
	echo "</table>";
?>

<?php
	echo "<p><b>2.Attempt(Success/Failure)</b><br>";
  //=====graphics size=======
	$sizex=550;
	$sizey=300;
	
	$im = ImageCreate ($sizex, $sizey);
	$background_color=ImageColorAllocate($im,255,255,255);
	$black_color=ImageColorAllocate($im,0,0,0);
	$white_color= imagecolorallocate($im, 255, 255, 255);
	
	$bgcolor=F8F8F8;
	$fgcolor=000000;
	
	$background_color = ImageColorAllocate ($im, 
				hexdec(substr($bgcolor, 0, 2)), 
				hexdec(substr($bgcolor, 2, 2)),
				hexdec(substr($bgcolor, 4, 2)));
	
	$text_color = ImageColorAllocate ($im, 
				hexdec(substr($fgcolor, 0, 2)), 
				hexdec(substr($fgcolor, 2, 2)),
				hexdec(substr($fgcolor, 4, 2)));
	
	//====graphics background=======
	ImageFilledRectangle($im,0,0,$sizex,$sizey,ImageColorAllocate ($im, 198, 198, 198));
	ImageFilledRectangle($im,2,2,$sizex,$sizey,ImageColorAllocate ($im, 130, 130, 130));
	$pointArr=array(0,$sizey-1,3,$sizey-4,4,$sizey-1);
	Imagefilledpolygon($im, $pointArr, 3, ImageColorAllocate ($im, 130, 130, 130));
	$pointArr=array($sizex-1,0,$sizex-4,3,$sizex,4);
	Imagefilledpolygon($im, $pointArr, 3, ImageColorAllocate ($im, 130, 130, 130));
	ImageFilledRectangle($im,2,2,$sizex-3,$sizey-3,$background_color);
	
	imagedashedline($im,30,30,100,100,$black_color);
	
	//draw the outline
	imageLine($im,40,20,40,200,$black_color);
	imageLine($im,40,20,480,20,$black_color);
	imageLine($im,480,200,480,20,$black_color);
	imageLine($im,40,200,480,200,$black_color);
	
	
	//get the max value
	$maxY=100;
	for($i=1;$i<3;$i++)
	{
		for($j=0;$j<sizeof($fieldAttr);$j++)
		{
			$tmpVar=$fieldAttr[$j];
			//echo "$i $tmpVar {$csta_data[$i][$tmpVar]}<br>";
			if($csta_data[$i][$tmpVar]>$maxY)
			{
				
				$maxY=$csta_data[$i][$tmpVar];
			}
		}
	}
	//echo "maxY=$maxY<br>";
	$style = array($black_color,$black_color,$black_color,$black_color,$black_color,$white_color,$white_color,$white_color,$white_color,$white_color);
	imagesetstyle($im, $style);
	for($i=10;$i>0;$i--)
	{
		$x1=40;
		$y1=20+(10-$i)*18;
		$x2=480;
		$y2=$y1;
		
		if($i!=10)
		{
			imageline($im, $x1,$y1,$x2,$y2, IMG_COLOR_STYLED);
		}
		
		$tmpV=$maxY/10*$i;
		$tmpV=floor($tmpV);
		if($tmpV > 10000)
		{
			$tmpV=floor($tmpV/100);
			$tmpV=$tmpV/10;
			$tmpV.="K";
		}
		imagestring($im,2,$x1-30,$y2-5,"$tmpV",$black_color);		
	}
	
	//=======draw the vitical line=======
	$u=floor(450/9);
	for($i=0;$i<9;$i++)
	{
		$x1=40+$i*$u;
		$y1=20;
		$x2=$x1;
		$y2=200;
		ImageDashedLine($im,$x1,$y1,$x2,$y2,$black_color);
	}
	
	//=======get colors============
	$color[0]=ImageColorAllocate($im,0x99,0x00,0x33);
	$color[1]=ImageColorAllocate($im,0x99,0x00,0xcc);
	$color[2]=ImageColorAllocate($im,0x99,0x99,0x00);
	$color[3]=ImageColorAllocate($im,0x99,0xcc,0x99);
	$color[4]=ImageColorAllocate($im,0x99,0xff,0x33);
	$color[5]=ImageColorAllocate($im,0xff,0x99,0x66);
	$color[6]=ImageColorAllocate($im,0xff,0x99,0xff);
	$color[7]=ImageColorAllocate($im,0xff,0xff,0x00);
	$color[8]=ImageColorAllocate($im,0x33,0x3,0x33);
	$color[9]=ImageColorAllocate($im,0x00,0xcc,0xff);
	$color[10]=ImageColorAllocate($im,0x00,0x66,0xcc);
	$color[11]=ImageColorAllocate($im,0x33,0xff,0x99);
	
	//======draw the legend string=======
	imagestring($im,2,160,205,"MO",$black_color);
	imagestring($im,2,360,205,"MT",$black_color);
	
		                 
	imagefilledrectangle($im,30,230,40,240,$color[0]);
	imagestring($im,2,50,230,"Attempt",$black_color);
	
	imagefilledrectangle($im,200,230,210,240,$color[1]);
	imagestring($im,2,220,230,"Success",$black_color);
	
	imagefilledrectangle($im,360,230,370,240,$color[2]);
	imagestring($im,2,380,230,"Operation fail",$black_color);
	
	imagefilledrectangle($im,30,250,40,260,$color[3]);
	imagestring($im,2,50,250,"Create VIM fail",$black_color);
	
	imagefilledrectangle($im,200,250,210,260,$color[4]);
	imagestring($im,2,220,250,"Connect local fail",$black_color);
	
	imagefilledrectangle($im,360,250,370,260,$color[5]);
	imagestring($im,2,380,250,"Connect VIM fail",$black_color);
	
	imagefilledrectangle($im,30,270,40,280,$color[6]);
	imagestring($im,2,50,270,"Notification request fail",$black_color);
	
	imagefilledrectangle($im,200,270,210,280,$color[7]);
	imagestring($im,2,220,270,"Exception retrieve fail",$black_color);
	
	imagefilledrectangle($im,360,270,370,280,$color[8]);
	imagestring($im,2,380,270,"Notify retrieve fail",$black_color);
	
	//==============draw the block=================
	$start_x=40;
	$end_x=480;
	$start_y=20;
	$end_y=200;
	
	$rect_index=0;
	//mo Attempt
	$mo_rect[$rect_index++]=array(
			'x1'=>($start_x+$u),
			'y1'=>($end_y-$csta_data[1]['Attempt']/$maxY*($end_y-$start_y)),
			'x2'=>($start_x+2*$u),
			'y2'=>($end_y)
	);
	
	$previous=0;
	//mo Success
	$mo_rect[$rect_index++]=array(
		'x1'=>($start_x+3*$u),
		'y1'=>($end_y-$csta_data[1]['Success']/$maxY*($end_y-$start_y)),
		'x2'=>($start_x+4*$u),
		'y2'=>($end_y)
	);
	$previous+=$csta_data[1]['Success']/$maxY*($end_y-$start_y);
	
	//mo oper_fail
	$mo_rect[$rect_index++]=array(
		'x1'=>($start_x+3*$u),
		'y1'=>($end_y-$csta_data[1]['oper_fail']/$maxY*($end_y-$start_y)-$previous),
		'x2'=>($start_x+4*$u),
		'y2'=>($end_y-$previous)
	);
	$previous+=$csta_data[1]['oper_fail']/$maxY*($end_y-$start_y);
	
	//mo cre_vim_fail
	$mo_rect[$rect_index++]=array(
		'x1'=>($start_x+3*$u),
		'y1'=>($end_y-$csta_data[1]['cre_vim_fail']/$maxY*($end_y-$start_y)-$previous),
		'x2'=>($start_x+4*$u),
		'y2'=>($end_y-$previous)
	);
	$previous+=$csta_data[1]['cre_vim_fail']/$maxY*($end_y-$start_y);
	
	//mo conn_local_fail
	$mo_rect[$rect_index++]=array(
		'x1'=>($start_x+3*$u),
		'y1'=>($end_y-$csta_data[1]['conn_local_fail']/$maxY*($end_y-$start_y)-$previous),
		'x2'=>($start_x+4*$u),
		'y2'=>($end_y-$previous)
	);
	$previous+=$csta_data[1]['conn_local_fail']/$maxY*($end_y-$start_y);
	
	//mo conn_vim_fail
	$mo_rect[$rect_index++]=array(
		'x1'=>($start_x+3*$u),
		'y1'=>($end_y-$csta_data[1]['conn_vim_fail']/$maxY*($end_y-$start_y)-$previous),
		'x2'=>($start_x+4*$u),
		'y2'=>($end_y-$previous)
	);
	$previous+=$csta_data[1]['conn_vim_fail']/$maxY*($end_y-$start_y);
	
	//mo noti_req_fail
	$mo_rect[$rect_index++]=array(
		'x1'=>($start_x+3*$u),
		'y1'=>($end_y-$csta_data[1]['noti_req_fail']/$maxY*($end_y-$start_y)-$previous),
		'x2'=>($start_x+4*$u),
		'y2'=>($end_y-$previous)
	);
	$previous+=$csta_data[1]['noti_req_fail']/$maxY*($end_y-$start_y);
	
	//mo excep_retr_fail
	$mo_rect[$rect_index++]=array(
		'x1'=>($start_x+3*$u),
		'y1'=>($end_y-$csta_data[1]['excep_retr_fail']/$maxY*($end_y-$start_y)-$previous),
		'x2'=>($start_x+4*$u),
		'y2'=>($end_y-$previous)
	);
	$previous+=$csta_data[1]['excep_retr_fail']/$maxY*($end_y-$start_y);
	
	//mo noti_retr_fail
	$mo_rect[$rect_index++]=array(
		'x1'=>($start_x+3*$u),
		'y1'=>($end_y-$csta_data[1]['noti_retr_fail']/$maxY*($end_y-$start_y)-$previous),
		'x2'=>($start_x+4*$u),
		'y2'=>($end_y-$previous)
	);
	$previous+=$csta_data[1]['noti_retr_fail']/$maxY*($end_y-$start_y);
	
	for($i=0;$i<$rect_index;$i++)
	{
		imagefilledrectangle($im,
			$mo_rect[$i]['x1'],
			$mo_rect[$i]['y1'],
			$mo_rect[$i]['x2'],
			$mo_rect[$i]['y2'],
			$color[$i]);
	}
	
	//mt Attempt
	$rect_index=0;
	$mt_rect[$rect_index++]=array(
			'x1'=>($start_x+5*$u),
			'y1'=>($end_y-$csta_data[2]['Attempt']/$maxY*($end_y-$start_y)),
			'x2'=>($start_x+6*$u),
			'y2'=>($end_y)
	);
	
	$previous=0;
	//mt Success
	$mt_rect[$rect_index++]=array(
		'x1'=>($start_x+7*$u),
		'y1'=>($end_y-$csta_data[2]['Success']/$maxY*($end_y-$start_y)),
		'x2'=>($start_x+8*$u),
		'y2'=>($end_y)
	);
	$previous+=$csta_data[2]['Success']/$maxY*($end_y-$start_y);
	
	//mt oper_fail
	$mt_rect[$rect_index++]=array(
		'x1'=>($start_x+7*$u),
		'y1'=>($end_y-$csta_data[2]['oper_fail']/$maxY*($end_y-$start_y)-$previous),
		'x2'=>($start_x+8*$u),
		'y2'=>($end_y-$previous)
	);
	$previous+=$csta_data[2]['oper_fail']/$maxY*($end_y-$start_y);
	
	//mt cre_vim_fail
	$mt_rect[$rect_index++]=array(
		'x1'=>($start_x+7*$u),
		'y1'=>($end_y-$csta_data[2]['cre_vim_fail']/$maxY*($end_y-$start_y)-$previous),
		'x2'=>($start_x+8*$u),
		'y2'=>($end_y-$previous)
	);
	$previous+=$csta_data[2]['cre_vim_fail']/$maxY*($end_y-$start_y);
	
	//mt conn_local_fail
	$mt_rect[$rect_index++]=array(
		'x1'=>($start_x+7*$u),
		'y1'=>($end_y-$csta_data[2]['conn_local_fail']/$maxY*($end_y-$start_y)-$previous),
		'x2'=>($start_x+8*$u),
		'y2'=>($end_y-$previous)
	);
	$previous+=$csta_data[2]['conn_local_fail']/$maxY*($end_y-$start_y);
	
	//mt conn_vim_fail
	$mt_rect[$rect_index++]=array(
		'x1'=>($start_x+7*$u),
		'y1'=>($end_y-$csta_data[2]['conn_vim_fail']/$maxY*($end_y-$start_y)-$previous),
		'x2'=>($start_x+8*$u),
		'y2'=>($end_y-$previous)
	);
	$previous+=$csta_data[2]['conn_vim_fail']/$maxY*($end_y-$start_y);
	
	//mt noti_req_fail
	$mt_rect[$rect_index++]=array(
		'x1'=>($start_x+7*$u),
		'y1'=>($end_y-$csta_data[2]['noti_req_fail']/$maxY*($end_y-$start_y)-$previous),
		'x2'=>($start_x+8*$u),
		'y2'=>($end_y-$previous)
	);
	$previous+=$csta_data[2]['noti_req_fail']/$maxY*($end_y-$start_y);
	
	//mt excep_retr_fail
	$mt_rect[$rect_index++]=array(
		'x1'=>($start_x+7*$u),
		'y1'=>($end_y-$csta_data[2]['excep_retr_fail']/$maxY*($end_y-$start_y)-$previous),
		'x2'=>($start_x+8*$u),
		'y2'=>($end_y-$previous)
	);
	$previous+=$csta_data[2]['excep_retr_fail']/$maxY*($end_y-$start_y);
	
	//mt noti_retr_fail
	$mt_rect[$rect_index++]=array(
		'x1'=>($start_x+7*$u),
		'y1'=>($end_y-$csta_data[2]['noti_retr_fail']/$maxY*($end_y-$start_y)-$previous),
		'x2'=>($start_x+8*$u),
		'y2'=>($end_y-$previous)
	);
	$previous+=$csta_data[2]['noti_retr_fail']/$maxY*($end_y-$start_y);
	
	for($i=0;$i<$rect_index;$i++)
	{
		imagefilledrectangle($im,
			$mt_rect[$i]['x1'],
			$mt_rect[$i]['y1'],
			$mt_rect[$i]['x2'],
			$mt_rect[$i]['y2'],
			$color[$i]);
	}

	//======show the service attempt graphics==========
	//-----输出图形文件，请勿修改
	//delete *.png
	$handle=opendir('.');
	while($file=readdir($handle)){
		$retval="";
		$pt=strrpos($file,".");
		if($pt) $retval=substr($file,$pt+1,strlen($file)-$pt);
		if($retval=='png') unlink($file);
	}
	$filename="irls_".time().".png";
	if(1){
		ImagePng ($im,"$filename");
		
		ImageDestroy($im);	
	}
	//-----显示图片  
	echo "<img src=\"$filename\"></img>";
adjust_content_tail("down");
?>
