« 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
| |||||||||||||||||||||||||||||||||||
| 54 | 9 | ▶ | if( defined $match && $match =~ /(.*\.(\w{3,4}))(?:\[([^\]]+)\])?$/) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $match and $match =~ /(.*\.(\w{3,4}))(?:\[([^\]]+)\])?$/u
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
| |||||||||||||||||||||||||||||||||||
| 55 | 3 | my($file,$type,$col) = ($1,$2,$3); | |||||||||||||||||||||||||||||||||
| 56 | # Support Wildcards | ||||||||||||||||||||||||||||||||||
| 57 | 3 | ▶ | my $matcher = $file =~ s/^\~// ? 'regexp' | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 58 | : $file =~ s/^\*// ? 'wildcard' | ||||||||||||||||||||||||||||||||||
| 59 | : 'terms'; | ||||||||||||||||||||||||||||||||||
| 60 | 3 | ▶ | $col //= -1; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$col //= -1
MC/DC100%1/1
$col //= -1
$col
Truth table100%2/2
A
A= $col
| |||||||||||||||||||||||||||||||||||
| 61 | 3 | $type = lc $type; | |||||||||||||||||||||||||||||||||
| 62 | 3 | ▶ | verbose({level=>2,color=>'magenta'}, sprintf "# %s attempt of %s type, %s[%s] %s", | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 63 | $self->name, $type, $file, $col, -f $file ? 'exists' : 'does not exist' | ||||||||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||||||||
| 65 | 3 | ▶ | if( exists $parsers{$type} && -f $file ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $parsers{$type} and -f $file
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
| |||||||||||||||||||||||||||||||||||
| 66 | 2 | my $uniq = $parsers{$type}->($file,$col); | |||||||||||||||||||||||||||||||||
| 67 | 2 | ▶ | if (defined $uniq && is_hashref($uniq) && scalar(keys %$uniq)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
defined $uniq and is_hashref($uniq) and scalar keys %$uniq
defined $uniq and is_hashref($uniq)
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)
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 87 | 1 | my %uniq = (); | |||||||||||||||||||||||||||||||||
| 88 | 1 | ▶ | while( my $row = $csv->getline($fh) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 89 | 3 | my $val; | |||||||||||||||||||||||||||||||||
| 90 | 3 | eval { | |||||||||||||||||||||||||||||||||
| 91 | 3 | $val = $row->[$col]; | |||||||||||||||||||||||||||||||||
| 92 | }; | ||||||||||||||||||||||||||||||||||
| 93 | 3 | ▶ | next unless defined $val; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
defined $_ and length $_
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
| |||||||||||||||||||||||||||||||||||
| 103 | 1 | debug({color=>'magenta'}, @rows); | |||||||||||||||||||||||||||||||||
| 104 | 1 | ▶ | if(@rows) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 146 | 0 | $data = $data->{$k}; | |||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||
| 148 | # At this point $data should contain our values | ||||||||||||||||||||||||||||||||||
| 149 | 0 | ▶ | if( is_arrayref($data) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 158 | unless keys %uniq; | ||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||
| 160 | 0 | return \%uniq; | |||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||
| 163 | 2 | 1; | |||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||
| 165 | __END__ | ||||||||||||||||||||||||||||||||||