<%perl>
# test-runs.html
#
# @author D. Chang
#
# @todo Adapt page to use Perl modules for processing.
# @todo Add option to view testing output.
# @todo Add support for test run running time.
# @todo Modify queries to sort by time.
# @todo Match individual query results by test run ID.
</%perl>

<& header.mas &>

<%perl>
my $unit_test_limit="8";

# For everything to be syncronized, it is important for the ORDER BYs to be
# the same.

# @todo Remove database connection code and related routines from within here.

use DBI;
use PS::MOPS::Test::SimtestLib;
my $dbuser="root";
my $dbpass="panstar1";
my $dbname="psmops_testing_results";
my $dbhost="schmopc01.ifa.hawaii.edu";
my $dbconnect="DBI:mysql:$dbname:$dbhost";
my $dbh=DBI->connect($dbconnect,$dbuser,$dbpass) || die "ERROR: Cannot connect to database $dbname.";
my $sql="SELECT test_runs.id, DATE_FORMAT(time,'%Y-%b-%d %a %H:%i'),user,
           DATE_FORMAT(MAX(tv.cvs_time),'%Y-%b-%d %a %H:%i')
         FROM test_runs
         INNER JOIN test_versions tv ON tv.test_run_id=test_runs.id
         GROUP BY tv.test_run_id
         ORDER BY test_runs.id DESC LIMIT $unit_test_limit";
#         WHERE ISNULL(test_runs.type)";
my $sth=$dbh->prepare($sql);
my $result=$sth->execute;

# Count test runs.
my $tr;
if($result){
    $tr=$sth->fetchall_arrayref();
}
$sql="SELECT test_runs.id, COUNT(utr.id)
      FROM test_runs
      INNER JOIN unit_test_results utr ON utr.test_run=test_runs.id
      WHERE utr.result != 'n'
      GROUP BY utr.test_run
      ORDER BY test_runs.id DESC LIMIT $unit_test_limit";
$sth=$dbh->prepare($sql);
$result=$sth->execute;

# Count successful tests.
my $test_cnts;
if($result){
    $test_cnts=$sth->fetchall_arrayref();
}
$sql="SELECT test_runs.id, COUNT(utr.id)
      FROM test_runs
      INNER JOIN unit_test_results utr ON utr.test_run=test_runs.id
      WHERE utr.result = 'p'
      GROUP BY utr.test_run
      ORDER BY test_runs.id DESC LIMIT $unit_test_limit";
$sth=$dbh->prepare($sql);
$result=$sth->execute;
my $pass_cnts;
if($result){
    $pass_cnts=$sth->fetchall_arrayref();
}

# Get sum of testing wallclock time.
$sql="SELECT test_runs.id, SUM(utr.wall)
      FROM test_runs
      INNER JOIN unit_test_results utr ON utr.test_run=test_runs.id
      WHERE (utr.result='p' OR utr.result='f')
      GROUP BY utr.test_run
      ORDER BY test_runs.id DESC LIMIT $unit_test_limit";
$sth=$dbh->prepare($sql);
$result=$sth->execute;
my $sum_wall;
if($result){
    $sum_wall=$sth->fetchall_arrayref();
}


# ****************************************************************************
# Retrieve simtest results.
# ****************************************************************************
$sql="SELECT id, DATE_FORMAT(time,'%Y-%b-%d %a %H:%i'), user, type
      FROM test_runs
      WHERE type='sim'
      ORDER BY id DESC LIMIT $unit_test_limit";
$sth=$dbh->prepare($sql);
$result=$sth->execute;
my $tr_for_str; # Test runs that are simtests.
if($result){
    $tr_for_str=$sth->fetchall_arrayref();
}

# Get simtest details.
$sql="SELECT test_runs.id, str.instance_name, str.ssm, str.survey, str.configuration, str.simtest_config, str.id
      FROM test_runs
      INNER JOIN simtest_results str ON str.test_run=test_runs.id
      ORDER BY test_runs.id DESC LIMIT $unit_test_limit";
$sth=$dbh->prepare($sql);
$result=$sth->execute;
my $str; # sim test result.
if($result){
    $str=$sth->fetchall_arrayref();
}

</%perl>

<h2>Recent Test Runs</h2>
<h3>Unit Testing</h3>

<table border="1">
<tr>
<td>Test Run ID</td>
<td>Testing Time (GMT)</td>
<td>User</td>
<td>Result</td>
<td>Wall Sum [s]</td>
<td>Last Test Time (GMT)</td>

</tr>

% for(my $i=0;$i<scalar(@$tr);$i++) {

<tr>
<td>
<% $tr->[$i]->[0] %>
</td>
<td>
<a href="unit-test-results.html?test_run_id=<% $tr->[$i]->[0] %>"><% $tr->[$i]->[1] %></a>
</td>
<td>
<% $tr->[0]->[2] %>
</td>

<% # @todo Check that test run ID for test counts matches test run ID for unit test results. %>
<%perl>
my $passed=$pass_cnts->[$i]->[1];
my $total=$test_cnts->[$i]->[1];
if($passed==$total){
    print "<td bgcolor=\"#009900\">$passed/$total</td>";
}
else {
    print "<td bgcolor=\"#ffce23\">$passed/$total</td>";
}
</%perl>

<td>

% printf("%.5f", $sum_wall->[$i]->[1]);

</td>

<td>
<a href="testing-state.html?test_run_id=<% $tr->[$i]->[0] %>"><% $tr->[$i]->[3] %>
</td>
</tr>

% }
</table>

<h3>Simulation Testing</h3>
<table border="1">
<tr>
<td>Test Run ID</td>
<td>Testing Time (GMT)</td>
<td>User</td>
<td>Result</td>
<td>Simtest Configuration</td>
<!--
<td>Simtest Configuration</td>
<td>Wall Sum</td>
-->
</tr>

% for(my $i=0;$i<scalar(@$tr_for_str);$i++) { # Iterate over simtest test runs.
% my $test_run_id=$tr_for_str->[$i]->[0];
<tr>
<td>
<!-- Test Run ID -->
% #@$tr_for_str->[$i]->[0]
<% $test_run_id %>
</td>

<td>
<!-- <a href="simtest-results.html?test_run_id=<% $tr_for_str->[$i]->[0] %>"><% $tr_for_str->[$i]->[1] %></a> -->
<% $tr_for_str->[$i]->[1] %>
</td>

<td>
<% $tr_for_str->[$i]->[2] %>
</td>


<%perl>

# Retrieve goals passed and goals total for current Test Run.
my($goals_passed, $goals_total)=getSimrunPassed_Total($test_run_id);

# Output passed/failed ratio.
# my $passed=$pass_cnts->[$i]->[1];
#my $total=$test_cnts->[$i]->[1];
if($goals_passed==$goals_total){
    print "<td bgcolor=\"#009900\">$goals_passed/$goals_total</td>";
}
else {
    print "<td bgcolor=\"#ffce23\">$goals_passed/$goals_total</td>";
}
</%perl>

<td>
<a href="simtest-config.html?test_run_id=<% $str->[$i]->[0] %>"><% $str->[$i]->[5] %></a>
</td>

</tr>

% }

</table>




<font size="-1">
<p>
<b>Last Test Time</b> Last CVS time of the testing components. Linked to testing state.<br>
<b>Result</b> Successful tests/Total tests run.<br>
<b>Wall Sum</b> Sum of the wallclock time of each test.<br>
Green-colored (all clear) results indicate all tests passed.<br>
Yellow-colored (warning) results indicate one or more tests did not pass.
</font>
</body>
</html>
