blib/lib/App/Prove/Elasticsearch/Indexer.pm

filter: Statementstmt 92.6% 88 / 95 Branchbran 86.1% 31 / 36 Conditioncond 43.4% 10 / 23 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 91.6% 11 / 12 Podpod 100% 5 / 5 totaltotal 79.0% 147 / 186 CC 30 SCAR 24.5
CC
30
Cov
84%
associate_case_with_result
19.6
bulk_index_results
12.0
check_index
6.0
CRAP 11.6
« blib/lib/App/Prove/Elasticsearch/Planner/Default.pm ↑ Summary blib/lib/App/Prove/Elasticsearch/Parser.pm »
1 # ABSTRACT: Define what data is to be uploaded to elasticsearch, and handle it's uploading
2 # PODNAME: App::Prove::Elasticsearch::Indexer
3  
4 package App::Prove::Elasticsearch::Indexer;
5 10 $App::Prove::Elasticsearch::Indexer::VERSION = '0.001';
6 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 ▶use App::Prove::Elasticsearch::Utils();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 ▶use Search::Elasticsearch();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use List::Util 1.33;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 10 our $index = 'testsuite';
15  
16 5 ▶sub index {
Subroutine index: called CC=1 SCAR=0.0
Pod documented
17 5 return $index;
18 }
19  
20 10 our $max_query_size = 1000;
21 10 our $e;
22 10 our $bulk_helper;
23 10 our $idx;
24  
25 5 ▶sub check_index {
Subroutine check_index: called CC=6 SCAR=17.9
Pod documented
26 5 my $conf = shift;
27  
28 5 ▶ my $port = $conf->{'server.port'} ? ':' . $conf->{'server.port'} : '';
Branch: 1 branch
BranchTrueFalseTotal
$conf->{'server.port'} ? : 3 2 5
29 5 ▶ die "server must be specified" unless $conf->{'server.host'};
Branch: 1 branch
BranchTrueFalseTotal
unless $conf->{"server.host"} 2 3 5
30 3 ▶ die("port must be specified") unless $port;
Branch: 1 branch
BranchTrueFalseTotal
unless $port 1 2 3
31 2 my $serveraddress = "$conf->{'server.host'}$port";
32 2 ▶ $e //= Search::Elasticsearch->new(
Condition66%2/3
$App::Prove::Elasticsearch::Indexer::e //= "Search::Elasticsearch"->new("nodes", $serveraddress)
l!l&&r!l&&!r
110
MC/DC0%0/2
$App::Prove::Elasticsearch::Indexer::e //= "Search::Elasticsearch"->new("nodes", $serveraddress)
$App::Prove::Elasticsearch::Indexer::e "Search::Elasticsearch"->new("nodes", $serveraddress)
Truth table66%2/3
A //= B
A= $App::Prove::Elasticsearch::Indexer::eB= "Search::Elasticsearch"->new("nodes", $serveraddress)
ABresult
1X1
011
000
33 nodes => $serveraddress,
34 );
35  
36 #XXX for debugging
37 #$e->indices->delete( index => $index );
38  
39 2 ▶ if (!$e->indices->exists(index => $index)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($App::Prove::Elasticsearch::Indexer::e->indices->exists("index", $App::Prove::Elasticsearch::Indexer::index)) 1 1 2
40 1 $e->indices->create(
41 index => $index,
42 body => {
43 index => {
44 number_of_shards => "3",
45 number_of_replicas => "2",
46 similarity => {default => {type => "classic"}}
47 },
48 analysis => {
49 analyzer => {
50 default => {
51 type => "custom",
52 tokenizer => "whitespace",
53 filter => [
54 'lowercase', 'std_english_stop', 'custom_stop'
55 ]
56 }
57 },
58 filter => {
59 std_english_stop => {
60 type => "stop",
61 stopwords => "_english_"
62 },
63 custom_stop => {
64 type => "stop",
65 stopwords => [ "test", "ok", "not" ]
66 }
67 }
68 },
69 mappings => {
70 testsuite => {
71 properties => {
72 id => {type => "integer"},
73 elapsed => {type => "integer"},
74 occurred => {
75 type => "date",
76 format => "yyyy-MM-dd HH:mm:ss"
77 },
78 executor => {
79 type => "text",
80 analyzer => "default",
81 fielddata => "true",
82 term_vector => "yes",
83 similarity => "classic",
84 fields => {keyword => {type => "keyword"}}
85 },
86 status => {
87 type => "text",
88 analyzer => "default",
89 fielddata => "true",
90 term_vector => "yes",
91 similarity => "classic",
92 fields => {keyword => {type => "keyword"}}
93 },
94 version => {
95 type => "text",
96 analyzer => "default",
97 fielddata => "true",
98 term_vector => "yes",
99 similarity => "classic",
100 fields => {keyword => {type => "keyword"}}
101 },
102 test_version => {
103 type => "text",
104 analyzer => "default",
105 fielddata => "true",
106 term_vector => "yes",
107 similarity => "classic",
108 fields => {keyword => {type => "keyword"}}
109 },
110 platform => {
111 type => "text",
112 analyzer => "default",
113 fielddata => "true",
114 term_vector => "yes",
115 similarity => "classic",
116 fields => {keyword => {type => "keyword"}}
117 },
118 path => {
119 type => "text",
120 analyzer => "default",
121 fielddata => "true",
122 term_vector => "yes",
123 similarity => "classic",
124 fields => {keyword => {type => "keyword"}}
125 },
126 defect => {
127 type => "text",
128 analyzer => "default",
129 fielddata => "true",
130 term_vector => "yes",
131 similarity => "classic",
132 fields => {keyword => {type => "keyword"}}
133 },
134 steps_planned => {type => "integer"},
135 body => {
136 type => "text",
137 analyzer => "default",
138 fielddata => "true",
139 term_vector => "yes",
140 similarity => "classic",
141 },
142 name => {
143 type => "text",
144 analyzer => "default",
145 fielddata => "true",
146 term_vector => "yes",
147 similarity => "classic",
148 fields => {keyword => {type => "keyword"}}
149 },
150 steps => {
151 properties => {
152 number => {type => "integer"},
153 text => {type => "text"},
154 status => {type => "text"},
155 elapsed => {type => "integer"},
156 }
157 },
158 }
159 }
160 }
161 }
162 );
163 1 return 1;
164 }
165 1 return 0;
166 }
167  
168 3 ▶sub index_results {
Subroutine index_results: called CC=5 SCAR=16.2
Pod documented
169 3 my ($result) = @_;
170  
171 3 ▶ die("check_index must be run first") unless $e;
Branch: 1 branch
BranchTrueFalseTotal
unless $App::Prove::Elasticsearch::Indexer::e 1 2 3
172  
173 2 ▶ $idx //= App::Prove::Elasticsearch::Utils::get_last_index($e, $index);
Condition66%2/3
$App::Prove::Elasticsearch::Indexer::idx //= App::Prove::Elasticsearch::Utils::get_last_index($App::Prove::Elasticsearch::Indexer::e, $App::Prove::Elasticsearch::Indexer::index)
l!l&&r!l&&!r
110
MC/DC0%0/2
$App::Prove::Elasticsearch::Indexer::idx //= App::Prove::Elasticsearch::Utils::get_last_index($App::Prove::Elasticsearch::Indexer::e, $App::Prove::Elasticsearch::Indexer::index)
$App::Prove::Elasticsearch::Indexer::idx App::Prove::Elasticsearch::Utils::get_last_index($App::Prove::Elasticsearch::Indexer::e, $App::Prove::Elasticsearch::Indexer::index)
Truth table66%2/3
A //= B
A= $App::Prove::Elasticsearch::Indexer::idxB= App::Prove::Elasticsearch::Utils::get_last_index($App::Prove::Elasticsearch::Indexer::e, $App::Prove::Elasticsearch::Indexer::index)
ABresult
1X1
011
000
174 2 $idx++;
175  
176 2 $e->index(
177 index => $index,
178 id => $idx,
179 type => $index,
180 body => $result,
181 );
182  
183 2 my $doc_exists =
184 $e->exists(index => $index, type => 'testsuite', id => $idx);
185 2 ▶ if (!defined($doc_exists) || !int($doc_exists)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $doc_exists && int $doc_exists) { } 1 1 2
Condition66%2/3
defined $doc_exists && int $doc_exists
!ll&&!rl&&r
011
MC/DC50%1/2
defined $doc_exists && int $doc_exists
defined $doc_exists int $doc_exists
Truth table66%2/3
A && B
A= defined $doc_existsB= int $doc_exists
ABresult
0X0
100
111
186 1 die
187 "Failed to Index $result->{'name'}, could find no record with ID $idx\n";
188 } else {
189 1 print
190 "Successfully Indexed test: $result->{'name'} with result ID $idx\n";
191 }
192 }
193  
194 0 ▶sub bulk_index_results {
Subroutine bulk_index_results: not called CC=3 SCAR=24.8
Pod documented
195 0 my @results = @_;
196 0 ▶ $bulk_helper //= $e->bulk_helper(
Condition0%0/3
$App::Prove::Elasticsearch::Indexer::bulk_helper //= $App::Prove::Elasticsearch::Indexer::e->bulk_helper("index", $App::Prove::Elasticsearch::Indexer::index, "type", $App::Prove::Elasticsearch::Indexer::index)
l!l&&r!l&&!r
000
MC/DC0%0/2
$App::Prove::Elasticsearch::Indexer::bulk_helper //= $App::Prove::Elasticsearch::Indexer::e->bulk_helper("index", $App::Prove::Elasticsearch::Indexer::index, "type", $App::Prove::Elasticsearch::Indexer::index)
$App::Prove::Elasticsearch::Indexer::bulk_helper $App::Prove::Elasticsearch::Indexer::e->bulk_helper("index", $App::Prove::Elasticsearch::Indexer::index, "type", $App::Prove::Elasticsearch::Indexer::index)
Truth table0%0/3
A //= B
A= $App::Prove::Elasticsearch::Indexer::bulk_helperB= $App::Prove::Elasticsearch::Indexer::e->bulk_helper("index", $App::Prove::Elasticsearch::Indexer::index, "type", $App::Prove::Elasticsearch::Indexer::index)
ABresult
1X1
011
000
197 index => $index,
198 type => $index,
199 );
200  
201 0 ▶ $idx //= App::Prove::Elasticsearch::Utils::get_last_index($e, $index);
Condition0%0/3
$App::Prove::Elasticsearch::Indexer::idx //= App::Prove::Elasticsearch::Utils::get_last_index($App::Prove::Elasticsearch::Indexer::e, $App::Prove::Elasticsearch::Indexer::index)
l!l&&r!l&&!r
000
MC/DC0%0/2
$App::Prove::Elasticsearch::Indexer::idx //= App::Prove::Elasticsearch::Utils::get_last_index($App::Prove::Elasticsearch::Indexer::e, $App::Prove::Elasticsearch::Indexer::index)
$App::Prove::Elasticsearch::Indexer::idx App::Prove::Elasticsearch::Utils::get_last_index($App::Prove::Elasticsearch::Indexer::e, $App::Prove::Elasticsearch::Indexer::index)
Truth table0%0/3
A //= B
A= $App::Prove::Elasticsearch::Indexer::idxB= App::Prove::Elasticsearch::Utils::get_last_index($App::Prove::Elasticsearch::Indexer::e, $App::Prove::Elasticsearch::Indexer::index)
ABresult
1X1
011
000
202  
203 0 $bulk_helper->index(map { $idx++; {id => $idx, source => $_} } @results);
204 0 $bulk_helper->flush();
205 }
206  
207 7 ▶sub associate_case_with_result {
Subroutine associate_case_with_result: called CC=19 SCAR=29.7
Pod documented
208 7 my %opts = @_;
209  
210 7 ▶ die("check_index must be run first") unless $e;
Branch: 1 branch
BranchTrueFalseTotal
unless $App::Prove::Elasticsearch::Indexer::e 1 6 7
211  
212 my %q = (
213 index => $index,
214 body => {
215 query => {
216 bool => {
217 must => [
218 {
219 match => {
220 name => $opts{case},
221 }
222 },
223 6 ],
224 },
225 },
226 },
227 );
228  
229 #It's normal to have multiple platforms in a document.
230 6 foreach my $plat (@{$opts{platforms}}) {
231 11 push(@{$q{body}{query}{bool}{must}}, {match => {platform => $plat}});
232 }
233  
234 #It's NOT normal to have multiple versions in a document.
235 6 foreach my $version (@{$opts{versions}}) {
236 push(
237 6 @{$q{body}{query}{bool}{should}},
238 {match => {version => $version}}
239 );
240 }
241  
242 #Paginate the query, TODO short-circuit when we stop getting results?
243 6 my $hits = App::Prove::Elasticsearch::Utils::do_paginated_query(
244 $e, $max_query_size,
245 %q
246 );
247 6 ▶ return 0 unless scalar(@$hits);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$hits 0 6 6
248  
249 #Now, update w/ the defect.
250 6 my $failures = 0;
251 6 my $attempts = 0;
252 6 foreach my $hit (@$hits) {
253 $hit->{_source}->{platform} = [ $hit->{_source}->{platform} ]
254 6 ▶ if ref($hit->{_source}->{platform}) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $hit->{"_source"}{"platform"} ne "ARRAY" 0 6 6
255 6 ▶ next if (scalar(@{$opts{versions}}) && !$hit->{_source}->{version});
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$opts{"versions"};} and not $hit->{"_source"}{"version"} 0 6 6
Condition33%1/3
scalar @{$opts{"versions"};} and not $hit->{"_source"}{"version"}
!ll&&!rl&&r
060
MC/DC0%0/2
scalar @{$opts{"versions"};} and not $hit->{"_source"}{"version"}
scalar @{$opts{"versions"};} not $hit->{"_source"}{"version"}
Truth table33%1/3
A and not(B)
A= scalar @{$opts{"versions"};}B= not $hit->{"_source"}{"version"}
ABresult
0X0
100
111
256 next
257 6 ▶ unless List::Util::any { $hit->{_source}->{version} eq $_ }
Subroutine __ANON__: called CC=1 SCAR=0.0
258 6 ▶ @{$opts{versions}};
Branch: 1 branch
BranchTrueFalseTotal
unless &List::Util::any(sub { $hit->{"_source"}{"version"} eq $_; } , @{$opts{"versions"};}) 1 5 6
259 5 ▶ next if (scalar(@{$opts{platforms}}) && !$hit->{_source}->{platform});
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$opts{"platforms"};} and not $hit->{"_source"}{"platform"} 0 5 5
Condition33%1/3
scalar @{$opts{"platforms"};} and not $hit->{"_source"}{"platform"}
!ll&&!rl&&r
050
MC/DC0%0/2
scalar @{$opts{"platforms"};} and not $hit->{"_source"}{"platform"}
scalar @{$opts{"platforms"};} not $hit->{"_source"}{"platform"}
Truth table33%1/3
A and not(B)
A= scalar @{$opts{"platforms"};}B= not $hit->{"_source"}{"platform"}
ABresult
0X0
100
111
260 9 ▶ next unless List::Util::all {
Subroutine __ANON__: called CC=1 SCAR=0.0
261 9 my $p = $_;
262 9 grep { $_ eq $p } @{$hit->{_source}->{platform}}
263 }
264 5 ▶ @{$opts{platforms}};
Branch: 1 branch
BranchTrueFalseTotal
unless &List::Util::all(sub { my $p = $_; grep {$_ eq $p;} @{$$hit{"_source"}{"platform"};}; } , @{$opts{"platforms"};}) 1 4 5
265 4 ▶ next unless $hit->{_source}->{name} eq $opts{case};
Branch: 1 branch
BranchTrueFalseTotal
unless $hit->{"_source"}{"name"} eq $opts{"case"} 1 3 4
266  
267 3 $attempts++;
268  
269 #Merge the existing defects with the ones we are adding in
270 3 ▶ $hit->{defect} //= [];
Condition100%2/2
$hit->{"defect"} //= []
l!l
21
MC/DC100%1/1
$hit->{"defect"} //= []
$hit->{"defect"}
Truth table100%2/2
A
A= $hit->{"defect"}
Aresult
11
00
271 my @df_merged =
272 3 List::Util::uniq((@{$hit->{defect}}, @{$opts{defects}}));
273  
274 my %update = (
275 index => $index,
276 id => $hit->{_id},
277 3 type => 'result',
278 body => {
279 doc => {
280 defect => \@df_merged,
281 },
282 }
283 );
284 3 ▶ $update{body}{doc}{status} = $opts{status} if $opts{status};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"status"} 3 0 3
285  
286 3 my $res = $e->update(%update);
287  
288 print "Associated cases to document $hit->{_id}\n"
289 3 ▶ if $res->{result} eq 'updated';
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"result"} eq "updated" 1 2 3
290 3 ▶ if (!grep { $res->{result} eq $_ } qw{updated noop}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$res->{"result"} eq $_;} "updated", "noop") 1 2 3
291 1 print
292 "Something went wrong associating cases to document $hit->{_id}!\n$res->{result}\n";
293 1 $failures++;
294 }
295 }
296  
297 6 ▶ print "No cases matching your query could be found. No action was taken.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $attempts 3 3 6
298 unless $attempts;
299  
300 6 return $failures;
301 }
302  
303 10 1;
304  
305 __END__
« blib/lib/App/Prove/Elasticsearch/Planner/Default.pm ↑ Summary blib/lib/App/Prove/Elasticsearch/Parser.pm »