#!/usr/bin/env bash

#
# Copyright 2015-2019 Intel Corporation.
# This software and the related documents are Intel copyrighted materials, and your use of them 
# is governed by the express license under which they were provided to you ("License"). Unless the 
# License provides otherwise, you may not use, modify, copy, publish, distribute, disclose or 
# transmit this software or the related documents without Intel's prior written permission.
# 
# This software and the related documents are provided as is, with no express or implied warranties, 
# other than those that are expressly stated in the License.
# 
#


populateMetastore () {
  # make sure that this method stops when an error occured
  BIG_BENCH_STOP_AFTER_FAILURE="1"

  #runCmdWithErrorCheck "$BIG_BENCH_BIN_DIR/bigBench" cleanMetastore $LIST_OF_USER_OPTIONS
  #"$BIG_BENCH_BIN_DIR/bigBench" cleanMetastore $LIST_OF_USER_OPTIONS
  runCmdWithErrorCheck runEngineCmd -f "$BIG_BENCH_POPULATE_METASTORE_FILE"

  runCmdWithErrorCheck hadoop fs -mkdir -p "$BIG_BENCH_HDFS_ABSOLUTE_TEMP_DIR"
  runCmdWithErrorCheck hadoop fs -mkdir -p "$BIG_BENCH_HDFS_ABSOLUTE_QUERY_RESULT_DIR"
  runCmdWithErrorCheck hadoop fs -chmod ugo+rw "$BIG_BENCH_HDFS_ABSOLUTE_TEMP_DIR"
  runCmdWithErrorCheck hadoop fs -chmod ugo+rw "$BIG_BENCH_HDFS_ABSOLUTE_QUERY_RESULT_DIR"
}

helpModule () {
  echo "This module populates the metastore with the tables for the generated dataset"
  echo
  echo "Options:"
  echo -e "-d\tdatabase to use"
  echo -e "-h\tshow this help"
  echo
  echo "INTERNAL options:"
  echo -e "-v\tsql script for metastore population"
  echo -e "-z\tfile with user defined engine settings"
}

runModule () {
  echo "==============================================="
  echo "Adding/Updating generated files to hive metastore"
  echo "==============================================="

  local LOG_FILE_EXEC_TIMES="${BIG_BENCH_LOGS_DIR}/times.csv"
  if [ ! -e "$LOG_FILE_EXEC_TIMES" ]
  then
    touch "$LOG_FILE_EXEC_TIMES"
    echo "STARTDATE_EPOCH|STOPDATE_EPOCH|DURATION_MS|STARTDATE|STOPDATE|DURATION|QUERY|SCALE_FACTOR|BENCHMARK_PHASE|STREAM|SUCCESSFUL|EXIT_CODE|HAS_RESULT|HDFS_SIZE" >> "${LOG_FILE_EXEC_TIMES}"
  fi

  if [ ! -w "$LOG_FILE_EXEC_TIMES" ]
  then
    echo "ERROR: cannot write to: $LOG_FILE_EXEC_TIMES, no permission"
    return 1
  fi

   #-- time measurement of exectuion
  local STARTDATE="`date +%Y/%m/%d:%H:%M:%S`"
  local STARTDATE_EPOCH="`date +%s`" # seconds since epoch

  # Run the main method
  populateMetastore 2>&1 | tee -a "$BIG_BENCH_LOADING_STAGE_LOG" 2>&1
  local EXIT_CODE=${PIPESTATUS[0]}

  if [ "$EXIT_CODE" -eq 0 ]
  then
    local successOrFail="SUCCESS"
  else
    local successOrFail="FAILED"
  fi

  local STOPDATE="`date +%Y/%m/%d:%H:%M:%S`"
  local STOPDATE_EPOCH="`date +%s`" # seconds since epoch
  local DIFF_s="$(($STOPDATE_EPOCH - $STARTDATE_EPOCH))"
  local DIFF_ms="$(($DIFF_s * 1000))"
  local DURATION="$(($DIFF_s / 3600 ))h $((($DIFF_s % 3600) / 60))m $(($DIFF_s % 60))s"

  echo "======= Load data into hive time =========" | tee -a "$BIG_BENCH_LOADING_STAGE_LOG" 2>&1
  echo "Start timestamp: $STARTDATE $STARTDATE_EPOCH" | tee -a "$BIG_BENCH_LOADING_STAGE_LOG" 2>&1
  echo "Stop  timestamp: $STOPDATE $STOPDATE_EPOCH" | tee -a "$BIG_BENCH_LOADING_STAGE_LOG" 2>&1
  echo "Duration:  $DURATION" | tee -a "$BIG_BENCH_LOADING_STAGE_LOG" 2>&1
  echo "----- result -----" | tee -a "$BIG_BENCH_LOADING_STAGE_LOG" 2>&1

  echo "Load data ${successOrFail} exit code: ${EXIT_CODE}" | tee -a "$BIG_BENCH_LOADING_STAGE_LOG" 2>&1 
  echo "${STARTDATE_EPOCH}|${STOPDATE_EPOCH}|${DIFF_ms}|${STARTDATE}|${STOPDATE}|${DURATION}|Load data|${BIG_BENCH_SCALE_FACTOR}|Load data|${BIG_BENCH_STREAM_NUMBER}|${successOrFail}|${EXIT_CODE}||" >> "${LOG_FILE_EXEC_TIMES}"

  echo "time&status: ${LOG_FILE_EXEC_TIMES}" | tee -a "$BIG_BENCH_LOADING_STAGE_LOG" 2>&1
  echo "full log: $BIG_BENCH_LOADING_STAGE_LOG" | tee -a "$BIG_BENCH_LOADING_STAGE_LOG" 2>&1
  echo "===========================" | tee -a "$BIG_BENCH_LOADING_STAGE_LOG" 2>&1

  return "${EXIT_CODE}"
}
