#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then
	LANG001="All protocol"
	LANG002="Unknown protocol"
fi


load_appstat()
{
	[ "${CGI_sort}" = "" ] && CGI_sort="last"
	[ "${CGI_bridge}" = "" ] && CGI_bridge=0
	[ "${CGI_page}" = "" ] && CGI_page=1
	[ "${CGI_limit}" = "" ] && CGI_limit=30

	if [ "${CGI_type}" = "v6" ]; then
		cmdargs="bridge=9"
	else
		cmdargs="bridge=${CGI_bridge}"
	fi

	[ "${CGI_topn}" != "" ] && cmdargs="${cmdargs} topn=${CGI_topn}"
	[ "${CGI_app}" != "" -a "${CGI_app}" != "any" ] && cmdargs="${cmdargs} app=${CGI_app}"
	[ "${CGI_showsum}" = "on" ] && cmdargs="${cmdargs} showsum"

	end=$((${CGI_page} * ${CGI_limit}))
	start=$((${end} - ${CGI_limit}))

	# appid appname flow node aup adown all per1 lup ldown lall per2 bpsup bpsdn bpsall natupbps natdownbps theother
	${FLOWEYE} app stat ${cmdargs} sort=${CGI_sort} | grep -i "${CGI_keyword}" | awk \
	-v end=${end} -v start=${start} \
	'BEGIN{
		row = 0;
		dot = "";
		printf "{\"data\":[";
	}{
        row++;
        if(row > end || row < start) next;

		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"appid\":\"%s\",", $(col++);
		printf "\"appname\":\"%s\",", $(col++);
		printf "\"flow\":\"%s\",", $(col++);
		printf "\"node\":\"%s\",", $(col++);
		printf "\"aup\":\"%s\",", $(col++);
		printf "\"adown\":\"%s\",", $(col++);
		printf "\"all\":\"%s\",", $(col++);
		printf "\"per1\":\"%s\",", $(col++);
		printf "\"lup\":\"%s\",", $(col++);
		printf "\"ldown\":\"%s\",", $(col++);
		printf "\"lall\":\"%s\",", $(col++);
		printf "\"per2\":\"%s\",", $(col++);
		printf "\"bpsup\":\"%s\",", $(col++);
		printf "\"bpsdn\":\"%s\",", $(col++);
		printf "\"last\":\"%s\",", $(col++);
		printf "\"natbpsup\":\"%s\",", $(col++);
		printf "\"natbpsdown\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "],\"total\":%s}", row;
	}'
}


load_app_rate()
{
	[ "${CGI_sort}" = "" ] && CGI_sort="bps"

	if [ "${CGI_pxy_id}" != "" ]; then
		wan=`${FLOWEYE} nat getproxy ${CGI_pxy_id} | grep "^name=" | cut -d"=" -f2`
	fi

	${FLOWEYE} app rate link=${wan} sort=${CGI_sort} | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		if ( $5 == 0) next;

		col = 1;

		printf "%s", dot;
		printf "{";
		printf "\"app_name\":\"%s\",", $(col++);
		printf "\"app_cname\":\"%s\",", $(col++);
		printf "\"bpsup\":\"%s\",", $(col++);
		printf "\"bpsdn\":\"%s\",", $(col++);
		printf "\"bps\":\"%s\"", $(col++);
		printf "}";
		if( dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


get_app_type()
{
	${FLOWEYE} app showtree all | awk \
	-v appname=${CGI_appname} \
	'{
		if($3 == appname)
			printf "\"%s\"", $NF;
	}'
}


case "${CGI_action}" in
	"load_apptree")
		. ../common/ajax_object_lib
		retjson 0 "OK" "`lib_apptree_list`"
		;;

	"load_appstat")
		retjson 0 "OK" "`load_appstat`"
		;;

	"load_app_rate")
		retjson 0 "OK" "`load_app_rate`"
		;;

	"get_app_info")
		retjson 0 "OK" "`get_app_info`"
		;;

	"get_app_type")
		retjson 0 "OK" "`get_app_type`"
		;;

	*)
		retjson 1 "UNKNOW_ACTION!"
		;;
esac
