<?php

/*
** CBLM
** $Id: util.php,v 1.8 2006/06/20 22:49:13 sella Exp $
** Copyright (c) 2005-2006 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

function buildSQL() {
	/* Convert f[] into normal. */
	if (isset($_REQUEST['f'])) {
		while (list(, $val) = each($_REQUEST['f'])) {
			$_REQUEST[$val] = '';
		}
	}

	# Fixup period
	$_REQUEST['period'] = isset($_REQUEST['period']) ? $_REQUEST['period'] : 120;

	if ($_REQUEST['secs'] == -1) {
		unset($_REQUEST['secs']);
	}

	if (isset($_REQUEST['secs'])) {
		if ($_REQUEST['period'] <= $_REQUEST['secs']) {
			$_REQUEST['period'] = $_REQUEST['secs'] * 2;
		}
	}

	/* 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 't_paz':
				$sql .= "SUM(tos_a2z)/SUM(rx)*100 t_paz,";
				break;

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

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

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

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

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

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

			case 'p_pza':
				$sql .= "SUM(pri_z2a)/SUM(rx)*100 p_pza,";
				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);

	/* 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 'period':
				$sql .= sprintf("UNIX_TIMESTAMP() - UNIX_TIMESTAMP(data.timestamp) < %d AND ", $_REQUEST['period']);
				break;

			case 'tos':
				if (isset($_REQUEST['tos']) && $_REQUEST['tos'] != -1) {
					list($tos, $pri) = split(':', $_REQUEST['tos']);
					$sql .= sprintf("tos = %d AND pri = %d AND ", $tos, $pri);
				}
				break;

			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':
				echo "Not implemented yet..<BR>";
				break;

			case 'edate':
				echo "Not implemented yet..<BR>";
				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 ";
	}

	/* 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;
		}
	}

	$sql = ereg_replace('(^.*)ORDER BY $', '\1', $sql);
	$sql = ereg_replace('(^.*),$', '\1', $sql);

	return $sql;
}

/*
 * Local Variables:
 * c-basic-offset: 3
 * tab-width: 3
 * End:
 * vim: noet ts=3 sw=3 sts=3
 */

?>
