Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
aggregate counts using one query
  • Loading branch information
mickeyn committed Jul 8, 2016
1 parent 31f63e1 commit 9a71cba
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
26 changes: 26 additions & 0 deletions lib/MetaCPAN/Document/Release.pm
Expand Up @@ -285,6 +285,32 @@ use warnings;
use Moose;
extends 'ElasticSearchX::Model::Document::Set';

sub aggregate_status_by_author {
my ( $self, $pauseid ) = @_;
my $agg = $self->es->search(
{
index => $self->index->name,
type => 'release',
body => {
query => {
term => { author => $pauseid }
},
aggregations => {
count => { terms => { field => 'status' } }
},
size => 0,
}
}
);
my %ret = ( cpan => 0, latest => 0, backpan => 0 );
if ($agg) {
$ret{ $_->{'key'} } = $_->{'doc_count'}
for @{ $agg->{'aggregations'}{'count'}{'buckets'} };
}
$ret{'backpan-only'} = delete $ret{'backpan'};
return \%ret;
}

sub find_depending_on {
my ( $self, $modules ) = @_;
return $self->filter(
Expand Down
21 changes: 2 additions & 19 deletions lib/MetaCPAN/Server/Controller/Author.pm
Expand Up @@ -33,29 +33,12 @@ sub get : Path('') : Args(1) {
my $st = $file->{_source} || $file->{fields};
if ( $st and $st->{pauseid} ) {
$st->{release_count}
= $self->_get_author_release_status_counts( $c, $st->{pauseid} );
= $c->model('CPAN::Release')
->aggregate_status_by_author( $st->{pauseid} );
}
$c->stash($st)
|| $c->detach( '/not_found',
['The requested field(s) could not be found'] );
}

sub _get_author_release_status_counts {
my ( $self, $c, $pauseid ) = @_;
my %ret;
for my $status (qw< cpan backpan latest >) {
$ret{$status} = $c->model('CPAN::Release')->filter(
{
and => [
{ term => { author => $pauseid } },
{ term => { status => $status } }
]
}
)->count
|| 0;
}
$ret{'backpan-only'} = delete $ret{'backpan'};
return \%ret;
}

1;

0 comments on commit 9a71cba

Please sign in to comment.