#!/bin/bash

#set -xuve

if [ $# -ne 3 ]; then
	echo "Wrong number of arguments"
	echo "E.g.: $0 <ARCH> <EXPID> <ESTIMATED RUNTIME>"
	exit 1
fi

ARCH=$1
EXPID=$2
estimated_time=$3 # sim (job) estimated computing time (in hours)

case $ARCH in
	mn)
		rsrc=/gpfs/scratch/*/*
		error_file="err"
		out_file="out"
		;;
	ithaca)
		rsrc=/scratch/cfu/*
		error_file="\.e"
		;;
	ecmwf)
		rsrc=c2a:/scratch/ms
		error_file="err"
		;;
	hector)
		rsrc=
		error_file=""
		;;
	lindgren)
		rsrc=/cfs/klemming/scratch/*/*
		;;
	jaguar)
		rsrc=
		error_file=""
		;;
	*)
		echo "!!! $ARCH is not available !!!"
		exit 1
		;;
esac

TYPE="sim"
LOG_PATH="$rsrc/$EXPID/LOG_$EXPID"
sdates=$(ls -1 ${LOG_PATH}/${EXPID}_*_${TYPE}_COMPLETED | cut -d '_' -f 3 | uniq)
echo "Name,Queue,Run,Wasted,Failed"
for s in $sdates; do
	mems_chunks=$(ls -1tr ${LOG_PATH}/${EXPID}_${s}_*_${TYPE}_COMPLETED | cut -d '_' -f 4-5 | uniq)
	for m_c in $mems_chunks; do
		job=${EXPID}_${s}_${m_c}_${TYPE}
		submit_time=$(ls -ltr ${LOG_PATH}/${job}.cmd | awk '{print $6 " " $7 " " $8}')
		submit_time=$(date --date="${submit_time}" +%s)
		failed_times=$(($(ls -1tr ${LOG_PATH}/${job}*${error_file} | wc -l) -1 ))
		start_time=$(grep "Started" $(ls -1tr ${LOG_PATH}/${job}*${out_file} | tail -n 1) | cut -d" " -f 3-)
		start_time=$(date --date="${start_time}" +%s)
		end_time=$(grep "Results" $(ls -1tr ${LOG_PATH}/${job}*${out_file} | tail -n 1) | cut -d" " -f 4-)
		end_time=$(date --date="${end_time}" +%s)
		queued_time=$(echo "scale=2; (${start_time}-${submit_time}) / 3600" | bc)
		run_time=$(echo "scale=2; (${end_time}-${start_time}) / 3600" | bc)
		wasted_time=$(echo "scale=2; ${run_time}-${estimated_time}" |bc)
		echo "${job},${queued_time},${run_time},${wasted_time},${failed_times}"
	done
done
