<commit_before>#/!/bin/bash

#==============================================================================
# Script for running socket server with specified configuration file.
#==============================================================================

# DIRRECTORY="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# DIRECTORY=$(echo $(pwd) )
# Application directory
DIRECTORY="$( cd "$( dirname "$0" )/../" && pwd )"
# File with list of created servers
SERVERS_LIST="/tmp/servers_list"
touch $SERVERS_LIST

# Help message
#==============================================================================
function help(){
    echo -e "This script is used to manipulate Telemetry data servers. It
allows spawn, terminate TCP-servers, backup and clear data after them."
    echo -e "\t-h show this help text"
    echo -e "\t-s config: Spawn server with specified configuration."
    echo -e "\t-t config: Terminate server."
    echo -e "\t-b config: Backup server's data storage and database."
    echo -e "\t-c config: Clear server's data storage and database."
}

# Run function
#==============================================================================
function clear_server(){
    # Get arguments
    mysql_db="$1"
    mysql_user="$2"
    mysql_pass="$3"
    host="$4"
    catalog="$5"

    # Run script which will drop database
    python3 $DIRECTORY/scripts/clear.py $mysql_user $mysql_pass $mysql_db $host "drop"
    # TODO remove data catalog
}

# Runs server
#==============================================================================
function run(){
    # Run server
    cd $DIRECTORY/src/
    nohup python3 main.py $1 > /dev/null 2>&1 &
    PID=$!

    if [ $? -eq 0 ]; then
        # Success
        echo "Server is succesfully run in background. PID:" $PID
        append_to_servers_list ${CONFIG['server_name']} ${CONFIG['port']} $PID
        return 1
    else
        # Fail
        echo "Fail to run server."
        return 0
    fi
}

# Backup function
#==============================================================================
function backup(){
    # Get arguments
    mysql_db="$1"
    mysql_user="$2"
    mysql_pass="$3"
    host="$4"

    # Create file
    filename="./backups/$mysql_db.sql"
    touch $filename
    info "Filename is $filename."

    # Backup database
    mysqldump --single-transaction --flush-logs --master-data=2 -h"$host" \
        -u"$mysql_user" -p"$mysql_pass" --databases "$mysql_db" > $filename

    if [ $? -eq 0 ]; then
        # Success
        return 0
    else
        # Fail
        return 1
    fi
}

#==============================================================================
# Utils
#==============================================================================
GREEN='\033[0;32m'
BLUE='\033[0;34m'
RED='\033[0;31m'
NC='\033[0m'
function head(){
 echo -e "${BLUE}=============================================================================="
 echo -e "$1"
 echo -e "==============================================================================${NC}"
}

function info(){
 echo -e "${BLUE}$1${NC}"
}

function success(){
 echo -e "${GREEN}$1${NC}"
}

function error(){
 echo -e "${RED}=============================================================================="
 echo -e "$1"
 echo -e "==============================================================================${NC}"
}

function append_to_servers_list(){
    echo "Name: $1. Port: $2. PID: $3" >> $SERVERS_LIST
}

function list_all_open_server(){
    info "To stop server type following command. 'kill PID'"

    # TMP file used for server list cleaning
    TMP=$(mktemp)
    # Delete already killed servers
    while IFS='' read -r line || [[ -n "$line" ]]; do
        PID=$(echo $line | cut -d' ' -f6)

        ps -p $PID > /dev/null
        if [[ $? -ne 0 ]]
        then
            awk "!/$PID/" $SERVERS_LIST > $TMP && mv $TMP $SERVERS_LIST
        fi
    done < $SERVERS_LIST

    info "List of servers:"
    cat $SERVERS_LIST
}


#==============================================================================
# Main
#==============================================================================

# echo $0
# echo $(basename $0)
# echo $(dirname $0)
# echo $(readlink -f $0)
# exit

