-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
121 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use strict; | ||
use warnings; | ||
use v5.36; | ||
|
||
use Cpanel::JSON::XS qw< decode_json >; | ||
use Getopt::Long; | ||
use Path::Tiny qw< path >; | ||
|
||
use MetaCPAN::Logger qw< :log :dlog >; | ||
|
||
use MetaCPAN::ES; | ||
use MetaCPAN::Ingest qw< | ||
handle_error | ||
read_url | ||
>; | ||
|
||
# args | ||
my ( $json_file, $test ); | ||
GetOptions( | ||
"json=s" => \$json_file, | ||
"test" => \$test | ||
); | ||
my $cover_url //= 'http://cpancover.com/latest/cpancover.json'; | ||
my $cover_dev_url //= 'http://cpancover.com/latest/cpancover_dev.json'; | ||
|
||
# setup | ||
my %valid_keys | ||
= map { $_ => 1 } qw< branch condition statement subroutine total >; | ||
|
||
my $es = MetaCPAN::ES->new( index => "cover", type => "cover" ); | ||
my $bulk = $es->bulk(); | ||
|
||
my $data = retrieve_cover_data(); | ||
|
||
log_info {'Updating the cover index'}; | ||
|
||
for my $dist ( sort keys %{$data} ) { | ||
for my $version ( keys %{ $data->{$dist} } ) { | ||
my $release = $dist . '-' . $version; | ||
my $rel_check = $es->search( | ||
index => 'cpan', | ||
type => 'release', | ||
size => 0, | ||
body => { | ||
query => { term => { name => $release } }, | ||
}, | ||
); | ||
if ( $rel_check->{hits}{total} ) { | ||
log_info { "Adding release info for '" . $release . "'" }; | ||
} | ||
else { | ||
log_warn { "Release '" . $release . "' does not exist." }; | ||
next; | ||
} | ||
|
||
my %doc_data = %{ $data->{$dist}{$version}{coverage}{total} }; | ||
|
||
for my $k ( keys %doc_data ) { | ||
delete $doc_data{$k} unless exists $valid_keys{$k}; | ||
} | ||
|
||
$bulk->update( { | ||
id => $release, | ||
doc => { | ||
distribution => $dist, | ||
version => $version, | ||
release => $release, | ||
criteria => \%doc_data, | ||
}, | ||
doc_as_upsert => 1, | ||
} ); | ||
} | ||
} | ||
|
||
$bulk->flush; | ||
|
||
### | ||
|
||
sub retrieve_cover_data { | ||
return decode_json( path($json_file)->slurp ) if $json_file; | ||
|
||
my $url = $test ? $cover_dev_url : $cover_url; | ||
|
||
return decode_json( read_url($url) ); | ||
} | ||
|
||
1; | ||
|
||
__END__ | ||
=pod | ||
=head1 SYNOPSIS | ||
# bin/metacpan cover [--test] [json_file] | ||
=head1 DESCRIPTION | ||
Retrieves the CPAN Cover data from its source and | ||
updates our ES information. | ||
=cut |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters