#!/bin/sh

. ../common/ajax_common


check_switch_model()
{

	switch_model="$(${FLOWEYE} swchip stat | grep switch_model | cut -d '=' -f 2)"
    
    if [ "$switch_model" = "NGTAP-PX8" ]; then  
		retjson 0 "yes"
	else
		retjson 1 "no"
	fi
}


today_chart_dump()
{	
	[ "${CGI_hours}" = "" ] && CGI_hours=24
	[ "${CGI_tmend}" = "" ] && CGI_tmend=`date +%s`

	CHART_DUMP="${FLOWEYE} chart dump" 
	[ "${CGI_isntmeye}" = "ntmeye" ] && CHART_DUMP="${NTMEYE} chart dump"

	${CHART_DUMP} json=1 hours=${CGI_hours} tmend=${CGI_tmend} ${CGI_tags}
}


chart_dump_sum()
{
	[ "${CGI_hours}" = "" ] && CGI_hours=24
	[ "${CGI_tmend}" = "" ] && CGI_tmend=`date +%s`

	loop=0
	tags=""
	dot=""

	printf "["

	for tag in `echo ${CGI_tags} | tr "," " "`
	do
		loop=$((${loop} + 1))
		tags="${tags} tag=${CGI_pxyType}_${tag}"

		if [ ${loop} -eq 40 ]; then
			printf "${dot}"

			${FLOWEYE} chart dump json=1 hours=${CGI_hours} tmend=${CGI_tmend} ${tags}
			loop=0
			tags=""
			[ "${dot}" = "" ] && dot=","
		fi
	done

	printf "]"
}


load_chart_timesum()
{
	[ "${CGI_time}" = "" ] && CGI_time=`date +%Y%m`

	#day daysecs min avg max
	${FLOWEYE} chart timesum tag=${CGI_tag} time=${CGI_time} | awk \
	'BEGIN{
		dot = "";
		printf "[";	
	}{
		col = 1;

		printf "%s", dot;
		printf "{";
		printf "\"day\":\"%s\",", $(col++);
		printf "\"secs\":\"%s\",", $(col++) +86400;
		printf "\"min\":\"%s\",", $(col++);
		printf "\"avg\":\"%s\",", $(col++);
		printf "\"max\":\"%s\"", $(col++);
		printf "}";
		if (dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


history_chart_dump()
{
	tags=""

	for tag in `echo ${CGI_tags} | tr "|" " "`
	do
		tags="${tags} tag=${tag}"
	done

	[ "${CGI_showmax}" = "1" ] && tags="${tags} showmax=1"
	
	if [ "${CGI_types}" = "3day" ]; then
		tmend=`date +%s`
		hours=24
		tmend1=`date +%s`
		tmend2=$((${tmend1} - ( 60 * 60 * 24 ) ))
		tmend3=$((${tmend1} - ( 60 * 60 * 48 ) ))

		printf "["
		if [ "${CGI_isntmeye}" ]; then
			printf "`${CGI_isntmeye} chart dump json=1 hours=${hours} tmend=${tmend1} ${tags}`,"
			printf "`${CGI_isntmeye} chart dump json=1 hours=${hours} tmend=${tmend2} ${tags}`,"
			printf "`${CGI_isntmeye} chart dump json=1 hours=${hours} tmend=${tmend3} ${tags}`"
		else
			printf "`${FLOWEYE} chart dump json=1 hours=${hours} tmend=${tmend1} ${tags}`,"
			printf "`${FLOWEYE} chart dump json=1 hours=${hours} tmend=${tmend2} ${tags}`,"
			printf "`${FLOWEYE} chart dump json=1 hours=${hours} tmend=${tmend3} ${tags}`"
		fi
		printf "]"
	else
		tmend=`date +%s`
		hours1=24
		hours2=$((24*7))
		hours3=$((24*30))

		printf "["
		if [ "${CGI_isntmeye}" ]; then
			printf "`${CGI_isntmeye} chart dump json=1 hours=${hours1} tmend=${tmend} ${tags}`,"
			printf "`${CGI_isntmeye} chart dump json=1 hours=${hours2} tmend=${tmend} ${tags}`,"
			printf "`${CGI_isntmeye} chart dump json=1 hours=${hours3} tmend=${tmend} ${tags}`"
		else
			

			if [ "${CGI_his}" = "pdf" ]; then
				printf "`${FLOWEYE} chart dump json=1 hours=${CGI_hours} tmend=${CGI_tmend} ${tags}`,"
				printf "`${FLOWEYE} chart dump json=1 hours=${CGI_hours} tmend=${CGI_tmend} ${tags}`,"
				printf "`${FLOWEYE} chart dump json=1 hours=${CGI_hours} tmend=${CGI_tmend} ${tags}`"
			else
				printf "`${FLOWEYE} chart dump json=1 hours=${hours1} tmend=${tmend} ${tags}`,"
				printf "`${FLOWEYE} chart dump json=1 hours=${hours2} tmend=${tmend} ${tags}`,"
				printf "`${FLOWEYE} chart dump json=1 hours=${hours3} tmend=${tmend} ${tags}`"
			fi
		fi
		printf "]"
	fi
}


case "${CGI_action}" in
    "check_switch_model")
	    check_switch_model
		;;

	"chart_dump")
		retjson 0 "OK" "`today_chart_dump`"
		;;
		
	"chart_dump_sum")
		retjson 0 "OK" "`chart_dump_sum`"
		;;

	"load_chart_timesum")
		retjson 0 "OK" "`load_chart_timesum`"
		;;

	"history_chart_dump")
		retjson 0 "OK" "`history_chart_dump`"
		;;

	*)
		retjson 1 "UNKNOW_ACTION"
		;;
esac
