Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'master' into mickey/use_favorite_agg_by_distributions_e…
…ndpoint
  • Loading branch information
oalders committed Jul 4, 2017
2 parents cd9e887 + cbb4938 commit eef2417
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 51 deletions.
55 changes: 4 additions & 51 deletions lib/MetaCPAN/Web/Controller/Mirrors.pm
Expand Up @@ -12,61 +12,14 @@ sub index : Path : Args(0) {
$c->browser_max_age('1d');
$c->cdn_max_age('1d');

my $location;
my @protocols;
if ( my $q = $c->req->parameters->{'q'} ) {
my @parts = split( /\s+/, $q );
foreach my $part (@parts) {
push( @protocols, $part )
if ( grep { $_ eq $part } qw(http ftp rsync) );
}
if ( $q =~ /loc\:([^\s]+)/ ) {
$location = [ split( /,/, $1 ) ];
}
}

my @filters
= map +{ filter => { missing => { field => $_ } } },
@protocols;
my $query = $c->req->parameters->{'q'};
my $data = $c->model('API::Mirror')->search($query)->get;

my $data = $c->model('API')->request(
'/mirror/_search',
{
size => 999,
query => (
@filters
? {
bool => { must_not => { bool => should => \@filters } }
}
: { match_all => {} }
),
$location
? (
sort => {
_geo_distance => {
location => [ $location->[1], $location->[0] ],
order => 'asc',
unit => 'km'
}
}
)
: ( sort => [ 'continent', 'country' ] )
}
)->get;
my $latest = [
map {
{
%{ $_->{_source} }, distance => $location
? $_->{sort}->[0]
: undef
}
} @{ $data->{hits}->{hits} }
];
$c->stash(
{
mirrors => $latest,
mirrors => $data->{mirrors},
took => $data->{took},
total => $data->{hits}->{total},
total => $data->{total},
template => 'mirrors.html',
}
);
Expand Down
14 changes: 14 additions & 0 deletions lib/MetaCPAN/Web/Model/API/Mirror.pm
@@ -0,0 +1,14 @@
package MetaCPAN::Web::Model::API::Mirror;
use Moose;
use namespace::autoclean;

extends 'MetaCPAN::Web::Model::API';

sub search {
my ( $self, $query ) = @_;
$self->request( "/mirror/search", undef, { q => $query } );
}

__PACKAGE__->meta->make_immutable;

1;

0 comments on commit eef2417

Please sign in to comment.