Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #679 from metacpan/mickey/favorite_leaderboard_end…
…point

Added /favorite/leaderboard API endpoint
  • Loading branch information
oalders committed Jun 4, 2017
2 parents 6576607 + 0f1c712 commit aca9e3e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/MetaCPAN/Document/Favorite.pm
Expand Up @@ -127,5 +127,33 @@ sub recent {
};
}

sub leaderboard {
my $self = shift;

my $body = {
size => 0,
query => { match_all => {} },
aggregations => {
leaderboard =>
{ terms => { field => 'distribution', size => 600 }, },
},
};

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

my @leaders
= @{ $ret->{aggregations}{leaderboard}{buckets} }[ 0 .. 99 ];

return {
leaderboard => \@leaders,
took => $ret->{took},
total => $ret->{total}
};
}

__PACKAGE__->meta->make_immutable;
1;
7 changes: 7 additions & 0 deletions lib/MetaCPAN/Server/Controller/Favorite.pm
Expand Up @@ -45,5 +45,12 @@ sub recent : Path('recent') : Args(0) {
$c->stash($data);
}

sub leaderboard : Path('leaderboard') : Args(0) {
my ( $self, $c ) = @_;
my $data = $self->model($c)->raw->leaderboard();
$data or return;
$c->stash($data);
}

__PACKAGE__->meta->make_immutable;
1;

0 comments on commit aca9e3e

Please sign in to comment.