Issue #80: Allow user to insert data from an interval

parent 52993bce
......@@ -13,15 +13,15 @@ function usage() {
echo "Uso: $0 [OPÇÃO] <--inicio YYYY-MM> <--fim YYYY-MM>"
echo ""
echo "Conexão com o ElasticSearch"
echo -e " --sem-elastic\tNão utiliza o ElasticStack para armazenar os dados, implica em --csv"
echo -e " -E, --sem-elastic\tNão utiliza o ElasticStack para armazenar os dados, implica em --csv"
echo -e " -u, --usuario\t\tUsuário do ElasticSearch (default: em branco)"
echo -e " -s, --senha\t\tSenha do ElasticSearch (default: sem senha)"
echo -e " -H, --hostname\tHostname do ElasticSearch (default: localhost)"
echo ""
echo "Definição de período de tempo para inserção"
echo -e " -i, --inicio\t\tAno e mês de início dos dados, no formato YYYY-MM (default: 2016-01)"
echo -e " -f, --fim\t\tAno e mês de fim dos dados, no formato YYYY-MM (default: 2016-01)"
echo -e " -i, --inicio\t\tAno e mês de início dos dados, no formato YYYY-MM (default: current year-month)"
echo -e " -f, --fim\t\tAno e mês de fim dos dados, no formato YYYY-MM (default: current year-month)"
echo ""
echo "Definição das bases a serem inseridas"
echo -e " --tudo\t\tInsere todas as bases (despesas, diarias e servidores)"
......@@ -34,82 +34,116 @@ function usage() {
echo ""
}
usage
exit 1
function echoerr() {
echo "$@" 1>&2;
}
function echoerr_and_exit() {
echoerr "$@"
usage
exit 1
}
function empty_arg_sanity_check() {
local arg=$1
local arg_name=$2
local arg_to_check=$1
if [[ $arg = -* ]]; then
echo "Você esqueceu de fornecer o valor para o argumento '$arg_name'"
usage
return false
if [[ "$arg_to_check" = -* || "$arg_to_check" = "" ]]; then
return 1
else
return true
echo $arg_to_check
return 0
fi
}
function date_sanity_check() {
local date_to_check=$1
if grep -q '^[0-9]{4}-[0-9]{2}$' <<< "$date_to_check"; then
return true
local current_epoch=$(date -d "$(date +%Y-%m-01)" "+%s")
local given_year=
local given_month=
local given_epoch=
if empty_arg_sanity_check "$date_to_check" | egrep -q '^[0-9]{4}-[0-9]{2}$'; then
# check if user is trying to insert data from the future
given_year=$(cut -d- -f1 <<< "$date_to_check")
given_month=$(cut -d- -f2 <<< "$date_to_check")
given_epoch=$(date -d "${given_year}-${given_month}-01" "+%s")
if test $given_epoch -gt $current_epoch; then
return 2
fi
echo $date_to_check
return 0
else
echo "Data inválida $date_to_check"
usage
return false
return 1
fi
}
PROGRAM_NAME="${0##*/}"
ARG_PARSE=$(getopt -o hu:s:i:f: --long help,tudo,despesas,diarias,servidores,inicio,fim,usuario,senha,hostname,csv -n $PROGRAM_NAME -- "$@")
ARG_PARSE=$(getopt -o hu:s:i:f:E --long help,tudo,despesas,diarias,servidores,inicio,fim,usuario,senha,hostname,sem-elastic,mantem-csv -n $PROGRAM_NAME -- "$@")
if [[ $? -ne 0 ]]; then
echo "Error while reading command line arguments, exiting."
exit 1
fi
KEEP_CSV=true
KEEP_CSV=false
DATA_ALL=false
DATA_EXPENSES=false
DATA_TRAVEL_EXPENSES=false
DATA_WORKERS=false
DATA_START="2016-01"
DATA_FINISH="2016-01"
DATA_START=
DATA_FINISH=
ELASTICSTACK=true
ELASTIC_USER=
ELASTIC_USERNAME=
ELASTIC_PASSWORD=
ELASTIC_HOSTNAME="localhost"
while true; do
# echo "S1='$1' S2='$2'"
case "$1" in
-E | --sem-elastic ) ELASTICSTACK=false; shift ;;
--despesas ) DATA_EXPENSES=true; shift ;;
--diarias ) DATA_TRAVEL_EXPENSES=true ; shift ;;
--servidores ) DATA_WORKERS=true ; shift ;;
--csv ) KEEP_CSV=true ; shift ;;
--tudo )
DATA_ALL=true
shift ;;
--mantem-csv ) KEEP_CSV=true ; shift ;;
-s | --senha )
ELASTIC_PASSWORD=$(empty_arg_sanity_check "$2" "--senha")
ELASTIC_PASSWORD=$(empty_arg_sanity_check "$2")
if test -z "$ELASTIC_PASSWORD"; then
echoerr_and_exit "Você não forneceu o valor para '$1'"
fi
shift 2 ;;
-H | --hostname )
ELASTIC_HOSTNAME=$(empty_arg_sanity_check "$2" "--hostname")
ELASTIC_HOSTNAME=$(empty_arg_sanity_check "$2")
if test -z "$ELASTIC_HOSTNAME"; then
echoerr_and_exit "Você não forneceu o valor para '$1'"
fi
shift 2 ;;
-u | --usuario )
ELASTIC_USER=$(empty_arg_sanity_check "$2" "--usuario")
ELASTIC_USERNAME=$(empty_arg_sanity_check "$2")
if test -z "$ELASTIC_USERNAME"; then
echoerr_and_exit "Você não forneceu o valor para '$1'"
fi
shift 2 ;;
-i | --inicio )
DATA_START=$(empty_arg_sanity_check "$2" "--inicio")
DATA_START=$(date_sanity_check "$2" "--inicio")
DATA_START=$(date_sanity_check "$2")
if test -z "$DATA_START"; then
echoerr_and_exit "Data inválida em '$1': '$2'"
fi
shift 2 ;;
-f | --fim )
DATA_FINISH=$(empty_arg_sanity_check "$2" "--fim")
DATA_FINISH=$(date_sanity_check "$2" "--fim")
shift 2;;
if test -z "$DATA_FINISH"; then
echoerr_and_exit "Data inválida em '$1': '$2'"
fi
shift 2 ;;
-h | --help )
usage
exit 0
exit 0 ;;
-- )
shift
break ;;
......@@ -119,58 +153,59 @@ while true; do
esac
done
if [[ -z "$MONTH" || -z "$YEAR" ]]; then
usage()
exit 1
fi
#
# Some more arguments sanity check
#
<<<<<<< f1f999c5e8025779464c35e4eb7385462fe71456
year=$1
month=$2
user=$3
passwd=$4
insertions=$5
# Treating the cases where the year input is invalid
if [[ "${#year}" != "4" ]]; then
echo " Year is invalid. Please enter a 4 digit year."
echo " Example: $0 2016 12 myuser mypass all"
exit
# start can't be null
if test -z "$DATA_START"; then
echoerr_and_exit "--inicio não pode ser nulo"
fi
# Treating the case where month is too big
if [[ "${#month}" -gt "2" ]]; then
echo " Month is invalid. Please enter a 2 digit month."
echo " Example: $0 2016 12 myuser mypass all"
exit
# if finish is not provided, the user is adding only one month of data
if test -z "$DATA_FINISH"; then
DATA_FINISH=$DATA_START
fi
# Treating the case where the user uses only 1 digit to the month value
if [[ "${#month}" -eq "1" ]]; then
month="0$month"
fi
start_year=$(cut -d- -f1 <<< "$DATA_START")
start_month=$(cut -d- -f2 <<< "$DATA_START")
finish_year=$(cut -d- -f1 <<< "$DATA_FINISH")
finish_month=$(cut -d- -f2 <<< "$DATA_FINISH")
if [[ "$insertions" == "all" || "$insertions" == "expenses" ]]; then
# First, insert Expenses data.
echo "Inserting Expenses from ${year}-${month}..."
(cd expenses && ./insert_expenses.sh $year $month $user $passwd)
=======
if "$DATA_EXPENSES"; then
# First, insert Expenses data.
echo "Inserting Expenses from ${MONTH}-${YEAR}..."
(cd expenses && ./insert_expenses.sh $1 $2 $3 $4)
>>>>>>> continuando
fi
for year in $(seq $start_year $finish_year); do
# choose last month to consider in this year
if test $year -eq $finish_year; then
to_month=$finish_month
else
to_month="12"
fi
if [[ "$insertions" == "all" || "$insertions" == "travel_allowances" ]]; then
# We should now insert Travel allowance data.
echo "Inserting Travel Allowances from ${year}-${month}..."
(cd travel_allowances && ./insert_travel_allowances.sh $year $month $user $passwd)
fi
# find month to start from
if test $year -eq $start_year; then
from_month=$start_month
else
from_month="01"
fi
if [[ "$insertions" == "all" || "$insertions" == "workers" ]]; then
# Now, insert Workers data.
echo "Inserting Workers from ${year}-${month}..."
(cd workers && ./insert_register_payment.sh $year $month $user $passwd)
fi
# insert data for each month
for month in $(seq -w $from_month $to_month); do
if test "$DATA_ALL" -o "$DATA_EXPENSES"; then
echo "Inserting Expenses from ${year}-${month}..."
(cd expenses && ./insert_expenses.sh $year $month $ELASTIC_USERNAME $ELASTIC_PASSWORD)
fi
if test "$DATA_ALL" -o "$DATA_TRAVEL_EXPENSES"; then
echo "Inserting Travel Allowances from ${year}-${month}..."
(cd travel_allowances && ./insert_travel_allowances.sh $year $month $ELASTIC_USERNAME $ELASTIC_PASSWORD)
fi
if test "$DATA_ALL" -o "$DATA_WORKERS"; then
echo "Inserting Workers from ${year}-${month}..."
(cd workers && ./insert_register_payment.sh $year $month $ELASTIC_USERNAME $ELASTIC_PASSWORD)
fi
done
done
exit 0
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment