#!/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} ${CGI_tags} 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;
	}'
}


lib_apptree_list()
{
	${FLOWEYE} app showtree all | awk \
	-v lang="${PALANG}" -v showany="${CGI_showany}" -v showtype="${CGI_showtype}" \
	'BEGIN{
		dot = "";
		pro_any = lang == "" ? "Э" : "any";
		pro_unk = lang == "" ? "δ֪Ӧ" : "unknown";

		printf "[";

		if(showany == "") {
			dot = ",";
			printf "{";
			printf "\"pid\":\"1044\",";
			printf "\"id\":\"9000\",";
			printf "\"field\":\"any\",";
			printf "\"title\":\"%s\",", pro_any;
			printf "\"type\":\"sysapp\"";
			printf "}";
			printf "%s{", dot;
			printf "\"pid\":\"1044\",";
			printf "\"id\":\"9001\",";
			printf "\"field\":\"default\",";
			printf "\"title\":\"%s\",", pro_unk;
			printf "\"type\":\"sysapp\"";
			printf "}";
		}
	}{
		if($3 == "unknown" || $3 == "default") next;
		if(showtype != "" && $5 != showtype) next;

		title = lang == "" ? $4 : $3;

		printf "%s{", dot;
		if(dot == "") dot = ",";

		printf "\"pid\":\"%s\",", $1;
		printf "\"id\":\"%s\",", $2;
		printf "\"field\":\"%s\",", $3;
		printf "\"title\":\"%s\",", title;

		if($5 == "sysgrp" || $5 == "usragp") {
			printf "\"type\":\"%s\",", $5;
			printf "\"children\":[]";
		} else
			printf "\"type\":\"%s\"", $5;

		printf "}";
	}END{
		printf "]";
	}'
}


get_app_tree()
{
	${FLOWEYE} app showtree all | awk \
	'BEGIN{
		dot = "";
		printf "{";
	}{
		printf "%s\"%s\":{", dot, $2;
		printf "\"pid\":\"%s\"", $1;
		printf ",\"id\":\"%s\"", $2;
		printf ",\"app_name\":\"%s\"", $3;
		printf ",\"app_cname\":\"%s\"", $4;
		printf ",\"type\":\"%s\"", $5;
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "}";
	}'
}


case "${CGI_action}" in
	"load_apptree")
		#ARM汾shell, иֵ
		#. ../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_type")
		retjson 0 "OK" "`get_app_type`"
		;;

	"get_app_tree")
		retjson 0 "OK" "`get_app_tree`"
		;;

	*)
		retjson 1 "UNKNOW_ACTION!"
		;;
esac
