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

filter: Statementstmt 100% 182 / 182 Branchbran 98.6% 71 / 72 Conditioncond 79.4% 31 / 39 MC/DCmcdc 59.0% 13 / 22 Subroutinesub 100% 24 / 24 Podpod 90.0% 9 / 10 totaltotal 94.5% 330 / 349 CC 66 SCAR 24.3
CC
66
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 CodeRef Enum Maybe 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.002';
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 573 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
23 573 my ( $self, $args ) = @_;
24 573 ▶ $self->_set__columns_given(1) if exists $args->{columns};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"columns"} 455 118 573
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 frame => ( is => 'ro', isa => Maybe[CodeRef] );
43  
44  
45 100 has size => ( is => 'ro', isa => PositiveNum, default => 56 );
46  
47  
48 #### Geometry, all derived from size
49  
50 # Pointy-top hexagon: flat sides left and right, a corner at top and bottom.
51 202742 ▶sub hex_width { sqrt(3) * $_[0]->size }
Subroutine hex_width: called CC=1 SCAR=0.0
Pod documented
52  
53  
54 1255 ▶sub hex_height { 2 * $_[0]->size }
Subroutine hex_height: called CC=1 SCAR=0.0
Pod documented
55  
56  
57 # Air between the sides of two neighbouring hexagons.
58 104273 ▶sub gap { $_[0]->size / 7 }
Subroutine gap: called CC=1 SCAR=0.0
Pod documented
59  
60  
61 # Centre to centre inside a row.
62 104273 ▶sub step_x { $_[0]->hex_width + $_[0]->gap }
Subroutine step_x: called CC=1 SCAR=0.0
Pod documented
63  
64  
65 # Centre to centre between two rows: the same distance as inside a row, seen
66 # along the diagonal, so the gap is the same on all six sides.
67 45432 ▶sub step_y { $_[0]->step_x * sqrt(3) / 2 }
Subroutine step_y: called CC=1 SCAR=0.0
Pod documented
68  
69  
70 # Band above a group that holds its heading; the baseline sits inside it.
71 269 ▶sub heading_height { $_[0]->size * 0.6 }
Subroutine heading_height: called CC=1 SCAR=0.0
Pod documented
72  
73  
74 221 ▶sub heading_baseline { $_[0]->size * 0.4 }
Subroutine heading_baseline: called CC=1 SCAR=0.0
Pod documented
75  
76  
77 # Between the lowest hexagon of a group and the heading band of the next.
78 176 ▶sub group_gap { $_[0]->size * 0.35 }
Subroutine group_gap: called CC=1 SCAR=0.0
Pod documented
79  
80  
81 #### Layout
82  
83 574 ▶sub layout {
Subroutine layout: called CC=20 SCAR=30.0
Pod documented
84 574 my ( $self ) = @_;
85 574 my $cells = $self->_unique_cells;
86 574 my $deps = $self->_dependencies($cells);
87 574 my $depth = $self->_depths($deps);
88  
89 574 my ( %by_group, $unnamed );
90 574 for my $cell (@$cells) {
91 44177 my $group = $cell->group;
92 44177 ▶ push @{ defined $group ? $by_group{$group} ||= [] : $unnamed ||= [] }, $cell;
Branch: 1 branch
BranchTrueFalseTotal
defined $group ? : 285 43892 44177
Condition100%4/4
defined $group ? $by_group{$group} ||= [] : $unnamed ||= []
l!l
43753560
$by_group{$group} ||= []
l!l
136149
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
93 }
94 574 my @groups = map { [ $_, $by_group{$_} ] } sort keys %by_group;
95 574 ▶ push @groups, [ undef, $unnamed ] if $unnamed;
Branch: 1 branch
BranchTrueFalseTotal
if $unnamed 560 14 574
96 574 ▶ my $headings = @groups > 1 || ( @groups && defined $groups[0][0] );
Condition100%6/6
@groups > 1 || @groups && defined $groups[0][0]
l!l&&r!l&&!r
743497
@groups and defined $groups[0][0]
!ll&&!rl&&r
94883
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
97  
98 574 my $packed = $self->mode eq 'packed';
99 my $columns = $packed
100 574 ▶ ? $self->_packed_columns( [ map { scalar @{ $_->[1] } } @groups ], $headings )
Branch: 1 branch
BranchTrueFalseTotal
$packed ? : 235 339 574
101 : undef;
102  
103 574 my $radius = $self->size;
104 574 my ( @placed, @placed_groups );
105 574 my ( $width, $top ) = ( 0, 0 );
106 574 for my $entry (@groups) {
107 709 my ( $name, $members ) = @$entry;
108 709 ▶ $top += $self->group_gap if @placed_groups;
Branch: 1 branch
BranchTrueFalseTotal
if @placed_groups 144 565 709
109 709 ▶ my $comb_top = $top + ( $headings ? $self->heading_height : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$headings ? : 221 488 709
110 709 ▶ my @rows = $packed
Branch: 1 branch
BranchTrueFalseTotal
$packed ? : 303 406 709
Condition66%2/3
$columns || $self->_ceil(@$members / $self->rows)
l!l&&r!l&&!r
287160
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
111 ? $self->_packed_rows( $members, $columns || $self->_ceil( @$members / $self->rows ) )
112 : $self->_rows( $members, $depth );
113 709 for my $row ( 0 .. $#rows ) {
114 24827 ▶ my $shift = $row % 2 ? $self->step_x / 2 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$row % 2 ? : 12193 12634 24827
115 24827 for my $column ( 0 .. $#{ $rows[$row] } ) {
116 44177 my $cell = $rows[$row][$column];
117 44177 my $x = $shift + $self->hex_width / 2 + $column * $self->step_x;
118 44177 my $right = $x + $self->hex_width / 2;
119 44177 ▶ $width = $right if $right > $width;
Branch: 1 branch
BranchTrueFalseTotal
if $right > $width 2464 41713 44177
120 push @placed, {
121 id => $cell->id,
122 name => $cell->name,
123 group => $name,
124 44177 depth => $depth->{ $cell->id },
125 row => $row,
126 column => $column,
127 x => $self->_round($x),
128 y => $self->_round( $comb_top + $radius + $row * $self->step_y )
129 };
130 }
131 }
132 709 my $bottom = $comb_top + $self->hex_height + $#rows * $self->step_y;
133 709 ▶ push @placed_groups, {
Branch: 1 branch
BranchTrueFalseTotal
$headings ? : 221 488 709
134 name => $name,
135 heading => $headings
136 ? { x => 0, y => $self->_round( $top + $self->heading_baseline ) }
137 : undef,
138 y => $self->_round($top),
139 height => $self->_round( $bottom - $top )
140 };
141 709 $top = $bottom;
142 }
143  
144 574 my @edges;
145 574 for my $id ( sort keys %$deps ) {
146 40460 push @edges, map { { from => $id, to => $_ } }
147 44177 sort grep { $_ ne $id } @{ $deps->{$id} };
148 }
149  
150 return {
151 574 width => $self->_round($width),
152 height => $self->_round($top),
153 groups => \@placed_groups,
154 cells => \@placed,
155 edges => \@edges
156 };
157 }
158  
159  
160 # The cells with an id, the first of each id.
161 574 ▶sub _unique_cells {
Subroutine _unique_cells: called CC=2 SCAR=7.0
162 574 my ( $self ) = @_;
163 574 my %seen;
164 574 ▶ return [ grep { defined $_->id && !$seen{ $_->id }++ } @{ $self->cells } ];
Condition66%2/3
defined $_->id && !$seen{$_->id}++
!ll&&!rl&&r
0344177
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
165 }
166  
167 # id => ids of the cells it depends on, only those in the picture.
168 574 ▶sub _dependencies {
Subroutine _dependencies: called CC=5 SCAR=17.1
169 574 my ( $self, $cells ) = @_;
170 574 my %known = map { $_->id => 1 } @$cells;
171 574 my %deps;
172 574 for my $cell (@$cells) {
173 44177 my %seen;
174 $deps{ $cell->id } = [
175 44177 ▶ 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
441770
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
176 ];
177 }
178 574 return \%deps;
179 }
180  
181 # id => depth. Tarjan's strongly connected components with an explicit stack,
182 # so neither a cycle nor a long chain can loop or exhaust the call stack. A
183 # component is complete only after everything it depends on, so its depth is
184 # known the moment it is closed; the cells of a cycle get the same one.
185 574 ▶sub _depths {
Subroutine _depths: called CC=19 SCAR=29.4
186 574 my ( $self, $deps ) = @_;
187 574 my ( %index, %low, %on_stack, %depth, @stack );
188 574 my $counter = 0;
189 574 for my $root ( sort keys %$deps ) {
190 44177 ▶ next if defined $index{$root};
Branch: 1 branch
BranchTrueFalseTotal
if defined $index{$root} 30103 14074 44177
191 14074 my @work = ( [ $root, 0 ] );
192 14074 ▶ while (@work) {
Branch: 1 branch
BranchTrueFalseTotal
if (@work) 74280 14074 88354
193 74280 my $frame = $work[-1];
194 74280 my $id = $frame->[0];
195 74280 ▶ unless ( defined $index{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $index{$id}) 44177 30103 74280
196 44177 $index{$id} = $low{$id} = $counter++;
197 44177 push @stack, $id;
198 44177 $on_stack{$id} = 1;
199 }
200 74280 my $descend;
201 74280 ▶ while ( $frame->[1] < @{ $deps->{$id} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->[1] < @{$deps->{$id};}) 40474 44177 84651
202 40474 my $dep = $deps->{$id}[ $frame->[1]++ ];
203 40474 ▶ unless ( defined $index{$dep} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $index{$dep}) 30103 10371 40474
204 30103 $descend = $dep;
205 30103 last;
206 }
207 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
208 }
209 74280 ▶ if ( defined $descend ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $descend) 30103 44177 74280
210 30103 push @work, [ $descend, 0 ];
211 30103 next;
212 }
213 44177 ▶ if ( $low{$id} == $index{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($low{$id} == $index{$id}) 24163 20014 44177
214 24163 my ( @members, %member );
215 24163 ▶ while (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 44177 0 44177
216 44177 my $member = pop @stack;
217 44177 delete $on_stack{$member};
218 44177 $member{$member} = 1;
219 44177 push @members, $member;
220 44177 ▶ last if $member eq $id;
Branch: 1 branch
BranchTrueFalseTotal
if $member eq $id 24163 20014 44177
221 }
222 24163 my $level = 0;
223 24163 for my $dep ( map { @{ $deps->{$_} } } @members ) {
224 40474 ▶ next if $member{$dep};
Branch: 1 branch
BranchTrueFalseTotal
if $member{$dep} 20044 20430 40474
225 20430 ▶ $level = $depth{$dep} + 1 if $depth{$dep} >= $level;
Branch: 1 branch
BranchTrueFalseTotal
if $depth{$dep} >= $level 20395 35 20430
226 }
227 24163 $depth{$_} = $level for @members;
228 }
229 44177 pop @work;
230 44177 ▶ next unless @work;
Branch: 1 branch
BranchTrueFalseTotal
unless @work 14074 30103 44177
231 30103 my $parent = $work[-1][0];
232 30103 ▶ $low{$parent} = $low{$id} if $low{$id} < $low{$parent};
Branch: 1 branch
BranchTrueFalseTotal
if $low{$id} < $low{$parent} 19998 10105 30103
233 }
234 }
235 574 return \%depth;
236 }
237  
238 # The drawn rows of one group: one depth after the other, each sorted by
239 # name and cut into rows of at most `columns` cells.
240 406 ▶sub _rows {
Subroutine _rows: called CC=5 SCAR=16.1
241 406 my ( $self, $cells, $depth ) = @_;
242 406 my %by_depth;
243 406 push @{ $by_depth{ $depth->{ $_->id } } }, $_ for @$cells;
244 406 my @rows;
245 406 for my $level ( sort { $a <=> $b } keys %by_depth ) {
246 46425 ▶ 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
457276980
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
247 20553 @{ $by_depth{$level} };
248 20553 ▶ push @rows, [ splice @sorted, 0, $self->columns ] while @sorted;
Branch: 1 branch
BranchTrueFalseTotal
@sorted and do { push @rows, [splice(@sorted, 0, $self->columns)] } 24100 20553 44653
249 }
250 406 return @rows;
251 }
252  
253 #### Packed
254  
255 # The column count all blocks share, or nothing when `rows` decides it block
256 # by block. A given `columns` wins; else, without `rows`, the count whose
257 # content, with the caller's frame around it, comes closest to `aspect`,
258 # compared as a ratio so that too wide and too tall weigh the same.
259 235 ▶sub _packed_columns {
Subroutine _packed_columns: called CC=8 SCAR=20.8
260 235 my ( $self, $sizes, $headings ) = @_;
261 235 ▶ return $self->columns if $self->_columns_given;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_columns_given 152 83 235
262 83 ▶ return if $self->has_rows;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_rows 14 69 83
263 69 my ( $most ) = sort { $b <=> $a } @$sizes;
264 69 my ( $best, $miss ) = ( 1 );
265 69 ▶ for my $columns ( 1 .. $most || 1 ) {
Condition100%2/2
$most || 1
l!l
672
MC/DC100%1/1
$most || 1
$most
Truth table100%2/2
A
A= $most
Aresult
11
00
266 516 my ( $width, $height ) = $self->_packed_extent( $sizes, $columns, $headings );
267 516 ▶ my ( $whole_width, $whole_height ) = $self->frame
Branch: 1 branch
BranchTrueFalseTotal
$self->frame ? : 347 169 516
268 ? $self->frame->( $width, $height )
269 : ( $width + $self->frame_width, $height + $self->frame_height );
270 516 my $off = abs( log( $whole_width / $whole_height ) - log( $self->aspect ) );
271 516 ▶ ( $best, $miss ) = ( $columns, $off ) if !defined $miss || $off < $miss;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $miss or $off < $miss 252 264 516
Condition100%3/3
not defined $miss or $off < $miss
l!l&&r!l&&!r
69183264
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
272 }
273 69 return $best;
274 }
275  
276 # Width and height of the content when blocks of these sizes are packed into
277 # $columns: the same sums `layout` makes while it places the cells.
278 516 ▶sub _packed_extent {
Subroutine _packed_extent: called CC=10 SCAR=23.0
279 516 my ( $self, $sizes, $columns, $headings ) = @_;
280 516 my ( $width, $height ) = ( 0, 0 );
281 516 for my $index ( 0 .. $#$sizes ) {
282 546 my $rows = $self->_ceil( $sizes->[$index] / $columns );
283 546 for my $row ( 0 .. $rows - 1 ) {
284 1725 ▶ my $count = $row < $rows - 1 ? $columns : $sizes->[$index] - $row * $columns;
Branch: 1 branch
BranchTrueFalseTotal
$row < $rows - 1 ? : 1179 546 1725
285 1725 ▶ my $right = ( $row % 2 ? $self->step_x / 2 : 0 )
Branch: 1 branch
BranchTrueFalseTotal
$row % 2 ? : 722 1003 1725
286 + $self->hex_width + ( $count - 1 ) * $self->step_x;
287 1725 ▶ $width = $right if $right > $width;
Branch: 1 branch
BranchTrueFalseTotal
if $right > $width 744 981 1725
288 }
289 546 ▶ $height += ( $index ? $self->group_gap : 0 )
Branch: 2 branches
BranchTrueFalseTotal
$index ? : 32 514 546
$headings ? : 48 498 546
290 + ( $headings ? $self->heading_height : 0 )
291 + $self->hex_height + ( $rows - 1 ) * $self->step_y;
292 }
293 516 ▶ return ( $width || 1, $height || 1 );
Condition100%4/4
$width || 1
l!l
5142
$height || 1
l!l
5142
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
294 }
295  
296 # The drawn rows of one packed block: sorted by id, cut into rows of
297 # $columns cells.
298 303 ▶sub _packed_rows {
Subroutine _packed_rows: called CC=2 SCAR=6.9
299 303 my ( $self, $cells, $columns ) = @_;
300 303 my @sorted = sort { $a->id cmp $b->id } @$cells;
301 303 my @rows;
302 303 ▶ push @rows, [ splice @sorted, 0, $columns ] while @sorted;
Branch: 1 branch
BranchTrueFalseTotal
@sorted and do { push @rows, [splice(@sorted, 0, $columns)] } 727 303 1030
303 303 return @rows;
304 }
305  
306 562 ▶sub _ceil {
Subroutine _ceil: called CC=2 SCAR=6.9
307 562 my ( $self, $value ) = @_;
308 562 ▶ return int($value) + ( $value > int($value) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$value > int $value ? : 353 209 562
309 }
310  
311 # Two decimals, as a number: the same on every platform, and no '-0'.
312 91141 ▶sub _round {
Subroutine _round: called CC=1 SCAR=0.0
313 91141 my ( $self, $value ) = @_;
314 91141 return sprintf( '%.2f', $value ) + 0;
315 }
316  
317 100 1;
318  
319 __END__
« bin/comb-svg ↑ Summary blib/lib/Kubernetes/Comb/SVG.pm »