<?
/*
Group by: src, dst, src+dst
Order by: Any field
Start/End Hour
Start/End Date
Probe Name
Interval = all, 60, <=3600, 3600, <=86400, 86400:

SELECT
Fields
FROM
Tables
WHERE
Interval
start/end hour
start/end date
GROUP
src,dst,src+dst
ORDER
any field
*/

	$con = mysql_connect('localhost', 'cblm', 'o-soraizu');
	mysql_select_db('cblm');


	/* Select Fields */
	$sql = "SELECT ";

	reset($_REQUEST);
	while (list($key, $val) = each($_REQUEST)) {
		switch($key) {
			case 'src_name':
				$sql .= "p_src.name src_name,";
				break;

			case 'dst_name':
				$sql .= "p_dst.name dst_name,";
				break;

			case 'src_ip':
				$sql .= "INET_NTOA(data.src) src_ip,";
				break;

			case 'dst_ip':
				$sql .= "INET_NTOA(data.dst) dst_ip,";
				break;

			case 'avail':
				$sql .= "SUM(rx)/SUM(tx)*100 avail,";
				break;

			case 'loss':
				$sql .= "100-(SUM(rx)/SUM(tx)*100) loss,";
				break;

			case 'r_min':
				$sql .= "AVG(rtt_min)/1000.0 r_min,";
				break;

			case 'r_max':
				$sql .= "AVG(rtt_max)/1000 r_max,";
				break;

			case 'r_avg':
				$sql .= "AVG(rtt_avg)/1000 r_avg,";
				break;

			case 'r_per':
				$sql .= "AVG(rtt_min)/AVG(rtt_avg)*100 r_per,";
				break;

			case 'o_min':
				$sql .= "AVG(ott_min)/1000 o_min,";
				break;

			case 'o_max':
				$sql .= "AVG(ott_max)/1000 o_max,";
				break;

			case 'o_avg':
				$sql .= "AVG(ott_avg)/1000 o_avg,";
				break;

			case 'o_per':
				$sql .= "AVG(ott_min)/AVG(ott_avg)*100 o_per,";
				break;

			case 'j_min':
				$sql .= "AVG(jtr_min)/1000 j_min,";
				break;

			case 'j_max':
				$sql .= "AVG(jtr_max)/1000 j_max,";
				break;

			case 'j_avg':
				$sql .= "AVG(jtr_avg)/1000 j_avg,";
				break;

			case 'j_per':
				$sql .= "AVG(jtr_min)/AVG(jtr_avg)*100 j_per,";
				break;

			case 'count':
				$sql .= "COUNT(data.id) count,";
				break;

			case 'start':
				$sql .= "MIN(data.timestamp) start,";
				break;

			case 'end':
				$sql .= "MAX(data.timestamp) end,";
				break;
		}
	}
	$sql = ereg_replace('(^.*),$', '\1 ', $sql);
	echo "$sql<P>\n";
	

	/* From Tables */
	$sql .= "FROM data LEFT JOIN probes AS p_src ON (p_src.ip = src) LEFT JOIN probes AS p_dst ON (p_dst.ip = dst) ";

	/* Where */
	$sql .= "WHERE ";
	reset($_REQUEST);
	while (list($key, $val) = each($_REQUEST)) {
		switch($key) {
			case 'secs':
				$sql .= sprintf("secs = %lu AND ", $val);
				break;

			case 'secs_ge':
				$sql .= sprintf("secs >= %lu AND ", $val);
				break;

			case 'secs_le':
				$sql .= sprintf("secs <= %lu AND ", $val);
				break;

			case 'hour':
				$start = ereg_replace('^([0-9]+):([0-9]+)$', '\1', $_REQUEST['hour']);
				$end = ereg_replace('^([0-9]+):([0-9]+)$', '\2', $_REQUEST['hour']);

				if (is_numeric($start) && is_numeric($end)) {
					if ($start > $end) {
						$sql .= sprintf("(HOUR(data.timestamp) >= %lu OR HOUR(data.timestamp) <= %lu) AND ", $start, $end);
					} else {
						$sql .= sprintf("(HOUR(data.timestamp) >= %lu AND HOUR(data.timestamp) <= %lu) AND ", $start, $end);
					}
				}
				break;

			case 'sdate':
				break;

			case 'edate':
				break;
		}
	}
	$sql = ereg_replace('(^.*) WHERE $', '\1 ', $sql);
	$sql = ereg_replace('(^.*) AND $', '\1 ', $sql);

	/* Group */
	if (isset($_REQUEST['g_src'])) {
		$sql .= "GROUP BY p_src.ip ";
	} else if (isset($_REQUEST['g_dst'])) {
		$sql .= "GROUP BY p_dst.ip ";
	} else {
		$sql .= "GROUP BY p_src.ip,p_dst.ip ";
	}

	echo "$sql<P>\n";

	/* Order */
	$sql .= "ORDER BY ";
	reset($_REQUEST);
	while (list($key, $val) = each($_REQUEST)) {
		switch ($key) {
			case 'o_src_name':
				$sql .= "src_name";
				$sql .= ($val) ? " DESC," : ",";
				break;

			case 'o_dst_name':
				$sql .= "dst_name";
				$sql .= ($val) ? " DESC," : ",";
				break;

			case 'o_src_ip':
				$sql .= "src_ip";
				$sql .= ($val) ? " DESC," : ",";
				break;

			case 'o_dst_ip':
				$sql .= "dst_ip";
				$sql .= ($val) ? " DESC," : ",";
				break;

			case 'o_avail':
				$sql .= "avail";
				$sql .= ($val) ? " DESC," : ",";
				break;

			case 'o_loss':
				$sql .= "loss";
				$sql .= ($val) ? " DESC," : ",";
				break;

			case 'o_r_min':
				$sql .= "r_min";
				$sql .= ($val) ? " DESC," : ",";
				break;

			case 'o_r_max':
				$sql .= "r_max";
				$sql .= ($val) ? " DESC," : ",";
				break;

			case 'o_r_avg':
				$sql .= "r_avg";
				$sql .= ($val) ? " DESC," : ",";
				break;

			case 'o_r_per':
				$sql .= "r_per";
				$sql .= ($val) ? " DESC," : ",";
				break;

			case 'o_o_min':
				$sql .= "o_min";
				$sql .= ($val) ? " DESC," : ",";
				break;

			case 'o_o_max':
				$sql .= "o_max";
				$sql .= ($val) ? " DESC," : ",";
				break;

			case 'o_o_avg':
				$sql .= "o_avg";
				$sql .= ($val) ? " DESC," : ",";
				break;

			case 'o_o_per':
				$sql .= "o_per";
				$sql .= ($val) ? " DESC," : ",";
				break;

			case 'o_j_min':
				$sql .= "j_min";
				$sql .= ($val) ? " DESC," : ",";
				break;

			case 'o_j_max':
				$sql .= "j_max";
				$sql .= ($val) ? " DESC," : ",";
				break;

			case 'o_j_avg':
				$sql .= "j_avg";
				$sql .= ($val) ? " DESC," : ",";
				break;

			case 'o_j_per':
				$sql .= "j_per";
				$sql .= ($val) ? " DESC," : ",";
				break;
		}
	}
	echo "$sql<P>\n";
	$sql = ereg_replace('(^.*)ORDER BY $', '\1', $sql);
	$sql = ereg_replace('(^.*),$', '\1', $sql);

	echo "$sql<P>\n";

	//$sql = "SELECT p_src.name src_name, p_dst.name dst_name, INET_NTOA(data.src) src_ip, INET_NTOA(data.dst) dst_ip, SUM(rx)/SUM(tx)*100 avail, 100-(SUM(rx)/SUM(tx)*100) loss, AVG(rtt_min)/1000.0 r_min, AVG(rtt_max)/1000 r_max, AVG(rtt_avg)/1000 r_avg, AVG(ott_min)/1000 o_min, AVG(ott_max)/1000 o_max, AVG(ott_avg)/1000 o_avg, AVG(jtr_min)/1000 j_min, AVG(jtr_max)/1000 j_max, AVG(jtr_avg)/1000 j_avg FROM data LEFT JOIN probes AS p_src ON (p_src.ip = src) LEFT JOIN probes AS p_dst ON (p_dst.ip = dst) WHERE secs = 3600 GROUP BY src, dst ORDER BY src, dst";

	/* Query database */
	$result = mysql_query($sql);

	/* Display Header */
	printf("<table>\n<tr bgcolor=#aeaeae align=center>\n");
	$line = mysql_fetch_array($result, MYSQL_ASSOC);
	while (list($id, $col_value) = each($line)) {
		switch ($id) {
			case 'src_name':
				printf('<td>Source</td>');
				break;

			case 'dst_name':
				printf('<td>Destination</td>');
				break;

			case 'src_ip':
				printf('<td>Source IP</td>');
				break;

			case 'dst_ip':
				printf('<td>Destination IP</td>');
				break;

			case 'avail':
				printf('<td>Avail</td>');
				break;

			case 'loss':
				printf('<td>Loss</td>');
				break;

			case 'r_min':
				printf('<td>RTT Min</td>');
				break;

			case 'r_max':
				printf('<td>RTT Max</td>');
				break;

			case 'r_avg':
				printf('<td>RTT Avg</td>');
				break;

			case 'r_per':
				printf('<td>RTT Perc</td>');
				break;

			case 'o_min':
				printf('<td>OTT Min</td>');
				break;

			case 'o_max':
				printf('<td>OTT Max</td>');
				break;

			case 'o_avg':
				printf('<td>OTT Avg</td>');
				break;

			case 'o_per':
				printf('<td>OTT Perc</td>');
				break;

			case 'j_min':
				printf('<td>JTR Min</td>');
				break;

			case 'j_max':
				printf('<td>JTR Max</td>');
				break;

			case 'j_avg':
				printf('<td>JTR Avg</td>');
				break;

			case 'j_per':
				printf('<td>JTR Perc</td>');
				break;

			case 'count':
				printf('<td>Count</td>');
				break;

			case 'start':
				printf('<td>Start Date</td>');
				break;

			case 'end':
				printf('<td>End Date</td>');
				break;

			default:
				printf('<td>Def(%s)</td>', $id);
		}
	}
	printf("</tr>\n");

	/* Display data */
	do {
		printf("<tr bgcolor=#ababab align=center>\n");
		reset($line);
		while (list($id, $val) = each($line)) {
			switch ($id) {
				case 'src_name':
					printf('<td>%s</td>', $val);
					break;

				case 'dst_name':
					printf('<td>%s</td>', $val);
					break;

				case 'src_ip':
					printf('<td>%s</td>', $val);
					break;

				case 'dst_ip':
					printf('<td>%s</td>', $val);
					break;

				case 'loss':
					if ($val >= 1) {
						printf('<td><font color=red>%0.2f%%</td>', $val);
					} else {
						printf('<td>%0.2f%%</td>', $val);
					}
					break;

				case 'avail':
					if ($val <= 99) {
						printf('<td><font color=red>%0.2f%%</td>', $val);
					} else {
						printf('<td>%0.2f%%</td>', $val);
					}
					break;

				case 'j_min':
				case 'o_min':
				case 'r_min':
					printf('<td>%0.2f ms</td>', $val);
					break;

				case 'j_max':
				case 'o_max':
				case 'r_max':
					printf('<td>%0.2f ms</td>', $val);
					break;

				case 'j_avg':
				case 'o_avg':
				case 'r_avg':
					printf('<td>%0.2f ms</td>', $val);
					break;

				case 'r_per':
				case 'o_per':
				case 'j_per':
					if ($val >= 90) {
						printf('<td>%0.2f%%</td>', $val);
					} else if ($val >= 70) {
						printf('<td><font color=yellow>%0.2f%%</td>', $val);
					} else {
						printf('<td><font color=red>%0.2f%%</td>', $val);
					}
					break;

				case 'count':
					printf('<td>%lu</td>', $val);
					break;

				case 'end':
				case 'start':
					printf('<td>%s</td>', $val);
					break;

				default:
					printf('<td>def(%s)</td>', $val);
			}
		}
		printf("</tr>\n");
	} while ($line = mysql_fetch_array($result, MYSQL_ASSOC));
/*
	printf("<tr><td>Source</td><td>Destination</td><td>s_ip</td><td>d_ip</td><td>Avail</td><td>Loss</td><td>RTT Min</td><td>RTT Max</td><td>RTT Avg</td><td>OTT Min</td><td>OTT Max</td><td>OTT Avg</td><td>JTR Min</td><td>JTR Max</td><td>JTR Avg</td></tr>\n");
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
		echo "\t<tr bgcolor=grey>\n";
		while (list($id, $col_value) = each($line)) {
			if (is_numeric($col_value)) {
				printf("\t\t<td><center>%0.3f</center></td>\n", $col_value);
			} else {
				printf("\t\t<td>%s</td>\n", $col_value);
			}
		}
		echo "\t</tr>\n";
	}
*/
	printf("</table>\n");

	// Free resultset
	mysql_free_result($result);

	// Closing connection
	mysql_close($con);

?>
