-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrefill-cpanstatsdb.pl
executable file
·281 lines (233 loc) · 7.55 KB
/
refill-cpanstatsdb.pl
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
#!/usr/local/bin/perl
# use 5.010;
use strict;
use warnings;
=head1 NAME
=head1 SYNOPSIS
~/src/installed-perls/v5.16.0/4e6d/bin/perl bin/refill-cpanstatsdb.pl
=head1 OPTIONS
=over 8
=cut
my @opt = <<'=back' =~ /B<--(\S+)>/g;
=item B<--help|h!>
This help
=item B<--quiet>
Be less chatty
=item B<--veryquiet>
Be silent
=item B<--finishlimit=i>
A query that yields a result with less rows than this number is the
signal to refrain from further refill queries and finish this program.
Defaults to 0 which means other limits are needed to stop this
program.
Note: before we invented the sleep parameters, this was the way how we
stopped the program. Probably not needed anymore.
=item B<--maxins=i>
No default, which means no limit. Maximum number of records to inject.
If set to zero, we test the surroundings, then exit.
=item B<--maxtime=i>
Maximum time in seconds this program should run. Defaults to 1770. If
set to zero, no limit.
=item B<--sleeplimit=i>
A query that yields a result with less rows than this number is the
signal to sleep for $Opt{sleeptime} seconds before querying again.
Defaults to 500. Do not set it too low, it would produce an annoying
amount of logfiles.
=item B<--sleeptime=i>
For how long to sleep in the case of $Opt{sleeplimit} undercut.
Defaults to 150 seconds.
=back
=head1 DESCRIPTION
Replacement for the job that downloaded the whole cpanstats.db and
gunzipped it.
Now we simply repeatedly fetch the descriptions for the next 2500
reports until the supply dries out. Thus we reach a new max, write all
the stuff to the db and let the other jobs work from there.
=head1 TODO
remove unneeded data, maybe split them out.
=head1 SEE ALSO
refill-cpanstatsdb-minutes.pl
=cut
use FindBin;
use lib "$FindBin::Bin/lib";
use IPC::ConcurrencyLimit;
use Dumpvalue;
use File::Basename ();
use File::Path ();
use File::Spec;
use File::Temp;
use Getopt::Long;
use Pod::Usage;
use Hash::Util qw(lock_keys);
use List::Util qw(min);
our %Opt;
lock_keys %Opt, map { /([^=|!]+)/ } @opt;
GetOptions(\%Opt,
@opt,
) or pod2usage(1);
if ($Opt{help}) {
pod2usage(0);
}
my $verbosity = 2;
$verbosity = 1 if($Opt{quiet});
$verbosity = 0 if($Opt{veryquiet});
$Opt{finishlimit} ||= 0;
$Opt{sleeplimit} ||= 500;
$Opt{sleeptime} ||= 150;
$Opt{maxtime} = 1770 unless defined $Opt{maxtime};
sub concurrency_limit {
my $lockfile = shift;
my $limit = IPC::ConcurrencyLimit->new(
max_procs => 1,
path => $lockfile,
);
my $limitid = $limit->get_lock;
if(not $limitid) {
warn "Another process appears to be still running. Exiting.";
exit(0);
}
return $limit;
}
my $limit = concurrency_limit("/tmp/refill-testers-db/lock");
use DBI;
use Time::HiRes qw(time);
use JSON::XS ();
use List::Util qw(max);
use CPAN::Testers::WWW::Reports::Query::Reports;
our $jsonxs = JSON::XS->new->indent(0);
my($sth,$current_max_id);
{
my $dbh = DBI->connect("dbi:mysql:dbname=cpantesters", 'root', '') or die "Could not connect to 'cpantesters': $DBI::err";
my $sql = "select max(id) from cpanstats";
$sth = $dbh->prepare($sql);
{
my $rv = eval { $sth->execute(); };
unless ($rv) {
my $err = $sth->errstr;
die "Warning: error occurred while executing '$sql': $err";
}
}
my(@row) = $sth->fetchrow_array();
$current_max_id = $row[0] || 0;
print "$0: INFO: In cpantesters db found max id '$current_max_id'\n" if($verbosity == 2);
$sql = "INSERT INTO cpanstats
(id,guid,state,dist,version,platform,perl,osname,osvers) values
(?, ?, ?, ?, ?, ?, ?, ?, ?)";
$sth = $dbh->prepare($sql);
}
my $query = CPAN::Testers::WWW::Reports::Query::Reports->new;
my $nextid;
$nextid = $current_max_id+1;
my($inscount) = 0;
my($queries_n,$queries_time) = (0,0);
QUERY: while () {
print sprintf "$0: %s: Next query starting with %s\n", scalar gmtime(), $nextid if($verbosity == 2);
my $result = $query->range("$nextid-");
my $querycnt = keys %$result;
my $thismax = $querycnt > 0 ? max(keys %$result) : undef;
print sprintf "$0: %s: Got %d records from '%s' to '%s'\n", scalar gmtime(), $querycnt, $nextid, $thismax||"<UNDEF>" if($verbosity == 2);
if (defined($Opt{maxins}) && $Opt{maxins} <= 0) {
last QUERY;
}
if ( $Opt{finishlimit} && $querycnt < $Opt{finishlimit}) {
last QUERY;
}
unless ($thismax){
if ($Opt{maxtime} && time+$Opt{sleeptime}-$^T >= $Opt{maxtime}) {
last QUERY;
} else {
sleep $Opt{sleeptime};
next QUERY;
}
}
# so we have some work to do
my @gmtime = gmtime;
my $logfile = sprintf
(
"%s/var/refill-cpanstatsdb/%04d/%02d/%04d%02d%02dT%02d%02d-%d-MAX.json.gz",
$ENV{HOME},
1900+$gmtime[5],
1+$gmtime[4],
1900+$gmtime[5],
1+$gmtime[4],
@gmtime[3,2,1],
$nextid,
);
File::Path::mkpath File::Basename::dirname $logfile;
if (-e $logfile) {
die "ALERT: found '$logfile', will not overwrite it";
}
open my $fh, "|-", "gzip -9c > $logfile" or die "Could not open gzip to '$logfile': $!";
my $next_log = time + 60;
# dist => "Attribute-Overload",
# fulldate => 201205262229,
# guid => "4454e538-a782-11e1-802a-3db30df65b4f",
# id => 22285792,
# osname => "linux",
# osvers => "2.6.18-1.2798.fc6",
# perl => "5.16.0 RC0",
# platform => "i686-linux-thread-multi-64int-ld",
# postdate => 201205,
# state => "fail",
# tester => "Khen1950fx\@aol.com",
# type => 2,
# version => "1.100710",
my $i = 0;
my $max_seen;
REC: for my $id (sort {$a <=> $b} keys %$result) {
if (defined($Opt{maxins}) && $inscount >= $Opt{maxins}) {
last REC;
}
if ($Opt{maxtime} && time-$^T >= $Opt{maxtime}) {
last REC;
}
$max_seen = $id;
my $record = $result->{$id};
if ($id > $current_max_id) {
my $start = time;
$sth->execute($id,@{$record}{qw(guid state dist version platform perl osname osvers)});
$queries_n++;
$queries_time += time - $start;
}
# ddx $record; # see also Data::Dump line
print $fh $jsonxs->encode($record), "\n";
$i++;
if (time >= $next_log) {
print sprintf "$0: %s: %d records inserted\n", scalar gmtime(), $i if($verbosity == 2);
$next_log += 60;
}
$inscount++;
}
close $fh or die "Could not close gzip to '$logfile': $!";
my $finallogfile = $logfile;
unless ($max_seen) {
$max_seen = $nextid - 1;
}
$finallogfile =~ s/MAX/$max_seen/;
rename $logfile, $finallogfile or die "Could not rename $logfile, $finallogfile: $!";
if (defined($Opt{maxins}) && $inscount >= $Opt{maxins}) {
last QUERY;
}
my $sleeptime = 0;
if ( $Opt{sleeplimit} && $querycnt < $Opt{sleeplimit} ) {
$sleeptime = $Opt{sleeptime};
}
if ($Opt{maxtime} && time+$sleeptime-$^T >= $Opt{maxtime}) {
last QUERY;
}
if ($sleeptime) {
sleep $sleeptime;
}
$nextid = $thismax+1;
}
if ($queries_n) {
print sprintf "$0: STATS: %s inserts, avg ins time per rec %.5f\n", $queries_n, $queries_time/$queries_n if($verbosity != 0);
}
# for the record: today I added the two:
# CREATE INDEX ixdist ON cpanstats (dist); # took ca 30 minutes
# CREATE INDEX ixtypestate ON cpanstats (type, state);
# DROP INDEX ixvers;
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# End: