Skip to content

Commit 23b4179

Browse files
committed
move file history from model to query
1 parent 41c022f commit 23b4179

File tree

4 files changed

+82
-83
lines changed

4 files changed

+82
-83
lines changed

lib/MetaCPAN/Document/File/Set.pm

Lines changed: 0 additions & 72 deletions
This file was deleted.

lib/MetaCPAN/Query/File.pm

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,5 +677,74 @@ sub find_pod {
677677
}
678678
}
679679

680+
sub history {
681+
my ( $self, $type, $name, $path, $opts ) = @_;
682+
683+
$opts ||= {};
684+
if ( ref $path ) {
685+
$path = join '/', @$path;
686+
}
687+
688+
my $source = $opts->{fields};
689+
690+
my $query
691+
= $type eq "module"
692+
? {
693+
nested => {
694+
path => 'module',
695+
query => {
696+
constant_score => {
697+
filter => {
698+
bool => {
699+
must => [
700+
{ term => { "module.authorized" => true } },
701+
{ term => { "module.indexed" => true } },
702+
{ term => { "module.name" => $name } },
703+
]
704+
}
705+
}
706+
}
707+
}
708+
}
709+
}
710+
: $type eq "file" ? {
711+
bool => {
712+
must => [
713+
{ term => { path => $path } },
714+
{ term => { distribution => $name } },
715+
]
716+
}
717+
}
718+
719+
# XXX: to fix: no filtering on 'release' so this query
720+
# will produce modules matching duplications. -- Mickey
721+
: $type eq "documentation" ? {
722+
bool => {
723+
must => [
724+
{ match_phrase => { documentation => $name } },
725+
{ term => { indexed => true } },
726+
{ term => { authorized => true } },
727+
]
728+
}
729+
}
730+
: return undef;
731+
732+
my $res = $self->es->search(
733+
es_doc_path('file'),
734+
body => {
735+
query => $query,
736+
size => 500,
737+
sort => [ { date => 'desc' } ],
738+
( $source ? ( _source => $source ) : () ),
739+
},
740+
);
741+
742+
return {
743+
took => $res->{took},
744+
total => hit_total($res),
745+
files => [ map $_->{_source}, @{ $res->{hits}{hits} } ],
746+
};
747+
}
748+
680749
__PACKAGE__->meta->make_immutable;
681750
1;

lib/MetaCPAN/Server/Controller/Search/History.pm

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ with 'MetaCPAN::Server::Role::JSONP';
1212
has '+type' => ( default => 'file' );
1313

1414
sub get : Local : Path('') : Args {
15-
my ( $self, $c, @args ) = @_;
16-
my $data = $self->model($c)->history(@args)->raw;
17-
$c->stash( $data->all );
15+
my ( $self, $c, $type, $name, @path ) = @_;
16+
my $fields = $c->res->fields;
17+
my $data = $c->model('ESQuery')
18+
->file->history( $type, $name, \@path, { fields => $fields } );
19+
$c->stash($data);
1820
}
1921

2022
1;

t/release/moose.t

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use strict;
22
use warnings;
33
use lib 't/lib';
44

5-
use MetaCPAN::Server::Test qw( model );
6-
use MetaCPAN::Util qw( true false hit_total );
5+
use MetaCPAN::Server::Test qw( model query );
6+
use MetaCPAN::Util qw( true false );
77
use Test::More;
88

99
my $model = model();
@@ -85,16 +85,16 @@ $signature = $model->doc('file')->query( {
8585
ok( !$signature, 'SIGNATURE is not pod' );
8686

8787
{
88-
my $files = $model->doc('file');
89-
my $module = $files->history( module => 'Moose' )->raw->all;
90-
my $file = $files->history( file => 'Moose', 'lib/Moose.pm' )->raw->all;
88+
my $files = query()->file;
89+
my $module = $files->history( module => 'Moose' );
90+
my $file = $files->history( file => 'Moose', 'lib/Moose.pm' );
9191

9292
is_deeply( $module->{hits}, $file->{hits},
9393
'history of Moose and lib/Moose.pm match' );
94-
is( hit_total($module), 2, 'two hits' );
94+
is( $module->{total}, 2, 'two hits' );
9595

96-
my $pod = $files->history( documentation => 'Moose::FAQ' )->raw->all;
97-
is( hit_total($pod), 1, 'one hit' );
96+
my $pod = $files->history( documentation => 'Moose::FAQ' );
97+
is( $pod->{total}, 1, 'one hit' );
9898
}
9999

100100
done_testing;

0 commit comments

Comments
 (0)