blib/lib/CPAN/WWW/Top100/Generator.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package CPAN::WWW::Top100::Generator;
2  
3 =pod
4  
5 =head1 NAME
6  
7 CPAN::WWW::Top100::Generator - Create or update the website for http://ali.as/top100
8  
9 =head1 DESCRIPTION
10  
11 This module is used to generate the website content for the B<CPAN Top 100> website.
12  
13 This module (for now) has no moving parts...
14  
15 =cut
16  
17 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use File::Spec 0.80 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use HTML::Spry::DataSet 0.01 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 0 ▶use Google::Chart 0.05014;
Subroutine BEGIN: called CC=1 SCAR=2.6
23 use List::Util 0.01;
24  
25 # Download and load the CPAN data
26 use CPANDB 0.10 {
27 maxage => 0
28 };
29  
30 our $VERSION = '0.10';
31  
32  
33  
34  
35  
36 #####################################################################
37 # Main Methods
38  
39 sub new {
40 my $class = shift;
41  
42 # Create the basic object
43 my $self = bless {
44 spry => HTML::Spry::DataSet->new,
45 @_,
46 }, $class;
47  
48 # Check params
49 unless ( defined $self->dir and -d $self->dir ) {
50 die "Missing or invalid directory";
51 }
52  
53 return $self;
54 }
55  
56 sub spry {
57 $_[0]->{spry};
58 }
59  
60 sub dir {
61 $_[0]->{dir};
62 }
63  
64 sub file {
65 File::Spec->catfile( $_[0]->dir, $_[1] );
66 }
67  
68 sub run {
69 my $self = shift;
70  
71 # Build the Heavy 100 index
72 $self->dataset(
73 'ds1' => 'Heavy 100',
74 [ 'Rank', 'Dependencies', 'Author', 'Distribution' ],
75 'd.weight',
76 );
77  
78 # Build the Volatile 100 index
79 $self->dataset(
80 'ds2' => 'Volatile 100',
81 [ 'Rank', 'Dependents', 'Author', 'Distribution' ],
82 'd.volatility',
83 );
84  
85 # Build the Debian 100 index
86 $self->dataset(
87 'ds3' => 'Debian 100',
88 [ 'Rank', 'Dependents', 'Author', 'Distribution' ],
89 'd.volatility * 0',
90 );
91  
92 # Build the Downstream 100 index
93 $self->dataset(
94 'ds4' => 'Downstream 100',
95 [ 'Rank', 'Dependents', 'Author', 'Distribution' ],
96 'd.volatility * 0',
97 );
98  
99 # Build the Meta 100 (Level 1)
100 $self->dataset(
101 'ds5' => 'Meta 100',
102 [ 'Rank', 'Dependents', 'Author', 'Distribution' ],
103 'd.volatility * ( 1 - d.meta )',
104 );
105  
106 # Build the Meta 100 index (Level 2)
107 $self->dataset(
108 'ds6' => 'Meta 100',
109 [ 'Rank', 'Dependents', 'Author', 'Distribution' ],
110 'd.volatility * 0',
111 );
112  
113 # Build the Meta 100 index (Level 3)
114 $self->dataset(
115 'ds7' => 'Meta 100',
116 [ 'Rank', 'Dependents', 'Author', 'Distribution' ],
117 'd.volatility * 0',
118 );
119  
120 # Build the FAIL 100 index
121 $self->dataset(
122 'ds8' => 'FAIL 100',
123 [ 'Rank', 'Score', 'FAIL', 'Author', 'Distribution' ],
124 'd.volatility * (d.fail + d.unknown)',
125 'd.fail + d.unknown',
126 );
127  
128 # Write out the data file
129 $self->spry->write(
130 $self->file('data.html')
131 );
132  
133 return 1;
134 }
135  
136 sub dataset {
137 my ($self, $name, $title, $header, $score, @more ) = @_;
138 my @report = $self->report(
139 sql_score => $score,
140 sql_more => @more ? \@more : '',
141 );
142 $self->spry->add( $name, $header, @report );
143 $self->chart( $title, @report )->render_to_file(
144 filename => $self->file( "$name.png" ),
145 );
146 }
147  
148 sub report {
149 my $self = shift;
150 my %param = @_;
151 my $list = CPANDB->selectall_arrayref(
152 $self->_distsql( %param ),
153 );
154 unless ( $list ) {
155 die("Report SQL failed in " . CPANDB->dsn);
156 }
157 $self->_rank( $list );
158 return @$list;
159 }
160  
161 sub chart {
162 my $self = shift;
163 my $title = shift;
164 my @report = map { $_->[1] } @_;
165 my $scale = List::Util::max @report;
166 my @data = map {
167 $scale ? ($_ / $scale * 100) : 0
168 } @report;
169 Google::Chart->new(
170 title => { text => $title },
171 type => 'Line',
172 data => \@data,
173 );
174 }
175  
176  
177  
178  
179  
180 #####################################################################
181 # Support Methods
182  
183 # Prepends ranks in place (ugly, but who cares for now)
184 sub _rank {
185 my $self = shift;
186 my $table = shift;
187 my $rank = 0;
188 my @ranks = ();
189 foreach my $i ( 0 .. $#$table ) {
190 if ( $i == 0 ) {
191 $rank = 1;
192 } elsif ( $table->[$i]->[0] ne $table->[$i - 1]->[0] ) {
193 $rank = $i + 1;
194 }
195 #if ( $i > 0 and $table->[$i]->[0] eq $table->[$i - 1]->[0] ) {
196 # push @ranks, "$rank=";
197 #} elsif ( $i < $#$table and $table->[$i]->[0] eq $table->[$i + 1]->[0] ) {
198 # push @ranks, "$rank=";
199 #} else {
200 push @ranks, $rank;
201 #}
202 }
203  
204 # Prepend the rank to the table
205 foreach my $i ( 0 .. $#$table ) {
206 unshift @{ $table->[$i] }, $ranks[$i];
207 }
208  
209 return $table;
210 }
211  
212 sub _distsql {
213 my $self = shift;
214 my %param = @_;
215 $param{sql_limit} ||= 100;
216 unless ( defined $param{sql_score} ) {
217 die "Failed to define a score metric";
218 }
219 if ( $param{sql_more} ) {
220 $param{sql_more} = join '',
221 map { "\n\t$_," } @{$param{sql_more}};
222 } else {
223 $param{sql_more} = '';
224 }
225 return <<"END_SQL";
226 select
227 $param{sql_score} as score,$param{sql_more}
228 d.author as author,
229 d.distribution as distribution
230 from
231 distribution d
232 order by
233 score desc,
234 author asc,
235 distribution asc
236 limit
237 $param{sql_limit}
238 END_SQL
239 }
240  
241 1;
242  
243 =pod
244  
245 =head1 SUPPORT
246  
247 Bugs should be reported via the CPAN bug tracker at
248  
249 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CPAN-WWW-Top100-Generator>
250  
251 For other issues, contact the author.
252  
253 =head1 AUTHOR
254  
255 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
256  
257 =head1 COPYRIGHT
258  
259 Copyright 2009 - 2010 Adam Kennedy.
260  
261 This program is free software; you can redistribute
262 it and/or modify it under the same terms as Perl itself.
263  
264 The full text of the license can be found in the
265 LICENSE file included with this module.
266  
267 =cut
↑ Summary