<?php

include_once 'TestViewSequenz.php';

function cmpRuneTime($a, $b) {
    if ($a->runTime == $b->runTime) {
        return 0;
    }
    return ($a->runTime < $b->runTime) ? 1 : -1;
}

function cmpName($a, $b) {
    if ($a->name == $b->name) {
        return 0;
    }
    return ($a->name < $b->name) ? -1 : 1;
}

function cmpLockTime($a, $b) {
    if ($a->lockTime == $b->lockTime) {
        return 0;
    }
    return ($a->lockTime < $b->lockTime) ? -1 : 1;
}

function cmpMsgCount($a, $b) {
    if ($a->msgCount == $b->msgCount) {
        return 0;
    }
    return ($a->msgCount < $b->msgCount) ? 1 : -1;
}

function aggrateTest($querry, $tests, $name) {
    $tmpTest = new TestViewSequenz();

    $count = 0;
    foreach ($tests as $key => $value) {
        if (preg_match($querry, $key) == true) {
            $count++;
            $tmpTest->msgCount += $value->msgCount;
            $tmpTest->lockTime += $value->lockTime;
            $tmpTest->runTime += $value->runTime;
            $tmpTest->lockTimeMsgCount += $value->lockTimeMsgCount;
            $tmpTest->lockRunTime += $value->lockRunTime;
        }
    }

    $tmpTest->msgCount /= $count;
    $tmpTest->lockTime /= $count;
    $tmpTest->runTime /= $count;
    $tmpTest->lockTimeMsgCount /= $count;
    $tmpTest->lockRunTime /= $count;
    $tmpTest->name = $name;
    return $tmpTest;
}

function createAggKeyList() {
    $maxFirst = 2;
    $firstX = "[0-$maxFirst]";

    $maxSecond = 2;
    $secondX = "[0-$maxSecond]";

    $maxThird = 5;
    $thirdX = "[0-$maxThird]";

    $keyList = array();

    for ($i = 0; $i <= $maxFirst + 1; $i++) {

        if ($i > $maxFirst) {
            $firstChar = "x";
            $firstRegex = $firstX;
        } else {
            $firstChar = "$i";
            $firstRegex = "$i";
        }

        for ($j = 0; $j <= $maxSecond + 1; $j++) {

            if ($j > $maxSecond) {
                $secondChar = "x";
                $secondRegex = $secondX;
            } else {
                $secondChar = "$j";
                $secondRegex = "$j";
            }

            for ($k = 0; $k <= $maxThird + 1; $k++) {

                if ($k > $maxThird) {
                    $thirdChar = "x";
                    $thirdRegex = $thirdX;
                } else {
                    $thirdChar = "$k";
                    $thirdRegex = "$k";
                }

                $tmpName = $firstChar . $secondChar . $thirdChar;
                $tmpRegex = '/^' . $firstRegex . $secondRegex . $thirdRegex . '$/';

                if (strpos($tmpName, "x") === false) {
                    continue;
                }

                $keyList["$tmpName"] = $tmpRegex;
            }
        }
    }
    return $keyList;
}

$sortMethod = $_GET["sort"];
if (!$sortMethod) {
    $sortMethod = "cmpName";
}

$testPath = $_GET["path"];
if (!$sortMethod) {
    $testPath = "evol/20131009/";
}

$commandCount = 1;
$commands;
$tests = array();
$dir = opendir($testPath);

while ($file = readdir($dir)) {
    if (eregi("[0-9]{2,3}.json", $file) && !eregi("[0-9]{2,3}.json_", $file)) {
        $tmpTest = new TestViewSequenz($testPath . $file);
        $tests["$tmpTest->name"] = $tmpTest;
    } else if (eregi("commands.json", $file)) {
        $sourceString = file_get_contents($testPath . $file);
        $source = json_decode($sourceString, true);
        $commandCount = count($source);
        $commands = $source;
    }
}

$keys = createAggKeyList();

foreach ($keys as $key => $value) {
    $tests["$key"] = aggrateTest($value, $tests, "$key");
}

echo "<table border=\"1\">";
echo "<tr><th><a href=\"?path=$testPath&sort=cmpName\">Setting</a></th><th><a href=\"?path=$testPath&sort=cmpRuneTime\">Runetime</a></th><th><a href=\"?path=$testPath&sort=cmpLockTime\">Locktime</a></th><th><a href=\"?path=$testPath&sort=cmpMsgCount\">Msgcount</a></th><th>Locktime per Msg</th><th>Locktime per runtime</th></tr>";

usort($tests, $sortMethod);

foreach ($tests as $testcase) {
    echo "<tr>";
    echo "<td style=\"padding:35px\"><a href=\"testdetails.php?testfile=$testcase->path\" target=\"_blank\">$testcase->name</a></td><td style=\"padding:35px\">$testcase->runTime</td><td style=\"padding:35px\">$testcase->lockTime</td><td style=\"padding:35px\">$testcase->msgCount</td><td style=\"padding:35px\">" . $testcase->lockTimeMsgCount . "</td><td style=\"padding:35px\">" . $testcase->lockRunTime . "</td>";
    echo "</tr>";
}

echo "</table>";
echo "Settings: <PeerPlacingStrategyType><PeerPlacingStrategyMetricType><MaintanceMomentMetricType><br/>";
echo " <pre>       
        enum PeerPlacingStrategyType {
            BALANCED_PEER_PLACING = 0,
            CREATE_MASTER_FIRST,
            DYNAMIC_PEER_PLACING
        };

        enum PeerPlacingStrategyMetricType {
            OUCMETRIC_CLUSTERSIZE = 0,
            OUCMETRIC_RESOURCEVALUE,
            OUCMETRIC_RESOURCEVALUECLUSTERSIZE
        };       

        enum MaintanceMomentMetricType {
            NOMOMENTMETRIC = 0,
            RESOURCEVALUEMOMENTMETRIC,
            MESSAGESEQCOUNTMOMENTMETRIC,
            LOGICTIMEMOMENTMETRIC,
            MESSAGECOUNTMOMENTMETRIC,
            RESOURCEVALUEMOMENTMETRIC2CHANCE, // 5
            BALANCENETWORKMOMENTMETRIC,
            MESSAGECLUSTERCOUNTMOMENTMETRIC
        }; 
</pre><br/>";
echo "commands: <br>";
$index = 0;
foreach ($commands as $command) {
    echo $index++ . " => " . $command . "<br>";
}
?>
