<?php
include_once ("../core/config.php");
//$UTENTE = login();
//isadmin();

include_once "../jpgraph/jpgraph.php";
include_once "../jpgraph/jpgraph_bar.php";
//include_once "../jpgraph/jpgraph_line.php";
//include_once "../jpgraph/jpgraph_plotline.php";
//include_once( "../jpgraph/jpgraph_date.php" );

//$ip = $dato['ip'];
if (isset ($_GET['ip'])) 	$ip = $_GET['ip'];
if (isset ($_GET['tempo'])) 	$tempo = $_GET['tempo']; 


switch ($tempo) {
	case 'HOUR':
		$gruppo = "MINUTE";
		$format1 = "%d%H%i";
		$format2 = "%H:%i";
		$limit = 59;
		$interval = 5;
		break;
	case 'DAY':
		$gruppo = "HOUR";
		$format1 = "%m%d%H";
		$format2 = "%H";
		$limit = 23;
		$interval = 1;
		break;
	case 'WEEK':
		$gruppo = "DAY";
		$format1 = "%m%d";
		$format2= "%d.%m";
		$limit = 7;
		$interval = 1;
		break;
	case 'MONTH':
		$gruppo = "DAY";
		$format1 = "%m%d";
		$format2= "%d.%m";
		$limit = 30;
		$interval = 2;
		break;
	case 'YEAR':
		$gruppo = "MONTH";
		$format1 = "%Y%m";
		$format2= "%b";
		$limit = 12;
		$interval = 1;
		break;

}

$query = "	SELECT 
			DATE_FORMAT(data_destroy,'$format1'),
			DATE_FORMAT(data_destroy,'$format2'),
			SUM(inviati),
			SUM(ricevuti)
		FROM
			proxy_dati
		WHERE
			ip = '$ip'
		AND
			data_destroy >= DATE_SUB(NOW(), INTERVAL 1 $tempo)
		GROUP BY 
			$gruppo(data_destroy)
		UNION
			SELECT
				DATE_FORMAT(data_destroy,'$format1'),
				DATE_FORMAT(data_destroy,'$format2'),
				SUM(inviati),
				SUM(ricevuti)
			FROM
				proxy_dati_storico
			WHERE
				ip = '$ip'
			AND
				data_destroy >= DATE_SUB(NOW(), INTERVAL 1 $tempo)
			GROUP BY 
				$gruppo(data_destroy)
		UNION
			SELECT 
				DATE_FORMAT(DATE_SUB(NOW(), INTERVAL tempo $gruppo), '$format1'),
				DATE_FORMAT(DATE_SUB(NOW(), INTERVAL tempo $gruppo), '$format2'),
				0,
				0
			FROM
				(SELECT @i:= @i - 1 AS tempo FROM INFORMATION_SCHEMA.COLLATION_CHARACTER_SET_APPLICABILITY, ( SELECT @i:= $limit) As i ) As Y
			WHERE
				tempo BETWEEN 0 AND $limit;
	";

$res = mysql_query( $query, $DB_ID  );

$xdata = array();
$ydata = array(array());
$tmp = array(array());
$tag = array(array());
$alt = array(array());

while ( list ($tempo, $scritta, $inviati, $ricevuti) = mysql_fetch_array($res)) {
	$xdata[$tempo]			= $tempo;
	if (!isset ($tmp['inv'][$tempo]) ) 	$tmp['inv'][$tempo] 		= $inviati;
	if (!isset ($tmp['ric'][$tempo]) )	$tmp['ric'][$tempo] 		= $ricevuti;
	if (!isset ($tmp['tag'][$tempo]) )	$tmp['tag'][$tempo] 		= $scritta;
	if (!isset ($tmp['txtR'][$tempo]) )	$tmp['txtR'][$tempo] 		= "Ricevuti: $ricevuti";
	if (!isset ($tmp['txtI'][$tempo]) )	$tmp['txtI'][$tempo] 		= "Inviati: $inviati";
};

//print_r($xdata); exit;

asort($xdata);
foreach ($xdata as $key => $val) {
	$ydata['inv'][] = $tmp['inv'][$val];
	$ydata['ric'][] = $tmp['ric'][$val];
	$ydata['tag'][] = $tmp['tag'][$val];
	$ydata['dst'][] = "#";
	$ydata['txtR'][] = $tmp['txtR'][$val];
	$ydata['txtI'][] = $tmp['txtI'][$val];
}

//print_r($ydata); exit;

$l = 900;
$h = 400;

$g = new Graph($l,$h);
$g->SetMargin(80,20,50,80);
$g->SetScale('textlin');
$g->yaxis->SetLabelFormatCallback('byteConvert');
$g->xaxis->SetLabelAngle(45);
$g->xaxis->SetTextTickInterval($interval,0);

$g->legend->Pos(0.05,0.1);

$b1plot = new BarPlot ( $ydata['inv'] );
$b2plot = new BarPlot ( $ydata['ric'] );
$gbplot = new GroupBarPlot (array( $b1plot , $b2plot ));
//$gbplot = new AccBarPlot (array( $b1plot , $b2plot ));
$g->Add ( $gbplot ); 

$b1plot->SetFillColor('red');
$b1plot->SetShadow('gray',0.8);
$b1plot->SetLegend("Inviati");
$b1plot->SetCSIMTargets($ydata['dst'],$ydata['txtI']);

$b2plot->SetFillColor('blue');
$b2plot->SetShadow('gray',0.8);
$b2plot->SetLegend("Ricevuti");
$b2plot->SetCSIMTargets($ydata['dst'],$ydata['txtR']);

$g->xaxis->SetTickLabels($ydata['tag']);
//$g->StrokeCSIM('graph.php','');
$g->Stroke();
?>
