#!/bin/sh
#
# ci-qa-summary [platform ...]
#
# Used in concert with CI daily QA runs and data stashed at
# https://github.com/performancecopilot/qa-reports
#
# Based on qa-summary that processes the logs from pcp-daily rather than CI.
#

_usage()
{
    echo "Usage: $0 [options] [platform ...]"
    echo 'Options:'
    echo '  -a      report all tests, not just failing ones (implies -n)'
    echo '  -c      report counts not percentages'
    echo '  -d      debug'
    echo '  -f      sort failure map on %fail [default sort on Test #]'
    echo '  -m N    minimum number of tests run to be reported [default 60]'
    echo '  -n      report skipped and not run tests (reported as "S" and "N")'
    exit 1
}

# must have tried to run at least this number to qualify
# 40 = > -g sanity circa PCP 2.7.8 [minus a few not runs]
#
minrun=40

base=$HOME/src/qa-reports
debug=false
aflag=false
cflag=false
nflag=false
failsort=false
while getopts 'acdfm:n?' p
do
    case "$p"
    in
	a)	aflag=true
		nflag=true
		;;
	c)	cflag=true
		;;
	d)	debug=true
		;;
	f)	failsort=true
		;;
	m)	minrun=$OPTARG
		;;
	n)	nflag=true
		;;
	?)	_usage
		# NOTREACHED
    esac
done
shift `expr $OPTIND - 1`

sts=0
if $debug
then
    tmp=`pwd`/tmp-summary
    trap "exit \$sts" 0 1 2 3 15
else
    tmp=/var/tmp/$$
    trap "rm -f $tmp.*; exit \$sts" 0 1 2 3 15
fi
rm -f $tmp.*

