From dae20cd432a811e03e0c5c17ad00dba6ecb30560 Mon Sep 17 00:00:00 2001 From: Marco Vittorini Orgeas Date: Fri, 23 Oct 2015 19:25:30 +0200 Subject: [PATCH 1/3] No backpan: closes #397. --- lib/MetaCPAN/Web/Controller/Author.pm | 13 +++++++-- lib/MetaCPAN/Web/Model/API/Release.pm | 40 +++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/lib/MetaCPAN/Web/Controller/Author.pm b/lib/MetaCPAN/Web/Controller/Author.pm index f881133c3d..544abf1202 100644 --- a/lib/MetaCPAN/Web/Controller/Author.pm +++ b/lib/MetaCPAN/Web/Controller/Author.pm @@ -61,8 +61,17 @@ sub index : Chained('root') PathPart('') Args(0) { = $c->model('API::Favorite')->by_user( $author->{user} )->recv; $took += $faves_data->{took} || 0; - $faves = [ map { $_->{fields} } @{ $faves_data->{hits}->{hits} } ]; - single_valued_arrayref_to_scalar($faves); + my @all_fav = map { $_->{fields}->{distribution} } + @{ $faves_data->{hits}->{hits} }; + my $noLatest = $c->model('API::Release')->no_latest(@all_fav); + + $faves = [ + map { + my $distro = $_->{fields}->{distribution}; + $noLatest->{$distro} ? () : $_->{fields}; + } @{ $faves_data->{hits}->{hits} } + ]; + $self->single_valued_arrayref_to_scalar($faves); $faves = [ sort { $b->{date} cmp $a->{date} } @{$faves} ]; } diff --git a/lib/MetaCPAN/Web/Model/API/Release.pm b/lib/MetaCPAN/Web/Model/API/Release.pm index 3b94fbcbe3..973361eb31 100644 --- a/lib/MetaCPAN/Web/Model/API/Release.pm +++ b/lib/MetaCPAN/Web/Model/API/Release.pm @@ -4,6 +4,9 @@ use namespace::autoclean; extends 'MetaCPAN::Web::Model::API'; +use List::Util qw(first); +use List::MoreUtils qw(uniq); + =head1 NAME MetaCPAN::Web::Model::Release - Catalyst Model @@ -441,6 +444,43 @@ sub topuploaders { ); } +sub no_latest { + my ( $self, @distributions ) = @_; + + # If there are no distributions return + return {} unless (@distributions); + + @distributions = uniq @distributions; + my $result = $self->request( + '/release/_search', + { + size => scalar @distributions, + query => { + filtered => { + query => { match_all => {} }, + filter => { + and => [ + { terms => { distribution => \@distributions } }, + { term => { status => 'latest' } } + ] + } + } + }, + fields => [qw(distribution status)] + } + )->recv; + + my @latest + = map { $_->{fields}->{distribution} } @{ $result->{hits}->{hits} }; + + my %no_latest = map { + my $distro = $_; + ( first { $_ eq $distro } @latest ) ? () : ( $distro, 1 ); + } @distributions; + + return \%no_latest; +} + __PACKAGE__->meta->make_immutable; 1; From 56bb4479aef06f9bace99624cebe1014f5b616e3 Mon Sep 17 00:00:00 2001 From: Marco Vittorini Orgeas Date: Sat, 24 Oct 2015 18:20:47 +0200 Subject: [PATCH 2/3] no_latest: use callback. --- lib/MetaCPAN/Web/Controller/Author.pm | 5 ++-- lib/MetaCPAN/Web/Model/API/Release.pm | 36 ++++++++++++++++++--------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/lib/MetaCPAN/Web/Controller/Author.pm b/lib/MetaCPAN/Web/Controller/Author.pm index 544abf1202..b6a072064f 100644 --- a/lib/MetaCPAN/Web/Controller/Author.pm +++ b/lib/MetaCPAN/Web/Controller/Author.pm @@ -63,12 +63,13 @@ sub index : Chained('root') PathPart('') Args(0) { my @all_fav = map { $_->{fields}->{distribution} } @{ $faves_data->{hits}->{hits} }; - my $noLatest = $c->model('API::Release')->no_latest(@all_fav); + my $noLatest = $c->model('API::Release')->no_latest(@all_fav)->recv; + $took += $noLatest->{took} || 0; $faves = [ map { my $distro = $_->{fields}->{distribution}; - $noLatest->{$distro} ? () : $_->{fields}; + $noLatest->{no_latest}->{$distro} ? () : $_->{fields}; } @{ $faves_data->{hits}->{hits} } ]; $self->single_valued_arrayref_to_scalar($faves); diff --git a/lib/MetaCPAN/Web/Model/API/Release.pm b/lib/MetaCPAN/Web/Model/API/Release.pm index 973361eb31..54ee71ad6a 100644 --- a/lib/MetaCPAN/Web/Model/API/Release.pm +++ b/lib/MetaCPAN/Web/Model/API/Release.pm @@ -446,12 +446,13 @@ sub topuploaders { sub no_latest { my ( $self, @distributions ) = @_; + my $cv = $self->cv; # If there are no distributions return return {} unless (@distributions); @distributions = uniq @distributions; - my $result = $self->request( + $self->request( '/release/_search', { size => scalar @distributions, @@ -468,17 +469,28 @@ sub no_latest { }, fields => [qw(distribution status)] } - )->recv; - - my @latest - = map { $_->{fields}->{distribution} } @{ $result->{hits}->{hits} }; - - my %no_latest = map { - my $distro = $_; - ( first { $_ eq $distro } @latest ) ? () : ( $distro, 1 ); - } @distributions; - - return \%no_latest; + )->cb( + sub { + my $data = shift->recv; + my @latest + = map { $_->{fields}->{distribution} } + @{ $data->{hits}->{hits} }; + $cv->send( + { + took => $data->{took}, + no_latest => { + map { + my $distro = $_; + ( first { $_ eq $distro } @latest ) + ? () + : ( $distro, 1 ); + } @distributions + } + } + ); + } + ); + return $cv; } __PACKAGE__->meta->make_immutable; From 2f022e4ed9bf6300d3de84cf332ed1f84652aff7 Mon Sep 17 00:00:00 2001 From: Olaf Alders Date: Thu, 11 May 2017 18:54:31 +0200 Subject: [PATCH 3/3] single_valued_arrayref_to_scalar is a function, not a method. --- lib/MetaCPAN/Web/Controller/Author.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/MetaCPAN/Web/Controller/Author.pm b/lib/MetaCPAN/Web/Controller/Author.pm index b6a072064f..148552b982 100644 --- a/lib/MetaCPAN/Web/Controller/Author.pm +++ b/lib/MetaCPAN/Web/Controller/Author.pm @@ -72,7 +72,7 @@ sub index : Chained('root') PathPart('') Args(0) { $noLatest->{no_latest}->{$distro} ? () : $_->{fields}; } @{ $faves_data->{hits}->{hits} } ]; - $self->single_valued_arrayref_to_scalar($faves); + single_valued_arrayref_to_scalar($faves); $faves = [ sort { $b->{date} cmp $a->{date} } @{$faves} ]; }