Skip to content

Commit

Permalink
Merge pull request #1209 from metacpan/haarg/move-more-queries
Browse files Browse the repository at this point in the history
move some queries out of the document namespace to query
  • Loading branch information
mickeyn authored May 6, 2024
2 parents 56c711a + c0a2b28 commit e894929
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 28 deletions.
27 changes: 2 additions & 25 deletions lib/MetaCPAN/Document/Release/Set.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ has query_release => (
by_author
by_author_and_name
by_author_and_names
find
get_contributors
get_files
latest_by_author
latest_by_distribution
modules
predecessor
recent
requires
reverse_dependencies
Expand All @@ -41,31 +43,6 @@ sub _build_query_release {
);
}

sub find {
my ( $self, $name ) = @_;
my $file = $self->filter( {
and => [
{ term => { distribution => $name } },
{ term => { status => 'latest' } }
]
} )->sort( [ { date => 'desc' } ] )->raw->first;
return unless $file;

my $data = $file->{_source}
|| single_valued_arrayref_to_scalar( $file->{fields} );
return $data;
}

sub predecessor {
my ( $self, $name ) = @_;
return $self->filter( {
and => [
{ term => { distribution => $name } },
{ not => { filter => { term => { status => 'latest' } } } },
]
} )->sort( [ { date => 'desc' } ] )->first;
}

sub find_github_based {
shift->filter( {
and => [
Expand Down
46 changes: 46 additions & 0 deletions lib/MetaCPAN/Query/Release.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1260,5 +1260,51 @@ sub _numify {
version->new($ver)->numify;
}

sub predecessor {
my ( $self, $name ) = @_;

my $res = $self->es->search(
index => $self->index_name,
type => 'release',
body => {
query => {
bool => {
must => [ { term => { distribution => $name } }, ],
must_not => [ { term => { status => 'latest' } }, ],
},
},
sort => [ { date => 'desc' } ],
size => 1,
},
);
my ($release) = $res->{hits}{hits}[0];
return unless $release;
return $release->{_source};
}

sub find {
my ( $self, $name ) = @_;

my $res = $self->es->search(
index => $self->index_name,
type => 'release',
body => {
query => {
bool => {
must => [
{ term => { distribution => $name } },
{ term => { status => 'latest' } },
],
},
},
sort => [ { date => 'desc' } ],
size => 1,
},
);
my ($file) = $res->{hits}{hits}[0];
return undef unless $file;
return $file->{_source};
}

__PACKAGE__->meta->make_immutable;
1;
5 changes: 2 additions & 3 deletions lib/MetaCPAN/Server/Controller/Diff.pm
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ sub release : Chained('index') : PathPart('release') : Args(1) {

my ( $latest, $previous );
try {
$latest = $c->model('CPAN::Release')->raw->find($name);
$previous
= $c->model('CPAN::Release')->raw->predecessor($name)->{_source};
$latest = $c->model('CPAN::Release')->find($name);
$previous = $c->model('CPAN::Release')->predecessor($name);
}
catch {
$c->detach('/not_found');
Expand Down

0 comments on commit e894929

Please sign in to comment.