#!/bin/sh

# This script produces an HTML table of all-to-all bandwidth and latency
# measurements, forecasts and errors for a set of NWS hosts.  Numbers can be
# retrieved either from Globus MDS entries (-m switch), in which case the
# program globus-mds-search must be present in some directory in $PATH, or
# directly from the NWS (-n switch: the default) in which case nws_extract must
# be available.  To produce a table for a clique, execute the command
#   html-hosts `nws_search -v name=XXX member | sed 's/,/ /g'`
# substituting the clique name for XXX.

dataSource=nws_extract
NAMESERVER=""
SINGLEPAGE="no"
EXTRACT_OPTS="-h 0 -n 1 -f measurement,mae_forecast,mae_error,source,destination"

while [ $# -gt 0 ]; do
	case "$1" in
	-m)
		dataSource=globus-mds-search
		echo "-m is temporarly not supported."
		exit 1
		;;
	-n) 
		dataSource=nws_extract
		;;
	-N)
		if [ -z "${2}" ]; then
			break;
		fi
		NAMESERVER="-N ${2}"
		shift
		;;
	-s)
		SINGLEPAGE="yes"
		;;
	*) 
		break
		;;
	esac
	shift
done

if [ $# -lt 2 ]; then
	echo 'usage: html_hosts [-mn] [-N nameserver] host host ...'
	exit 1
fi

bwFile=".hosts.bandwidth.$$"
ltFile=".hosts.latency.$$"

# Grab numbers from the appropriate source.  nws_extract produces a single
# line of output per experiment consisting of the time, measurement*,
# forecast*, error*, method, source*, dest*, and resource name.  We only care
# about the starred items.  globus-mds-search puts each attribute/value pair on
# a separate line.  We filter output from each program to arrange the fields we
# want on a single line.
if [ "$dataSource" = "globus-mds-search" ]; then
	searchCommand='globus-mds-search -F" " -h mds.globus.org -b "project=NWS, ou=PCL, o=University of California San Diego, o=Globus, c=US" "(objectclass=GlobusNetworkPerformance)"'
	eval $searchCommand throughput throughput_prediction throughput_mse \
                      source destination | grep -v "^hostpair" | \
	awk ' {if(NF>1) {printf "%s ", $2; if($1=="destination") print ""}}' > $bwFile
	eval $searchCommand latency latency_prediction latency_mse \
                      source destination | grep -v "^hostpair" | \
	awk ' {if(NF>1) {printf "%s ", $2; if($1=="destination") print ""}}' > $ltFile
else
	# workaround for nws_extract bug version 2.1 and previous
	for src in $*; do
		for dst in $*; do
			if [ "$src" = "$dst" ]; then
				continue
			fi
			nws_extract ${NAMESERVER} ${EXTRACT_OPTS} band $src $dst >> $bwFile
			nws_extract ${NAMESERVER} ${EXTRACT_OPTS} lat $src $dst >> $ltFile
		done
	done
fi

#
# sanity check on the extraction
CHECK="`wc -c $bwFile|cut -f 1 -d '.'`"
if [ ${CHECK} -eq 0 ]; then
	echo "No data gathered: try to specify nameserver with -N option."
	exit 1
fi

# print header if we are in single page mode
if [ "$SINGLEPAGE" = "yes" ]; then
	echo "<html><body>"
fi

# Produce the table header and the first row that lists all destinations.
columnCount=`expr $# + 1`
rowCount=`expr $# + $#`  ;# One row for machine names, one for measurements.

# header (table definition)
echo "   <table border cellpadding=1 cellspacing=0>"
echo "   <tr>"
echo "      <th colspan=3 rowspan=2>Source machine</td>"
echo "      <th colspan=$columnCount align=center>Destination machine</th>"

# print the destination machine name
echo "   <tr>"
for destination in $*; do
	echo "      <th align=center>$destination</th>"
done

# let's put the machines name down
for source in $*; do

	echo "   <tr>"
	echo "      <th align=left>$source"
	echo "      <td align=center>"
	echo "      <th align=right>"
	echo "      <small>bw<br>forecast<br>error<hr>lt<br>forecast<br>error</small>"

	# let's get the data 
	for destination in $*; do

		# Grab bandwidth 
		nwsAttributes="`grep \"$source.*$destination\" $bwFile | sed \"s/ \{1,\}/ /g\"`"
		bw="`echo "$nwsAttributes" | cut -f 1 -d ' '`"
		bwForecast="`echo "$nwsAttributes" | cut -f 2 -d ' '`"
		bwError="`echo "$nwsAttributes" | cut -f 3 -d ' '`"
		if [ -z "${nwsAttributes}" ]; then
			bw="-"
			bwForecast="-"
			bwError="-"
		fi
		echo "$nwsAttributes" >> ciccio
		echo "$bw $bwForecast $bwError" >> ciccio
		# Grab latency
		nwsAttributes="`grep \"$source.*$destination\" $ltFile | sed \"s/ \{1,\}/ /g\"`"
		lt="`echo "$nwsAttributes" | cut -f 1 -d ' '`"
		ltForecast="`echo "$nwsAttributes" | cut -f 2 -d ' '`"
		ltError="`echo "$nwsAttributes" | cut -f 3 -d ' '`"
		if [ -z "${nwsAttributes}" ]; then
			lt="-"
			ltForecast="-"
			ltError="-"
		fi

		# Put the data into the table.
		echo "   <td>$bw<br>$bwForecast<br>$bwError<hr>$lt<br>$ltForecast<br>$ltError"
	done
done

# Finish off the table with a caption.
echo "   <caption>Bandwidth values are measured in Megabits/second; latency in milliseconds</caption>"
echo "   </table>"

# print end of page 
if [ "${SINGLEPAGE}" = "yes" ]; then
	echo "</body></html>"
fi

#rm -f $bwFile $ltFile
exit 0
