#!/bin/sh
# PCP QA Test No. 1886
# Test function evaluation in pmseries queries
#
# Copyright (c) 2020 Red Hat.  All Rights Reserved.
#
seq=`basename $0`
echo "QA output created by $seq"
path=""

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

# This test is not run if we dont have pmseries and redis installed.
_check_series

_cleanup()
{
    [ -n "$redisport" ] && redis-cli -p $redisport shutdown
    _restore_config $PCP_SYSCONF_DIR/pmseries
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

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

_filter_source()
{
    sed \
	-e "s,$here,PATH,g" \
    #end
}

_sort_context()
{
    sed -e 's/    Context: //g' -e 's/, /\n/g' | \
    LC_COLLATE=POSIX sort | \
    $PCP_AWK_PROG 'BEGIN { printf "    Context: " } {
	if (count == 0) {printf("%s", $0)} else {printf(", %s", $0)};
	count++
    } END { print out }'
}

# real QA test starts here
redisport=`_find_free_port`
_save_config $PCP_SYSCONF_DIR/pmseries
$sudo rm -f $PCP_SYSCONF_DIR/pmseries/*

echo "Start test Redis server ..."
redis-server --port $redisport > $tmp.redis 2>&1 &
echo "PING"
pmsleep 0.125
redis-cli -p $redisport ping
echo

args="-p $redisport -Z UTC"

echo "== Load metric data into this redis instance"
pmseries $args --load "{source.path: \"$here/archives/proc\"}" | _filter_source

echo;echo "== Verify rate() function for a singular metric"
pmseries $args 'kernel.all.pswitch[count:5]'
echo Compared to:
pmseries $args 'rate(kernel.all.pswitch[count:5])'
echo

echo;echo "== Verify min/max() functions for a non-sigular metric"
pmseries $args 'kernel.all.load[count:5]'
pmseries $args 'max(kernel.all.load[count:5])'
pmseries $args 'min(kernel.all.load[count:5])'

echo;echo "== Verify min/max() functions for a non-sigular metric, only one instance"
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5]'
echo Compared to max:
pmseries $args 'max(kernel.all.load{instance.name == "5 minute"}[count:5])'
echo Compared to min:
pmseries $args 'min(kernel.all.load{instance.name == "5 minute"}[count:5])'

echo;echo "== Verify min/max() functions for a singular metric"
pmseries $args 'kernel.all.uptime[count:10]'
echo Compared to max:
pmseries $args 'max(kernel.all.uptime[count:10])'
echo Compared to min:
pmseries $args 'min(kernel.all.uptime[count:10])'

echo;echo "== Verify rescale() functions for a singular metric"
pmseries $args 'kernel.all.uptime[count:2]'
echo Compared to rescale to sec:
pmseries $args 'rescale(kernel.all.uptime[count:2], "sec")'
echo Compared to rescale to min:
pmseries $args 'rescale(kernel.all.uptime[count:2], "min")'
echo Compared to rescale to min and then hour:
pmseries $args 'rescale(rescale(kernel.all.uptime[count:2], "min"), "hour")'

echo;echo "== Verify abs() functions for a non-singular metric"
pmseries $args 'kernel.all.load[count:5]'
echo Compared to abs:
pmseries $args 'abs(kernel.all.load[count:5])'

echo;echo "== Verify abs() functions for a non-singular metric, only one instance"
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5]'
echo Compared to abs:
pmseries $args 'abs(kernel.all.load{instance.name == "5 minute"}[count:5])'

echo;echo "== Verify log() functions for a singular metric"
pmseries $args 'kernel.all.pswitch[count:2]'
echo Compared to log:
pmseries $args 'log(kernel.all.pswitch[count:2],3)'
echo Compared to 'log(log(..,3), 2):'
pmseries $args 'log(log(kernel.all.pswitch[count:2],3),2)'

echo;echo "== Verify floor() functions for a non-singular metric"
pmseries $args 'kernel.all.load[count:5]'
echo Compared to floor:
pmseries $args 'floor(kernel.all.load[count:5])'

echo;echo "== Verify round() functions for a non-singular metric"
pmseries $args 'kernel.all.load[count:5]'
echo Compared to round:
pmseries $args 'round(kernel.all.load[count:5])'

echo;echo "== Verify round() functions for a singular metric"
pmseries $args 'kernel.all.uptime[count:5]'
echo Compared to round:
pmseries $args 'round(kernel.all.uptime[count:5])'

echo;echo "== Verify addition functions for a singular metric"
pmseries $args 'kernel.all.uptime[count:5]'
echo 'Compared to series + series:'
pmseries $args 'kernel.all.uptime[count:5] + kernel.all.uptime[count:5]'

echo;echo "== Verify addition functions for a non-singular metric"
pmseries $args 'kernel.all.load[count:5]'
echo 'Compared to series + series:'
pmseries $args 'kernel.all.load[count:5] + kernel.all.load[count:5]'

echo;echo "== Verify addition functions for a non-singular metric, only one instance"
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5]'
echo 'Compared to series + series:'
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5] + kernel.all.load{instance.name == "5 minute"}[count:5]'

echo;echo "== Verify subtraction functions for a singular metric"
pmseries $args 'kernel.all.uptime[count:5]'
echo 'Compared to series - series:'
pmseries $args 'kernel.all.uptime[count:5] - kernel.all.uptime[count:5]'

echo;echo "== Verify subtraction functions for a non-singular metric"
pmseries $args 'kernel.all.load[count:5]'
echo 'Compared to series - series:'
pmseries $args 'kernel.all.load[count:5] - kernel.all.load[count:5]'

echo;echo "== Verify subtraction functions for a non-singular metric, only one instance"
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5]'
echo 'Compared to series - series:'
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5] - kernel.all.load{instance.name == "5 minute"}[count:5]'

echo;echo "== Verify division functions for a singular metric"
pmseries $args 'kernel.all.uptime[count:5]'
echo 'Compared to series / series:'
pmseries $args 'kernel.all.uptime[count:5] / kernel.all.uptime[count:5]'

echo;echo "== Verify division functions for a non-singular metric"
pmseries $args 'kernel.all.load[count:5]'
echo 'Compared to series / series:'
pmseries $args 'kernel.all.load[count:5] / kernel.all.load[count:5]'

echo;echo "== Verify division functions for a non-singular metric, only one instance"
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5]'
echo 'Compared to series / series:'
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5] / kernel.all.load{instance.name == "5 minute"}[count:5]'

# success, all done
status=0
exit
