lib/App/ElasticSearch/Utilities/Aggregations.pm

filter: Statementstmt 79.5% 109 / 137 Branchbran 42.1% 27 / 64 Conditioncond 46.8% 15 / 32 MC/DCmcdc 10.0% 2 / 20 Subroutinesub 56.2% 9 / 16 Podpod 100% 3 / 3 totaltotal 60.6% 165 / 272 CC 56 SCAR 30.5
CC
56
Cov
65%
es_flatten_aggregations
33.0
__ANON__
33.0
is_single_stat
20.0
CRAP 21.1
« lib/App/ElasticSearch/Utilities/HTTPRequest.pm ↑ Summary lib/App/ElasticSearch/Utilities/Query.pm »
1 package App::ElasticSearch::Utilities::Aggregations;
2 # ABSTRACT: Code to simplify creating and working with Elasticsearch aggregations
3  
4 8 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 ▶use App::ElasticSearch::Utilities qw(es_format_numeric);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 8 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 exports => [ qw(
12 expand_aggregate_string
13 es_flatten_aggregations es_flatten_aggs
14 is_single_stat
15 )],
16 groups => {
17 default => [qw(
18 expand_aggregate_string
19 es_flatten_aggregations es_flatten_aggs
20 is_single_stat
21 )],
22 },
23 8 };
24  
25 8 my %Aggregations;
26  
27  
28 $Aggregations{terms} = {
29 1 ▶ params => sub { $_[0] && $_[0] =~ /^\d+$/ ? { size => $_[0] } : {} },
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
$_[0] && $_[0] =~ /^\d+$/u ? : 1 0 1
Condition33%1/3
$_[0] and $_[0] =~ /^\d+$/u
!ll&&!rl&&r
001
MC/DC0%0/2
$_[0] and $_[0] =~ /^\d+$/u
$_[0] $_[0] =~ /^\d+$/u
Truth table33%1/3
A and B
A= $_[0]B= $_[0] =~ /^\d+$/u
ABresult
0X0
100
111
30 8 type => 'bucket',
31 composite => 1,
32 };
33  
34  
35 $Aggregations{significant_terms} = {
36 0 ▶ params => sub { $_[0] =~ /^\d+$/ ? { size => $_[0] } : {} },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\d+$/u ? : 0 0 0
37 8 type => 'bucket',
38 };
39  
40  
41 $Aggregations{rare_terms} = {
42 0 ▶ params => sub { $_[0] =~ /^\d+$/ ? { max_doc_count => $_[0] } : {} },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\d+$/u ? : 0 0 0
43 8 type => 'bucket',
44 };
45  
46  
47 $Aggregations{histogram} = {
48 0 ▶ params => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
49 0 ▶ return unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 0 0
50 0 ▶ return unless $_[0] > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] > 0 0 0 0
51 0 return { interval => $_[0] };
52 },
53 8 type => 'bucket',
54 composite => 1,
55 };
56  
57  
58 $Aggregations{date_histogram} = {
59 0 ▶ params => sub { { calendar_interval => $_[0] || '1h' } },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_[0] || '1h'
l!l
00
MC/DC0%0/1
$_[0] || '1h'
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
60 8 type => 'bucket',
61 composite => 1,
62 };
63  
64  
65 $Aggregations{geohash_grid} = {
66 0 ▶ params => sub { $_[0] =~ /^\d+$/ ? { precision => $_[0] } : {} },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\d+$/u ? : 0 0 0
67 8 type => 'bucket',
68 composite => 1,
69 };
70  
71  
72 8 $Aggregations{missing} = { type => 'bucket' };
73  
74  
75 8 $Aggregations{avg} = { single_stat => 1, type => 'metric' };
76 8 $Aggregations{max} = { single_stat => 1, type => 'metric' };
77 8 $Aggregations{min} = { single_stat => 1, type => 'metric' };
78 8 $Aggregations{sum} = { single_stat => 1, type => 'metric' };
79  
80  
81 8 $Aggregations{cardinality} = { single_stat => 1, type => 'metric' };
82  
83  
84 8 $Aggregations{stats} = { type => 'metric' };
85  
86  
87 8 $Aggregations{extended_stats} = { type => 'metric' };
88  
89  
90 $Aggregations{percentiles} = {
91 0 ▶ params => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
92 0 ▶ my @pcts = $_[0] ? split /,/, $_[0] : qw(25 50 75 90);
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 0 0
93 0 return { percents => \@pcts };
94 },
95 8 };
96  
97  
98 8 $Aggregations{geo_centroid} = { type => 'metric' };
99  
100  
101  
102  
103 0 ▶sub is_single_stat {
Subroutine is_single_stat: not called CC=4 SCAR=30.0
Pod documented
104 0 my ($agg) = @_;
105 0 ▶ return unless $agg;
Branch: 1 branch
BranchTrueFalseTotal
unless $agg 0 0 0
106 0 ▶ return unless exists $Aggregations{$agg};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Aggregations{$agg} 0 0 0
107 0 ▶ return unless exists $Aggregations{$agg}->{single_stat};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Aggregations{$agg}{"single_stat"} 0 0 0
108 0 return $Aggregations{$agg}->{single_stat};
109 }
110  
111  
112 4 ▶sub expand_aggregate_string {
Subroutine expand_aggregate_string: called CC=16 SCAR=29.9
Pod documented
113 4 my ($token) = @_;
114  
115 4 my %aggs = ();
116 4 foreach my $def ( split /\+/, $token ) {
117 4 ▶ my $alias = $def =~ s/^(\w+)=// ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$def =~ s/^(\w+)=//u ? : 1 3 4
118 4 my @parts = split /:/, $def, 3;
119 4 ▶ if( @parts == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts == 1) 1 3 4
120 1 ▶ $alias ||= $def;
Condition33%1/3
$alias ||= $def
l!l&&r!l&&!r
010
MC/DC0%0/2
$alias ||= $def
$alias $def
Truth table33%1/3
A ||= B
A= $aliasB= $def
ABresult
1X1
011
000
121 1 $aggs{$alias} = { terms => { field => $def, size => 20 } };
122 1 next;
123 }
124 3 my ($agg, $field);
125 3 ▶ if( exists $Aggregations{$parts[0]} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Aggregations{$parts[0]}) { } 0 3 3
126 0 $agg = shift @parts;
127 0 $field = shift @parts;
128 }
129 else {
130 3 $agg = 'terms';
131 3 $field = shift @parts;
132 }
133 3 my $params = {};
134 3 my $paramStr = shift @parts;
135  
136 3 ▶ if( $paramStr && $paramStr =~ /\w+=/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($paramStr and $paramStr =~ /\w+=/u) { } 2 1 3
elsif (exists $Aggregations{$agg}{'params'}) { } 1 0 1
Condition66%2/3
$paramStr and $paramStr =~ /\w+=/u
!ll&&!rl&&r
012
MC/DC50%1/2
$paramStr and $paramStr =~ /\w+=/u
$paramStr $paramStr =~ /\w+=/u
Truth table66%2/3
A and B
A= $paramStrB= $paramStr =~ /\w+=/u
ABresult
0X0
100
111
137 # split on commas using a positive lookahead for a "word="
138 2 foreach my $token (split /,(?=\w+=)/, $paramStr) {
139 2 my ($k,$v) = split /=/, $token, 2;
140 2 ▶ next unless $k and $v;
Branch: 1 branch
BranchTrueFalseTotal
unless $k and $v 0 2 2
Condition33%1/3
$k and $v
!ll&&!rl&&r
002
MC/DC0%0/2
$k and $v
$k $v
Truth table33%1/3
A and B
A= $kB= $v
ABresult
0X0
100
111
141 2 ▶ $params->{$k} = $v =~ /,/ ? [ split /,/, $v ] : $v;
Branch: 1 branch
BranchTrueFalseTotal
$v =~ /,/u ? : 0 2 2
142 }
143 }
144 elsif( exists $Aggregations{$agg}->{params} ) {
145 # Process parameters
146 1 $params = $Aggregations{$agg}->{params}->($paramStr);
147 }
148 3 ▶ $alias ||= join ".", $agg eq 'terms' ? ($field) : ($agg, $field);
Branch: 1 branch
BranchTrueFalseTotal
$agg eq 'terms' ? : 2 0 2
Condition66%2/3
$alias ||= join(".", $agg eq "terms" ? $field : ($agg, $field))
l!l&&r!l&&!r
120
MC/DC0%0/2
$alias ||= join(".", $agg eq "terms" ? $field : ($agg, $field))
$alias join(".", $agg eq "terms" ? $field : ($agg, $field))
Truth table66%2/3
A ||= B
A= $aliasB= join(".", $agg eq "terms" ? $field : ($agg, $field))
ABresult
1X1
011
000
149 3 ▶ $aggs{$alias} = { $agg => { field => $field, %{ $params || {} } } };
Condition50%1/2
$params || {}
l!l
30
MC/DC0%0/1
$params || {}
$params
Truth table50%1/2
A
A= $params
Aresult
11
00
150 }
151 4 return \%aggs;
152 }
153  
154  
155 7 ▶sub es_flatten_aggregations {
Subroutine es_flatten_aggregations: called CC=21 SCAR=35.0
Pod documented
156 7 my ($result,$field,$parent) = @_;
157  
158 7 ▶ $parent ||= [];
Condition100%2/2
$parent ||= []
l!l
43
MC/DC100%1/1
$parent ||= []
$parent
Truth table100%2/2
A
A= $parent
Aresult
11
00
159 7 my @rows = ();
160  
161 7 my @remove = qw(
162 doc_count_error_upper_bound
163 sum_other_doc_count
164 );
165  
166 7 my $row = dclone($parent);
167 4 ▶ my $extract = sub {
Subroutine __ANON__: called CC=9 SCAR=35.0
168 4 my ($key, $hash) = @_;
169  
170 4 ▶ if( defined $hash->{value_as_string} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $hash->{'value_as_string'}) { } 0 4 4
elsif (defined $hash->{'value'}) { } 0 4 4
elsif ($hash->{'values'}) { } 0 4 4
171 0 push @{ $row }, $key, $hash->{value_as_string};
172 }
173 elsif( defined $hash->{value} ) {
174 0 push @{ $row }, $key, es_format_numeric($hash->{value});
175 }
176 elsif( $hash->{values} ) {
177 0 foreach my $k ( sort keys %{ $hash->{values} } ) {
178 0 push @{ $row }, "$key.$k", $hash->{values}{$k}
179 0 ▶ if $hash->{values}{$k};
Branch: 1 branch
BranchTrueFalseTotal
if $hash->{"values"}{$k} 0 0 0
180 }
181 }
182 else {
183 4 foreach my $k (sort keys %{ $hash }) {
184 4 ▶ last if $k eq 'buckets';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "buckets" 4 0 4
185 0 push @{ $row }, "$key.$k", $hash->{$k}
186 0 ▶ if defined $hash->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if defined $hash->{$k} 0 0 0
187 }
188 }
189 7 };
190  
191 7 ▶ if( $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field) { } 4 3 7
192 4 delete $result->{$_} for @remove;
193 4 ▶ if( $result->{key} and exists $result->{doc_count} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"key"} and exists $result->{"doc_count"}) 4 0 4
Condition33%1/3
$result->{"key"} and exists $result->{"doc_count"}
!ll&&!rl&&r
004
MC/DC0%0/2
$result->{"key"} and exists $result->{"doc_count"}
$result->{"key"} exists $result->{"doc_count"}
Truth table33%1/3
A and B
A= $result->{"key"}B= exists $result->{"doc_count"}
ABresult
0X0
100
111
194 4 my $k = delete $result->{key};
195 4 my $ks = delete $result->{key_as_string};
196 4 ▶ push @{ $row }, $field, $ks || $k;
Condition66%2/3
$ks || $k
l!l&&r!l&&!r
130
MC/DC0%0/2
$ks || $k
$ks $k
Truth table66%2/3
A || B
A= $ksB= $k
ABresult
1X1
011
000
197 4 ▶ push @{ $row }, "$field.raw", $k if $ks;
Branch: 1 branch
BranchTrueFalseTotal
if $ks 1 3 4
198 4 ▶ push @{ $row }, "$field.hits", delete $result->{doc_count} || 0;
Condition50%1/2
delete $result->{'doc_count'} || 0
l!l
40
MC/DC0%0/1
delete $result->{'doc_count'} || 0
delete $result->{'doc_count'}
Truth table50%1/2
A
A= delete $result->{'doc_count'}
Aresult
11
00
199 }
200 4 my %buckets = ();
201 4 foreach my $k ( sort keys %{ $result } ) {
202 1 ▶ if( ref $result->{$k} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result->{$k} eq "HASH") 1 0 1
203 1 $extract->($k, $result->{$k});
204  
205 1 ▶ if( my $buckets = delete $result->{$k}{buckets} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $buckets = delete $result->{$k}{"buckets"}) 1 0 1
206 1 $buckets{$k} = $buckets;
207 }
208 }
209 }
210 4 ▶ if( keys %buckets ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %buckets) { } 1 3 4
211 1 foreach my $k ( sort keys %buckets ) {
212 1 ▶ if( @{ $buckets{$k} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$buckets{$k};}) { } 1 0 1
213 1 foreach my $bucket ( @{ $buckets{$k} } ) {
214 1 push @rows, @{ es_flatten_aggregations($bucket, $k, $row) };
215 }
216 }
217 else {
218 0 push @rows, $row;
219 }
220 }
221 }
222 else {
223 3 push @rows, $row;
224 }
225 }
226 else {
227 3 foreach my $k ( sort keys %{ $result } ) {
228 3 delete $result->{$k}{$_} for @remove;
229 3 $extract->($k, $result->{$k});
230 3 my $buckets = delete $result->{$k}{buckets};
231 3 ▶ if( $buckets and @{ $buckets } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buckets and @{$buckets;}) { } 3 0 3
Condition33%1/3
$buckets and @{$buckets;}
!ll&&!rl&&r
003
MC/DC0%0/2
$buckets and @{$buckets;}
$buckets @{$buckets;}
Truth table33%1/3
A and B
A= $bucketsB= @{$buckets;}
ABresult
0X0
100
111
232 3 foreach my $bucket ( @{ $buckets } ) {
233 3 push @rows, @{ es_flatten_aggregations($bucket,$k,$row) };
234 }
235 }
236 else {
237 0 push @rows, $row;
238 }
239 }
240 }
241  
242 7 return \@rows;
243 }
244  
245 # Setup Aliases
246 8 *es_flatten_aggs = \&es_flatten_aggregations;
247  
248  
249 8 1;
250  
251 __END__
« lib/App/ElasticSearch/Utilities/HTTPRequest.pm ↑ Summary lib/App/ElasticSearch/Utilities/Query.pm »