| 1 | # PODNAME: App::Prove::Elasticsearch::Utils | ||||||||||||
| 2 | # ABSTRACT: common functions used by multiple modules in the distribution. | ||||||||||||
| 3 | |||||||||||||
| 4 | package App::Prove::Elasticsearch::Utils; | ||||||||||||
| 5 | 30 | $App::Prove::Elasticsearch::Utils::VERSION = '0.001'; | |||||||||||
| 6 | 30 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 30 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 30 | ▶ | use Config::Simple(); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 30 | ▶ | use File::HomeDir(); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 3 | ▶ | sub process_configuration { | ||||||||||
|
Subroutine
process_configuration: called CC=8 SCAR=22.1
Pod
documented
| |||||||||||||
| 13 | 3 | my $args = shift; | |||||||||||
| 14 | 3 | my $conf = {}; | |||||||||||
| 15 | |||||||||||||
| 16 | 3 | ▶ | my $homedir = File::HomeDir::my_home() || '.'; | ||||||||||
|
Condition50%1/2
File::HomeDir::my_home() || '.'
MC/DC0%0/1
File::HomeDir::my_home() || '.'
File::HomeDir::my_home()
Truth table50%1/2
A
A= File::HomeDir::my_home()
| |||||||||||||
| 17 | 3 | ▶ | if (-e $homedir) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 18 | 2 | ▶ | unless (Config::Simple->import_from("$homedir/elastest.conf", $conf)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 19 | 0 | ▶ | warn Config::Simple->error() if -e "$homedir/elastest.conf"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 20 | } | ||||||||||||
| 21 | } | ||||||||||||
| 22 | |||||||||||||
| 23 | 3 | my @kvp = (); | |||||||||||
| 24 | 3 | my ($key, $value); | |||||||||||
| 25 | 3 | foreach my $arg (@$args) { | |||||||||||
| 26 | 4 | @kvp = split(/=/, $arg); | |||||||||||
| 27 | 4 | ▶ | if (scalar(@kvp) < 2) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 28 | 0 | ||||||||||||
| 29 | "Unrecognized Argument '$arg' to App::Prove::Plugin::Elasticsearch, ignoring\n"; | ||||||||||||
| 30 | 0 | next; | |||||||||||
| 31 | } | ||||||||||||
| 32 | 4 | $key = shift @kvp; | |||||||||||
| 33 | 4 | $value = join('', @kvp); | |||||||||||
| 34 | 4 | $conf->{$key} = $value; | |||||||||||
| 35 | } | ||||||||||||
| 36 | |||||||||||||
| 37 | #Set ENV for use by harness | ||||||||||||
| 38 | 3 | foreach my $key (keys(%$conf)) { | |||||||||||
| 39 | 6 | my $km = uc($key); | |||||||||||
| 40 | 6 | $km =~ s/\./_/g; | |||||||||||
| 41 | 6 | $ENV{$km} = $conf->{$key}; | |||||||||||
| 42 | } | ||||||||||||
| 43 | |||||||||||||
| 44 | 3 | return $conf; | |||||||||||
| 45 | } | ||||||||||||
| 46 | |||||||||||||
| 47 | 3 | ▶ | sub require_indexer { | ||||||||||
|
Subroutine
require_indexer: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 48 | 3 | my $conf = shift; | |||||||||||
| 49 | my $index_suffix = | ||||||||||||
| 50 | 3 | ▶ | $conf->{'client.indexer'} ? "::" . $conf->{'client.indexer'} : ''; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | 3 | my $indexer = "App::Prove::Elasticsearch::Indexer$index_suffix"; | |||||||||||
| 52 | |||||||||||||
| 53 | 3 | eval "require $indexer"; | |||||||||||
| 54 | 3 | ▶ | die $@ if $@; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 55 | |||||||||||||
| 56 | #Set ENV for use by harness | ||||||||||||
| 57 | 2 | $ENV{CLIENT_INDEXER} = $indexer; | |||||||||||
| 58 | 2 | return $indexer; | |||||||||||
| 59 | } | ||||||||||||
| 60 | |||||||||||||
| 61 | 1 | ▶ | sub require_searcher { | ||||||||||
|
Subroutine
require_searcher: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 62 | 1 | my $conf = shift; | |||||||||||
| 63 | 1 | return _require_generic( | |||||||||||
| 64 | $conf, | ||||||||||||
| 65 | 'App::Prove::Elasticsearch::Searcher', | ||||||||||||
| 66 | 'client.autodiscover', | ||||||||||||
| 67 | 'CLIENT_AUTODISCOVER' | ||||||||||||
| 68 | ); | ||||||||||||
| 69 | } | ||||||||||||
| 70 | |||||||||||||
| 71 | 2 | ▶ | sub require_blamer { | ||||||||||
|
Subroutine
require_blamer: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 72 | 2 | my $conf = shift; | |||||||||||
| 73 | 2 | return _require_generic( | |||||||||||
| 74 | $conf, | ||||||||||||
| 75 | 'App::Prove::Elasticsearch::Blamer', | ||||||||||||
| 76 | 'client.blamer', | ||||||||||||
| 77 | 'CLIENT_BLAMER' | ||||||||||||
| 78 | ); | ||||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | 2 | ▶ | sub require_planner { | ||||||||||
|
Subroutine
require_planner: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 82 | 2 | my $conf = shift; | |||||||||||
| 83 | 2 | return _require_generic( | |||||||||||
| 84 | $conf, | ||||||||||||
| 85 | 'App::Prove::Elasticsearch::Planner', | ||||||||||||
| 86 | 'client.planner', | ||||||||||||
| 87 | 'CLIENT_PLANNER' | ||||||||||||
| 88 | ); | ||||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | 2 | ▶ | sub require_platformer { | ||||||||||
|
Subroutine
require_platformer: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 92 | 2 | my $conf = shift; | |||||||||||
| 93 | 2 | return _require_generic( | |||||||||||
| 94 | $conf, | ||||||||||||
| 95 | 'App::Prove::Elasticsearch::Platformer', | ||||||||||||
| 96 | 'client.platformer', | ||||||||||||
| 97 | 'CLIENT_PLATFORMER' | ||||||||||||
| 98 | ); | ||||||||||||
| 99 | } | ||||||||||||
| 100 | |||||||||||||
| 101 | 2 | ▶ | sub require_queue { | ||||||||||
|
Subroutine
require_queue: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 102 | 2 | my $conf = shift; | |||||||||||
| 103 | 2 | return _require_generic( | |||||||||||
| 104 | $conf, | ||||||||||||
| 105 | 'App::Prove::Elasticsearch::Queue', | ||||||||||||
| 106 | 'client.queue', | ||||||||||||
| 107 | 'CLIENT_QUEUE' | ||||||||||||
| 108 | ); | ||||||||||||
| 109 | } | ||||||||||||
| 110 | |||||||||||||
| 111 | 2 | ▶ | sub require_versioner { | ||||||||||
|
Subroutine
require_versioner: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 112 | 2 | my $conf = shift; | |||||||||||
| 113 | 2 | return _require_generic( | |||||||||||
| 114 | $conf, | ||||||||||||
| 115 | 'App::Prove::Elasticsearch::Versioner', | ||||||||||||
| 116 | 'client.versioner', | ||||||||||||
| 117 | 'CLIENT_VERSIONER' | ||||||||||||
| 118 | ); | ||||||||||||
| 119 | } | ||||||||||||
| 120 | |||||||||||||
| 121 | 2 | ▶ | sub require_runner { | ||||||||||
|
Subroutine
require_runner: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 122 | 2 | my $conf = shift; | |||||||||||
| 123 | 2 | return _require_generic( | |||||||||||
| 124 | $conf, | ||||||||||||
| 125 | 'App::Prove::Elasticsearch::Runner', | ||||||||||||
| 126 | 'client.runner', | ||||||||||||
| 127 | 'CLIENT_RUNNER' | ||||||||||||
| 128 | ); | ||||||||||||
| 129 | } | ||||||||||||
| 130 | |||||||||||||
| 131 | 2 | ▶ | sub require_provisioner { | ||||||||||
|
Subroutine
require_provisioner: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||
| 132 | 2 | my $module = shift; | |||||||||||
| 133 | 2 | ▶ | $module //= 'Default'; | ||||||||||
|
Condition50%1/2
$module //= "Default"
MC/DC0%0/1
$module //= "Default"
$module
Truth table50%1/2
A
A= $module
| |||||||||||||
| 134 | 2 | my $module_full = "App::Prove::Elasticsearch::Provisioner::$module"; | |||||||||||
| 135 | 2 | eval "require $module_full"; | |||||||||||
| 136 | 2 | ▶ | die $@ if $@; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 137 | |||||||||||||
| 138 | #Set ENV for use by harness | ||||||||||||
| 139 | 1 | ▶ | if ($ENV{CLIENT_PROVISIONERS}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 140 | 0 | $ENV{CLIENT_PROVISIONERS} .= ":$module_full"; | |||||||||||
| 141 | } else { | ||||||||||||
| 142 | 1 | $ENV{CLIENT_PROVISIONERS} = "$module_full"; | |||||||||||
| 143 | } | ||||||||||||
| 144 | 1 | return $module_full; | |||||||||||
| 145 | } | ||||||||||||
| 146 | |||||||||||||
| 147 | 13 | ▶ | sub _require_generic { | ||||||||||
|
Subroutine
_require_generic: called CC=3 SCAR=11.0
| |||||||||||||
| 148 | 13 | my ($conf, $prefix, $suffix_key, $envvar) = @_; | |||||||||||
| 149 | 13 | ▶ | my $suffix = $conf->{$suffix_key} // 'Default'; | ||||||||||
|
Condition100%2/2
$conf->{$suffix_key} // "Default"
MC/DC100%1/1
$conf->{$suffix_key} // "Default"
$conf->{$suffix_key}
Truth table100%2/2
A
A= $conf->{$suffix_key}
| |||||||||||||
| 150 | 13 | my $module = "${prefix}::$suffix"; | |||||||||||
| 151 | |||||||||||||
| 152 | 13 | eval "require $module"; | |||||||||||
| 153 | 13 | ▶ | die $@ if $@; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 154 | |||||||||||||
| 155 | #Set ENV for use by harness | ||||||||||||
| 156 | 6 | $ENV{$envvar} = $suffix; | |||||||||||
| 157 | 6 | return $module; | |||||||||||
| 158 | } | ||||||||||||
| 159 | |||||||||||||
| 160 | 2 | ▶ | sub get_last_index { | ||||||||||
|
Subroutine
get_last_index: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 161 | 2 | my ($e, $index) = @_; | |||||||||||
| 162 | |||||||||||||
| 163 | 2 | my $res = $e->search( | |||||||||||
| 164 | index => $index, | ||||||||||||
| 165 | body => { | ||||||||||||
| 166 | query => {match_all => {}}, | ||||||||||||
| 167 | sort => {id => {order => "desc"}}, | ||||||||||||
| 168 | size => 1 | ||||||||||||
| 169 | } | ||||||||||||
| 170 | ); | ||||||||||||
| 171 | |||||||||||||
| 172 | 2 | my $hits = $res->{hits}->{hits}; | |||||||||||
| 173 | 2 | ▶ | return 0 unless scalar(@$hits); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 174 | |||||||||||||
| 175 | 1 | return $res->{hits}->{total}; | |||||||||||
| 176 | } | ||||||||||||
| 177 | |||||||||||||
| 178 | 6 | ▶ | sub do_paginated_query { | ||||||||||
|
Subroutine
do_paginated_query: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 179 | 6 | my ($e, $max_query_size, %q) = @_; | |||||||||||
| 180 | 6 | my $offset = 0; | |||||||||||
| 181 | 6 | my $hits = []; | |||||||||||
| 182 | 6 | my $hitcounter = $max_query_size; | |||||||||||
| 183 | 6 | ▶ | while ($hitcounter == $max_query_size) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 184 | 6 | $q{size} = $max_query_size; | |||||||||||
| 185 | 6 | $q{from} = ($max_query_size * $offset); | |||||||||||
| 186 | 6 | my $res = $e->search(%q); | |||||||||||
| 187 | 6 | push(@$hits, @{$res->{hits}->{hits}}); | |||||||||||
| 188 | 6 | $hitcounter = scalar(@{$res->{hits}->{hits}}); | |||||||||||
| 189 | 6 | $offset++; | |||||||||||
| 190 | } | ||||||||||||
| 191 | 6 | return $hits; | |||||||||||
| 192 | } | ||||||||||||
| 193 | |||||||||||||
| 194 | 30 | 1; | |||||||||||
| 195 | |||||||||||||
| 196 | __END__ | ||||||||||||