#!/bin/bash -e
#
# Apache v2 license
# Copyright (C) 2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#

function concat_params() {
    RET=""
    for i in "$@"; do
        if [[ "$RET" == "" ]]; then
            RET="$i"
        else
            RET="${RET} $i"
        fi
    done
    echo "$RET"
} 

function workload_settings() {
    RET=""
    for i in "$@"; do
        LOWER=$(echo "$i" | tr '[:upper:]' '[:lower:]')
        if [[ "$RET" == "" ]]; then
            RET="${LOWER}:$(eval echo \$$i)"
        else
            RET="${RET};${LOWER}:$(eval echo \$$i)"
        fi
    done
    echo "$RET"
}

### workspace
DIR="$( cd "$( dirname "$0" )" &> /dev/null && pwd )"
# source "$DIR/script/common.sh"

DB_TYPE=${1:-mysql}
### HammerDB
TPCC_NUM_WAREHOUSES=${TPCC_NUM_WAREHOUSES:-10}
TPCC_THREADS_BUILD_SCHEMA=${TPCC_THREADS_BUILD_SCHEMA:-10}
TPCC_HAMMER_NUM_VIRTUAL_USERS=${TPCC_HAMMER_NUM_VIRTUAL_USERS:-"10"}
TPCC_MINUTES_OF_RAMPUP=${TPCC_MINUTES_OF_RAMPUP:-2}
TPCC_RUNTIMER_SECONDS=${TPCC_RUNTIMER_SECONDS:-600}
TPCC_MINUTES_OF_DURATION=${TPCC_MINUTES_OF_DURATION:-5}
TPCC_TOTAL_ITERATIONS=${TPCC_TOTAL_ITERATIONS:-10000000}
TPCC_WAIT_COMPLETE_MILLSECONDS=${TPCC_WAIT_COMPLETE_MILLSECONDS:-5000}

HAMMERDB_KEYS="TPCC_NUM_WAREHOUSES TPCC_THREADS_BUILD_SCHEMA TPCC_HAMMER_NUM_VIRTUAL_USERS TPCC_MINUTES_OF_RAMPUP TPCC_RUNTIMER_SECONDS \
TPCC_MINUTES_OF_DURATION TPCC_TOTAL_ITERATIONS TPCC_WAIT_COMPLETE_MILLSECONDS"

###Mysql
MAX_CONNECTIONS=${MAX_CONNECTIONS:-45}
TABLE_OPEN_CACHE=${TABLE_OPEN_CACHE:-8000}
TABLE_OPEN_CACHE_INSTANCES=${TABLE_OPEN_CACHE_INSTANCES:-16}
BACK_LOG=${BACK_LOG:-1500}
PERFORMANCE_SCHEMA=${PERFORMANCE_SCHEMA:-0}
MAX_PREPARED_STMT_COUNT=${MAX_PREPARED_STMT_COUNT:-128000}
CHARACTER_SET_SERVER=${CHARACTER_SET_SERVER:-latin1}
COLLATION_SERVER=${COLLATION_SERVER:-latin1_swedish_ci}
TRANSACTION_ISOLATION=${TRANSACTION_ISOLATION:-repeatable-read}
INNODB_FILE_PER_TABLE=${INNODB_FILE_PER_TABLE:-1}
INNODB_OPEN_FILES=${INNODB_OPEN_FILES:-4000}
INNODB_BUFFER_POOL_SIZE=${INNODB_BUFFER_POOL_SIZE:-1073741824}
INNODB_FLUSH_LOG_AT_TRX_COMMIT=${INNODB_FLUSH_LOG_AT_TRX_COMMIT:-0}
JOIN_BUFFER_SIZE=${JOIN_BUFFER_SIZE:-32768}
SORT_BUFFER_SIZE=${SORT_BUFFER_SIZE:-32768}
INNODB_STATS_PERSISTENT=${INNODB_STATS_PERSISTENT:-1}
INNODB_SPIN_WAIT_DELAY=${INNODB_SPIN_WAIT_DELAY:-6}
INNODB_MAX_PURGE_LAG_DELAY=${INNODB_MAX_PURGE_LAG_DELAY:-300000}
INNODB_MAX_PURGE_LAG=${INNODB_MAX_PURGE_LAG:-0}
INNODB_LRU_SCAN_DEPTH=${INNODB_LRU_SCAN_DEPTH:-9000}
INNODB_PURGE_THREADS=${INNODB_PURGE_THREADS:-4}
INNODB_ADAPTIVE_HASH_INDEX=${INNODB_ADAPTIVE_HASH_INDEX:-0}
INNODB_SYNC_SPIN_LOOPS=${INNODB_SYNC_SPIN_LOOPS:-90}
INSTANCE_CLASS=${INSTANCE_CLASS:-db.t3.micro}

MYSQL_KEYS="MAX_CONNECTIONS TABLE_OPEN_CACHE TABLE_OPEN_CACHE_INSTANCES BACK_LOG PERFORMANCE_SCHEMA MAX_PREPARED_STMT_COUNT CHARACTER_SET_SERVER COLLATION_SERVER TRANSACTION_ISOLATION \
INNODB_FILE_PER_TABLE INNODB_OPEN_FILES INNODB_BUFFER_POOL_SIZE INNODB_FLUSH_LOG_AT_TRX_COMMIT JOIN_BUFFER_SIZE SORT_BUFFER_SIZE INNODB_STATS_PERSISTENT INNODB_SPIN_WAIT_DELAY \
INNODB_MAX_PURGE_LAG_DELAY INNODB_MAX_PURGE_LAG INNODB_LRU_SCAN_DEPTH INNODB_PURGE_THREADS INNODB_ADAPTIVE_HASH_INDEX INNODB_SYNC_SPIN_LOOPS INSTANCE_CLASS"

###mssql
SINGLE_DATABASES_SKU_NAME=${SINGLE_DATABASES_SKU_NAME:-"BC_Gen5_20"}

ELASTIC_POOL_ENABLED=${ELASTIC_POOL_ENABLED:-false}
ELASTIC_POOL_VCORE_FAMILY=${ELASTIC_POOL_VCORE_FAMILY:-"Gen5"}
ELASTIC_POOL_SKU_TIER=${ELASTIC_POOL_SKU_TIER:-"GeneralPurpose"}
ELASTIC_POOL_SKU_CAPACITY=${ELASTIC_POOL_SKU_CAPACITY:-20}
MSSQL_KEYS="SINGLE_DATABASES_SKU_NAME ELASTIC_POOL_ENABLED ELASTIC_POOL_VCORE_FAMILY ELASTIC_POOL_SKU_TIER ELASTIC_POOL_SKU_CAPACITY"

### Logs Setting
source "$DIR/../../script/overwrite.sh"

if [[ "$DB_TYPE" == "mysql" ]]; then
    ALL_KEYS="$(concat_params $MYSQL_KEYS)"
elif [[ "$DB_TYPE" == "mssql" ]]; then
    ALL_KEYS="$(concat_params $MSSQL_KEYS)"
fi
ALL_KEYS="$(concat_params $ALL_KEYS $HAMMERDB_KEYS DB_TYPE)"
WORKLOAD_PARAMS=($ALL_KEYS)

### Docker Setting
DOCKER_IMAGE=""
DOCKER_OPTIONS=""

### Kubernetes Setting
RECONFIG_OPTIONS=""

### Script Setting
SCRIPT_ARGS="${DB_TYPE}"
source "$DIR/../../script/validate.sh"
