App/lcpan/Cmd/scripts_by_mention_count.pm

filter: Statementstmt 43.2% 16 / 37 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 75.0% 3 / 4 Podpod 100% 1 / 1 totaltotal 36.3% 20 / 55 CC 7 SCAR 36.8
CC
7
Cov
33%
handle_cmd
56.0
BEGIN
1.0
BEGIN
1.0
CRAP 39.5
« App/lcpan/Cmd/mods_by_mention_count.pm ↑ Summary App/lcpan/Cmd/script2author.pm »
1 package App::lcpan::Cmd::scripts_by_mention_count;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 require App::lcpan;
8  
9 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10 2 our $DATE = '2023-09-26'; # DATE
11 2 our $DIST = 'App-lcpan'; # DIST
12 2 our $VERSION = '1.074'; # VERSION
13  
14 2 our %SPEC;
15  
16 2 $SPEC{'handle_cmd'} = {
17 v => 1.1,
18 summary => 'List scripts ranked by number of mentions',
19 description => <<'_',
20  
21 This shows the list of most mentioned scripts, that is, scripts who are
22 linked/referred to the most in PODs.
23  
24 By default, each source module/script that mentions a script is counted as one
25 mention (`--count-per content`). Use `--count-per dist` to only count mentions
26 by modules/scripts from the same dist as one mention (so a script only gets a
27 maximum of 1 vote per dist). Use `--count-per author` to only count mentions by
28 modules/scripts from the same author as one mention (so a script only gets a
29 maximum of 1 vote per mentioning author).
30  
31 By default, only mentions from other authors are included. Use
32 `--include-self-mentions` to also include mentions from the same author.
33  
34 _
35 args => {
36 %App::lcpan::common_args,
37 include_self_mentions => {
38 schema => 'bool',
39 default => 0,
40 },
41 count_per => {
42 schema => ['str*', in=>['content', 'dist', 'author']],
43 default => 'content',
44 },
45 n => {
46 summary => 'Return at most this number of results',
47 schema => 'posint*',
48 },
49 },
50 };
51 0 ▶sub handle_cmd {
Subroutine handle_cmd: not called CC=7 SCAR=40.3
Pod documented
52 0 my %args = @_;
53  
54 0 my $state = App::lcpan::_init(\%args, 'ro');
55 0 my $dbh = $state->{dbh};
56  
57 0 ▶ my $count_per = $args{count_per} // 'content';
Condition0%0/2
$args{"count_per"} // "content"
l!l
00
MC/DC0%0/1
$args{"count_per"} // "content"
$args{"count_per"}
Truth table0%0/2
A
A= $args{"count_per"}
Aresult
11
00
58  
59 0 my @where = ("mention.script_name IS NOT NULL");
60  
61 push @where, "targetfile.cpanid <> srcfile.cpanid"
62 0 ▶ unless $args{include_self_mentions};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"include_self_mentions"} 0 0 0
63  
64 0 my $count = '*';
65 0 ▶ if ($count_per eq 'dist') {
Branch: 2 branches
BranchTrueFalseTotal
if ($count_per eq 'dist') { } 0 0 0
elsif ($count_per eq 'author') { } 0 0 0
66 0 $count = 'DISTINCT srcfile.id';
67 } elsif ($count_per eq 'author') {
68 0 $count = 'DISTINCT srcfile.cpanid';
69 }
70  
71 my $sql = "SELECT
72 script.name script,
73 targetfile.cpanid author,
74 COUNT($count) AS mention_count
75 FROM mention
76 LEFT JOIN file srcfile ON mention.source_file_id=srcfile.id
77 LEFT JOIN script ON mention.script_name=script.name
78 LEFT JOIN file targetfile ON script.file_id=targetfile.id
79 WHERE ".join(" AND ", @where)."
80 GROUP BY script.name
81 ORDER BY mention_count DESC
82 0 ▶".($args{n} ? "LIMIT ".(0+$args{n}) : "");
Branch: 1 branch
BranchTrueFalseTotal
$args{'n'} ? : 0 0 0
83  
84 0 my @res;
85 0 my $sth = $dbh->prepare($sql);
86 0 $sth->execute();
87 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
88 0 push @res, $row;
89 }
90  
91 0 require Data::TableData::Rank;
92 0 Data::TableData::Rank::add_rank_column_to_table(table => \@res, data_columns => ['mention_count']);
93  
94 0 my $resmeta = {};
95 0 $resmeta->{'table.fields'} = [qw/rank script author mention_count/];
96 0 [200, "OK", \@res, $resmeta];
97 }
98  
99 2 1;
100 # ABSTRACT: List scripts ranked by number of mentions
101  
102 __END__
« App/lcpan/Cmd/mods_by_mention_count.pm ↑ Summary App/lcpan/Cmd/script2author.pm »