Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move some queries out of the document namespace to query #1209

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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