#!/bin/bash

function ti_cmd_tpcc_go_run()
{
	local ti_file="${1}"
	local ti_args="${2}"
	local cmd_mod_names="${3}"
	local cmd_hosts="${4}"
	local cmd_indexes="${5}"
	local mods="${6}"
	shift 6

	if [ -z "${2+x}" ]; then
		echo "[cmd tpcc/load] usage: <cmd> threads [duration=20m] [benchmark_name=''] [tags=''] [log_detail_info=true] [tidb_host=auto] [tidb_port=auto]" >&2
		return 1
	fi

	local threads="${1}"

	local duration='20m'
	if [ ! -z "${2+x}" ]; then
		local duration="${2}"
	fi

	local name=''
	if [ ! -z "${3+x}" ]; then
		local name="${3}"
	fi

	local tags=''
	if [ ! -z "${4+x}" ]; then
		local tags="${4}"
	fi

	local log_detail_info='true'
	if [ ! -z "${5+x}" ]; then
		local log_detail_info="${5}"
	fi

	local tidb_host=''
	if [ ! -z "${6+x}" ]; then
		local tidb_host="${6}"
	fi
	local tidb_port=''
	if [ ! -z "${7+x}" ]; then
		local tidb_port="${7}"
	fi

	local ti="${integrated}/ops/ti.sh"
	local warehouses=`"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i 0 \
		"${ti_file}" "mysql" "select count(*) from warehouse" 'test' 'false' | trim_host | grep -v 'count'`

	local base_tags="warehouses:${warehouses},threads:${threads},dur:${duration}"
	if [ ! -z "${tags}" ]; then
		local tags="${base_tags},${tags},tag:${name}"
	else
		local tags="${base_tags},tag:${name}"
	fi

	if [ -z "${tidb_host}" ]; then
		if [ ! -z "${tidb_port}" ]; then
			echo "[cmd tpcc/run] tidb_host and tidb_port should be both auto or both not" >&2
			return 1
		fi
		local tidb_mod=`from_mods_random_mod "${mods}" 'tidb' 'false'`
		local tidb_host=`from_mod_get_host "${tidb_mod}"`
		local tidb_dir=`from_mod_get_dir "${tidb_mod}"`
		local tidb_port=`ssh_get_value_from_proc_info "${tidb_host}" "${tidb_dir}" 'tidb_port'`
	fi

	local ts=`date +%Y%m%d_%H%M%S`
	if [ ! -z "${name}" ]; then
		local name="_${name}"
	fi
	local entry_dir="${ti_file}.data"

	local work_dir="${entry_dir}/tpcc/${ts}${name}"
	mkdir -p "${work_dir}"

	echo "${tags}" > "${work_dir}/tags"

	date +%s > "${work_dir}/start"

	if [ "${log_detail_info}" == 'true' ]; then
		"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i "${cmd_indexes}" \
			"${ti_file}" tpcc/count | tee "${work_dir}/count"
		echo

		"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i "${cmd_indexes}" \
			"${ti_file}" ver full | tee "${work_dir}/version"
		echo

		"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i "${cmd_indexes}" \
			"${ti_file}" config | tee "${work_dir}/config"
		echo

		#local sql="SHOW STATS_BUCKETS"
		#"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i "${cmd_indexes}" \
		#	"${ti_file}" "mysql" "${sql}" 'test' 'false' | trim_host | tee "${work_dir}/buckets"
		#echo

		local sql="SHOW STATS_HISTOGRAMS"
		"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i "${cmd_indexes}" \
			"${ti_file}" "mysql" "${sql}" 'test' 'false' | trim_host | tee "${work_dir}/buckets"
		echo

		local sql="SHOW STATS_HEALTHY"
		"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i "${cmd_indexes}" \
			"${ti_file}" "mysql" "${sql}" 'test' 'false' | trim_host | tee "${work_dir}/healthy"
		echo

		local sql="SHOW STATS_META"
		"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i "${cmd_indexes}" \
			"${ti_file}" "mysql" "${sql}" 'test' 'false' | trim_host | tee "${work_dir}/meta"
		echo
	fi

	echo "=> ${ts}" | tee -a "${entry_dir}/tpcc/run.log"

	tpcc_go_run "${entry_dir}" "${tidb_host}" "${tidb_port}" "${warehouses}" "${threads}" "${duration}" | \
		tee "${work_dir}/run.log" | tee -a "${entry_dir}/tpcc/run.log"

	date +%s > "${work_dir}/end"
}

source "`cd $(dirname ${BASH_SOURCE[0]}) && pwd`/_env.sh"
auto_error_handle
ti_cmd_tpcc_go_run "${@}"
