Skip to content

Commit

Permalink
test results: Add tab with "previous results" in same scenario
Browse files Browse the repository at this point in the history
Querying the database for former test runs of the same scenario is a rather
costly operation which we do not want to do for multiple test results at once
but only for each individual test result (1:1 relation).

Related issue: https://progress.opensuse.org/issues/10212
  • Loading branch information
okurz committed Feb 10, 2016
1 parent e1f6970 commit 3a79826
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 2 deletions.
32 changes: 30 additions & 2 deletions lib/OpenQA/WebAPI/Controller/Test.pm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use strict;
use Mojo::Base 'Mojolicious::Controller';
use OpenQA::Utils;
use OpenQA::Schema::Result::Jobs;
use OpenQA::Scheduler::Scheduler qw/query_jobs/;
use OpenQA::Scheduler::Scheduler qw/query_jobs job_settings_subquery/;
use File::Basename;
use POSIX qw/strftime/;
use JSON qw/decode_json/;
Expand Down Expand Up @@ -233,7 +233,7 @@ sub read_test_modules {
}

sub show {
my ($self) = @_;
my ($self, $limit_previous) = @_;

return $self->reply->not_found if (!defined $self->param('testid'));

Expand All @@ -245,10 +245,14 @@ sub show {

return $self->reply->not_found unless $job;

my @scenario_keys = qw/DISTRI VERSION FLAVOR ARCH TEST/;
my $scenario = join('-', map { $job->settings_hash->{$_} } @scenario_keys);

$self->stash(testname => $job->settings_hash->{NAME});
$self->stash(distri => $job->settings_hash->{DISTRI});
$self->stash(version => $job->settings_hash->{VERSION});
$self->stash(build => $job->settings_hash->{BUILD});
$self->stash(scenario => $scenario);

# return $self->reply->not_found unless (-e $self->stash('resultdir'));

Expand Down Expand Up @@ -284,6 +288,30 @@ sub show {
$self->stash(ulogs => []);
}

my @conds;
push(@conds, {'me.state' => 'done'});
push(@conds, {'me.result' => {-not_in => [OpenQA::Schema::Result::Jobs::INCOMPLETE_RESULTS]}});
# instead of searching for "previous jobs" it might be better to search for same or previous builds
push(@conds, {id => {'<', $job->id}});
my %js_settings = map { $_ => $job->settings_hash->{$_} } @scenario_keys;
my $subquery = job_settings_subquery(%js_settings);
push(@conds, {'me.id' => {-in => $subquery->get_column('job_id')->as_query}});

my %attrs = (
rows => $limit_previous //= 10, # arbitrary limit of previous results to show
order_by => ['me.id DESC']);
my $previous_jobs_rs = $self->db->resultset("Jobs")->search({-and => \@conds}, \%attrs);
my @previous_jobs;
while (my $prev = $previous_jobs_rs->next) {
next unless ($job->settings_hash->{BUILD} >= $prev->settings_hash->{BUILD});
push(@previous_jobs, $prev);
}
for my $prev (@previous_jobs) {
$self->app->log->debug("settings (" . $prev->id . "): " . join('-', map { $prev->settings_hash->{$_} } @scenario_keys));
}
$self->stash(previous => \@previous_jobs);
$self->stash(limit_previous => $limit_previous);

$self->render('test/result');
}

Expand Down
42 changes: 42 additions & 0 deletions t/ui/16-tests_previous_results.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright (C) 2016 SUSE Linux GmbH
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

BEGIN {
unshift @INC, 'lib';
}

use Mojo::Base -strict;
use Test::More;
use Test::Mojo;
use OpenQA::Test::Case;

OpenQA::Test::Case->new->init_data;

use t::ui::PhantomTest;

my $t = Test::Mojo->new('OpenQA::WebAPI');

my $get = $t->get_ok('/tests/99946#previous')->status_is(200);
my $tab_label = $t->tx->res->dom->at('li a[href=#previous]')->all_text;
is($tab_label, q/Previous results (1)/, 'previous results with number is shown');
my $previous_results_header = $t->tx->res->dom->at('#previous #scenario')->all_text;
is($previous_results_header, q/Results for opensuse-13.1-DVD-i586-textmode, limited to 10/, 'header for previous results with scenario');
$get->element_exists('#res_99945', 'result from previous job');
$get->element_exists('#res_99945 .result_passed', 'previous job was passed');
my $build = $t->tx->res->dom->at('#previous #results .build')->all_text;
is($build, '0091', 'build of previous job is shown');

done_testing();
39 changes: 39 additions & 0 deletions templates/test/previous.html.ep
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<div id="scenario">
Results for <i><%= $scenario %></i>, limited to <%= $limit_previous %>
</div>
<div id="results">
<table id="results" class="display table table-striped no-wrap" style="width: 100%">
<thead>
<tr>
<th class="job">Result</th>
<th>Build</th>
<th>Finished</th>
</tr>
</thead>
% for my $job (@$previous) {
<tr>
% my $res = $job->result;
<td id="res_<%= $job->id %>">
% my $css = "";
% next unless ($job->state eq "done");
% $css .= " result_" . $res;
<span id="res-<%= $job->id %>">
<a href="<%= url_for('test', 'testid' => $job->id) %>">
<i class="status fa fa-circle<%= $css %>" title="Done: <%= $res %>"></i>
</a>
</span>
</td>
<td class="build"><%= $job->settings_hash->{BUILD} %></td>
<td class="t_finished">
%# <%= $job->t_finished %>
<abbr class="timeago" title="<%= $job->t_finished->datetime() %>Z"><%= format_time($job->t_finished) %></abbr>
(
<%= format_time_duration($job->t_finished - $job->t_started) %>
)
</td>
</tr>
% }
<tbody>
</tbody>
</table>
</div>
6 changes: 6 additions & 0 deletions templates/test/result.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
<li role="presentation"><a href="#downloads" aria-controls="downloads" role="tab" data-toggle="tab">Logs &amp; Assets</a></li>
<li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Settings</a></li>
<li role="presentation"><a href="#comments" aria-controls="comments" role="tab" data-toggle="tab">Comments (<%= scalar($job->comments) %>)</a></li>
<li role="presentation"><a href="#previous" aria-controls="previous" role="tab" data-toggle="tab">Previous results (<%= scalar(@$previous) %>)</a></li>
</ul>

<div class="tab-content">
Expand Down Expand Up @@ -241,6 +242,11 @@
%= include 'test/comments'
</div>
</div>
<div role="tabpanel" class="tab-pane" id="previous">
<div class="previous">
%= include 'test/previous'
</div>
</div>

</div>
</div>
Expand Down

0 comments on commit 3a79826

Please sign in to comment.