From c9aadd8b7d40bb5c184054253f79406fca9687ec Mon Sep 17 00:00:00 2001 From: aszlig Date: Sun, 26 Apr 2015 07:37:37 +0200 Subject: [PATCH] Jobset.pm: Factor out build->eval mapping. We now have a searchBuildsAndEvalsForJobset, which creates such a mapping for us, so we don't need to duplicate code in jobs_tab and channels_tab. Also, we're going to use this for the overview of a particular channel as well, so it makes sense to put it in CatalystUtils instead of directly in Jobset.pm. Instead of eval->jobs, it's now eval->builds, because it's really an aggregate over the builds schema, rather than the job schema. Signed-off-by: aszlig --- src/lib/Hydra/Controller/Jobset.pm | 62 ++++++--------------------- src/lib/Hydra/Helper/CatalystUtils.pm | 39 +++++++++++++++++ src/root/jobset-channels-tab.tt | 2 +- src/root/jobset-jobs-tab.tt | 2 +- 4 files changed, 53 insertions(+), 52 deletions(-) diff --git a/src/lib/Hydra/Controller/Jobset.pm b/src/lib/Hydra/Controller/Jobset.pm index 0c830d94..f2b801f1 100644 --- a/src/lib/Hydra/Controller/Jobset.pm +++ b/src/lib/Hydra/Controller/Jobset.pm @@ -108,77 +108,39 @@ sub jobs_tab : Chained('jobsetChain') PathPart('jobs-tab') Args(0) { $c->stash->{filter} = $c->request->params->{filter} // ""; my $filter = "%" . $c->stash->{filter} . "%"; - my @evals = $c->stash->{jobset}->jobsetevals->search({ hasnewbuilds => 1}, { order_by => "id desc", rows => 20 }); - - my $evals = {}; - my %jobs; - my $nrBuilds = 0; - - foreach my $eval (@evals) { - my @builds = $eval->builds->search( - { job => { ilike => $filter }, ischannel => 0 }, - { columns => ['id', 'job', 'finished', 'buildstatus'] }); - foreach my $b (@builds) { - my $jobName = $b->get_column('job'); - $evals->{$eval->id}->{timestamp} = $eval->timestamp; - $evals->{$eval->id}->{jobs}->{$jobName} = - { id => $b->id, finished => $b->finished, buildstatus => $b->buildstatus }; - $jobs{$jobName} = 1; - $nrBuilds++; - } - last if $nrBuilds >= 10000; - } + my ($evals, $builds) = searchBuildsAndEvalsForJobset( + $c->stash->{jobset}, + { job => { ilike => $filter }, ischannel => 0 }, + 10000 + ); if ($c->request->params->{showInactive}) { $c->stash->{showInactive} = 1; foreach my $job ($c->stash->{jobset}->jobs->search({ name => { ilike => $filter } })) { - next if defined $jobs{$job->name}; - $c->stash->{inactiveJobs}->{$job->name} = $jobs{$job->name} = 1; + next if defined $builds->{$job->name}; + $c->stash->{inactiveJobs}->{$job->name} = $builds->{$job->name} = 1; } } $c->stash->{evals} = $evals; - my @jobs = sort (keys %jobs); + my @jobs = sort (keys %$builds); $c->stash->{nrJobs} = scalar @jobs; splice @jobs, 250 if $c->stash->{filter} eq ""; $c->stash->{jobs} = [@jobs]; } -# XXX: Pretty much a rip-off of jobs-tab, make it DRY! sub channels_tab : Chained('jobsetChain') PathPart('channels-tab') Args(0) { my ($self, $c) = @_; $c->stash->{template} = 'jobset-channels-tab.tt'; - my @evals = $c->stash->{jobset}->jobsetevals->search( - { hasnewbuilds => 1}, - { order_by => "id desc", rows => 20 } + my ($evals, $builds) = searchBuildsAndEvalsForJobset( + $c->stash->{jobset}, + { ischannel => 1 } ); - my $evals = {}; - my %channels; - - foreach my $eval (@evals) { - my @builds = $eval->builds->search( - { ischannel => 1 }, - { columns => ['id', 'job', 'finished', 'buildstatus'] } - ); - foreach my $b (@builds) { - my $jobName = $b->get_column('job'); - - $evals->{$eval->id}->{timestamp} = $eval->timestamp; - $evals->{$eval->id}->{channels}->{$jobName} = { - id => $b->id, - finished => $b->finished, - buildstatus => $b->buildstatus - }; - - $channels{$jobName} = 1; - } - } - $c->stash->{evals} = $evals; - my @channels = sort (keys %channels); + my @channels = sort (keys %$builds); $c->stash->{channels} = [@channels]; } diff --git a/src/lib/Hydra/Helper/CatalystUtils.pm b/src/lib/Hydra/Helper/CatalystUtils.pm index d31b3f53..db4640e9 100644 --- a/src/lib/Hydra/Helper/CatalystUtils.pm +++ b/src/lib/Hydra/Helper/CatalystUtils.pm @@ -10,6 +10,7 @@ use Hydra::Helper::Nix; our @ISA = qw(Exporter); our @EXPORT = qw( getBuild getPreviousBuild getNextBuild getPreviousSuccessfulBuild + searchBuildsAndEvalsForJobset error notFound gone accessDenied forceLogin requireUser requireProjectOwner requireAdmin requirePost isAdmin isProjectOwner trim @@ -85,6 +86,44 @@ sub getPreviousSuccessfulBuild { } +sub searchBuildsAndEvalsForJobset { + my ($jobset, $condition, $maxBuilds) = @_; + + my @evals = $jobset->jobsetevals->search( + { hasnewbuilds => 1}, + { order_by => "id desc", + rows => 20 + }); + + my $evals = {}; + my %builds; + my $nrBuilds = 0; + + foreach my $eval (@evals) { + my @allBuilds = $eval->builds->search( + $condition, + { columns => ['id', 'job', 'finished', 'buildstatus'] } + ); + + foreach my $b (@allBuilds) { + my $jobName = $b->get_column('job'); + + $evals->{$eval->id}->{timestamp} = $eval->timestamp; + $evals->{$eval->id}->{builds}->{$jobName} = { + id => $b->id, + finished => $b->finished, + buildstatus => $b->buildstatus + }; + $builds{$jobName} = 1; + $nrBuilds++; + } + last if $maxBuilds && $nrBuilds >= $maxBuilds; + } + + return ($evals, \%builds); +} + + sub error { my ($c, $msg, $status) = @_; $c->response->status($status) if defined $status; diff --git a/src/root/jobset-channels-tab.tt b/src/root/jobset-channels-tab.tt index 2a5f72a5..27d1f4cf 100644 --- a/src/root/jobset-channels-tab.tt +++ b/src/root/jobset-channels-tab.tt @@ -24,7 +24,7 @@ [% INCLUDE renderJobName project=project.name jobset=jobset.name job=chan %] [% FOREACH eval IN evalIds %] - [% r = evals.$eval.channels.$chan; IF r.id %][% INCLUDE renderBuildStatusIcon size=16 build=r %][% END %] + [% r = evals.$eval.builds.$chan; IF r.id %][% INCLUDE renderBuildStatusIcon size=16 build=r %][% END %] [% END %] [% END %] diff --git a/src/root/jobset-jobs-tab.tt b/src/root/jobset-jobs-tab.tt index 3c5b0832..1a9dac17 100644 --- a/src/root/jobset-jobs-tab.tt +++ b/src/root/jobset-jobs-tab.tt @@ -59,7 +59,7 @@ [% INCLUDE renderJobName project=project.name jobset=jobset.name job=j %] [% FOREACH eval IN evalIds %] - [% r = evals.$eval.jobs.$j; IF r.id %][% INCLUDE renderBuildStatusIcon size=16 build=r %][% END %] + [% r = evals.$eval.builds.$j; IF r.id %][% INCLUDE renderBuildStatusIcon size=16 build=r %][% END %] [% END %] [% END %]