#!/bin/bash

function bail () { echo "\nError: ${1:-Unknown Error}\n"; exit ${2:-1}; }

runid=${1:-Unset}
[[ $runid == "Unset" ]] && bail "Specify runid as parameter"
 
rundir=run/$runid

# For replicas
#ansible-playbook -i hosts copy_monitor_logs.yml
#ansible-playbook -i hosts stop_monitoring.yml

mkdir $rundir
config_file=config_p4_bench.yml
cd $rundir
cp ../../$config_file .

p4port=`grep 666 $config_file | head -1 | sed -e 's/\s*port:\s*//' | sed -e 's/ \- //'`
p4user=`grep user $config_file | sed -e 's/\s*user:\s*//'`

port=`echo "$p4port" | cut -d: -f2`
instance=${port:0:1}

#cp ../../nethogs.out .
#cp ../../iftop.out .
#cp ../../logs/*nethogs.out .
#cp ../../logs/*iftop.out .

echo p4 -p $p4port -u $p4user configure show > config.out
p4 -p $p4port -u $p4user configure show > config.out
sudo ls -l /p4/$instance/ >> config.out

sudo mv /p4/$instance/logs/log .
#sudo mv /remote/FB_DB/p4/rep/h02_$instance/logs/log rep1.log
#sudo mv /remote/FB_DB/p4/rep/h03_$instance/logs/log rep2.log
#sudo mv /remote/FB_DB/p4/rep/h04_$instance/logs/log rep3.log
#sudo chmod +r rep*.log
~/bin/log2sql.py -d run log
#python3.6 ~/bin/log2sql.py -d run -s rep1.log
#python3.6 ~/bin/log2sql.py -d run -s rep2.log
#python3.6 ~/bin/log2sql.py -d run -s rep3.log

# cat config*

sqlite3 -header run.db <<EOF
.mode column
select cmd, count(cmd), 
round(cast(avg(completedLapse) AS DECIMAL(9,2)), 2) as "Avg Time", 
round(cast(max(completedLapse) AS DECIMAL(9, 2)), 2) as "Max Time"
from process
group by cmd;

select cmd, count(cmd),  round(cast(avg(completedLapse) AS DECIMAL(9,2)), 2) as "Avg Time", 
round(cast(max(completedLapse) AS DECIMAL(9, 2)), 2) as "Max Time",
cast(sum(completedLapse) as decimal(9,2)) as "Sum Time",
round(cast(avg(rpcsizeout) AS DECIMAL(9,2)), 2) as "Avg Sent(MB)",
cast(sum(rpcsizeout) AS DECIMAL(9,2)) as "Sum Sent(MB)",
round(cast(sum(rpcsizeout) as decimal(9,2)) / cast(sum(completedLapse) as decimal(9,2)), 2) as "Rate MB/s"
FROM process where (cmd = "user-sync") and completedLapse > 1;

select min(substr(starttime, 13, 8)), max(substr(endtime, 13, 8)), count(completedlapse) from process where cmd = "user-sync" and completedlapse > 1;
EOF

#for f in *nethogs.out
#do
#   echo $f
#   grep p4d $f | awk '{sum += $2; count += 1} END {printf "%d %0.0f %d\n", count, sum, sum/count}'
#done