while [[ $# -gt 0 ]]; do
    # Get command
    arg="$1"
    # Parse arguments
    #==========================================================================
    case $arg in
        -s|--spawn)
            head "Spawning server."
            config="$( cd "$(dirname "$2")" && pwd )""/$(basename $2)"<commit_msg>bin: Add inromation messages. Fix of messages width.<commit_after>#/!/bin/bash

#==============================================================================
# Script for running socket server with specified configuration file.
#==============================================================================

# DIRRECTORY="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# DIRECTORY=$(echo $(pwd) )
# Application directory
DIRECTORY="$( cd "$( dirname "$0" )/../" && pwd )"
# File with list of created servers
SERVERS_LIST="/tmp/servers_list"
touch $SERVERS_LIST

# Help message
#==============================================================================
function help(){
    echo -e "This script is used to manipulate Telemetry data servers. It
allows spawn, terminate TCP-servers, backup and clear data after them."
    echo -e "\t-h show this help text"
    echo -e "\t-s config: Spawn server with specified configuration."
    echo -e "\t-t config: Terminate server."
    echo -e "\t-b config: Backup server's data storage and database."
    echo -e "\t-c config: Clear server's data storage and database."
}

# Run function
#==============================================================================
function clear_server(){
    # Get arguments
    mysql_db="$1"
    mysql_user="$2"
    mysql_pass="$3"
    host="$4"
    catalog="$5"

    # Run script which will drop database
    python3 $DIRECTORY/scripts/clear.py $mysql_user $mysql_pass $mysql_db $host "drop"
    # TODO remove data catalog
}

# Runs server
#==============================================================================
function run(){
    # Run server
    cd $DIRECTORY/src/
    nohup python3 main.py $1 > /dev/null 2>&1 &
    PID=$!

    if [ $? -eq 0 ]; then
        # Success
        echo "Server is succesfully run in background. PID:" $PID
        append_to_servers_list ${CONFIG['server_name']} ${CONFIG['port']} $PID
        return 1
    else
        # Fail
        echo "Fail to run server."
        return 0
    fi
}

# Backup function
#==============================================================================
function backup(){
    # Get arguments
    mysql_db="$1"
    mysql_user="$2"
    mysql_pass="$3"
    host="$4"

    # Create file
    filename="./backups/$mysql_db.sql"
    touch $filename
    info "Filename is $filename."

    # Backup database
    mysqldump --single-transaction --flush-logs --master-data=2 -h"$host" \
        -u"$mysql_user" -p"$mysql_pass" --databases "$mysql_db" > $filename

    if [ $? -eq 0 ]; then
        # Success
        return 0
    else
        # Fail
        return 1
    fi
}

#==============================================================================
# Utils
#==============================================================================
GREEN='\033[0;32m'
BLUE='\033[0;34m'
RED='\033[0;31m'
NC='\033[0m'
function head(){
 echo -ne "${BLUE}"
 printf '%*s' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
 echo -e ${1}
 printf '%*s' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
 echo -e "${NC}"
}

function info(){
 echo -e "${BLUE}${1}${NC}"
}

function success(){
 echo -e "${GREEN}${1}${NC}"
}

function error(){
 echo -ne "${RED}"
 printf '%*s' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
 echo -e ${1}
 printf '%*s' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
 echo -e "${NC}"
}

function append_to_servers_list(){
    echo "Name: $1. Port: $2. PID: $3" >> $SERVERS_LIST
}

function list_all_open_server(){
    error "BE CAREFUL!!! This list can be wrong and doesn't have actual
    information. There is posibillity that it is not your server and you can 
    kill wrong process. Check what process you want to kill before doing it."
    echo "To stop server type following command. 'kill PID'"

    # TMP file used for server list cleaning
    TMP=$(mktemp)
    # Delete already killed servers
    while IFS='' read -r line || [[ -n "$line" ]]; do
        PID=$(echo $line | cut -d' ' -f6)

        ps -p $PID > /dev/null
        if [[ $? -ne 0 ]]
        then
            awk "!/$PID/" $SERVERS_LIST > $TMP && mv $TMP $SERVERS_LIST
        fi
    done < $SERVERS_LIST

    head "List of servers:"
    cat $SERVERS_LIST
}


#==============================================================================
# Main
#==============================================================================

# echo $0
# echo $(basename $0)
# echo $(dirname $0)
# echo $(readlink -f $0)
# exit

while [[ $# -gt 0 ]]; do
    # Get command
    arg="$1"
    # Parse arguments
    #==========================================================================
    case $arg in
        -s|--spawn)
            head "Spawning server."
            config="$( cd "$(dirname "$2")" && pwd )""/$(basename $2)"
{'fraction_non_alphanumeric': 0.34394217283417844, 'fraction_numerical': 0.013377926421404682, 'mean_word_length': 3.5755182625863773, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '51704', 'n_tokens_mistral': 2738, 'n_tokens_neox': 2539, 'n_words': 786}