CPANDB/Distribution.pm untested

filter: Statementstmt 0.0% 0 / 151 Branchbran 0.0% 0 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 235 SCAR 60.4
CC
20
Cov
0%
_dependants
72.0
quartile
20.0
_quartile
20.0
CRAP 420.0
1 package CPANDB::Distribution;
2  
3 use 5.008005;
4 use strict;
5 use warnings;
6 use DateTime 0.50 ();
7  
8 our $VERSION = '0.18';
9  
10 my $today = DateTime->today( time_zone => 'UTC' );
11  
12  
13  
14  
15  
16 ######################################################################
17 # DateTime Integration
18  
19 sub perl_version {
20 my $self = shift;
21 my @rows = CPANDB::Requires->select(
22 'where distribution = ? and module = ? and phase = ?',
23 $self->distribution,
24 'perl',
25 'runtime',
26 );
27 if ( @rows ) {
28 return $rows[0]->version;
29 } else {
30 return undef;
31 }
32 }
33  
34 sub uploaded_datetime {
35 my $self = shift;
36 my @date = split(/-/, $self->uploaded);
37 DateTime->new(
38 year => $date[0],
39 month => $date[1],
40 day => $date[2],
41 @CPANDB::LOCATION,
42 );
43 }
44  
45 sub age {
46 my $class = shift;
47 my $duration = $class->age_duration;
48 return $duration->in_units('days');
49 }
50  
51 sub age_duration {
52 my $class = shift;
53 my $latest = $class->uploaded_datetime;
54 my $today = DateTime->today( @CPANDB::LOCATION );
55 return $today - $latest;
56 }
57  
58 sub age_months {
59 $_[0]->age_duration->in_units('months');
60 }
61  
62 sub quartile {
63 my $self = shift;
64  
65 # Get the boundary dates
66 my @quartile = ref($self)->_quartile;
67  
68 # Find which quartile we are in
69 my $uploaded = $self->uploaded;
70 if ( $uploaded gt $quartile[0] ) {
71 return 1;
72 } elsif ( $uploaded gt $quartile[1] ) {
73 return 2;
74 } elsif ( $uploaded gt $quartile[2] ) {
75 return 3;
76 } else {
77 return 4;
78 }
79 }
80  
81 my @QUADRANT = ();
82  
83 sub _quartile {
84 return @QUADRANT if @QUADRANT;
85  
86 # Start with the total number of distributions
87 my $class = shift;
88 my $rows = $class->count;
89 my $mod = $rows % 4;
90 my $range = ($rows - $mod) / 4;
91  
92 # Find the last row in each quartile
93 foreach ( 1 .. 4 ) {
94 my $offset = ($range * $_) + $mod - 1;
95  
96 # Tweak the boundary rows to deal with row totals
97 # that are not divisible by four. By generous about
98 # moving edge cases up if so.
99 if ( $mod - $_ > 0 ) {
100 $offset = $offset - ( $mod - $_ );
101 }
102  
103 # Find the upload date for the resulting row
104 my @object = $class->select("order by uploaded desc limit 1 offset $offset");
105 unless ( @object == 1 ) {
106 die("Failed to find edge of quartile $_");
107 }
108  
109 push @QUADRANT, $object[0]->uploaded;
110 }
111  
112 return @QUADRANT;
113 }
114  
115  
116  
117  
118  
119 ######################################################################
120 # Graph Integration
121  
122 sub dependency_graph {
123 require Graph::Directed;
124 shift->_dependency( _class => 'Graph::Directed', @_ );
125 }
126  
127 sub dependants_graph {
128 require Graph::Directed;
129 shift->_dependants( _class => 'Graph::Directed', @_ );
130 }
131  
132 sub dependency_easy {
133 require Graph::Easy;
134 shift->_dependency( _class => 'Graph::Easy', @_ );
135 }
136  
137 sub dependants_easy {
138 require Graph::Easy;
139 shift->_dependants( _class => 'Graph::Easy', @_ );
140 }
141  
142 sub dependency_graphviz {
143 require GraphViz;
144 shift->_dependency( _class => 'GraphViz', @_ );
145 }
146  
147 sub dependants_graphviz {
148 require GraphViz;
149 shift->_dependants( _class => 'GraphViz', @_ );
150 }
151  
152 sub dependency_xgmml {
153 require Graph::XGMML;
154 my $self = shift;
155 my @param = ( @_ == 1 ) ? ( OUTPUT => IO::File->new( shift, 'w' ) ) : ( @_ );
156 $self->_dependency( _class => 'Graph::XGMML', @param );
157 }
158  
159 sub dependants_xgmml {
160 require Graph::XGMML;
161 my $self = shift;
162 my @param = ( @_ == 1 ) ? ( OUTPUT => IO::File->new( shift, 'w' ) ) : ( @_ );
163 $self->_dependants( _class => 'Graph::XGMML', @param );
164 }
165  
166 sub _dependency {
167 my $self = shift;
168 my %param = @_;
169 my $class = delete $param{_class};
170 my $phase = delete $param{phase};
171 my $perl = delete $param{perl};
172  
173 # Prepare support values for the algorithm
174 my $add_node = $class->can('add_vertex')
175 ? 'add_vertex'
176 : 'add_node';
177 my $sql_where = 'where distribution = ?';
178 my @sql_param = ();
179 if ( $phase ) {
180 $sql_where .= ' and phase = ?';
181 push @sql_param, $phase;
182 }
183 if ( $perl ) {
184 $sql_where .= ' and ( core is null or core >= ? )';
185 push @sql_param, $perl;
186 }
187  
188 # Pass any remaining params to the graph constructor
189 my $graph = $class->new( %param );
190  
191 # Fill the graph via simple list recursion
192 my @todo = ( $self->distribution );
193 my %seen = ( $self->distribution => 1 );
194 while ( @todo ) {
195 my $name = shift @todo;
196 $graph->$add_node( $name );
197  
198 # Find the distinct dependencies for this node
199 my %edge = ();
200 my @deps = grep {
201 not $edge{$_}++
202 } map {
203 $_->dependency
204 } CPANDB::Dependency->select(
205 $sql_where, $name, @sql_param,
206 );
207 foreach my $dep ( @deps ) {
208 $graph->add_edge( $name => $dep );
209 }
210  
211 # Push the new ones to the list
212 push @todo, grep { not $seen{$_}++ } @deps;
213 }
214  
215 return $graph;
216 }
217  
218 sub _dependants {
219 my $self = shift;
220 my %param = @_;
221 my $class = delete $param{_class};
222 my $phase = delete $param{phase};
223 my $perl = delete $param{perl};
224  
225 # Prepare support values for the algorithm
226 my $add_node = $class->can('add_vertex') ? 'add_vertex' : 'add_node';
227 my $sql_where = 'where dependency = ?';
228 my @sql_param = ();
229 if ( $phase ) {
230 $sql_where .= ' and phase = ?';
231 push @sql_param, $phase;
232 }
233 if ( $perl ) {
234 $sql_where .= ' and ( core is null or core >= ? )';
235 push @sql_param, $perl;
236 }
237  
238 # Pass any remaining params to the graph constructor
239 my $graph = $class->new( %param );
240  
241 # Fill the graph via simple list recursion
242 my @todo = ( $self->distribution );
243 my %seen = ( $self->distribution => 1 );
244 while ( @todo ) {
245 my $name = shift @todo;
246 next if $name =~ /^Task-/;
247 next if $name =~ /^Acme-Mom/;
248 $graph->$add_node( $name );
249  
250 # Find the distinct dependencies for this node
251 my %edge = ();
252 my @deps = grep {
253 not $edge{$_}++
254 } map {
255 $_->distribution
256 } CPANDB::Dependency->select(
257 $sql_where, $name, @sql_param,
258 );
259 foreach my $dep ( @deps ) {
260 next if $dep =~ /^Task-/;
261 next if $dep =~ /^Acme-Mom/;
262 $graph->add_edge( $name => $dep );
263 }
264  
265 # Push the new ones to the list
266 push @todo, grep { not $seen{$_}++ } @deps;
267 }
268  
269 return $graph;
270 }
271  
272 1;