if [ $# -eq 0 ]
then
    set -- `[ -d $base ] && cd $base && echo test-*`
fi

_what()
{
    cat
}

echo "==== QA Summary ===="
echo
echo " Run Pass Fail Nrun Host"
numqahosts=0
for vm
do
    cd $base
    if [ -d "$vm" ]
    then
	cd "$vm"
	vmdir="$vm"
    else
	vmdir=`echo ${vm}*`
	if [ -d "$vmdir" ]
	then
	    cd "$vmdir"
	else
	    echo "$vm: arrrgggh no $base/${vm}* directory"
	    continue
	fi
    fi
    basevmname=$vmdir
    rm -f $tmp.seen $tmp.found
    log=test.log
    if [ -f $log ]
    then
	touch $tmp.seen
	$debug && echo "Debug: == $vmdir log=$log =="
	# Looking for
	# Not run: 008 009 022 096 114 117 130 148 164 212 223 233 284 290 307 311 329 330 331 332 333 348 361 370 377 390 420 441 442 476 477 478 480 506 512 545 570 584 640 652 654 712 713 714
	# Failures: 062 169 244
	# Failed 3 of 516 tests
	# Passed all 67 tests
	#
	rm -f $tmp.dbg
	if egrep '^((Not run:)|Failures:|(Failed [0-9][0-9]* of [0-9][0-9]* tests)|(Passed all [0-9][0-9]* tests)|Info:|===)' <$log >/dev/null
	then
	    awk <$log >$tmp.out '
    /^Not run:/	{ notrun = NF - 2
		  nrlist = $3
		  for (i = 4; i <= NF; i++) nrlist = nrlist " " $i
		  state = 1
		  print NR,": Not run: start: notrun=" notrun >"'$tmp.dbg'"
		  next
		}
    /^Failures:/	{ nfail = NF - 1
		  flist = $2
		  for (i = 3; i <= NF; i++) flist = flist " " $i
		  print NR,": Failures: start: nfail=" nfail >"'$tmp.dbg'"
		  state = 2
		  next
		}
    /^Failed [0-9][0-9]* of [0-9][0-9]* tests/	{
		  failed = $2
		  run = $4
		  state = 3
		}
    /^Passed all [0-9][0-9]* tests/	{
		  failed = 0
		  run = $3
		  print NR,": Passed all: " run >"'$tmp.dbg'"
		  state = 3
		}
    state == 3	{ if (run >= '$minrun') printf "%4d %4d %4d %4d|%s|%s+%s\n",run,run-failed,failed,notrun,"'$basevmname'",flist,nrlist
		  state = 0
		  notrun = 0
		  nfail = 0
		}
    /^Info:/	{
		  state = 0
		  notrun = 0
		  nfail = 0
		  next
		}
    /^===/		{
		  state = 0
		  notrun = 0
		  nfail = 0
		  next
		}
    state == 1	{ # Not run: list broken over multiple lines
		  notrun += NF
		  for (i = 1; i <= NF; i++) nrlist = nrlist " " $i
		  print NR,": Not run: add",NF," notrun=" notrun >"'$tmp.dbg'"
		  next
		}
    state == 2	{ # Failures: list broken over multiple lines
		  nfail += NF
		  for (i = 1; i <= NF; i++) flist = flist " " $i
		  print NR,": Failures: add",NF," nfail=" nfail >"'$tmp.dbg'"
		  next
		}
    END		{ print NR,": END state=" state >"'$tmp.dbg'"
		}'
	    $debug && sed -e 's/^/Debug: line:/' <$tmp.dbg
	    mv $tmp.out $tmp.tmp
	    # $tmp.tmp line is ...
	    # <#run> <#passed> <#failed> <#notrun>|<hostname>|<failed list...>+<passed list>
	    if [ -s $tmp.tmp ]
	    then
		if $debug
		then
		    echo "Debug: summary lines ..."
		    sed -e 's/^/Debug: /' <$tmp.out
		fi
		sed -e 's/|[^|]*$//' <$tmp.tmp | _what >>$tmp.found
		nfail=`awk <$tmp.tmp '{print $3}'`
		if [ $nfail -gt 0 ]
		then
		    sed -e 's/.*|//' -e 's/+.*//' <$tmp.tmp \
		    | tr ' ' '\012' \
		    | sed -e "s/\$/ X $basevmname/" >>$tmp.map
		fi
		nnotrun=0
		if $nflag
		then
		    nnotrun=`sed -e 's/|.*//' <$tmp.tmp | awk '{print $4}'`
		    if [ $nnotrun -gt 0 ]
		    then
			sed -e 's/.*+//' <$tmp.tmp \
			| tr ' ' '\012' \
			| sed -e "s/\$/ N $basevmname/" >>$tmp.map
		    fi
		fi
		if $aflag || [ $nfail -gt 0 -o $nnotrun -gt 0 ]
		then
		    # pass lines look like:
		    # [97%] 1055 16s ...
		    # or
		    # [97%] 1055
		    # and may contain " [fixup]"
		    #
		    sed <$log -n \
				-e 's/ \[fixup]//' \
				-e '/^[[0-9][0-9]*%] [0-9][0-9]* [0-9][0-9]*s \.\.\./{
    s/.*] //
    s/ .*//
    p
    }' \
				-e '/^[[0-9][0-9]*%] [0-9][0-9]*$/{
    s/.*] //
    p
    }' \
		    | sed -e "s/\$/ P $basevmname/" >>$tmp.map
		fi
	    else
		$debug && echo "Debug: tag lines, but no QA results found"
	    fi
	else
	    $debug && echo "Debug: no QA result tag lines found"
	fi
    fi

    if [ -f $tmp.found ]
    then
	sort $tmp.found
	cat $tmp.found >>$tmp.counts
	numqahosts=`expr $numqahosts + 1`
    else
	if [ -f $tmp.seen ]
	then
	    printf "%-30s|%s\n" "Daily runs, but no QA" "$basevmname" | _what
	elif [ ! -f $tmp.seen ]
	then
	    printf "%-30s|%s\n" "No daily runs" "$basevmname" | _what
	fi
    fi

done

if [ -f $tmp.counts ]
then
    awk <$tmp.counts '
	{ nrun += $1; nfail += $3 }
END	{ printf "Summary: %d run, %d failed (%.2f%%)\n", nrun, nfail, 100*nfail/nrun }'
fi

if [ -f $tmp.map ]
then
    echo
    if $nflag
    then
	echo "==== QA Failure (X), Not Run (N) and Skipped (S) Map ===="
    else
	echo "==== QA Failure (X) and Not Run or Skipped (-) Map ===="
    fi
    echo
    ( ( sed -e 's/.* //' <$tmp.map | sort | uniq ) \
      ; echo "---" \
      ; sort -n $tmp.map \
      ; echo "END" \
    ) \
    | awk '
