Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added Activity controller + /activity endpoint
This new endpoint will replace the query currently generated and
sent from WEB.
  • Loading branch information
mickeyn committed May 28, 2017
1 parent 7f8212a commit 037fc5a
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
73 changes: 73 additions & 0 deletions lib/MetaCPAN/Document/Release.pm
Expand Up @@ -4,6 +4,7 @@ use strict;
use warnings;

use Moose;
use DateTime qw();
use Ref::Util qw();
use ElasticSearchX::Model::Document;

Expand Down Expand Up @@ -591,5 +592,77 @@ sub requires {
};
}

sub _activity_filters {
my ( $self, $params, $start ) = @_;
my ( $author, $distribution, $module, $f )
= @{$params}{qw( author distribution module f )};

my @filters
= ( { range => { date => { from => $start->epoch . '000' } } } );

push @filters, +{ term => { author => uc($author) } }
if $author;

push @filters, +{ term => { distribution => $distribution } }
if $distribution;

push @filters, +{ term => { 'dependency.module' => $module } }
if $module;

if ( $params->{f} and $params->{f} eq 'n' ) {
push @filters,
(
+{ term => { first => 1 } },
+{ terms => { status => [qw( cpan latest )] } },
);
}

return +{ bool => { must => \@filters } };
}

sub activity {
my ( $self, $params ) = @_;
my $res = $params->{res} // '1w';

my $start
= DateTime->now->truncate( to => 'month' )->subtract( months => 23 );

my $filters = $self->_activity_filters( $params, $start );

my $body = {
query => { match_all => {} },
aggregations => {
histo => {
filter => $filters,
aggregations => {
entries => {
date_histogram =>
{ field => 'date', interval => $res },
}
}
}
},
size => 0,
};

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

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

my $line = [
map {
$data->{ $start->clone->add( months => $_ )->epoch . '000' }
|| 0
} ( 0 .. 23 )
];

return { activity => $line };
}

__PACKAGE__->meta->make_immutable;
1;
20 changes: 20 additions & 0 deletions lib/MetaCPAN/Server/Controller/Activity.pm
@@ -0,0 +1,20 @@
package MetaCPAN::Server::Controller::Activity;

use strict;
use warnings;

use Moose;

BEGIN { extends 'MetaCPAN::Server::Controller' }

with 'MetaCPAN::Server::Role::JSONP';

sub get : Path('') : Args(0) {
my ( $self, $c ) = @_;
my $data = $c->model('CPAN::Release')->raw->activity( $c->req->params );
return unless $data;
$c->stash($data);
}

__PACKAGE__->meta->make_immutable;
1;

0 comments on commit 037fc5a

Please sign in to comment.