forked from barbie/cpan-testers-www-statistics
/
Statistics.pm
352 lines (250 loc) · 9.76 KB
/
Statistics.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package CPAN::Testers::WWW::Statistics;
use 5.006; #due to 'warnings' pragma
use warnings;
use strict;
use vars qw($VERSION);
$VERSION = '0.89';
#----------------------------------------------------------------------------
=head1 NAME
CPAN::Testers::WWW::Statistics - CPAN Testers Statistics website.
=head1 DESCRIPTION
CPAN Testers Statistics comprises the actual website pages, a CGI tool to find
testers, and some backend code to help map tester address to a real identity.
=cut
# -------------------------------------
# Library Modules
use base qw(Class::Accessor::Fast);
use Config::IniFiles;
use CPAN::Testers::Common::DBUtils;
use File::Basename;
use File::Path;
use IO::File;
use CPAN::Testers::WWW::Statistics::Pages;
use CPAN::Testers::WWW::Statistics::Graphs;
# -------------------------------------
# Public Methods
=head1 INTERFACE
=head2 The Constructor
=over 4
=item * new
Statistics creation object. Provides all the configuration and logging
functionality, as well the interface to the lower level functionality for Page
and Graph creation.
new() takes an option hash as an argument, which may contain the following
keys.
config => path to configuration file [required]
directory => path to output directory
mainstore => path to main data storage file
leadstore => path to leaderboard data storage file
templates => path to templates directory
database => path to SQLite database file
address => path to address file
builder => path to output file from builder log parser
logfile => path to logfile
logclean => will overwrite any existing logfile if set
Note that while 'directory', 'templates', 'database' and 'address' are optional
as parameters, if they are not provided as parameters, then they MUST be
specified within the 'MASTER' section of the configuration file.
=back
=cut
sub _alarm_handler () { return; }
sub new {
my $class = shift;
my %hash = @_;
my $self = {};
bless $self, $class;
# ensure we have a configuration file
die "Must specify the configuration file\n" unless( $hash{config});
die "Configuration file [$hash{config}] not found\n" unless(-f $hash{config});
# load configuration file
my $cfg;
local $SIG{'__WARN__'} = \&_alarm_handler;
eval { $cfg = Config::IniFiles->new( -file => $hash{config} ); };
die "Cannot load configuration file [$hash{config}]\n" unless($cfg && !$@);
$self->{cfg} = $cfg;
# configure databases
for my $db (qw(CPANSTATS)) {
die "No configuration for $db database\n" unless($cfg->SectionExists($db));
my %opts = map {my $v = $cfg->val($db,$_); defined($v) ? ($_ => $v) : () }
qw(driver database dbfile dbhost dbport dbuser dbpass);
$self->{$db} = CPAN::Testers::Common::DBUtils->new(%opts);
die "Cannot configure $db database\n" unless($self->{$db});
}
my %OSNAMES;
my @rows = $self->{CPANSTATS}->get_query('array',q{SELECT osname,ostitle FROM osname ORDER BY id});
for my $row (@rows) {
$OSNAMES{lc $row->[0]} ||= $row->[1];
}
$self->osnames( \%OSNAMES );
my @TOCOPY = split("\n", $cfg->val('TOCOPY','LIST'));
$self->tocopy(\@TOCOPY);
$self->mainstore(_defined_or( $hash{mainstore}, $cfg->val('MASTER','mainstore') ));
$self->leadstore(_defined_or( $hash{leadstore}, $cfg->val('MASTER','leadstore') ));
$self->templates(_defined_or( $hash{templates}, $cfg->val('MASTER','templates') ));
$self->database( _defined_or( $hash{database}, $cfg->val('MASTER','database' ) ));
$self->address( _defined_or( $hash{address}, $cfg->val('MASTER','address' ) ));
$self->logfile( _defined_or( $hash{logfile}, $cfg->val('MASTER','logfile' ) ));
$self->logclean( _defined_or( $hash{logclean}, $cfg->val('MASTER','logclean' ), 0 ));
$self->directory(_defined_or( $hash{directory}, $cfg->val('MASTER','directory') ));
$self->copyright( $cfg->val('MASTER','copyright') );
$self->builder( _defined_or( $hash{builder}, $cfg->val('MASTER','builder' ) ));
$self->_log("mainstore=".($self->mainstore || ''));
$self->_log("leadstore=".($self->leadstore || ''));
$self->_log("templates=".($self->templates || ''));
$self->_log("database =".($self->database || ''));
$self->_log("address =".($self->address || ''));
$self->_log("logfile =".($self->logfile || ''));
$self->_log("logclean =".($self->logclean || ''));
$self->_log("directory=".($self->directory || ''));
$self->_log("builder =".($self->builder || ''));
die "Must specify the output directory\n" unless($self->directory);
die "Must specify the template directory\n" unless($self->templates);
return $self;
}
=head2 Public Methods
=over 4
=item * make_pages
Method to manage the data update and creation of all the statistics web pages.
Note that this method incorporate all of the method functionality of update,
make_basics, make_matrix and make_stats.
=item * update
Method to manage the data update only.
=item * make_basics
Method to manage the creation of the basic statistics web pages.
=item * make_matrix
Method to manage the creation of the matrix style statistics web pages.
=item * make_stats
Method to manage the creation of the tabular style statistics web pages.
=item * make_leaders
Method to manage the creation of the OS leaderboard web pages.
=item * make_graphs
Method to manage the creation of all the statistics graphs.
=item * ranges
Returns the specific date range array reference, as held in the configuration
file.
=item * osname
Returns the print form of a recorded OS name.
=back
=cut
__PACKAGE__->mk_accessors(
qw( directory mainstore leadstore templates database address builder
logfile logclean copyright tocopy osnames));
sub make_pages {
my $self = shift;
$self->_check_files();
my $stats = CPAN::Testers::WWW::Statistics::Pages->new(parent => $self);
$stats->update_full();
}
sub update {
my $self = shift;
$self->_check_files();
my $stats = CPAN::Testers::WWW::Statistics::Pages->new(parent => $self);
$stats->update_data();
}
sub make_basics {
my $self = shift;
$self->_check_files();
my $stats = CPAN::Testers::WWW::Statistics::Pages->new(parent => $self);
$stats->build_basics();
}
sub make_matrix {
my $self = shift;
$self->_check_files();
my $stats = CPAN::Testers::WWW::Statistics::Pages->new(parent => $self);
$stats->build_matrices();
}
sub make_stats {
my $self = shift;
$self->_check_files();
my $stats = CPAN::Testers::WWW::Statistics::Pages->new(parent => $self);
$stats->build_stats();
}
sub make_leaders {
my $self = shift;
$self->_check_files();
my $stats = CPAN::Testers::WWW::Statistics::Pages->new(parent => $self);
$stats->build_leaders();
}
sub make_graphs {
my $self = shift;
my $stats = CPAN::Testers::WWW::Statistics::Graphs->new(parent => $self);
$stats->create();
}
sub ranges {
my ($self,$section) = @_;
return unless($section);
my @now = localtime(time);
if($now[4]==0) { $now[5]--; $now[4]=11; }
my $now = sprintf "%04d%02d", $now[5]+1900, $now[4]+1;
my @RANGES;
if($section eq 'NONE') {
@RANGES = ('00000000-99999999');
} else {
my @ranges = split("\n", $self->{cfg}->val($section,'LIST'));
for my $range (@ranges) {
my ($fdate,$tdate) = split('-',$range,2);
next if($fdate > $now);
$tdate = $now if($tdate > $now);
push @RANGES, "$fdate-$tdate";
}
}
return \@RANGES;
}
sub osname {
my ($self,$name) = @_;
my $osnames = $self->osnames();
return $osnames->{lc $name} || $name;
}
# -------------------------------------
# Private Methods
sub _check_files {
my $self = shift;
die "Template directory not found\n" unless(-d $self->templates);
die "Must specify the path of the SQL database\n" unless( $self->database);
die "Archive SQLite database not found\n" unless(-f $self->database);
die "Must specify the path of the address file\n" unless( $self->address);
die "Address file not found\n" unless(-f $self->address);
}
sub _log {
my $self = shift;
my $log = $self->logfile or return;
mkpath(dirname($log)) unless(-f $log);
my $mode = $self->logclean ? 'w+' : 'a+';
$self->logclean(0);
my @dt = localtime(time);
my $dt = sprintf "%04d/%02d/%02d %02d:%02d:%02d", $dt[5]+1900,$dt[4]+1,$dt[3],$dt[2],$dt[1],$dt[0];
my $fh = IO::File->new($log,$mode) or die "Cannot write to log file [$log]: $!\n";
print $fh "$dt ", @_, "\n";
$fh->close;
}
sub _defined_or {
while(@_) {
my $value = shift;
return $value if(defined $value);
}
return;
}
q("I am NOT a number!");
__END__
=head1 BUGS, PATCHES & FIXES
There are no known bugs at the time of this release. However, if you spot a
bug or are experiencing difficulties, that is not explained within the POD
documentation, please send bug reports and patches to the RT Queue (see below).
Fixes are dependant upon their severity and my availablity. Should a fix not
be forthcoming, please feel free to (politely) remind me.
RT Queue -
http://rt.cpan.org/Public/Dist/Display.html?Name=CPAN-Testers-WWW-Statistics
=head1 SEE ALSO
L<CPAN::Testers::Data::Generator>,
L<CPAN::WWW::Testers>
F<http://www.cpantesters.org/>,
F<http://stats.cpantesters.org/>,
F<http://wiki.cpantesters.org/>
=head1 AUTHOR
Barbie, <barbie@cpan.org>
for Miss Barbell Productions <http://www.missbarbell.co.uk>.
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2005-2011 Barbie for Miss Barbell Productions.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=cut