function heading() {
    printf "%4.4s","Host"
    printf " %5.5s",""		# %fail or nfail (-c)
    if (nflag) {
	printf " %5.5s",""	# %pass or npass (-c)
	printf " %5.5s",""	# %nrun or nnrun (-c)
    }
    for (i = 0; i < nh; i++) {
	h = host[i]
	sub(/^test-/, "", h)
	sub(/-container$/, "", h)
	sub(/-direct$/, "", h)
	if (h ~ /^centos/)
	    sub(/centos/, "c", h)
	else if (h ~ /^debian/)
	    sub(/debian/, "d", h)
	else if (h ~ /^fedora_rawhide/)
	    h = "frh"
	else if (h ~ /^fedora/)
	    sub(/fedora/, "f", h)
	else if (h ~ /^ubuntu/)
	    h = "u" substr(h, length(h)-3 ,2)
	printf " %3.3s",h
    }
    printf "\n"
    printf "%4.4s","Test"
    if (cflag)
	printf " %5.5s"," fail"
    else
	printf " %5.5s","%fail"
    if (nflag) {
	if (cflag) {
	    printf " %5.5s"," pass"
	    printf " %5.5s"," nrun"
	}
	else {
	    printf " %5.5s","%pass"
	    printf " %5.5s","%nrun"
	}
    }
    for (i = 0; i < nh; i++) printf " %3.3s"," "
    printf " %4.4s QA groups\n","Test"
}
BEGIN	{ nh = 0
	  state = 0
	  aflag = 0
	  if ("'"$aflag"'" == "true") aflag = 1
	  cflag = 0
	  if ("'"$cflag"'" == "true") cflag = 1
	  nflag = 0
	  if ("'"$nflag"'" == "true") nflag = 1
	}
$1 == "---"	{ heading()
		  state = 1
		  last = $1
		  next
		}
state == 0	{ host[nh] = $1; nh++; next }
$1 != last	{ if (last != "---") {
		      skip = nrun = bad = pass = 0
		      for (i = 0; i < nh; i++) {
			  if (fail[i] == "X") bad++
			  if (fail[i] == "P") {
			      pass++
			      fail[i] = " "
			  }
			  if (fail[i] == "N") {
			      nrun++
			      if (nflag == 0) fail[i] = "-"
			  }
			  if (fail[i] == "S") {
			      skip++
			      if (nflag == 0) fail[i] = "-"
			  }
		      }
		      if (bad > 0 || aflag) {
			  printf "%04d",last
			  if (nflag && nrun == nh) {
			    printf "   NOT RUN AT ALL "
			  }
			  else {
			      if (cflag)
				  printf "  %4d",bad
			      else
				  printf "  %3d%%",int(0.5+100*bad/'$numqahosts')
			      if (nflag) {
				  if (cflag) {
				      printf "  %4d",pass
				      printf "  %4d",nrun+skip
				  }
				  else {
				      if (pass > 0)
					  printf "  %3d%%",int(0.5+100*pass/'$numqahosts')
				      else
					  printf "  %3d%%",0
				      if (nrun > 0)
					  printf "  %3d%%",int(0.5+100*(nrun+skip)/'$numqahosts')
				      else
					  printf "  %3d%%",0
				  }
			      }
			  }
			  for (i = 0; i < nh; i++) {
			      printf " %3.3s",fail[i]
			  }
			  printf " %4.4s ",last
			  cmd = "( grep \"^" last " \" $HOME/src/pcp/qa/group || echo \"??? missing in QA group file\" ) | sed -e \"s/^[0-9]* //\""
			  system(cmd)
		      }
		  }
		  for (i = 0; i < nh; i++) fail[i] = "S"
		  last = $1
		 }
$1 == last	{ for (i = 0; i < nh; i++) {
		    if (host[i] == $3) {
			fail[i] = $2
			break
		    }
		  }
		}' \
    | sed \
	-e 's/ remote//' \
	-e 's/ sanity//' \
	-e 's/ ostest//' \
	-e 's/ local//' \
	-e 's/ oss//' \
	-e 's/ #[0-9][0-9]*//g' >$tmp.mapout
    num=0
    if $failsort
    then
	sed -e 2q <$tmp.mapout
	sed -e 1,2d <$tmp.mapout \
	| sort --key=2,2nr --key=1,1n 
    else
	cat $tmp.mapout
    fi \
    | while read line
    do
	if [ "$num" -eq 20 ]
	then
	    sed -e 2q <$tmp.mapout
	    num=0
	else
	    num=`expr $num + 1`
	fi
	echo "$line"
    done \
    | sed -e 's/^0/ /'
    sed -n -e 2p -e 2q <$tmp.mapout
    sed -n -e 1p -e 1q <$tmp.mapout
fi

