Skip to content
Closed
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
12 changes: 11 additions & 1 deletion lib/MetaCPAN/Web/Controller/Author.pm
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,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} } ];
my @all_fav = map { $_->{fields}->{distribution} }
@{ $faves_data->{hits}->{hits} };
my $noLatest = $c->model('API::Release')->no_latest(@all_fav)->recv;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we call this variable $no_latest? We've been avoiding snake case in general.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hash access will be slightly faster, so that's fine, but why not return a hash keyed on the distribution name? I'm not sure what we win by checking $noLatest->{no_latest}->{$distro} rather than $no_latest->{$distro}

I don't know if it matters or not but I tried to mimic ES data structures (seen that all the other methods returns an hash with some metadata and results are nested under the "hits" key) because I thought that metadata is still important (see the took key):

{
    took      => $data->{took},
    no_latest => {
        map {
            my $distro = $_;
            ( first { $_ eq $distro } @latest )
            ? ()
            : ( $distro, 1 );
        } @distributions
    }
}

As bonus you can easily check if there are any hits at all by inspecting the no_latest hash's key number.
Maybe it could be renamed to "hits", so then you can access it with $noLatest->{hits}->{$distro} which is semantically better.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get why you did it -- it makes sense to me, but we don't really need to mimic the ES structure in this case (or worry about how long the query took). The purpose of the model is to take the ES data structures and massage them into something we can easily work with.

In this case the it's basically a question of whether or not a condition is true. So for that, the structure above is a lot more complex than we need.

It's a big codebase with a lot of code to follow, so my inclination is to merge only what we need moving forward. If we need more info at some point in the future, we can rework the model, but we're best off avoiding all increased complexity if we don't actually need it right now. In this case returning an array of dist names would be my preference. We can always use a map to create a hash if we need it. Or we can return the hash, but the extra keys aren't adding value just yet.

$took += $noLatest->{took} || 0;

$faves = [
map {
my $distro = $_->{fields}->{distribution};
$noLatest->{no_latest}->{$distro} ? () : $_->{fields};
} @{ $faves_data->{hits}->{hits} }
];
$self->single_valued_arrayref_to_scalar($faves);
$faves = [ sort { $b->{date} cmp $a->{date} } @{$faves} ];
}
Expand Down
52 changes: 52 additions & 0 deletions lib/MetaCPAN/Web/Model/API/Release.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use namespace::autoclean;

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

use List::Util qw(first);
use List::MoreUtils qw(uniq);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can change this to use List::AllUtils qw( first uniq ); If List::AllUtils is not in our deps, we should add it.


=head1 NAME

MetaCPAN::Web::Model::Release - Catalyst Model
Expand Down Expand Up @@ -440,6 +443,55 @@ sub topuploaders {
);
}

sub no_latest {
my ( $self, @distributions ) = @_;
my $cv = $self->cv;

# If there are no distributions return
return {} unless (@distributions);

@distributions = uniq @distributions;
$self->request(
'/release/_search',
{
size => scalar @distributions,
query => {
filtered => {
query => { match_all => {} },
filter => {
and => [
{ terms => { distribution => \@distributions } },
{ term => { status => 'latest' } }
]
}
}
},
fields => [qw(distribution status)]
}
)->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;

1;