App/lcpan/Cmd/related_authors.pm

filter: Statementstmt 34.4% 20 / 58 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 4 / 5 Podpod 100% 1 / 1 totaltotal 32.0% 25 / 78 CC 9 SCAR 41.4
CC
9
Cov
28%
handle_cmd
90.0
BEGIN
1.0
BEGIN
1.0
CRAP 62.6
« App/lcpan/Cmd/rdeps_scripts.pm ↑ Summary App/lcpan/Cmd/dists_by_dep_count.pm »
1 package App::lcpan::Cmd::related_authors;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 require App::lcpan;
9  
10 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
11 1 our $DATE = '2023-09-26'; # DATE
12 1 our $DIST = 'App-lcpan'; # DIST
13 1 our $VERSION = '1.074'; # VERSION
14  
15 1 our %SPEC;
16  
17 $SPEC{'handle_cmd'} = {
18 v => 1.1,
19 summary => 'List other authors related to author(s)',
20 description => <<'_',
21  
22 This subcommand lists other authors that might be related to the author(s) you
23 specify. This is done in one of the ways below which you can choose.
24  
25 1. (the default) by finding authors whose modules tend to be mentioned together
26 in POD documentation.
27  
28 _
29 args => {
30 %App::lcpan::common_args,
31 %App::lcpan::authors_args,
32 #%App::lcpan::detail_args,
33 limit => {
34 summary => 'Maximum number of authors to return',
35 schema => ['int*', min=>0],
36 default => 20,
37 },
38 with_scores => {
39 summary => 'Return score-related fields',
40 schema => 'bool*',
41 },
42 #with_content_paths => {
43 # summary => 'Return the list of content paths where the authors\' module and the module of a related author are mentioned together',
44 # schema => 'bool*',
45 #},
46 sort => {
47 1 schema => ['array*', of=>['str*', in=>[map {($_,"-$_")} qw/score num_module_mentions num_module_mentions_together pct_module_mentions_together author/]], min_len=>1],
48 default => ['-score', '-num_module_mentions'],
49 },
50 },
51 };
52 0 ▶sub handle_cmd {
Subroutine handle_cmd: not called CC=9 SCAR=45.0
Pod documented
53 0 my %args = @_;
54  
55 0 my $state = App::lcpan::_init(\%args, 'ro');
56 0 my $dbh = $state->{dbh};
57  
58 0 my $authors = $args{authors};
59 0 my $authors_s = join(",", map {$dbh->quote(uc $_)} @$authors);
60  
61 #if ($args{with_content_paths} && @$modules > 1) {
62 # return [412, "Sorry, --with-content-paths currently works with only one specified module"];
63 #}
64  
65 0 my $limit = $args{limit};
66  
67 # authors' modules
68 0 my @modules;
69 0 my $sth_authors_modules = $dbh->prepare(
70 "SELECT name FROM module WHERE file_id IN (SELECT id FROM file WHERE cpanid IN ($authors_s))");
71 0 $sth_authors_modules->execute;
72 0 ▶ while (my @row = $sth_authors_modules->fetchrow_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@row) = $sth_authors_modules->fetchrow_array) 0 0 0
73 0 push @modules, $row[0];
74 }
75 0 log_trace("num_modules released by %s: %d", $authors, scalar(@modules));
76 0 ▶ return [400, "No modules released by author(s)"] unless @modules;
Branch: 1 branch
BranchTrueFalseTotal
unless @modules 0 0 0
77 0 my $modules_s = join(",", map {$dbh->quote($_)} @modules);
78  
79 # number of mentions of target modules
80 0 my ($num_module_mentions) = $dbh->selectrow_array(
81 "SELECT COUNT(*) FROM mention WHERE module_id IN (SELECT id FROM module m2 WHERE name IN ($modules_s))");
82  
83 0 ▶ return [400, "No module mentions for author(s)"] if $num_module_mentions < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $num_module_mentions < 1 0 0 0
84  
85 0 log_debug("num_module_mentions for %s: %d", $authors, $num_module_mentions);
86  
87 0 my @join = (
88 "LEFT JOIN module m2 ON mtn1.module_id=m2.id",
89 "LEFT JOIN file f ON m2.file_id=f.id",
90 );
91  
92 0 my @where = (
93 "mtn1.source_content_id IN (SELECT source_content_id FROM mention mtn2 WHERE module_id IN (SELECT id FROM module m2 WHERE name IN ($modules_s)))",
94 "m2.cpanid NOT IN ($authors_s)",
95 );
96  
97 0 ▶ my @order = map {/(-?)(.+)/; $2 . ($1 ? " DESC" : "")} @{$args{sort}};
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 0 0 0
98  
99 # sql parts, to make SQL statement readable
100 0 my $sp_num_module_mentions = "SELECT COUNT(*) FROM mention mnt3 WHERE module_id=m2.id";
101 0 my $sp_pct_module_mentions_together = "ROUND(100.0 * COUNT(*)/($sp_num_module_mentions), 2)";
102  
103 0 ▶ my $sql = "SELECT
Branch: 1 branch
BranchTrueFalseTotal
@order ? : 0 0 0
104 m2.cpanid author,
105 ($sp_num_module_mentions) num_module_mentions,
106 COUNT(*) num_module_mentions_together,
107 ($sp_pct_module_mentions_together) pct_module_mentions_together,
108 (COUNT(*) * COUNT(*) * ($sp_pct_module_mentions_together)) score
109 FROM mention mtn1
110 ".join("\n", @join)."
111 WHERE ".join(" AND ", @where)."
112 GROUP BY m2.cpanid
113 ".(@order ? "\nORDER BY ".join(", ", @order) : "")."
114 LIMIT $limit
115 ";
116  
117 # my $sql_with_content_paths;
118 # my $sth_with_content_paths;
119 # if ($args{with_content_paths}) {
120 # $sql_with_content_paths = "SELECT
121 # path
122 #FROM content c
123 #WHERE
124 # EXISTS(SELECT id FROM mention WHERE module_id=(SELECT id FROM module WHERE name=?) AND source_content_id=c.id) AND
125 # EXISTS(SELECT id FROM mention WHERE module_id=(SELECT id FROM module WHERE name=?) AND source_content_id=c.id)
126 #";
127 # $sth_with_content_paths = $dbh->prepare($sql_with_content_paths);#
128 # }
129  
130 0 my @res;
131 0 my $sth = $dbh->prepare($sql);
132 0 $sth->execute();
133 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
134 0 ▶ unless ($args{with_scores}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"with_scores"}) 0 0 0
135 0 delete $row->{$_} for qw(num_module_mentions num_module_mentions_together pct_module_mentions_together score);
136 }
137 #if ($args{with_content_paths}) {
138 # my @content_paths;
139 # $sth_with_content_paths->execute($modules->[0], $row->{module});
140 # while (my $row2 = $sth_with_content_paths->fetchrow_arrayref) {
141 # push @content_paths, $row2->[0];
142 # }
143 # $sth_with_content_paths->finish;
144 # $row->{content_paths} = \@content_paths;
145 #}
146 0 push @res, $row;
147 }
148 0 my $resmeta = {};
149 0 $resmeta->{'table.fields'} = [qw/author num_module_mentions num_module_mentions_together pct_module_mentions_together score/];
150  
151 0 [200, "OK", \@res, $resmeta];
152 }
153  
154 1 1;
155 # ABSTRACT: List other authors related to author(s)
156  
157 __END__
« App/lcpan/Cmd/rdeps_scripts.pm ↑ Summary App/lcpan/Cmd/dists_by_dep_count.pm »