#!/bin/sh

# cd $HOME/pg_stuff/pg_sql/pgsql.logical_replication/outs

files_pattern=$( date +"%Y%m%d" )
if [[ ! "$1" == "" ]]
then
   files_pattern=$1
fi
hostname -s
grep -E 'duration=|for scale in|for clients in|to=' testset.sh | xargs echo
echo

# for f in $(ls -1 out_20170226_1[6-9]*.txt out_20170226_2[0-9]*.txt out_20170227_*.txt)
# for f in $(ls -1 out_20170227_*.txt)
  for f in $(ls -1 *${files_pattern}*.txt)
do
   echo "-- $f"
#  grep -E 'version'               $f #| cut -b 1-21 | sort | uniq -c
#  grep -E '^-- self md5'          $f | cut -b 1-21 | sort | uniq -c
#  grep -E '^-- master  patch-md5' $f | sort | uniq -c
#  grep -E 'start_time'            $f | sort | uniq -c
#  grep -E '10devel' $f | sort | uniq -c
#  grep -E 'scale[ ]+([2-9][ ]|[1-9][0-9])' $f | sort | uniq -c
#  grep -E 'scale' $f | sort | uniq -c
   grep -E '^-- pgbench'           $f | sort | uniq -c
#  grep -E 'INIT_WAIT[ ]+[1-9]+'   $f | sort | uniq -c
   grep -E '^-- All is well'       $f | sort | uniq -c
   grep -E '^-- Not good'          $f | sort | uniq -c
#  grep -E '^tps = .*including'    $f | sort | uniq -c
#  grep -E '[1-9]+ seconds total'  $f | sort | uniq -c
#  grep -E 'count  [1-9]'          $f | sort | uniq -c
done 

echo "-----"
pg_dir=$HOME/pg_stuff/pg_installations
( cd $pg_dir && \
  grep -Ei 'ERROR|Trap|Assert|FATAL|closed the connection|violate|fault|already exists|replication timeout' pgsql.logical_replication/logfile.* pgsql.logical_replication2/logfile.* \
   | sort | uniq -c )
#   | grep -Ev 'canceling.autovacuum' \
echo "-----"

echo
#echo "Searching files like: "'*'"${files_pattern}"'*.txt'

echo -ne " ok: ";
num__ok=$( cat *${files_pattern}*.txt | grep -c '^-- All is well' )
printf "%3d   " $num__ok
#echo -ne 
#"   "
echo -ne $( ls -1 *${files_pattern}*.txt | head -n 1 )
echo -ne   " - "; echo -ne $( ls -1 *${files_pattern}*.txt | tail -n 1 )
echo

echo -ne "NOK: "; 
num_nok=$( cat *${files_pattern}*.txt | grep -c '^-- Not good' )
printf "%3d   " $num_nok

echo -ne $( ls -1 *${files_pattern}*.txt | head -n 1 )
echo -ne   " - "; echo -ne $( ls -1 *${files_pattern}*.txt | tail -n 1 )
echo
echo

tail -n 20  $( ls -1 *${files_pattern}*.txt | tail -n 1 )




# echo -n " ok: "; cat *${files_pattern}*.txt | grep -c '^-- All is well'
# echo -n "NOK: "; cat *${files_pattern}*.txt | grep -c '^-- Not good'   


