File Coverage

blib/lib/StorageDisplay/Data/Elem.pm
Criterion Covered Total %
statement 99 107 92.5
branch 32 38 84.2
condition 2 4 50.0
subroutine 20 20 100.0
pod 0 12 0.0
total 153 181 84.5


line stmt bran cond sub pod time code
1             #
2             # This file is part of StorageDisplay
3             #
4             # This software is copyright (c) 2014-2023 by Vincent Danjean.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9 10     10   255815 use strict;
  10         27  
  10         410  
10 10     10   53 use warnings;
  10         23  
  10         923  
11              
12             package StorageDisplay::Data::Elem;
13             # ABSTRACT: Handle something that will be displayed and can be linked
14             # from/to as a tree for StorageDisplay
15              
16             our $VERSION = '2.06'; # VERSION
17              
18 10     10   725 use Moose;
  10         649042  
  10         68  
19 10     10   59043 use namespace::sweep;
  10         22126  
  10         88  
20              
21 10     10   1607 use StorageDisplay::Role;
  10         24  
  10         310  
22 10     10   5849 use Object::ID;
  10         58175  
  10         62  
23 10     10   699 use Carp;
  10         19  
  10         4133  
24              
25             with (
26             "StorageDisplay::Role::Iterable"
27             => {
28             iterable => "StorageDisplay::Data::Elem",
29             name => "Recursive",
30             },
31             "StorageDisplay::Role::Style::Base::Elem"
32             );
33              
34             has 'consume' => (
35             is => 'ro',
36             isa => 'ArrayRef[StorageDisplay::Block]',
37             traits => [ 'Array' ],
38             default => sub { return []; },
39             lazy => 1,
40             handles => {
41             'consumeBlock' => 'push',
42             'consumedBlocks' => 'elements',
43             }
44             );
45              
46             has 'provide' => (
47             is => 'ro',
48             isa => 'ArrayRef[StorageDisplay::Block]',
49             traits => [ 'Array' ],
50             default => sub { return []; },
51             lazy => 1,
52             handles => {
53             'provideBlock' => 'push',
54             'allProvidedBlocks' => 'elements',
55             },
56             init_arg => undef,
57             );
58              
59             around 'provideBlock' => sub {
60             my $orig = shift;
61             my $self = shift;
62              
63             for my $b (@_) {
64             $b->providedBy($self);
65             }
66             #print STDERR "INFO ", $self->name, " : provides ", map { $_->name } (@_);
67             #print STDERR "\n";
68             return $self->$orig(@_);
69             };
70              
71             #sub BUILD {
72             # my $self = shift;
73             # my $args = shift;
74             #
75             # print STDERR "INFO: ", $self->name, " : consumes ",
76             # join(', ', map { $_->name } ($self->consumedBlocks)), "\n";
77             # return $self;
78             #}
79              
80             sub has_parent {
81 829     829 0 1696 my $self = shift;
82 829         41402 return $self->nb_parents == 1;
83             }
84              
85             sub parent {
86 822     822 0 1501 my $self = shift;
87 822 50       39445 if ($self->nb_parents != 1) {
88 0         0 croak "Unkown parent requested for ".$self->label;
89             }
90 822         41687 return ($self->parents)[0];
91             };
92              
93             has 'label' => (
94             is => 'rw',
95             isa => 'Str',
96             required => 0,
97             default => "NO LABEL",
98             );
99              
100             sub disp_size {
101 1051     1051 0 515552 my $self = shift;
102 1051         2256 my $size = shift;
103 1051         2124 my $unit = 'B';
104 1051         2136 my $d=2;
105             #print STDERR "\n\ninit size=$size\n";
106             {
107 10     10   7987 use bigrat;
  10         32926  
  10         49  
  1051         1869  
108 1051         2295 my $divide = 1;
109 1051 100       6231 if ($size >= 1024) { $unit = 'kiB'; }
  1035         240562  
110 1051 100       8800 if ($size >= 1048576) { $unit = 'MiB'; $divide *= 1024; }
  983         188939  
  983         4159  
111 1051 100       449854 if ($size >= 1073741824) { $unit = 'GiB'; $divide *= 1024; }
  857         181301  
  857         3132  
112 1051 100       373959 if ($size >= 1099511627776) { $unit = 'TiB'; $divide *= 1024; }
  238         54667  
  238         923  
113 1051 50       264055 if ($size >= 1125899906842624) { $unit = 'PiB'; $divide *= 1024; }
  0         0  
  0         0  
114 1051 50       211017 if ($size >= 1152921504606846976) { $unit = 'EiB'; $divide *= 1024; }
  0         0  
  0         0  
115              
116 1051 100       208432 if ($unit eq 'B') {
117 16         116 return "$size B";
118             } else {
119 1035         4647 $size /= $divide;
120             }
121 1035         519602 $size = $size * 1000 / 1024;
122 1035 100       844504 if ($size >= 10000) { $d = 1;}
  701         64820  
123 1035 100       35611 if ($size >= 100000) { $d = 0;}
  404         33874  
124             #print STDERR "size=$size ", ref($size), "\n";
125 1035         58256 $size=int($size/10**(3-$d)+0.5)*10**(3-$d);
126             #print STDERR "size=$size ", ref($size), "\n";
127 1035         3232179 $size = $size->numify();
128             }
129 1035         48199 return sprintf("%.$d"."f $unit", $size/1000);
130             }
131              
132             sub statecolor {
133 738     738 0 2060 my $self = shift;
134 738         1556 my $state = shift;
135              
136 738 100       4307 if ($state eq "free") {
    100          
    100          
    100          
    50          
    100          
    100          
    50          
    50          
137 104         361 return "green";
138             } elsif ($state eq "ok") {
139 26         191 return "green";
140             } elsif ($state eq "used") {
141 370         2176 return "yellow";
142             } elsif ($state eq "busy") {
143 134         443 return "pink";
144             } elsif ($state eq "unused") {
145 0         0 return "white";
146             } elsif ($state eq "unknown") {
147 42         301 return "lightgrey";
148             } elsif ($state eq "special") {
149 40         276 return "mediumorchid1";
150             } elsif ($state eq "warning") {
151 0         0 return "orange";
152             } elsif ($state eq "error") {
153 0         0 return "red";
154             } else {
155 22         79 return "red";
156             }
157             }
158              
159             sub dname {
160 756     756 0 1428 my $self = shift;
161 756         29767 return $self->name;
162             }
163              
164             has 'linkkind' => (
165             is => 'ro',
166             isa => 'Str',
167             required => 1,
168             lazy => 1,
169             default => sub {
170             my $self = shift;
171             my $kind = ref($self);
172             $kind =~ s/^StorageDisplay::Data:://;
173             if ($self->has_parent) {
174             my $pkind = ref($self->parent);
175             $pkind =~ s/^StorageDisplay::Data:://;
176             $kind =~ s/^$pkind//;
177             }
178             return $kind;
179             },
180             );
181              
182             sub rawlinkname {
183 1501     1501 0 2978 my $self = shift;
184 1501         64389 return $self->fullname;
185             }
186              
187             sub linkname {
188 1236     1236 0 2287 my $self = shift;
189 1236         3834 return '"'.$self->rawlinkname.'"';
190             }
191              
192             sub newElem {
193 665     665 0 1472 my $self = shift;
194 665         1358 my $baseclass = shift;
195              
196 665         1677 my $class = 'StorageDisplay::Data::'.$baseclass;
197 665         5572 return $class->new(@_);
198             }
199              
200             sub newChild {
201 352     352 0 13560 my $self = shift;
202              
203 352         1700 my $child = $self->newElem(@_);
204 352         185962 $self->addChild($child);
205              
206 352         3757 return $child;
207             }
208              
209             sub pushDotText {
210 818     818 0 2781 my $self = shift;
211 818         1421 my $text = shift;
212 818   50     2591 my $t = shift // "\t";
213              
214 818         1947 my @pushed = map { $t.$_ } @_;
  3884         10041  
215 818         1478 push @{$text}, @pushed;
  818         5891  
216             }
217              
218             sub dotSubNodes {
219 14     14 0 35 my $self = shift;
220 14   50     57 my $t = shift // "\t";
221 14         37 my @text=();
222 14         73 my $it = $self->iterator(recurse => 0);
223 14         25675 while (defined(my $e=$it->next)) {
224 124         1588 push @text, $e->dotNode($t);
225             }
226 14         1871 return @text;
227             }
228              
229             sub dotLinks {
230 526     526 0 956 my $self = shift;
231 526         1262 return ();
232             }
233              
234             1;
235              
236             __END__
237              
238             =pod
239              
240             =encoding UTF-8
241              
242             =head1 NAME
243              
244             StorageDisplay::Data::Elem - Handle something that will be displayed and can be linked
245              
246             =head1 VERSION
247              
248             version 2.06
249              
250             =head1 AUTHOR
251              
252             Vincent Danjean <Vincent.Danjean@ens-lyon.org>
253              
254             =head1 COPYRIGHT AND LICENSE
255              
256             This software is copyright (c) 2014-2023 by Vincent Danjean.
257              
258             This is free software; you can redistribute it and/or modify it under
259             the same terms as the Perl 5 programming language system itself.
260              
261             =cut