#!/usr/bin/env bash

## imports csv data files emitted from influx db (preset format) to an elastic search db
##
## © JFrog 2017
set -e
current_dir="$(dirname "$0")"
source $current_dir"/usr/migrate.cfg"
source $current_dir"/usr/include"

main (){
    show_message "Starting execution at : "$(now_time)
    show_message "-------------------------"
    show_message "Run UID : "$unique_run_id
    show_message "-------------------------"
    
    show_message "Logfiles for this run is at "$log_location
    show_message "CSV path used is "$conv_csv_location
    show_message "Data chunks path is "$chunk_location

    if [[ "$convert_csv" = true ]] ; then
        log "convert_csv set to true."
        go_parse_csv
    else
        show_message "Skipping csv parsing step - convert_csv set to false"
    fi
    if [[ "$insert_to_es" = true ]] ; then
        log "insert_to_es set to true."
        insert_to_db
    else
        show_message "Skipping inserting to db step - insert_to_es set to false"
    fi
    alias_index
    write_status
}

go_parse_csv() {
    show_message "Starting csv parsing at : "$(now_time)
    log "Finding csv files to parse"
    for (( pointer=0; pointer<$(( $csv_files_count )); pointer++ ))
    do
        show_message "Working on : ${csv_files[$pointer]}"
        awk -f $convertor_location ${csv_files[$pointer]} 
    done
}

insert_to_db(){
    check_chunk_folder
    if [[ $chunk_files_count -gt 1 ]] ;
    then
        show_message " "
        show_message "Starting to insert data to ES from: $chunk_location"
        show_message "index name: ${es_index_prefix}${unique_run_id}"
		show_message "Step 1: Inserting $es_repo_type_name type"
		set_type_mapping
        read_n_insert $es_repo_type_name
		show_message "Step 2: Inserting $es_instance_type_name type"
		read_n_insert $es_instance_type_name
        show_message "Completed insert"
        show_message "Check ${es_index_prefix}${unique_run_id} for data validity."
    else
        show_error "Could not find ndjson files to insert to es."
        exit 1;
    fi
}

write_status(){
	show_message " "
	show_message " "
	show_message "Execution completed."
	show_message " "
	if [[  "$partial_migration" = true ]]  ; then
        show_error "[WARNING]Failed to insert someparts of the mertrics data. Please refer log file for more info."
    fi
	
	if [[  "$alias_failed" = true ]]  ; then
        show_error "[FATAL] Failed to add alias to the elasticsearch index (index name: ${es_index_prefix}${unique_run_id}).Please refer log file for more info."
    fi
    echo "$unique_run_id $partial_migration $alias_failed" >> $tmp_location"/"$status_file
}

read_n_insert() {
    shopt -s nullglob
	local repo_type=$1
	local ndjson_files=${chunk_location}/${repo_type}/*.json
	local files_count=${#ndjson_files[@]}
	show_message "Found ndjson files under ${chunk_location}/${repo_type}/"
	for f in $ndjson_files
	do
		 show_message "Processing $f"
		 curl_es $f $repo_type
	done
	show_message "Inserted data to ${es_index_prefix}${unique_run_id}/${repo_type}"
}

set_type_mapping(){
	local repo_type=$1
    
    local action_str='{"mappings":{"repo_storage_info":{"properties":{"used_space":{"type":"double"}}},"storage_summary_info":{"properties":{"used_space":{"type":"double"},"artifacts_size":{"type":"double"}}}}}'

    log "Mapping action_str = $action_str"
    if [ -n "$userName" ];then
        log "Calling curl with username"
        response=$(curl -X PUT --user $userName:$password  "${es_base_url}${es_index_prefix}${unique_run_id}"  -H 'Content-Type: application/json' -d $action_str )
    else
        log "Calling curl without username"
        response=$(curl -X PUT   "${es_base_url}${es_index_prefix}${unique_run_id}/" -H 'Content-Type: application/json' -d ${action_str} )
    fi

}


alias_index(){
    show_message "Aliasing ${es_index_prefix}${unique_run_id} to ${es_alias_name}"
    local action_str='{"actions" : [{"add" : { "index" : "'${es_index_prefix}${unique_run_id}'", "alias" : "'${es_alias_name}'" } }]}'
    log "Alias action_str = $action_str" 
	if [ -n "$userName" ];then
    	log "Calling curl with username" 
        response=$(curl -X POST --user $userName:$password  "${es_base_url}_aliases?pretty" --output /dev/null -H 'Content-Type: application/json' -d "$action_str"  --write-out '%{http_code}')
    else
    	log "Calling curl without username" 
        response=$(curl -X POST   "${es_base_url}_aliases?pretty" --output /dev/null -H 'Content-Type: application/json' -d "$action_str" --write-out '%{http_code}')
    fi
    if [[ "$response" -eq 200 ]] ; then
		show_message "Aliasing operation successful "
    else
        alias_failed=true
        show_error "Aliasing operation seems to have failed. Reponse code "${response}". Skipping"
    fi
}

curl_es(){
    local line=$1
    local repo_type=$2
    show_message "Calling URL : ${es_base_url}${es_index_prefix}${unique_run_id}/$repo_type/$es_url_append and bulk inserting $line "
	if [ -n "$userName" ];then
    	log "Calling curl with username" 
        response=$(curl -X POST -H "Content-Type: application/json" --output /dev/null --user $userName:$password  -H "Cache-Control: no-cache" --data-binary @$line "${es_base_url}${es_index_prefix}${unique_run_id}/$repo_type/$es_url_append" --write-out '%{http_code}')
	else
    	log "Calling curl without username" 
        response=$(curl -X POST -H "Content-Type: application/json" --output /dev/null -H "Cache-Control: no-cache" --data-binary @$line "${es_base_url}${es_index_prefix}${unique_run_id}/$repo_type/$es_url_append" --write-out '%{http_code}')  
	fi
    if [[ "$response" -eq 200 ]] ; then
    	show_message "Insert operation successful"
    else
        partial_migration=true
        show_error "Insert operation seems to have failed. Reponse code "${response}". Skipping"
    fi
}

main;
exit;


