lib/App/ElasticSearch/Utilities/VersionHacks.pm

filter: Statementstmt 93.1% 81 / 87 Branchbran 68.4% 26 / 38 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 82.0% 119 / 145 CC 26 SCAR 21.4
CC
26
Cov
83%
_fix_version_request
13.6
_cluster_state_1_0
9.1
_search_params
7.9
CRAP 8.5
« lib/App/ElasticSearch/Utilities/QueryString/Text.pm ↑ Summary script/es-storage-overview.pl »
1 # ABSTRACT: Fix version issues to support all the things
2 package App::ElasticSearch::Utilities::VersionHacks;
3  
4 15 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 15 our $VERSION = '9.1'; # VERSION
9  
10 15 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use CLI::Helpers qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 exports => [ qw(
14 _fix_version_request
15 )],
16 15 };
17  
18 15 const my $MIN_VERSION => 1.0;
19 15 const my %SIMPLE => (
20 '_cluster/nodes' => {
21 default => '_nodes',
22 },
23 '_optimize' => {
24 default => '_forcemerge',
25 },
26 '_status' => {
27 default => '_stats',
28 }
29 );
30 15 my %CALLBACKS = (
31 '_cluster/state' => {
32 default => \&_cluster_state_1_0,
33 },
34 '_search' => {
35 default => \&_search_params,
36 },
37 '_cat/shards' => {
38 default => \&_cat_shards,
39 },
40 );
41  
42 11 ▶sub _fix_version_request {
Subroutine _fix_version_request: called CC=11 SCAR=26.1
43 11 my ($version,$url,$options,$data) = @_;
44 11 ▶ return ($url,$options,$data) unless length $version;
Branch: 1 branch
BranchTrueFalseTotal
unless length $version 0 11 11
45  
46 11 ▶ if (version->parse($version) < $MIN_VERSION) {
Branch: 1 branch
BranchTrueFalseTotal
if ("version"->parse($version) < $MIN_VERSION) 0 11 11
47 0 output({stderr=>1,color=>'red',sticky=>1},
48 "!!! Detected ElasticSearch Version '$version', which is < $MIN_VERSION, please upgrade your cluster !!!");
49 0 exit 1;
50 }
51  
52 11 ▶ if(exists $SIMPLE{$url}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $SIMPLE{$url}) { } 5 6 11
53 5 my $versions = join(", ", sort keys %{ $SIMPLE{$url} });
54 5 debug("Method changed in API, evaluating rewrite ($versions) against $version");
55 5 ▶ if(exists $SIMPLE{$url}->{$version}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $SIMPLE{$url}{$version}) { } 0 5 5
elsif (exists $SIMPLE{$url}{'default'}) { } 5 0 5
56 0 debug({indent=>1,color=>'yellow'}, "+ Rewriting $url to $SIMPLE{$url}->{$version}");
57 0 $url = $SIMPLE{$url}->{$version};
58 }
59 elsif(exists $SIMPLE{$url}->{default}) {
60 5 debug({indent=>1,color=>'yellow'}, "+ Rewriting $url to $SIMPLE{$url}->{default} by default rule");
61 5 $url = $SIMPLE{$url}->{default};
62 }
63 }
64 else {
65 6 my $cb;
66 6 foreach my $check (keys %CALLBACKS) {
67 12 ▶ next unless $url =~ /^\Q$check\E/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ /^\Q$check\E/iu 6 6 12
68 6 $cb = $check;
69 6 last;
70 }
71 6 ▶ if( defined $cb ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cb) 6 0 6
72 6 my $versions = join(", ", sort keys %{ $CALLBACKS{$cb} });
73 6 debug("Method changed in API, evaluating callback for $cb ($versions) against $version");
74 6 ▶ if(exists $CALLBACKS{$url}->{$version}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $CALLBACKS{$url}{$version}) { } 0 6 6
elsif (exists $CALLBACKS{$url}{'default'}) { } 6 0 6
75 0 debug({indent=>1,color=>'yellow'}, "+ Callback dispatched for $url");
76 0 ($url,$options,$data) = $CALLBACKS{$url}->{$version}->($url,$options,$data,$version);
77 }
78 elsif(exists $CALLBACKS{$url}->{default}) {
79 6 debug({indent=>1,color=>'yellow'}, "+ Callback dispatched for $url by default rule");
80 6 ($url,$options,$data) = $CALLBACKS{$url}->{default}->($url,$options,$data,$version);
81 }
82 }
83 }
84  
85 11 return ($url,$options,$data);
86 }
87  
88 15 my %_cluster_state = map { $_ => 1 } qw(
89 nodes
90 routing_table
91 metadata
92 indices
93 blocks
94 version
95 master_node
96 );
97  
98 2 ▶sub _cluster_state_1_0 {
Subroutine _cluster_state_1_0: called CC=9 SCAR=22.1
99 2 my ($url,$options,$data,$version) = @_;
100  
101 2 my @parts = split /\//, $url;
102  
103 # Translate old to new
104 2 ▶ if( @parts < 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts < 3) 2 0 2
105 2 verbose({color=>'yellow'}, "DEPRECATION: Attempting to use legacy API for _cluster/state on ES $version");
106 2 verbose({level=>2,indent=>1}, "See: http://www.elasticsearch.org/guide/en/reference/$version/cluster-state.html#cluster-state");
107 2 my @requested = ();
108 2 ▶ if( exists $options->{uri_param} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"uri_param"}) 1 1 2
109 my %filters =
110 1 map { /filter_(.+)$/; $1 => 1 }
111 1 ▶ grep { /^filter_/ && $options->{uri_param}{$_} }
Condition33%1/3
/^filter_/u && $options->{'uri_param'}{$_}
!ll&&!rl&&r
001
MC/DC0%0/2
/^filter_/u && $options->{'uri_param'}{$_}
/^filter_/u $options->{'uri_param'}{$_}
Truth table33%1/3
A && B
A= /^filter_/uB= $options->{'uri_param'}{$_}
ABresult
0X0
100
111
112 1 keys %{ $options->{uri_param} };
113 # Remove them from the parameters
114 1 delete $options->{uri_param}{"filter_$_"} for keys %filters;
115 1 ▶ if(keys %filters) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %filters) 1 0 1
116 1 foreach my $metric (sort keys %_cluster_state) {
117 7 ▶ push @requested, $metric unless exists $filters{$metric};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $filters{$metric} 6 1 7
118 }
119 }
120 }
121 2 ▶ push @requested, '_all' unless @requested;
Branch: 1 branch
BranchTrueFalseTotal
unless @requested 1 1 2
122 2 push @parts, join(',', @requested);
123 2 my $new_url = join('/',@parts);
124 2 verbose("~ Cluster State rewritten from $url to $new_url");
125 2 $url=$new_url;
126 }
127 2 return ($url,$options,$data);
128 }
129  
130 2 ▶sub _search_params {
Subroutine _search_params: called CC=7 SCAR=20.7
131 2 my ($url,$options,$data,$version) = @_;
132  
133 2 my @invalid = ();
134 2 ▶ push @invalid, "track_total_hits" if qv($version) < qv("6.0.0");
Branch: 1 branch
BranchTrueFalseTotal
if qv($version) < qv("6.0.0") 1 1 2
135 2 ▶ push @invalid, "rest_total_hits_as_int" if qv($version) < qv("7.0.0");
Branch: 1 branch
BranchTrueFalseTotal
if qv($version) < qv("7.0.0") 2 0 2
136  
137 2 ▶ if( @invalid && exists $options->{uri_param} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@invalid and exists $options->{"uri_param"}) 2 0 2
Condition33%1/3
@invalid and exists $options->{"uri_param"}
!ll&&!rl&&r
002
MC/DC0%0/2
@invalid and exists $options->{"uri_param"}
@invalid exists $options->{"uri_param"}
Truth table33%1/3
A and B
A= @invalidB= exists $options->{"uri_param"}
ABresult
0X0
100
111
138 2 foreach my $invalid ( @invalid ) {
139 delete $options->{uri_param}{$invalid}
140 3 ▶ if exists $options->{uri_param}{$invalid};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"uri_param"}{$invalid} 3 0 3
141 }
142 }
143  
144 2 return ($url,$options,$data);
145 }
146  
147 2 ▶sub _cat_shards {
Subroutine _cat_shards: called CC=2 SCAR=6.9
148 2 my ($url,$options,$data,$version) = @_;
149 2 ▶ if ( qv($version) >= qv("7.11.0") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (qv($version) >= qv("7.11.0")) 1 1 2
150 1 delete $options->{uri_param}{local};
151 }
152 2 return ($url,$options,$data);
153 }
154  
155 15 1;
156  
157 __END__
« lib/App/ElasticSearch/Utilities/QueryString/Text.pm ↑ Summary script/es-storage-overview.pl »