9631488db55ad8c48618e1958a6c8d8644ff0946
mspeir
  Mon Apr 18 08:52:48 2016 -0700
Changes based on CR. Added some dependecy notes to functions where it expects certain things to be set elsewhere in the script or by other functions. Simplified rounding method. Intialize output sting to include database. Change checkForIssues call to not use percentDiff for non-positional tables. refs #12561 #17041

diff --git src/utils/qa/qaAutoTrack.sh src/utils/qa/qaAutoTrack.sh
index 9ba3d9b..b90aace 100755
--- src/utils/qa/qaAutoTrack.sh
+++ src/utils/qa/qaAutoTrack.sh
@@ -63,68 +63,77 @@
 By default, all results are output to a file and only issues
 are output to the command line. Use the "-v" option to see
 results on the command line as well. All log files are output to:
 http://genecats.cse.ucsc.edu/qa/test-results/qaAutoTrack.
 
 Notes:
 	- For OMIM, ISCA, or ClinVar tracks use omim, isca, or clinvar as the table name.
 	- Can only be run once for each database/track pair per day.
 
 EOF
 }
 
 # Output function
 function outputCovDiff () {
 	# four positional arguments. $1 == prevLogFile. $2 == tblCov. $3 == tableName. $4 == tblDate.
+	# Dependency: Coverage information from previous log file (positional argument $1)
+	# If no previous log file exists or if previous log file doesn't contain coverage info,
+	# then no coverage diff will be calculated and the percentDiff variable will retain its
+	# default value of ""
+ 
 	if [[ $1 != "" ]] # Check for previous log file. True if file exists.
 	then
 		# get info needed for diff
 		rawCount=$(echo $2 | awk '{print $1}')
 		prevCov=$(egrep -A2 "^$3" $1 | grep "^Coverage New" | cut -d" " -f3-) # Grabs coverage from previous log file.
 		rawCountPrev=$(echo $prevCov | awk '{print $1}')
 
 		if [[ prevCov != "" ]] # Check needed so script doesn't fail if prevLogFile doesn't contain coverage info
 		then
 			# Calculate diff between new and old coverage
 			rawCountDiff=$(echo $(expr $rawCount - $rawCountPrev)|tr -d -)
 			rawCountAvg=$(expr $rawCount / 2 + $rawCountPrev / 2)
 			percentDiff=$(awk -v rcd=$rawCountDiff -v rca=$rawCountAvg 'BEGIN{print 100 * rcd / rca}')
 
 			# Build output string
 			output+="$3\nLast updated: $4\nCoverage New: $2\nCoverage Old: $prevCov\nCoverage Diff: $percentDiff%\n\n"
 		else
 			output+="$3\nLast updated: $4\nCoverage New: $2\n\n"
 		fi
 	else
 		output+="$3\nLast updated: $4\nCoverage New: $2\n\n"
 	fi
 }
 
 # Function to raise errors
 function checkForIssues () {
 	# four positional arguments. $1 == tblDate. $2 == tooOld. $3 == tableName. $4 == precentDiff.
+	# Dependency: percentDiff, positional argument $4, is expected to be set by outputCovDiff
+	# If percent diff is not set (so it retains default empty "" value), then checkForIssues
+	# will raise no errors.
+
 	# Raises an error if it's been too long since last update
 	if [ $(date -d "$1" +%s) -le $(date -d "$2" +%s) ]
 	then
 		issueNote+="$3 has not been updated since $1\n"
 	fi
 
 	# Raises error if coverage diff between track versions is too large
 	if [[ "$4" != "" ]]
 	then
 		#Round our percentDiff to 3 decimal places. Really small numbers don't play nice with bc.
-		percentDiffRounded=$(echo $4 | xargs printf "%.3f\n")
+		percentDiffRounded=$(printf "%.3f\n" "$4")
 		if [ $(echo "$percentDiffRounded >= 10" | bc) -ne 0 ]
 		then
 			issueNote+="Large coverage diff for $3\n"
 		fi
 	fi
 }
 
 ##### Parse command-line input #####
 
 OPTIND=1 # Reset is necessary if getopts was used previously in the script.  It is a good idea to make this local in a function.
 while getopts "hd:t:bv" opt
 do
 	case $opt in
 		h)
 			showHelp
@@ -154,30 +163,33 @@
 then
 	showHelp >&2
 	exit 1
 fi
 
 shift "$((OPTIND-1))" # Shift off the options and optional --.
 
 ##### Main Program #####
 
 # set currLogFile
 currLogFile="$logDir/$db.$tableName.$currDate.txt"
 
 # set info for prevLog
 prevLogDate=$(ls -Lt $logDir | sed -n /$db.$tableName/p | head -1 | awk -F . '{print $3}')
 
+#initialize output string
+output="\n$db\n"
+
 if [ -e $logDir/$db.$tableName.$prevLogDate.txt ]
 then
 	prevLogFile="$logDir/$db.$tableName.$prevLogDate.txt"
 fi
 
 # Can't run twice in one day as it messes up the "Coverage Old" output
 if [[ "$currDate" == "$prevLogDate" ]]
 then
 	echo -e "Previous log date is the same as today's date, $currDate"
 	exit 1
 fi
 
 # Set tooOld for different tables
 if  [[ "$tableName" == "clinvar" ]] || [[ "$tableName" == "grcIndcidentDb" ]]
 then 
@@ -251,31 +263,31 @@
         	do
 			tblDate=$(hgsql -Ne "SELECT UPDATE_TIME FROM information_schema.tables WHERE TABLE_SCHEMA='$db' AND TABLE_NAME='$tbl'")
 			# Only some omim tables have coordinates
                 	if [[ $tbl == "omimGene2" ]] || [[ $tbl == "omimAvSnp" ]] || [[ $tbl == "omimLocation" ]] || [[ $tableName == "isca" ]]
                 	then
                         	tblCov=$(ssh qateam@hgwbeta "featureBits -countGaps $db $tbl 2>&1")
 
 				outputCovDiff "$prevLogFile" "$tblCov" "$tbl" "$tblDate"
 				# Check for issues with table
 				checkForIssues "$tblDate" "$tooOld" "$tbl" "$percentDiff"
 
 			# Output for tables that don't contain coordinates
 			else
 				output+="$tbl\nLast updated: $tblDate\n\n"
 				# Check for issues with table
-				checkForIssues "$tblDate" "$tooOld" "$tbl" "$percentDiff"
+				checkForIssues "$tblDate" "$tooOld" "$tbl" ""
 			fi
 		done
 	# Tests for all other table based autopushed tracks
 	else
 		tblDate=$(hgsql -Ne "SELECT UPDATE_TIME FROM information_schema.tables WHERE TABLE_SCHEMA='$db' AND TABLE_NAME='$tableName'")
 		tblCov=$(ssh qateam@hgwbeta "featureBits -countGaps $db $tableName 2>&1")
 
 		outputCovDiff "$prevLogFile" "$tblCov" "$tableName" "$tblDate"
 
 		# Check for issues with table
 		checkForIssues "$tblDate" "$tooOld" "$tableName" "$percentDiff"
 	fi
 fi
 
 # Output results of tests