lib/App/ElasticSearch/Utilities/QueryString/FileExpansion.pm

filter: Statementstmt 74.4% 73 / 98 Branchbran 44.4% 16 / 36 Conditioncond 52.6% 10 / 19 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 92.8% 13 / 14 Podpod 0.0% 0 / 1 totaltotal 64.6% 115 / 178 CC 30 SCAR 34.5
CC
30
Cov
65%
_parse_json
110.0
handle_token
14.4
_parse_txt
6.6
CRAP 31.5
« lib/App/ElasticSearch/Utilities/Metrics.pm ↑ Summary lib/App/ElasticSearch/Utilities/HTTPRequest.pm »
1 package App::ElasticSearch::Utilities::QueryString::FileExpansion;
2 # ABSTRACT: Build a terms query from unique values in a column of a file
3  
4 2 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '9.1'; # VERSION
8  
9 2 ▶use CLI::Helpers qw(:output);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Slurp::Tiny qw(read_lines);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Ref::Util qw(is_ref is_arrayref is_hashref);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Text::CSV_XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 with 'App::ElasticSearch::Utilities::QueryString::Plugin';
18  
19 1 ▶sub _build_priority { 10; }
Subroutine _build_priority: called CC=1 SCAR=0.0
20  
21 2 my %parsers = (
22 txt => \&_parse_txt,
23 dat => \&_parse_txt,
24 csv => \&_parse_csv,
25 json => \&_parse_json,
26 );
27  
28  
29 9 ▶sub handle_token {
Subroutine handle_token: called CC=13 SCAR=26.6
Pod undocumented
30 9 my($self,$token) = @_;
31  
32 my $makeMatcher = sub {
33 my ($matcher,$field,$patterns) = @_;
34 my @tests;
35 foreach my $pattern (@{ $patterns }) {
36 push @tests, { $matcher => { $field => { value => $pattern } } };
37 }
38 return {
39 bool => {
40 should => \@tests,
41 minimum_should_match => 1,
42 }
43 }
44 9 };
45 my %make = (
46 terms => sub {
47 my ($field, $uniq) = @_;
48 return { terms => { $field => $uniq } };
49 },
50 regexp => sub { $makeMatcher->(regexp => @_) },
51 wildcard => sub { $makeMatcher->(wildcard => @_) },
52 9 );
53 9 ▶ if( my ($term,$match) = split /\:/, $token, 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = split(/\:/u, $token, 2)) 9 0 9
54 9 ▶ if( defined $match && $match =~ /(.*\.(\w{3,4}))(?:\[([^\]]+)\])?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $match and $match =~ /(.*\.(\w{3,4}))(?:\[([^\]]+)\])?$/u) 3 6 9
Condition66%2/3
defined $match and $match =~ /(.*\.(\w{3,4}))(?:\[([^\]]+)\])?$/u
!ll&&!rl&&r
063
MC/DC50%1/2
defined $match and $match =~ /(.*\.(\w{3,4}))(?:\[([^\]]+)\])?$/u
defined $match $match =~ /(.*\.(\w{3,4}))(?:\[([^\]]+)\])?$/u
Truth table66%2/3
A and B
A= defined $matchB= $match =~ /(.*\.(\w{3,4}))(?:\[([^\]]+)\])?$/u
ABresult
0X0
100
111
55 3 my($file,$type,$col) = ($1,$2,$3);
56 # Support Wildcards
57 3 ▶ my $matcher = $file =~ s/^\~// ? 'regexp'
Branch: 2 branches
BranchTrueFalseTotal
$file =~ s/^\~//u ? : 0 3 3
$file =~ s/^\*//u ? : 0 3 3
58 : $file =~ s/^\*// ? 'wildcard'
59 : 'terms';
60 3 ▶ $col //= -1;
Condition100%2/2
$col //= -1
l!l
21
MC/DC100%1/1
$col //= -1
$col
Truth table100%2/2
A
A= $col
Aresult
11
00
61 3 $type = lc $type;
62 3 ▶ verbose({level=>2,color=>'magenta'}, sprintf "# %s attempt of %s type, %s[%s] %s",
Branch: 1 branch
BranchTrueFalseTotal
-f $file ? : 2 1 3
63 $self->name, $type, $file, $col, -f $file ? 'exists' : 'does not exist'
64 );
65 3 ▶ if( exists $parsers{$type} && -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $parsers{$type} and -f $file) 2 1 3
Condition66%2/3
exists $parsers{$type} and -f $file
!ll&&!rl&&r
102
MC/DC50%1/2
exists $parsers{$type} and -f $file
exists $parsers{$type} -f $file
Truth table66%2/3
A and B
A= exists $parsers{$type}B= -f $file
ABresult
0X0
100
111
66 2 my $uniq = $parsers{$type}->($file,$col);
67 2 ▶ if (defined $uniq && is_hashref($uniq) && scalar(keys %$uniq)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uniq and is_hashref($uniq) and scalar keys %$uniq) 2 0 2
Condition40%2/5
defined $uniq and is_hashref($uniq) and scalar keys %$uniq
!ll
02
defined $uniq and is_hashref($uniq)
!ll&&!rl&&r
002
MC/DC0%0/2
defined $uniq and is_hashref($uniq) and scalar keys %$uniq
defined $uniq is_hashref($uniq)
Truth table33%1/3
A and B
A= defined $uniqB= is_hashref($uniq)
ABresult
0X0
100
111
68 2 verbose({color=>'cyan'},
69 sprintf "# FILE:%s[%s] contained %d unique elements.",
70 $file,
71 $col,
72 scalar(keys %$uniq),
73 );
74 2 my $qs = [ sort keys %{ $uniq } ];
75 2 return [{condition => $make{$matcher}->($term,$qs) }];
76 }
77 }
78 }
79 }
80 7 return;
81 }
82  
83 1 ▶sub _parse_csv {
Subroutine _parse_csv: called CC=4 SCAR=13.9
84 1 my ($file,$col) = @_;
85 1 my $csv = Text::CSV_XS->new({binary=>1,empty_is_undef=>1});
86 1 ▶ open my $fh, "<:encoding(utf8)", $file or die "Unable to read $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(utf8)", $file or die "Unable to read ${file}: $!" 1 0 1
87 1 my %uniq = ();
88 1 ▶ while( my $row = $csv->getline($fh) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $csv->getline($fh)) 3 1 4
89 3 my $val;
90 3 eval {
91 3 $val = $row->[$col];
92 };
93 3 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 3 3
94 3 $uniq{$val} = 1;
95 }
96 1 return \%uniq;
97 }
98  
99 1 ▶sub _parse_txt {
Subroutine _parse_txt: called CC=6 SCAR=18.9
100 1 my ($file,$col) = @_;
101 1 my %uniq=();
102 1 ▶ my @rows = grep { defined && length && !/^#/ } read_lines($file);
Condition33%2/6
defined $_ && length $_ && !/^#/u
!ll&&!rl&&r
003
defined $_ and length $_
!ll&&!rl&&r
003
MC/DC0%0/3
defined $_ && length $_ && !/^#/u
defined $_ length $_ !/^#/u
Truth table25%1/4
A and B && not(C)
A= defined $_B= length $_C= !/^#/u
ABCresult
0XX0
10X0
1100
1111
103 1 debug({color=>'magenta'}, @rows);
104 1 ▶ if(@rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rows) 1 0 1
105 1 for(@rows) {
106 3 chomp;
107 # Split on tabs or nulls
108 3 my @cols = split /[\t\0]/;
109 3 my $value = $cols[$col];
110 3 ▶ if(defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 3 0 3
111 3 $uniq{$value} = 1;
112 }
113 }
114 }
115 1 return \%uniq;
116 }
117  
118 0 ▶sub _parse_json {
Subroutine _parse_json: not called CC=10 SCAR=47.0
119 0 my ($file,$field) = @_;
120  
121 0 ▶ die "For new line delimited JSON, please specify the key, ie <field>:$file\[key.path.i.want\]"
Branch: 1 branch
BranchTrueFalseTotal
if $field eq -1 0 0 0
122 if $field eq "-1";
123  
124 0 my %uniq = ();
125 0 my $line = 0;
126 0 my @path = split /\./, $field; # Supports key.subkey.subsubkey format
127 0 JSON_LINE: foreach my $json ( read_lines($file) ) {
128 0 $line++;
129 0 my $data;
130 eval {
131 0 $data = decode_json($json);
132 0 1;
133 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $data = &decode_json($json); 1 } } or do { my $err = $@; output({"stderr", 1, "color", "yellow"}, sprintf("Invalid JSON in %s, line %d: %s", $file, $line, $err)); verbose({"stderr", 1, "color", "magenta", "indent", 1}, $json); next } 0 0 0
134 0 my $err = $@;
135 0 output({stderr=>1,color=>'yellow'}, sprintf "Invalid JSON in %s, line %d: %s",
136 $file,
137 $line,
138 $err,
139 );
140 0 verbose({stderr=>1,color=>'magenta',indent=>1}, $json);
141 0 next;
142 };
143 # Walk the path
144 0 foreach my $k (@path) {
145 0 ▶ next JSON_LINE unless exists $data->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{$k} 0 0 0
146 0 $data = $data->{$k};
147 }
148 # At this point $data should contain our values
149 0 ▶ if( is_arrayref($data) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_arrayref($data)) { } 0 0 0
elsif (not Ref::Util::XS::is_ref($data)) { } 0 0 0
150 0 $uniq{$_} = 1 for grep { !is_ref($_) } @{ $data };
151 }
152 elsif( !is_ref($data) ) {
153 0 $uniq{$data} = 1;
154 }
155 }
156  
157 0 ▶ die "Expected newline-delimited JSON in $file, but it was empty or didn't contain '$field'"
Branch: 1 branch
BranchTrueFalseTotal
unless keys %uniq 0 0 0
158 unless keys %uniq;
159  
160 0 return \%uniq;
161 }
162  
163 2 1;
164  
165 __END__
« lib/App/ElasticSearch/Utilities/Metrics.pm ↑ Summary lib/App/ElasticSearch/Utilities/HTTPRequest.pm »