#!/bin/sh -e

##########################################################################
#   Script description:
#       
#   Arguments:
#       
#   Returns:
#       
#   History:
#   Date        Name        Modification
#   2014-05-09  root        Begin
##########################################################################

usage()
{
    printf "Usage: $0 \n"
    exit 1
}


##########################################################################
#   Function description:
#       
#   Arguments:
#       
#   Returns:
#       
#   History:
#   Date        Name        Modification
#   2014-05-09  root        Begin
##########################################################################

line()
{
    cat << EOM >> $usage_report
==============================================================================
EOM
}


##########################################################################
#   Main
##########################################################################

if [ $# != 0 ]; then
    usage
fi

tempfile=usage-report.tmp
usage_report=usage-report.txt

printf 'Reading logs...\n'
sacct --allusers --noheader -o jobid,elapsed,exitcode,ncpus,user \
    | awk '$1 !~ "\\." && $2 != "00:00:00" { print $0 }' \
    > $tempfile

start_date=`head -1 ~slurm/Jobcomp | cut -d ' ' -f 8 | cut -d = -f 2 | cut -d T -f 1`
end_date=`tail -1 ~slurm/Jobcomp | cut -d ' ' -f 8 | cut -d = -f 2 | cut -d T -f 1`

rm -f $usage_report
line
printf "Cluster usage report for the period from %s to %s:\n" $start_date $end_date >> $usage_report

awk '
BEGIN   {
    user_count = 1;
    total_core_hours = 0;
    }
    {
	time = $2;
	if ( split(time, a, "-") == 2 )
	{
	    days = a[1];
	    time_minus_days = a[2];
	}
	else
	{
	    days = 0;
	    time_minus_days = time;
	}
	if ( split(time_minus_days, a, ":") == 3 )
	{
	    hours = a[1];
	    minutes = a[2];
	    seconds = a[3];
	}
	else
	{
	    printf("Error parsing time!\n");
	    exit 1;
	}
	
	elapsed_hours = days * 24 + hours + minutes / 60 + seconds / 3660;
	
	# Total CPU time occupied (elapsed * cores)
	cores = $4;
	core_hours = elapsed_hours * cores;
	total_core_hours += core_hours;

	# Add to user user_core_hours
	user = $5;
	user_core_hours[user] += core_hours;
	
	#print time, elapsed_hours, cores, core_hours, user_core_hours[user];
    }
END {
	for (user in user_core_hours)
	{
	    printf("%-10s %12.4f\n",  user, user_core_hours[user]);
	}
	printf("%-10s %12.4f\n",  "Total", total_core_hours);
    }'  $tempfile | sort -r -n -k 2 > $tempfile.2

line
cat << EOM >> $usage_report

The core-hours reported below provide an estimate of how long the computations
performed by the cluster over the report period would have taken on a
single CPU:

EOM

# Total core-hours
awk '$1 == "Total" {
    printf("Core-hours = %0.0f\n", $2);
    printf("Core-days = %0.0f\n", $2 / 24);
    printf("Core-weeks = %0.0f\n", $2 / 24 / 7);
    printf("Core-years = %0.0f\n", $2 / 24 / 365);
    }' \
    $tempfile.2 >> $usage_report

cat << EOM >> $usage_report

In reality, had the cluster not been available, this computation would
have been performed on multiple PC workstations and laptops.  In some cases,
multiple CPUs would have been used (typically 2 or 4) on an individual PC.

Notes:

o   Many computational models run on the cluster are simply not feasible to
    run on a PC due to time or memory limitations.

o   The cluster is used, to some extent, IN ADDITION to lab PCs, rather than
    INSTEAD OF lab PCs.

o   The number of PCs that would be used in the absence of the cluster is
    difficult to determine and highly variable.

o   Most of the computation would be concentrated on the lab PCs of the most
    active cluster users.  These users would see enormous time savings.

    As an example, suppose our most active user used a single 4-core PC instead
    of the cluster:
EOM

# Add gecos info
awk -f /usr/local/libexec/add-gecos.awk $tempfile.2 > $tempfile.3

# Biggest user
fgrep -v Total $tempfile.3 | head -1 | \
    awk ' { printf("    Time required for most active user using one 4-core PC = %0.1f years.\n\n",
	$2 / 24 / 365 / 4);
    }' >> $usage_report

# Format results
line
printf "Usage by individual user:\n" >> $usage_report

# Total users
users=`wc -l $tempfile.3 | cut -d ' ' -f 1`
printf '\n%s people used a measurable amount of cluster time during this period.\n\n' \
    $users >> $usage_report

printf "%-10s %12s  User Info\n" "Username" "Core-hours" >> $usage_report
sort -r -n -k 2 $tempfile.3 >> $usage_report

# Some colleges may have 0 time
set +e

line
printf "\nCore-hours by school/college:\n\n" >> $usage_report

# By college
for college in CEAS LS SPH SFS LSOB; do
    grep $college $usage_report > $college-report.txt
    college_time=`awk '
    {
	total_time += $2;
    }
END {
	printf("%0.4f\n", total_time);
    }'  $college-report.txt`
    printf "%-10s %s\n" $college $college_time >> $tempfile.4
done

#sort -r -n -k 2 $tempfile.4 >> $usage_report
cat $tempfile.4 >> $usage_report

for college in CEAS LS SPH SFS LSOB; do
    printf "\nReport for %s:\n\n" $college >> $usage_report
    cat $college-report.txt >> $usage_report
    rm -f $college-report.txt
done

rm -f $tempfile*

less $usage_report

