#!/bin/bash
set -e

startdate=`date +%m-%d-%H:%M:%S.%2N`
echo "Text table created start at date: ${startdate}"
# create text tables: Refer Clause_2/create_text_tables.sql for more detail
beeline -u jdbc:hive2://tpcds1:10000  --maxWidth=1000 -f  create_text_tables.sql       > ../logs/create_text.log 2>&1
startdate=`date +%m-%d-%H:%M:%S.%2N`
echo "Text table created end at date: ${startdate}"
echo "Text table created end at date: ${startdate}" >> ../logs/create_text.log

echo "##########Load data start###########################"
# Start of the timed portion of the Database (in HOLODESK format) Load
startdate=`date +%m-%d-%H:%M:%S.%2N`
echo "Load data start date: ${startdate}"

# load data to tables for test
{ sleep 300
beeline -u jdbc:hive2://tpcds1:10000  --maxWidth=1000 -f  create_cs.sql     > ../logs/create_cs.log 2>&1
thisdate=`date +%m-%d-%H:%M:%S.%2N`
echo "created catalog_sales at: ${thisdate}"
echo "created catalog_sales at: ${thisdate}" >> ../logs/create_cs.log

beeline -u jdbc:hive2://tpcds1:10000  --maxWidth=1000 -f  create_sr.sql     > ../logs/create_sr.log 2>&1
thisdate=`date +%m-%d-%H:%M:%S.%2N`
echo "created store_returns at: ${thisdate}"
echo "created store_returns at: ${thisdate}" >> ../logs/create_sr.log

} &
{
beeline -u jdbc:hive2://tpcds1:10000  --maxWidth=1000 -f  create_ss.sql       > ../logs/create_ss.log 2>&1
thisdate=`date +%m-%d-%H:%M:%S.%2N`
echo "created store_sales at: ${thisdate}"
echo "created store_sales at: ${thisdate}" >> ../logs/create_ss.log

beeline -u jdbc:hive2://tpcds1:10000  --maxWidth=1000 -f  create_small_tables.sql      > ../logs/create_small_tables.log 2>&1
thisdate=`date +%m-%d-%H:%M:%S.%2N`
echo "created small_tables at: ${thisdate}"
echo "created small_tables at: ${thisdate}" >> ../logs/create_small_tables.log
} &
{
sleep 600
beeline -u jdbc:hive2://tpcds1:10000  --maxWidth=1000 -f  create_ws.sql         > ../logs/create_ws.log 2>&1
thisdate=`date +%m-%d-%H:%M:%S.%2N`
echo "created web_sales at: ${thisdate}"
echo "created web_sales at: ${thisdate}" >> ../logs/create_ws.log
} &

{
sleep 900
beeline -u jdbc:hive2://tpcds1:10000  --maxWidth=1000 -f  create_wr.sql       > ../logs/create_wr.log 2>&1
thisdate=`date +%m-%d-%H:%M:%S.%2N`
echo "created web_returns at: ${thisdate}"
echo "created web_returns at: ${thisdate}" >> ../logs/create_wr.log

beeline -u jdbc:hive2://tpcds1:10000  --maxWidth=1000 -f  create_cr.sql   > ../logs/create_cr.log 2>&1
thisdate=`date +%m-%d-%H:%M:%S.%2N`
echo "created catalog_returns at: ${thisdate}"
echo "created catalog_returns at: ${thisdate}" >> ../logs/create_cr.log

beeline -u jdbc:hive2://tpcds1:10000  --maxWidth=1000 -f  create_inv.sql         > ../logs/create_inv.log 2>&1
thisdate=`date +%m-%d-%H:%M:%S.%2N`
echo "created inventory at: ${thisdate}"
echo "created inventory at: ${thisdate}" >> ../logs/create_inv.log

} &
wait

echo "########## Create table end ###########################"
thisdate=`date +%m-%d-%H:%M:%S.%2N`
echo "LOAD DATA AUDIT START DATE: ${thisdate}"


beeline -u jdbc:hive2://tpcds1:10000  --maxWidth=1000 -f desc_tables.sql > ../logs/table_schema.out 2>&1
thisdate=`date +%m-%d-%H:%M:%S.%2N`
echo "Collect table schema at: ${thisdate}"
echo "Collect table schema at: ${thisdate}" >> ../logs/table_schema.out

echo "Collecting row count for each table"
beeline -u jdbc:hive2://tpcds1:10000  --maxWidth=1000 -f count_table.sql > ../logs/row_count.out 2>&1
thisdate=`date +%m-%d-%H:%M:%S.%2N`
echo "Collect table row count at: ${thisdate}"
echo "Collect table row count at: ${thisdate}" >> ../logs/row_count.out

echo "LOAD DATA AUDIT END DATE: ${thisdate}"

# collect statistics 
./collect_statistics.sh > ../logs/staticstics.log 2>&1
thisdate=`date +%m-%d-%H:%M:%S.%2N`
echo "collected statistics at:  ${thisdate}"
echo "collected statistics at:  ${thisdate}" >> ../logs/staticstics.log

pdsh -w tpcds[1-16] "echo 3 > /proc/sys/vm/drop_caches"
thisdate=`date +%m-%d-%H:%M:%S.%2N`
echo "Clean caches at: ${thisdate}"

enddate=`date +%m-%d-%H:%M:%S.%2N`
echo "Load data end date: ${enddate}"

export LDENDDATE=${enddate}

echo "Please note: Total LOAD TIME should be: (Load data end date - Load data start date) - (AUDIT END DATE - AUDIT START DATE)."
# End of the timed portion of the Database Load
echo "################# Load end #######################"
