Skip to content

Commit

Permalink
Added /favorite/leaderboard 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 3, 2017
1 parent 15547fe commit e2b2a42
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 e2b2a42

Please sign in to comment.