blib/lib/Kubernetes/Comb/SVG/Cell.pm

filter: Statementstmt 100% 156 / 156 Branchbran 100% 70 / 70 Conditioncond 100% 38 / 38 MC/DCmcdc 100% 21 / 21 Subroutinesub 100% 28 / 28 Podpod 100% 5 / 5 totaltotal 100% 318 / 318 CC 55 SCAR 16.8
CC
55
Cov
100%
from_cr
11.0
_reason
9.0
cells_from
8.0
CRAP 5.4
« blib/lib/Kubernetes/Comb/SVG.pm ↑ Summary examples/demo.pl »
1 package Kubernetes::Comb::SVG::Cell;
2 # ABSTRACT: One Comb custom resource, normalised for layout and drawing
3  
4  
5 102 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 102 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 102 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 102 ▶use Types::Standard qw( ArrayRef Bool HashRef Maybe Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 102 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 102 our $VERSION = '0.002';
12  
13 102 has name => ( is => 'ro', isa => Str, required => 1 );
14  
15  
16 102 has namespace => ( is => 'ro', isa => Maybe[Str] );
17  
18  
19 102 has id => ( is => 'lazy', isa => Str, init_arg => undef );
20  
21  
22 44220 ▶sub _build_id {
Subroutine _build_id: called CC=2 SCAR=6.9
23 44220 my ( $self ) = @_;
24 44220 ▶ return defined $self->namespace ? $self->namespace.'/'.$self->name : $self->name;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->namespace ? : 565 43655 44220
25 }
26  
27 102 has class => ( is => 'ro', isa => Maybe[Str] );
28  
29  
30 102 has phase => ( is => 'ro', isa => Str, default => 'Unknown' );
31  
32  
33 102 has raw_phase => ( is => 'ro', isa => Maybe[Str] );
34  
35  
36 102 has enabled => ( is => 'ro', isa => Bool, default => 1 );
37  
38  
39 1 ▶has depends_on => ( is => 'ro', isa => ArrayRef[Str], default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
40  
41  
42 44422 ▶has dependencies => ( is => 'rwp', isa => ArrayRef[Str], init_arg => undef, default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
43  
44  
45 44422 ▶has missing => ( is => 'rwp', isa => ArrayRef[Str], init_arg => undef, default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
46  
47  
48 1 ▶has endpoints => ( is => 'ro', isa => ArrayRef[HashRef], default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
49  
50  
51 102 has borrowed => ( is => 'ro', isa => Bool, default => 0 );
52  
53  
54 102 has upstream_recorded => ( is => 'ro', isa => Bool, default => 0 );
55  
56  
57 102 has upstream_class => ( is => 'ro', isa => Maybe[Str] );
58  
59  
60 102 has upstream_context => ( is => 'ro', isa => Maybe[Str] );
61  
62  
63 1 ▶has upstream_via => ( is => 'ro', isa => ArrayRef[Str], default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
64  
65  
66 102 has group => ( is => 'ro', isa => Maybe[Str] );
67  
68  
69 102 has message => ( is => 'ro', isa => Maybe[Str] );
70  
71  
72 102 has reason => ( is => 'ro', isa => Maybe[Str] );
73  
74  
75 4932 ▶sub known_phases { qw( Running Pending Blocked NeedsConfig Disabled Error Stopped NotDeployed ) }
Subroutine known_phases: called CC=1 SCAR=0.0
Pod documented
76  
77  
78 349 ▶sub borrowing_phases { qw( Running Pending ) }
Subroutine borrowing_phases: called CC=1 SCAR=0.0
Pod documented
79  
80  
81 44412 ▶sub is_known_phase {
Subroutine is_known_phase: called CC=2 SCAR=6.9
Pod documented
82 44412 my ( $self, $phase ) = @_;
83 44412 ▶ return 0 unless defined $phase;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $phase 42115 2297 44412
84 2297 return scalar grep { $_ eq $phase } $self->known_phases;
85 }
86  
87  
88 44435 ▶sub from_cr {
Subroutine from_cr: called CC=11 SCAR=24.0
Pod documented
89 44435 my ( $self, $cr, %opt ) = @_;
90 44435 $cr = $self->_hash($cr);
91 44435 my $meta = $self->_hash( $cr->{metadata} );
92 44435 my $spec = $self->_hash( $cr->{spec} );
93 44435 my $status = $self->_hash( $cr->{status} );
94  
95 44435 my $name = $self->_str( $meta->{name} );
96 44435 ▶ croak __PACKAGE__.'->from_cr: Comb without metadata.name'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 14 44421 44435
97 unless defined $name;
98  
99 44421 my $enabled = $self->_unless_false( $spec->{enabled} );
100 44421 my $raw_phase = $self->_str( $status->{phase} );
101 44421 ▶ my $phase = !$enabled ? 'Disabled'
Branch: 2 branches
BranchTrueFalseTotal
!$enabled ? : 19 44402 44421
$self->is_known_phase($raw_phase) ? : 2220 42182 44402
102 : $self->is_known_phase($raw_phase) ? $raw_phase
103 : 'Unknown';
104  
105 44421 my $upstream = $self->_hash( $status->{upstream} );
106 44421 ▶ my $recorded = %$upstream ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
%$upstream ? : 400 44021 44421
107 my $borrowed = $recorded
108 && $self->_unless_false( $upstream->{reachable} )
109 44421 ▶ && ( grep { $_ eq $phase } $self->borrowing_phases ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$recorded && $self->_unless_false($upstream->{'reachable'}) && grep({$_ eq $phase;} $self->borrowing_phases) ? : 293 44128 44421
Condition100%6/6
$recorded and $self->_unless_false($upstream->{"reachable"}) and grep {$_ eq $phase;} $self->borrowing_phases
!ll&&!rl&&r
4407355293
$recorded and $self->_unless_false($upstream->{"reachable"})
!ll&&!rl&&r
4402152348
MC/DC100%3/3
$recorded and $self->_unless_false($upstream->{"reachable"}) and grep {$_ eq $phase;} $self->borrowing_phases
$recorded $self->_unless_false($upstream->{"reachable"}) grep {$_ eq $phase;} $self->borrowing_phases
Truth table100%4/4
A and B and C
A= $recordedB= $self->_unless_false($upstream->{"reachable"})C= grep {$_ eq $phase;} $self->borrowing_phases
ABCresult
0XX0
10X0
1100
1111
110 44421 my $label = $self->_str( $opt{group_label} );
111  
112 return $self->new(
113 name => $name,
114 namespace => $self->_str( $meta->{namespace} ),
115 class => $self->_str( $spec->{class} ),
116 phase => $phase,
117 raw_phase => $raw_phase,
118 enabled => $enabled,
119 depends_on => [ $self->_strings( $spec->{dependsOn} ) ],
120 endpoints => [ $self->_endpoints( $status->{endpoints} ) ],
121 borrowed => $borrowed,
122 upstream_recorded => $recorded,
123 upstream_class => $self->_str( $upstream->{class} ),
124 upstream_context => $self->_str( $upstream->{context} ),
125 upstream_via => [ $self->_strings( $upstream->{via} ) ],
126 group => defined $label
127 ? $self->_str( $self->_hash( $meta->{labels} )->{$label} )
128 : undef,
129 message => $phase eq 'Running'
130 ? undef
131 : $self->_message( $status->{conditions} ),
132 reason => $phase eq 'Running'
133 ? undef
134 44421 ▶ : $self->_reason( $status->{conditions}, $phase, $raw_phase )
Branch: 3 branches
BranchTrueFalseTotal
defined $label ? : 1353 43068 44421
$phase eq 'Running' ? : 272 44149 44421
$phase eq 'Running' ? : 272 44149 44421
135 );
136 }
137  
138  
139 590 ▶sub cells_from {
Subroutine cells_from: called CC=8 SCAR=20.8
Pod documented
140 590 my ( $self, $input, %opt ) = @_;
141 590 $input = $self->_plain($input);
142 my @crs = ref $input eq 'ARRAY' ? @$input
143 : ref $input eq 'HASH' && exists $input->{items}
144 ? $self->_list( $input->{items} )
145 590 ▶ : defined $input ? ( $input )
Branch: 3 branches
BranchTrueFalseTotal
ref $input eq 'ARRAY' ? : 345 245 590
ref $input eq 'HASH' && exists $input->{'items'} ? : 241 4 245
defined $input ? : 3 1 4
Condition100%3/3
ref $input eq "HASH" and exists $input->{"items"}
!ll&&!rl&&r
13241
MC/DC100%2/2
ref $input eq "HASH" and exists $input->{"items"}
ref $input eq "HASH" exists $input->{"items"}
Truth table100%3/3
A and B
A= ref $input eq "HASH"B= exists $input->{"items"}
ABresult
0X0
100
111
146 : ();
147 590 my ( @cells, %by_id, %by_name );
148 590 for my $cr (@crs) {
149 44222 my $cell = $self->from_cr( $cr, %opt );
150 44217 ▶ next if $by_id{ $cell->id };
Branch: 1 branch
BranchTrueFalseTotal
if $by_id{$cell->id} 3 44214 44217
151 44214 $by_id{ $cell->id } = $cell;
152 44214 push @{ $by_name{ $cell->name } }, $cell;
153 44214 push @cells, $cell;
154 }
155 585 $_->_resolve( \%by_id, \%by_name ) for @cells;
156 585 return @cells;
157 }
158  
159  
160 # Sorts depends_on into the ids of cells in the set and the entries that
161 # match none, or more than one.
162 44210 ▶sub _resolve {
Subroutine _resolve: called CC=5 SCAR=16.1
163 44210 my ( $self, $by_id, $by_name ) = @_;
164 44210 my ( @dependencies, @missing, %seen );
165 44210 for my $entry ( @{ $self->depends_on } ) {
166 40492 my $cell = $self->_dependency( $entry, $by_id, $by_name );
167 40492 ▶ push @missing, $entry unless $cell;
Branch: 1 branch
BranchTrueFalseTotal
unless $cell 17 40475 40492
168 40492 ▶ push @dependencies, $cell->id if $cell && !$seen{ $cell->id }++;
Branch: 1 branch
BranchTrueFalseTotal
if $cell and not $seen{$cell->id}++ 40474 18 40492
Condition100%3/3
$cell and not $seen{$cell->id}++
!ll&&!rl&&r
17140474
MC/DC100%2/2
$cell and not $seen{$cell->id}++
$cell not $seen{$cell->id}++
Truth table100%3/3
A and not(B)
A= $cellB= not $seen{$cell->id}++
ABresult
0X0
100
111
169 }
170 44210 $self->_set_dependencies( \@dependencies );
171 44210 $self->_set_missing( \@missing );
172 44210 return;
173 }
174  
175 # 'namespace/name' is an id. A bare name is the cell of that name in the own
176 # namespace, else the only cell of that name.
177 40492 ▶sub _dependency {
Subroutine _dependency: called CC=6 SCAR=17.9
178 40492 my ( $self, $entry, $by_id, $by_name ) = @_;
179 40492 ▶ return $by_id->{$entry} if index( $entry, '/' ) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($entry, "/") != -1 10 40482 40492
180 40482 ▶ my $own = $by_id->{ defined $self->namespace ? $self->namespace.'/'.$entry : $entry };
Branch: 1 branch
BranchTrueFalseTotal
defined $self->namespace ? : 45 40437 40482
181 40482 ▶ return $own if $own;
Branch: 1 branch
BranchTrueFalseTotal
if $own 40450 32 40482
182 32 ▶ my @named = @{ $by_name->{$entry} || [] };
Condition100%2/2
$by_name->{$entry} || []
l!l
1913
MC/DC100%1/1
$by_name->{$entry} || []
$by_name->{$entry}
Truth table100%2/2
A
A= $by_name->{$entry}
Aresult
11
00
183 32 ▶ return @named == 1 ? $named[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@named == 1 ? : 16 16 32
184 }
185  
186 #### Reading odd data
187  
188 # An object answering TO_JSON becomes what it answers; anything else stays.
189 447160 ▶sub _plain {
Subroutine _plain: called CC=4 SCAR=13.9
190 447160 my ( $self, $value ) = @_;
191 447160 my $hops = 0;
192 447160 ▶ $value = $value->TO_JSON
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($value) and $value->can("TO_JSON") and $hops++ < 8 and do { $value = $value->TO_JSON } 25 447160 447185
Condition100%6/6
builtin::blessed($value) and $value->can("TO_JSON") and $hops++ < 8
!ll&&!rl&&r
447159125
builtin::blessed($value) and $value->can("TO_JSON")
!ll&&!rl&&r
447158126
MC/DC100%3/3
builtin::blessed($value) and $value->can("TO_JSON") and $hops++ < 8
builtin::blessed($value) $value->can("TO_JSON") $hops++ < 8
Truth table100%4/4
A and B and C
A= builtin::blessed($value)B= $value->can("TO_JSON")C= $hops++ < 8
ABCresult
0XX0
10X0
1100
1111
193 while blessed $value && $value->can('TO_JSON') && $hops++ < 8;
194 447160 return $value;
195 }
196  
197 224768 ▶sub _hash {
Subroutine _hash: called CC=2 SCAR=6.9
198 224768 my ( $self, $value ) = @_;
199 224768 $value = $self->_plain($value);
200 224768 ▶ return ref $value eq 'HASH' ? $value : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'HASH' ? : 133600 91168 224768
201 }
202  
203 132960 ▶sub _list {
Subroutine _list: called CC=2 SCAR=6.9
204 132960 my ( $self, $value ) = @_;
205 132960 $value = $self->_plain($value);
206 132960 ▶ return ref $value eq 'ARRAY' ? @$value : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 1394 131566 132960
207 }
208  
209 # A non-empty plain scalar, as a string; everything else is undef.
210 403661 ▶sub _str {
Subroutine _str: called CC=4 SCAR=13.9
211 403661 my ( $self, $value ) = @_;
212 403661 ▶ return undef if !defined $value || ref $value || !length $value;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value or not length $value 312430 91231 403661
Condition100%6/6
not defined $value or ref $value or not length $value
l!l&&r!l&&!r
312427391231
not defined $value or ref $value
l!l&&r!l&&!r
3124002791234
MC/DC100%3/3
not defined $value or ref $value or not length $value
not defined $value ref $value not length $value
Truth table100%4/4
not(A) or B or not(C)
A= not defined $valueB= ref $valueC= not length $value
ABCresult
1XX1
01X1
0011
0000
213 91231 return ''.$value;
214 }
215  
216 # A list of names: an array of strings, or one string. No duplicates, order kept.
217 88842 ▶sub _strings {
Subroutine _strings: called CC=3 SCAR=11.0
218 88842 my ( $self, $value ) = @_;
219 88842 $value = $self->_plain($value);
220 88842 my %seen;
221 88908 ▶ return grep { defined && !$seen{$_}++ }
Condition100%3/3
defined $_ && !$seen{$_}++
!ll&&!rl&&r
48380240526
MC/DC100%2/2
defined $_ && !$seen{$_}++
defined $_ !$seen{$_}++
Truth table100%3/3
A && not(B)
A= defined $_B= !$seen{$_}++
ABresult
0X0
100
111
222 88842 ▶ map { $self->_str($_) }
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 40458 48384 88842
223 ref $value eq 'ARRAY' ? @$value : ( $value );
224 }
225  
226 # A tri-state flag (spec.enabled, status.upstream.reachable): unset is true,
227 # only a false value -- false, 0, '', the string 'false' -- says no.
228 44821 ▶sub _unless_false {
Subroutine _unless_false: called CC=5 SCAR=16.1
229 44821 my ( $self, $value ) = @_;
230 44821 ▶ return 1 unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 44700 121 44821
231 121 ▶ return 0 unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 48 73 121
232 73 ▶ return 1 if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 28 45 73
233 45 ▶ return lc $value eq 'false' ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
lc $value eq 'false' ? : 23 22 45
234 }
235  
236 44421 ▶sub _endpoints {
Subroutine _endpoints: called CC=3 SCAR=11.0
237 44421 my ( $self, $value ) = @_;
238 44421 my @endpoints;
239 44421 for my $entry ( $self->_list($value) ) {
240 54 $entry = $self->_hash($entry);
241 54 my $name = $self->_str( $entry->{name} );
242 54 ▶ next unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 8 46 54
243 46 push @endpoints, { name => $name, port => $self->_str( $entry->{port} ) };
244 }
245 44421 return @endpoints;
246 }
247  
248 44149 ▶sub _message {
Subroutine _message: called CC=3 SCAR=11.0
249 44149 my ( $self, $value ) = @_;
250 44149 my %seen;
251 600 ▶ my @messages = grep { defined && !$seen{$_}++ }
Condition100%3/3
defined $_ && !$seen{$_}++
!ll&&!rl&&r
545451
MC/DC100%2/2
defined $_ && !$seen{$_}++
defined $_ !$seen{$_}++
Truth table100%3/3
A && not(B)
A= defined $_B= !$seen{$_}++
ABresult
0X0
100
111
252 44149 map { $self->_str( $self->_hash($_)->{message} ) } $self->_list($value);
253 44149 ▶ return @messages ? join( "\n", @messages ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@messages ? : 46 44103 44149
254 }
255  
256 # A text as it is compared with a phase: lower case, letters and digits only.
257 2812 ▶sub _bare {
Subroutine _bare: called CC=1 SCAR=0.0
258 2812 my ( $self, $value ) = @_;
259 2812 $value = lc $value;
260 2812 $value =~ s/[^\p{L}\p{N}]//g;
261 2812 return $value;
262 }
263  
264 # Why a cell is not Running: the reason of the Ready condition, else of the
265 # first condition that is not True. A reason that says NotChecked or only
266 # repeats one of @phases tells nothing; the first line of the message of that
267 # condition stands in, unless it repeats a phase as well.
268 44149 ▶sub _reason {
Subroutine _reason: called CC=9 SCAR=22.0
269 44149 my ( $self, $value, @phases ) = @_;
270 44149 my @conditions = grep { %$_ } map { $self->_hash($_) } $self->_list($value);
271 44149 ▶ my ( $condition ) = grep { ( $self->_str( $_->{type} ) // '' ) eq 'Ready' } @conditions;
Condition100%2/2
$self->_str($_->{"type"}) // ""
l!l
57515
MC/DC100%1/1
$self->_str($_->{"type"}) // ""
$self->_str($_->{"type"})
Truth table100%2/2
A
A= $self->_str($_->{"type"})
Aresult
11
00
272 44149 ▶ ( $condition ) = grep { ( $self->_str( $_->{status} ) // '' ) ne 'True' } @conditions
Branch: 1 branch
BranchTrueFalseTotal
unless $condition 43604 545 44149
Condition100%2/2
$self->_str($_->{"status"}) // ""
l!l
1314
MC/DC100%1/1
$self->_str($_->{"status"}) // ""
$self->_str($_->{"status"})
Truth table100%2/2
A
A= $self->_str($_->{"status"})
Aresult
11
00
273 unless $condition;
274 44149 ▶ return undef unless $condition;
Branch: 1 branch
BranchTrueFalseTotal
unless $condition 43588 561 44149
275  
276 561 my %says_nothing = map { $self->_bare($_) => 1 } '', 'NotChecked', grep { defined } @phases;
277 561 ▶ my ( $line ) = grep { /\S/ } split /\R/, $self->_str( $condition->{message} ) // '';
Condition100%2/2
$self->_str($condition->{"message"}) // ""
l!l
44517
MC/DC100%1/1
$self->_str($condition->{"message"}) // ""
$self->_str($condition->{"message"})
Truth table100%2/2
A
A= $self->_str($condition->{"message"})
Aresult
11
00
278 561 for my $text ( $self->_str( $condition->{reason} ), $line ) {
279 597 ▶ next unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 28 569 597
280 569 $text =~ s/\A\s+|\s+\z//g;
281 569 ▶ return $text unless $says_nothing{ $self->_bare($text) };
Branch: 1 branch
BranchTrueFalseTotal
unless $says_nothing{$self->_bare($text)} 545 24 569
282 }
283 16 return undef;
284 }
285  
286 102 1;
287  
288 __END__
« blib/lib/Kubernetes/Comb/SVG.pm ↑ Summary examples/demo.pl »