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

filter: Statementstmt 100% 180 / 180 Branchbran 98.5% 69 / 70 Conditioncond 79.4% 31 / 39 MC/DCmcdc 59.0% 13 / 22 Subroutinesub 100% 24 / 24 Podpod 90.0% 9 / 10 totaltotal 94.4% 326 / 345 CC 65 SCAR 24.2
CC
65
Cov
97%
layout
20.0
_depths
19.0
_packed_extent
10.0
CRAP 11.3
« bin/comb-svg ↑ Summary blib/lib/Kubernetes/Comb/SVG.pm »
1 package Kubernetes::Comb::SVG::Layout;
2 # ABSTRACT: Places cells in groups and a honeycomb, by dependency depth or packed
3  
4  
5 100 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 100 ▶use Types::Common::Numeric qw( PositiveInt PositiveNum PositiveOrZeroNum );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 100 ▶use Types::Standard qw( ArrayRef Bool Enum Object );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 100 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 100 our $VERSION = '0.001';
11  
12 1 ▶has cells => ( is => 'ro', isa => ArrayRef[Object], default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
13  
14  
15 100 has columns => ( is => 'ro', isa => PositiveInt, default => 6 );
16  
17  
18 # Whether columns came from the caller: the packed mode must tell a given 6
19 # from the default 6.
20 100 has _columns_given => ( is => 'rwp', isa => Bool, init_arg => undef, default => 0 );
21  
22 513 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
23 513 my ( $self, $args ) = @_;
24 513 ▶ $self->_set__columns_given(1) if exists $args->{columns};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"columns"} 399 114 513
25 }
26  
27 100 has mode => ( is => 'ro', isa => Enum[qw( depth packed )], default => 'depth' );
28  
29  
30 100 has rows => ( is => 'ro', isa => PositiveInt, predicate => 'has_rows' );
31  
32  
33 100 has aspect => ( is => 'ro', isa => PositiveNum, default => 16 / 9 );
34  
35  
36 100 has frame_width => ( is => 'ro', isa => PositiveOrZeroNum, default => 0 );
37  
38  
39 100 has frame_height => ( is => 'ro', isa => PositiveOrZeroNum, default => 0 );
40  
41  
42 100 has size => ( is => 'ro', isa => PositiveNum, default => 56 );
43  
44  
45 #### Geometry, all derived from size
46  
47 # Pointy-top hexagon: flat sides left and right, a corner at top and bottom.
48 198782 ▶sub hex_width { sqrt(3) * $_[0]->size }
Subroutine hex_width: called CC=1 SCAR=0.0
Pod documented
49  
50  
51 1111 ▶sub hex_height { 2 * $_[0]->size }
Subroutine hex_height: called CC=1 SCAR=0.0
Pod documented
52  
53  
54 # Air between the sides of two neighbouring hexagons.
55 102705 ▶sub gap { $_[0]->size / 7 }
Subroutine gap: called CC=1 SCAR=0.0
Pod documented
56  
57  
58 # Centre to centre inside a row.
59 102705 ▶sub step_x { $_[0]->hex_width + $_[0]->gap }
Subroutine step_x: called CC=1 SCAR=0.0
Pod documented
60  
61  
62 # Centre to centre between two rows: the same distance as inside a row, seen
63 # along the diagonal, so the gap is the same on all six sides.
64 44731 ▶sub step_y { $_[0]->step_x * sqrt(3) / 2 }
Subroutine step_y: called CC=1 SCAR=0.0
Pod documented
65  
66  
67 # Band above a group that holds its heading; the baseline sits inside it.
68 214 ▶sub heading_height { $_[0]->size * 0.6 }
Subroutine heading_height: called CC=1 SCAR=0.0
Pod documented
69  
70  
71 166 ▶sub heading_baseline { $_[0]->size * 0.4 }
Subroutine heading_baseline: called CC=1 SCAR=0.0
Pod documented
72  
73  
74 # Between the lowest hexagon of a group and the heading band of the next.
75 140 ▶sub group_gap { $_[0]->size * 0.35 }
Subroutine group_gap: called CC=1 SCAR=0.0
Pod documented
76  
77  
78 #### Layout
79  
80 514 ▶sub layout {
Subroutine layout: called CC=20 SCAR=30.0
Pod documented
81 514 my ( $self ) = @_;
82 514 my $cells = $self->_unique_cells;
83 514 my $deps = $self->_dependencies($cells);
84 514 my $depth = $self->_depths($deps);
85  
86 514 my ( %by_group, $unnamed );
87 514 for my $cell (@$cells) {
88 43620 my $group = $cell->group;
89 43620 ▶ push @{ defined $group ? $by_group{$group} ||= [] : $unnamed ||= [] }, $cell;
Branch: 1 branch
BranchTrueFalseTotal
defined $group ? : 230 43390 43620
Condition100%4/4
defined $group ? $by_group{$group} ||= [] : $unnamed ||= []
l!l
43237501
$by_group{$group} ||= []
l!l
118112
MC/DC100%1/1
defined $group ? $by_group{$group} ||= [] : $unnamed ||= []
defined $group ? $by_group{$group} ||= [] : $unnamed
MC/DC100%1/1
$by_group{$group} ||= []
$by_group{$group}
Truth table100%2/2
A
A= defined $group ? $by_group{$group} ||= [] : $unnamed
Aresult
11
00
Truth table100%2/2
A
A= $by_group{$group}
Aresult
11
00
90 }
91 514 my @groups = map { [ $_, $by_group{$_} ] } sort keys %by_group;
92 514 ▶ push @groups, [ undef, $unnamed ] if $unnamed;
Branch: 1 branch
BranchTrueFalseTotal
if $unnamed 501 13 514
93 514 ▶ my $headings = @groups > 1 || ( @groups && defined $groups[0][0] );
Condition100%6/6
@groups > 1 || @groups && defined $groups[0][0]
l!l&&r!l&&!r
562456
@groups and defined $groups[0][0]
!ll&&!rl&&r
94472
MC/DC100%3/3
@groups > 1 || @groups && defined $groups[0][0]
@groups > 1 @groups defined $groups[0][0]
Truth table100%4/4
A || B and C
A= @groups > 1B= @groupsC= defined $groups[0][0]
ABCresult
1XX1
0111
00X0
0100
94  
95 514 my $packed = $self->mode eq 'packed';
96 my $columns = $packed
97 514 ▶ ? $self->_packed_columns( [ map { scalar @{ $_->[1] } } @groups ], $headings )
Branch: 1 branch
BranchTrueFalseTotal
$packed ? : 195 319 514
98 : undef;
99  
100 514 my $radius = $self->size;
101 514 my ( @placed, @placed_groups );
102 514 my ( $width, $top ) = ( 0, 0 );
103 514 for my $entry (@groups) {
104 613 my ( $name, $members ) = @$entry;
105 613 ▶ $top += $self->group_gap if @placed_groups;
Branch: 1 branch
BranchTrueFalseTotal
if @placed_groups 108 505 613
106 613 ▶ my $comb_top = $top + ( $headings ? $self->heading_height : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$headings ? : 166 447 613
107 613 ▶ my @rows = $packed
Branch: 1 branch
BranchTrueFalseTotal
$packed ? : 263 350 613
Condition66%2/3
$columns || $self->_ceil(@$members / $self->rows)
l!l&&r!l&&!r
247160
MC/DC0%0/2
$columns || $self->_ceil(@$members / $self->rows)
$columns $self->_ceil(@$members / $self->rows)
Truth table66%2/3
A || B
A= $columnsB= $self->_ceil(@$members / $self->rows)
ABresult
1X1
011
000
108 ? $self->_packed_rows( $members, $columns || $self->_ceil( @$members / $self->rows ) )
109 : $self->_rows( $members, $depth );
110 613 for my $row ( 0 .. $#rows ) {
111 24621 ▶ my $shift = $row % 2 ? $self->step_x / 2 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$row % 2 ? : 12123 12498 24621
112 24621 for my $column ( 0 .. $#{ $rows[$row] } ) {
113 43620 my $cell = $rows[$row][$column];
114 43620 my $x = $shift + $self->hex_width / 2 + $column * $self->step_x;
115 43620 my $right = $x + $self->hex_width / 2;
116 43620 ▶ $width = $right if $right > $width;
Branch: 1 branch
BranchTrueFalseTotal
if $right > $width 2157 41463 43620
117 push @placed, {
118 id => $cell->id,
119 name => $cell->name,
120 group => $name,
121 43620 depth => $depth->{ $cell->id },
122 row => $row,
123 column => $column,
124 x => $self->_round($x),
125 y => $self->_round( $comb_top + $radius + $row * $self->step_y )
126 };
127 }
128 }
129 613 my $bottom = $comb_top + $self->hex_height + $#rows * $self->step_y;
130 613 ▶ push @placed_groups, {
Branch: 1 branch
BranchTrueFalseTotal
$headings ? : 166 447 613
131 name => $name,
132 heading => $headings
133 ? { x => 0, y => $self->_round( $top + $self->heading_baseline ) }
134 : undef,
135 y => $self->_round($top),
136 height => $self->_round( $bottom - $top )
137 };
138 613 $top = $bottom;
139 }
140  
141 514 my @edges;
142 514 for my $id ( sort keys %$deps ) {
143 40460 push @edges, map { { from => $id, to => $_ } }
144 43620 sort grep { $_ ne $id } @{ $deps->{$id} };
145 }
146  
147 return {
148 514 width => $self->_round($width),
149 height => $self->_round($top),
150 groups => \@placed_groups,
151 cells => \@placed,
152 edges => \@edges
153 };
154 }
155  
156  
157 # The cells with an id, the first of each id.
158 514 ▶sub _unique_cells {
Subroutine _unique_cells: called CC=2 SCAR=7.0
159 514 my ( $self ) = @_;
160 514 my %seen;
161 514 ▶ return [ grep { defined $_->id && !$seen{ $_->id }++ } @{ $self->cells } ];
Condition66%2/3
defined $_->id && !$seen{$_->id}++
!ll&&!rl&&r
0343620
MC/DC50%1/2
defined $_->id && !$seen{$_->id}++
defined $_->id !$seen{$_->id}++
Truth table66%2/3
A && not(B)
A= defined $_->idB= !$seen{$_->id}++
ABresult
0X0
100
111
162 }
163  
164 # id => ids of the cells it depends on, only those in the picture.
165 514 ▶sub _dependencies {
Subroutine _dependencies: called CC=5 SCAR=17.1
166 514 my ( $self, $cells ) = @_;
167 514 my %known = map { $_->id => 1 } @$cells;
168 514 my %deps;
169 514 for my $cell (@$cells) {
170 43620 my %seen;
171 $deps{ $cell->id } = [
172 43620 ▶ grep { defined && $known{$_} && !$seen{$_}++ } @{ $cell->dependencies || [] }
Condition37%3/8
defined $_ && $known{$_} && !$seen{$_}++
!ll&&!rl&&r
0040474
defined $_ and $known{$_}
!ll&&!rl&&r
0040474
$cell->dependencies || []
l!l
436200
MC/DC0%0/3
defined $_ && $known{$_} && !$seen{$_}++
defined $_ $known{$_} !$seen{$_}++
MC/DC0%0/1
$cell->dependencies || []
$cell->dependencies
Truth table25%1/4
A and B && not(C)
A= defined $_B= $known{$_}C= !$seen{$_}++
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $cell->dependencies
Aresult
11
00
173 ];
174 }
175 514 return \%deps;
176 }
177  
178 # id => depth. Tarjan's strongly connected components with an explicit stack,
179 # so neither a cycle nor a long chain can loop or exhaust the call stack. A
180 # component is complete only after everything it depends on, so its depth is
181 # known the moment it is closed; the cells of a cycle get the same one.
182 514 ▶sub _depths {
Subroutine _depths: called CC=19 SCAR=29.4
183 514 my ( $self, $deps ) = @_;
184 514 my ( %index, %low, %on_stack, %depth, @stack );
185 514 my $counter = 0;
186 514 for my $root ( sort keys %$deps ) {
187 43620 ▶ next if defined $index{$root};
Branch: 1 branch
BranchTrueFalseTotal
if defined $index{$root} 30103 13517 43620
188 13517 my @work = ( [ $root, 0 ] );
189 13517 ▶ while (@work) {
Branch: 1 branch
BranchTrueFalseTotal
if (@work) 73723 13517 87240
190 73723 my $frame = $work[-1];
191 73723 my $id = $frame->[0];
192 73723 ▶ unless ( defined $index{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $index{$id}) 43620 30103 73723
193 43620 $index{$id} = $low{$id} = $counter++;
194 43620 push @stack, $id;
195 43620 $on_stack{$id} = 1;
196 }
197 73723 my $descend;
198 73723 ▶ while ( $frame->[1] < @{ $deps->{$id} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->[1] < @{$deps->{$id};}) 40474 43620 84094
199 40474 my $dep = $deps->{$id}[ $frame->[1]++ ];
200 40474 ▶ unless ( defined $index{$dep} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $index{$dep}) 30103 10371 40474
201 30103 $descend = $dep;
202 30103 last;
203 }
204 10371 ▶ $low{$id} = $index{$dep} if $on_stack{$dep} && $index{$dep} < $low{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $on_stack{$dep} and $index{$dep} < $low{$id} 16 10355 10371
Condition100%3/3
$on_stack{$dep} and $index{$dep} < $low{$id}
!ll&&!rl&&r
103411416
MC/DC100%2/2
$on_stack{$dep} and $index{$dep} < $low{$id}
$on_stack{$dep} $index{$dep} < $low{$id}
Truth table100%3/3
A and B
A= $on_stack{$dep}B= $index{$dep} < $low{$id}
ABresult
0X0
100
111
205 }
206 73723 ▶ if ( defined $descend ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $descend) 30103 43620 73723
207 30103 push @work, [ $descend, 0 ];
208 30103 next;
209 }
210 43620 ▶ if ( $low{$id} == $index{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($low{$id} == $index{$id}) 23606 20014 43620
211 23606 my ( @members, %member );
212 23606 ▶ while (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 43620 0 43620
213 43620 my $member = pop @stack;
214 43620 delete $on_stack{$member};
215 43620 $member{$member} = 1;
216 43620 push @members, $member;
217 43620 ▶ last if $member eq $id;
Branch: 1 branch
BranchTrueFalseTotal
if $member eq $id 23606 20014 43620
218 }
219 23606 my $level = 0;
220 23606 for my $dep ( map { @{ $deps->{$_} } } @members ) {
221 40474 ▶ next if $member{$dep};
Branch: 1 branch
BranchTrueFalseTotal
if $member{$dep} 20044 20430 40474
222 20430 ▶ $level = $depth{$dep} + 1 if $depth{$dep} >= $level;
Branch: 1 branch
BranchTrueFalseTotal
if $depth{$dep} >= $level 20395 35 20430
223 }
224 23606 $depth{$_} = $level for @members;
225 }
226 43620 pop @work;
227 43620 ▶ next unless @work;
Branch: 1 branch
BranchTrueFalseTotal
unless @work 13517 30103 43620
228 30103 my $parent = $work[-1][0];
229 30103 ▶ $low{$parent} = $low{$id} if $low{$id} < $low{$parent};
Branch: 1 branch
BranchTrueFalseTotal
if $low{$id} < $low{$parent} 19998 10105 30103
230 }
231 }
232 514 return \%depth;
233 }
234  
235 # The drawn rows of one group: one depth after the other, each sorted by
236 # name and cut into rows of at most `columns` cells.
237 350 ▶sub _rows {
Subroutine _rows: called CC=5 SCAR=16.1
238 350 my ( $self, $cells, $depth ) = @_;
239 350 my %by_depth;
240 350 push @{ $by_depth{ $depth->{ $_->id } } }, $_ for @$cells;
241 350 my @rows;
242 350 for my $level ( sort { $a <=> $b } keys %by_depth ) {
243 46402 ▶ my @sorted = sort { $a->name cmp $b->name || $a->id cmp $b->id }
Condition66%2/3
$a->name cmp $b->name || $a->id cmp $b->id
l!l&&r!l&&!r
457046980
MC/DC0%0/2
$a->name cmp $b->name || $a->id cmp $b->id
$a->name cmp $b->name $a->id cmp $b->id
Truth table66%2/3
A || B
A= $a->name cmp $b->nameB= $a->id cmp $b->id
ABresult
1X1
011
000
244 20497 @{ $by_depth{$level} };
245 20497 ▶ push @rows, [ splice @sorted, 0, $self->columns ] while @sorted;
Branch: 1 branch
BranchTrueFalseTotal
@sorted and do { push @rows, [splice(@sorted, 0, $self->columns)] } 24044 20497 44541
246 }
247 350 return @rows;
248 }
249  
250 #### Packed
251  
252 # The column count all blocks share, or nothing when `rows` decides it block
253 # by block. A given `columns` wins; else, without `rows`, the count whose
254 # content, with the caller's frame around it, comes closest to `aspect`,
255 # compared as a ratio so that too wide and too tall weigh the same.
256 195 ▶sub _packed_columns {
Subroutine _packed_columns: called CC=7 SCAR=19.5
257 195 my ( $self, $sizes, $headings ) = @_;
258 195 ▶ return $self->columns if $self->_columns_given;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_columns_given 116 79 195
259 79 ▶ return if $self->has_rows;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_rows 14 65 79
260 65 my ( $most ) = sort { $b <=> $a } @$sizes;
261 65 my ( $best, $miss ) = ( 1 );
262 65 ▶ for my $columns ( 1 .. $most || 1 ) {
Condition100%2/2
$most || 1
l!l
632
MC/DC100%1/1
$most || 1
$most
Truth table100%2/2
A
A= $most
Aresult
11
00
263 468 my ( $width, $height ) = $self->_packed_extent( $sizes, $columns, $headings );
264 468 my $off = abs( log( ( $width + $self->frame_width ) / ( $height + $self->frame_height ) )
265 - log( $self->aspect ) );
266 468 ▶ ( $best, $miss ) = ( $columns, $off ) if !defined $miss || $off < $miss;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $miss or $off < $miss 242 226 468
Condition100%3/3
not defined $miss or $off < $miss
l!l&&r!l&&!r
65177226
MC/DC100%2/2
not defined $miss or $off < $miss
not defined $miss $off < $miss
Truth table100%3/3
not(A) or B
A= not defined $missB= $off < $miss
ABresult
1X1
011
000
267 }
268 65 return $best;
269 }
270  
271 # Width and height of the content when blocks of these sizes are packed into
272 # $columns: the same sums `layout` makes while it places the cells.
273 468 ▶sub _packed_extent {
Subroutine _packed_extent: called CC=10 SCAR=23.0
274 468 my ( $self, $sizes, $columns, $headings ) = @_;
275 468 my ( $width, $height ) = ( 0, 0 );
276 468 for my $index ( 0 .. $#$sizes ) {
277 498 my $rows = $self->_ceil( $sizes->[$index] / $columns );
278 498 for my $row ( 0 .. $rows - 1 ) {
279 1561 ▶ my $count = $row < $rows - 1 ? $columns : $sizes->[$index] - $row * $columns;
Branch: 1 branch
BranchTrueFalseTotal
$row < $rows - 1 ? : 1063 498 1561
280 1561 ▶ my $right = ( $row % 2 ? $self->step_x / 2 : 0 )
Branch: 1 branch
BranchTrueFalseTotal
$row % 2 ? : 646 915 1561
281 + $self->hex_width + ( $count - 1 ) * $self->step_x;
282 1561 ▶ $width = $right if $right > $width;
Branch: 1 branch
BranchTrueFalseTotal
if $right > $width 672 889 1561
283 }
284 498 ▶ $height += ( $index ? $self->group_gap : 0 )
Branch: 2 branches
BranchTrueFalseTotal
$index ? : 32 466 498
$headings ? : 48 450 498
285 + ( $headings ? $self->heading_height : 0 )
286 + $self->hex_height + ( $rows - 1 ) * $self->step_y;
287 }
288 468 ▶ return ( $width || 1, $height || 1 );
Condition100%4/4
$width || 1
l!l
4662
$height || 1
l!l
4662
MC/DC100%1/1
$width || 1
$width
MC/DC100%1/1
$height || 1
$height
Truth table100%2/2
A
A= $width
Aresult
11
00
Truth table100%2/2
A
A= $height
Aresult
11
00
289 }
290  
291 # The drawn rows of one packed block: sorted by id, cut into rows of
292 # $columns cells.
293 263 ▶sub _packed_rows {
Subroutine _packed_rows: called CC=2 SCAR=6.9
294 263 my ( $self, $cells, $columns ) = @_;
295 263 my @sorted = sort { $a->id cmp $b->id } @$cells;
296 263 my @rows;
297 263 ▶ push @rows, [ splice @sorted, 0, $columns ] while @sorted;
Branch: 1 branch
BranchTrueFalseTotal
@sorted and do { push @rows, [splice(@sorted, 0, $columns)] } 577 263 840
298 263 return @rows;
299 }
300  
301 514 ▶sub _ceil {
Subroutine _ceil: called CC=2 SCAR=6.9
302 514 my ( $self, $value ) = @_;
303 514 ▶ return int($value) + ( $value > int($value) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$value > int $value ? : 329 185 514
304 }
305  
306 # Two decimals, as a number: the same on every platform, and no '-0'.
307 89660 ▶sub _round {
Subroutine _round: called CC=1 SCAR=0.0
308 89660 my ( $self, $value ) = @_;
309 89660 return sprintf( '%.2f', $value ) + 0;
310 }
311  
312 100 1;
313  
314 __END__
« bin/comb-svg ↑ Summary blib/lib/Kubernetes/Comb/SVG.pm »