Skip to content

Commit 2015015

Browse files
committed
Merge pull request #462 from CPAN-API/import_river_data
Added script which fetches and indexes river data
2 parents 3e38047 + 873522e commit 2015015

File tree

8 files changed

+172
-3
lines changed

8 files changed

+172
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/var
1010
/t/var/tmp/
1111
/t/var/darkpan/
12+
/t/var/log/
1213
/etc/metacpan_local.pl
1314
metacpan_server_local.conf
1415

lib/MetaCPAN/Document/Distribution.pm

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use namespace::autoclean;
77
use Moose;
88
use ElasticSearchX::Model::Document;
99

10-
use MetaCPAN::Types qw( ArrayRef BugSummary );
10+
use MetaCPAN::Types qw( ArrayRef BugSummary RiverSummary);
1111

1212
has name => (
1313
is => 'ro',
@@ -22,6 +22,13 @@ has bugs => (
2222
writer => '_set_bugs',
2323
);
2424

25+
has river => (
26+
is => 'ro',
27+
isa => RiverSummary,
28+
dynamic => 1,
29+
writer => '_set_river',
30+
);
31+
2532
sub releases {
2633
my $self = shift;
2734
return $self->index->type("release")

lib/MetaCPAN/Role/Logger.pm

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ sub set_logger_once {
4949
# XXX This doesn't belong here.
5050
sub _build_logger {
5151
my ($config) = @_;
52-
my $log = Log::Log4perl->get_logger( $ARGV[0] );
52+
my $log = Log::Log4perl->get_logger( $ARGV[0]
53+
|| 'this_would_have_been_argv_0_but_there_is_no_such_thing' );
5354
foreach my $c (@$config) {
5455
my $layout = Log::Log4perl::Layout::PatternLayout->new( $c->{layout}
5556
|| qq{%d %p{1} %c: %m{chomp}%n} );

lib/MetaCPAN/Role/Script.pm

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use Log::Contextual qw( :log :dlog );
99
use MetaCPAN::Model;
1010
use MetaCPAN::Types qw(:all);
1111
use Moose::Role;
12+
use Carp ();
1213

1314
has 'cpan' => (
1415
is => 'ro',
@@ -74,7 +75,7 @@ sub handle_error {
7475
log_fatal {$error};
7576

7677
# Die if configured (for the test suite).
77-
die $error if $self->die_on_error;
78+
Carp::croak $error if $self->die_on_error;
7879
}
7980

8081
sub index {

lib/MetaCPAN/Script/River.pm

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package MetaCPAN::Script::River;
2+
3+
use Moose;
4+
use namespace::autoclean;
5+
6+
use JSON::MaybeXS qw( decode_json );
7+
use Log::Contextual qw( :log :dlog );
8+
use LWP::UserAgent;
9+
use MetaCPAN::Types qw( ArrayRef Str Uri);
10+
11+
with 'MetaCPAN::Role::Script', 'MooseX::Getopt';
12+
13+
has river_url => (
14+
is => 'ro',
15+
isa => Uri,
16+
coerce => 1,
17+
required => 1,
18+
default => 'https://neilb.org/FIXME',
19+
);
20+
21+
has _ua => (
22+
is => 'ro',
23+
isa => 'LWP::UserAgent',
24+
default => sub { LWP::UserAgent->new },
25+
);
26+
27+
sub run {
28+
my $self = shift;
29+
my $summaries = $self->retrieve_river_summaries;
30+
$self->index_river_summaries($summaries);
31+
32+
return 1;
33+
}
34+
35+
sub index_river_summaries {
36+
my ( $self, $summaries ) = @_;
37+
$self->index->refresh;
38+
my $dists = $self->index->type('distribution');
39+
my $bulk = $self->index->bulk( size => 300 );
40+
for my $summary (@$summaries) {
41+
my $dist = delete $summary->{dist};
42+
my $doc = $dists->get($dist);
43+
$doc ||= $dists->new_document( { name => $dist } );
44+
$doc->_set_river($summary);
45+
$bulk->put($doc);
46+
}
47+
$bulk->commit;
48+
}
49+
50+
sub retrieve_river_summaries {
51+
my $self = shift;
52+
my $resp = $self->_ua->get( $self->river_url );
53+
54+
$self->handle_error( $resp->status_line ) unless $resp->is_success;
55+
56+
return decode_json $resp->content;
57+
}
58+
59+
__PACKAGE__->meta->make_immutable;
60+
61+
1;
62+
63+
=pod
64+
65+
=head1 SYNOPSIS
66+
67+
# bin/metacpan river
68+
69+
=head1 DESCRIPTION
70+
71+
Retrieves the CPAN river data from its source and
72+
updates our ES information.
73+
74+
This can then be accessed here:
75+
76+
http://api.metacpan.org/distribution/Moose
77+
http://api.metacpan.org/distribution/HTTP-BrowserDetect
78+
79+
=cut
80+

lib/MetaCPAN/Types/Internal.pm

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use MooseX::Types -declare => [
2626
PerlMongers
2727
Tests
2828
BugSummary
29+
RiverSummary
2930
)
3031
];
3132

@@ -104,6 +105,9 @@ subtype BugSummary,
104105
source => Str
105106
];
106107

108+
subtype RiverSummary,
109+
as Dict [ ( map { $_ => Optional [Int] } qw(total immediate bucket) ), ];
110+
107111
subtype Resources,
108112
as Dict [
109113
license => Optional [ ArrayRef [Str] ],

t/script/river.t

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use strict;
2+
use warnings;
3+
4+
use lib 't/lib';
5+
6+
use Git::Helpers qw( checkout_root );
7+
use MetaCPAN::Script::River;
8+
use MetaCPAN::Script::Runner;
9+
use MetaCPAN::Server::Test;
10+
use MetaCPAN::TestHelpers;
11+
use Test::More;
12+
use URI;
13+
14+
my $config = MetaCPAN::Script::Runner::build_config;
15+
16+
# local json file with structure from https://github.com/CPAN-API/cpan-api/issues/460
17+
my $root = checkout_root();
18+
my $file = URI->new('t/var/river.json')->abs("file://$root/");
19+
$config->{'river_url'} = "$file";
20+
21+
my $river = MetaCPAN::Script::River->new_with_options($config);
22+
ok $river->run, 'runs and returns true';
23+
24+
my %expect = (
25+
'System-Command' => {
26+
total => 92,
27+
immediate => 4,
28+
bucket => 2,
29+
},
30+
'Text-Markdown' => {
31+
total => 92,
32+
immediate => 56,
33+
bucket => 2,
34+
}
35+
);
36+
37+
test_psgi app, sub {
38+
my $cb = shift;
39+
for my $dist ( keys %expect ) {
40+
my $test = $expect{$dist};
41+
subtest "Check $dist" => sub {
42+
my $url = "/distribution/$dist";
43+
ok( my $res = $cb->( GET $url ), "GET $url" );
44+
45+
# TRAVIS 5.18
46+
is( $res->code, 200, "code 200" );
47+
is(
48+
$res->header('content-type'),
49+
'application/json; charset=utf-8',
50+
'Content-type'
51+
);
52+
my $json = decode_json_ok($res);
53+
54+
# TRAVIS 5.18
55+
is_deeply( $json->{river}, $test,
56+
"$dist river summary roundtrip" );
57+
};
58+
}
59+
};
60+
61+
done_testing();

t/var/river.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"dist": "System-Command",
4+
"total": 92,
5+
"immediate": 4,
6+
"bucket": 2
7+
},
8+
{
9+
"dist": "Text-Markdown",
10+
"total": 92,
11+
"immediate": 56,
12+
"bucket": 2
13+
}
14+
]

0 commit comments

Comments
 (0)