Permalink
Browse files

Added /rating/by_distributions API endpoint

This endpoint will replace query sending from WEB
  • Loading branch information...
1 parent c2bd56c commit adb3a6e2dccaa0c5965f17a48d01acfa5fbb715f @mickeyn mickeyn committed Jun 24, 2017
Showing with 64 additions and 0 deletions.
  1. +52 −0 lib/MetaCPAN/Document/Rating.pm
  2. +12 −0 lib/MetaCPAN/Server/Controller/Rating.pm
@@ -51,5 +51,57 @@ sub _build_rating {
return $rating / scalar keys %details;
}
+__PACKAGE__->meta->make_immutable;
+
+package MetaCPAN::Document::Rating::Set;
+
+use strict;
+use warnings;
+
+use Moose;
+
+use MetaCPAN::Util qw( single_valued_arrayref_to_scalar );
+
+extends 'ElasticSearchX::Model::Document::Set';
+
+sub by_distributions {
+ my ( $self, $distributions ) = @_;
+
+ my $body = {
+ size => 0,
+ query => { terms => { distribution => $distributions } },
+ aggregations => {
+ ratings => {
+ terms => {
+ field => 'distribution'
+ },
+ aggregations => {
+ ratings_dist => {
+ stats => {
+ field => 'rating'
+ }
+ }
+ }
+ }
+ }
+ };
+
+ my $ret = $self->es->search(
+ index => $self->index->name,
+ type => 'rating',
+ body => $body,
+ );
+ return unless $ret->{hits}{total};
+
+ my $data = [ map { $_->{key} => $_->{ratings_dist} }
+ @{ $ret->{aggregations}{ratings}{buckets} } ];
+
+ return {
+ releases => $data,
+ total => $ret->{hits}{total},
+ took => $ret->{took}
+ };
+}
+
__PACKAGE__->meta->make_immutable;
1;
@@ -9,4 +9,16 @@ BEGIN { extends 'MetaCPAN::Server::Controller' }
with 'MetaCPAN::Server::Role::JSONP';
+sub by_distributions : Path('by_distributions') : Args(0) {
+ my ( $self, $c ) = @_;
+ my $distributions
+ = $c->req->body_data
+ ? $c->req->body_data->{distribution}
+ : [ $c->req->param('distribution') ];
+ return unless $distributions and @{$distributions};
+ my $data = $self->model($c)->raw->by_distributions($distributions);
+ return unless $data;
+ $c->stash($data);
+}
+
1;

0 comments on commit adb3a6e

Please sign in to comment.