Skip to content

Commit

Permalink
Added /release/top_uploaders API endpoint
Browse files Browse the repository at this point in the history
This new endpoint will be used to replace the query sent from
WEB.
  • Loading branch information
mickeyn committed Jun 5, 2017
1 parent 7609d9b commit a4c67d3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
45 changes: 45 additions & 0 deletions lib/MetaCPAN/Document/Release.pm
Expand Up @@ -750,5 +750,50 @@ sub versions {
return { releases => $data };
}

sub top_uploaders {
my ( $self, $range ) = @_;
my $range_filter = {
range => {
date => {
from => $range eq 'all' ? 0 : DateTime->now->subtract(
$range eq 'weekly' ? 'weeks'
: $range eq 'monthly' ? 'months'
: $range eq 'yearly' ? 'years'
: 'weeks' => 1
)->truncate( to => 'day' )->iso8601
},
}
};

my $body = {
query => { match_all => {} },
aggregations => {
author => {
aggregations => {
entries => {
terms => { field => 'author', size => 50 }
}
},
filter => $range_filter,
},
},
size => 0,
};

my $ret = $self->es->search(
index => $self->index->name,
type => 'release',
body => $body,
);

my $counts = { map { $_->{key} => $_->{doc_count} }
@{ $ret->{aggregations}{author}{entries}{buckets} } };

return {
counts => $counts,
took => $ret->{took}
};
}

__PACKAGE__->meta->make_immutable;
1;
8 changes: 8 additions & 0 deletions lib/MetaCPAN/Server/Controller/Release.pm
Expand Up @@ -97,5 +97,13 @@ sub versions : Path('versions') : Args(1) {
$c->stash($data);
}

sub top_uploaders : Path('top_uploaders') : Args() {
my ( $self, $c ) = @_;
my $range = $c->req->param('range') || 'weekly';
my $data = $self->model($c)->raw->top_uploaders($range);
return unless $data;
$c->stash($data);
}

__PACKAGE__->meta->make_immutable;
1;

0 comments on commit a4c67d3

Please sign in to comment.