#!/bin/sh

. ../common/ajax_common


load_rateobj_config()
{
    ${FLOWEYE} rateobj stat | awk -F"=" \
    'BEGIN{
        dot = "";
        printf "{";
    }{
        printf "%s\"%s\":\"%s\"", dot, $1, $2;
        if(dot == "") dot = ",";
    }END{
        printf "}";
    }'
}


set_rateobj_config()
{
    errmsg=`${FLOWEYE} rateobj config account_share=${CGI_account_share}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧ:${errmsg}"
    else
        sync_floweye "rateobj config account_share=${CGI_account_share}"
        WEB_LOGGER "ù" "account_share=${CGI_account_share}"
        retjson 0 "ɹ"
    fi
}


list_rateobj()
{
	case "${CGI_sort_key}" in
		"account")
			sort_key="-k1"
			;;

		"group")
			sort_key="-k2"
			;;

		"online")
			sort_key="-k3"
			;;

		"inrate")
			sort_key="-k4"
			;;

		"outrate")
			sort_key="-k5"
			;;

		"indrop_bps")
			sort_key="-k6"
			;;

		"outdrop_bps")
			sort_key="-k7"
			;;

        "indrop_byte")
			sort_key="-k8"
			;;

		"outdrop_byte")
			sort_key="-k9"
			;;

		*)
			sort_key="-k3"
			;;
	esac

	[ "${CGI_sort_type}" = "desc" ] && sort_type="-r"

    [ "${CGI_page}" = "" ] && CGI_page=1
    [ "${CGI_limit}" = "" ] && CGI_limit=30

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

    ${FLOWEYE} rateobj list | grep -i "${CGI_keyword}" | sort ${sort_type} ${sort_key}  | awk \
    -v start=${start} -v end=${end} \
    'BEGIN{
        row = 0;
        dot = "";
        printf "{\"data\":[";
    }{
        row++;
        if( row > end  || row < start ) next;

        col = 1;
        printf "%s{", dot;
        printf "\"id\":\"%s\"", NR;
        printf ",\"account\":\"%s\"", $(col++);
        printf ",\"group\":\"%s\"", $(col++);
        printf ",\"online\":\"%s\"", $(col++);
        printf ",\"inrate\":\"%s\"", $(col++);
        printf ",\"outrate\":\"%s\"", $(col++);
        printf ",\"indrop_bps\":\"%s\"", $(col++);
        printf ",\"outdrop_bps\":\"%s\"", $(col++);
        printf ",\"indrop_byte\":\"%s\"", $(col++);
        printf ",\"outdrop_byte\":\"%s\"", $(col++);
        printf "}";
        if(dot == "") dot = ",";
    }END{
        printf "],\"total\":\"%s\"}", row;
    }'
}


get_rateobj()
{
    ${FLOWEYE} rateobj get name=${CGI_account} showip=1 | awk \
    'BEGIN{
        dot = "";
        printf "[";
    }{
        col = 1;
        printf "%s{", dot;
        printf "\"id\":\"%s\"", NR;
        printf ",\"ip\":\"%s\"", $(col++);
        printf ",\"time\":\"%s\"", $(col++);
        printf ",\"flow\":\"%s\"", $(col++);
        printf ",\"inbps\":\"%s\"", $(col++);
        printf ",\"outbps\":\"%s\"", $(col++);
        printf "}";
        if(dot == "") dot = ",";
    }END{
        printf "]";
    }'
}


case "${CGI_action}" in

    "load_rateobj_config")
        retjson 0 "OK" "`load_rateobj_config`"
        ;;

    "set_rateobj_config")
        action_check
        set_rateobj_config
        ;;

    "list_rateobj")
        retjson 0 "OK" "`list_rateobj`"
        ;;

    "get_rateobj")
        retjson 0 "OK" "`get_rateobj`"
        ;;

    *)
        retjson 1 "UNKNOW_ACTION"
        ;;

esac
