#!/bin/sh
# PCP QA Test No. 1169
# pmrep dynamic/fixed header tests.
#
# Copyright (c) 2018-2019 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"

. ./common.python

$python -c "from pcp import pmapi" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python pcp pmapi module not installed"
$python -c "from collections import OrderedDict" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python collections OrderedDict module not installed"

which pmrep >/dev/null 2>&1 || _notrun "pmrep not installed"

status=1        # failure is the default!
hostname=`hostname`
$sudo rm -rf $tmp $tmp.* $seq.full
trap "cd $here; rm -rf $tmp.*; exit \$status" 0 1 2 3 15

#  timezone: :Australia/Melbourne (reporting, current is AEDT-11)
#  this is not deterministic .... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_archive_filter()
{
    sed \
       -e '/timezone/s/ (reporting.*)//' \
       -e "s#$here#QAPATH#g" \
    #end
}

log="--archive $here/archives/dyninsts -z"

# real QA test starts here
echo "== stdout/std test"
pmrep $log -p -u -1            disk.dev.read disk.dev.write mem.util.used | _archive_filter

echo "== stdout/std test with extended header"
pmrep $log -p -u -1 -x -C      disk.dev.read disk.dev.write mem.util.used | _archive_filter

echo "== stdout/std test with repeating header"
pmrep $log -p -u -1 -E 3       disk.dev.read disk.dev.write mem.util.used | _archive_filter

echo "== stdout/std test with separate header"
pmrep $log -p -u -1 -g         disk.dev.read disk.dev.write mem.util.used | _archive_filter

echo "== stdout/std test with extended/repeating/separate header"
pmrep $log -p -u -1 -x -E 3 -g disk.dev.read disk.dev.write mem.util.used | _archive_filter


echo "== stdout/colxrow test"
pmrep $log -p -u -X test -1            disk.dev.read disk.dev.write mem.util.used | _archive_filter

echo "== stdout/colxrow test with extended header"
pmrep $log -p -u -X test -1 -x -C      disk.dev.read disk.dev.write mem.util.used | _archive_filter

echo "== stdout/colxrow test with repeating header"
pmrep $log -p -u -X test -1 -E 3       disk.dev.read disk.dev.write mem.util.used | _archive_filter

echo "== stdout/colxrow test with separate header"
pmrep $log -p -u -X test -1 -g         disk.dev.read disk.dev.write mem.util.used | _archive_filter

echo "== stdout/colxrow test with extended/repeating/separate header"
pmrep $log -p -u -X test -1 -x -E 3 -g disk.dev.read disk.dev.write mem.util.used | _archive_filter

echo "== stdout/colxrow test with derived metric missing values using rate"
pmrep $log -p -u -X test -1 -S @00:00:02 -s 2 -J 3 -e 'a = 100 * rate(disk.dev.read)' \
                                      disk.dev.read disk.dev.write a mem.util.used | _archive_filter

echo "== stdout/colxrow test with derived metric missing values using raw values"
pmrep $log -p -u -X test -1 -S @00:00:02 -s 2 -J 3 -e 'a = 100 * rate(disk.dev.read)' -r \
                                      disk.dev.read disk.dev.write a mem.util.used | _archive_filter


echo "== csv test"
pmrep $log -p -u -o csv -1         disk.dev.read disk.dev.write mem.util.used | _archive_filter

echo "== csv test with extended header"
pmrep $log -p -u -o csv -1 -x -C   disk.dev.read disk.dev.write mem.util.used | _archive_filter

echo "== csv test with repeating header"
pmrep $log -p -u -o csv -1 -E 3    disk.dev.read disk.dev.write mem.util.used | _archive_filter

echo "== csv test with extended/repeating header"
pmrep $log -p -u -o csv -1 -x -E 3 disk.dev.read disk.dev.write mem.util.used | _archive_filter


echo "== fixed header test"
pmrep $log -p -u -X test -7            disk.dev.read disk.dev.write mem.util.used | _archive_filter

echo "== fixed header test with extended header"
pmrep $log -p -u -X test -7 -x -C      disk.dev.read disk.dev.write mem.util.used | _archive_filter

echo "== fixed header test with repeating header"
pmrep $log -p -u -X test -7 -E 3       disk.dev.read disk.dev.write mem.util.used | _archive_filter

echo "== fixed header test with separate header"
pmrep $log -p -u -X test -7 -g         disk.dev.read disk.dev.write mem.util.used | _archive_filter

echo "== fixed header test with extended/repeating/separate header"
pmrep $log -p -u -X test -7 -x -E 3 -g disk.dev.read disk.dev.write mem.util.used | _archive_filter

echo "== fixed header test with derived metric missing values using rate"
pmrep $log -p -u -X test -7 -S @00:00:02 -s 2 -J 3 -e 'a = 100 * rate(disk.dev.read)' \
                                      disk.dev.read disk.dev.write a mem.util.used | _archive_filter

echo "== fixed header test with derived metric missing values using raw values"
pmrep $log -p -u -X test -7 -S @00:00:02 -s 2 -J 3 -e 'a = 100 * rate(disk.dev.read)' -r \
                                      disk.dev.read disk.dev.write a mem.util.used | _archive_filter


# Corner case to ensure noticing changing instances
echo "== ensure noticing changing instances"
log="--archive $here/archives/dyninsts2 -z"
pmrep $log -p -u -1 -r -s 4 disk.dev.read disk.dev.write | _archive_filter

# success, all done
status=0
exit
