-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfetchReleases.pl
executable file
·246 lines (196 loc) · 7.44 KB
/
fetchReleases.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
#! /usr/local/bin/perl
#---------------------------------------------------------------------
# fetchReleases.pl
# Copyright 2012 Christopher J. Madsen
#
# Get release dates from MetaCPAN and store them in the database
#---------------------------------------------------------------------
use strict;
use warnings;
use 5.010;
use DateTime ();
use DateTimeX::Seinfeld 0.02 ();
use DBI ();
use File::Temp qw(tempdir);
use Search::Elasticsearch ();
#---------------------------------------------------------------------
# Connect to the main database
my $db = DBI->connect("dbi:SQLite:dbname=seinfeld.db","","",
{ AutoCommit => 0, PrintError => 0, RaiseError => 1 });
$db->do("PRAGMA foreign_keys = ON");
#---------------------------------------------------------------------
# Elasticsearch can't efficiently sort a scrolled search.
# So, we dump the records into a temporary database and sort locally.
# (It's important to insert records by date, because the max date in
# the releases table tells us where to start the download next time.
# If something interrupts the download, we don't want to miss releases.
# Also, this way the author_num column is assigned in the order that
# people first uploaded something to CPAN.)
my $tempdir = tempdir(CLEANUP => 1);
my $tdb = DBI->connect("dbi:SQLite:dbname=$tempdir/temp.db","","",
{ AutoCommit => 0, PrintError => 0, RaiseError => 1 });
{
# Create a table in the temporary database
$tdb->do(<<'');
CREATE TABLE releases (
author TEXT NOT NULL,
filename TEXT NOT NULL,
date TIMESTAMP NOT NULL,
UNIQUE(author, filename)
)
my $addRelease = $tdb->prepare(<<'');
INSERT OR IGNORE INTO releases
(author, filename, date) VALUES (?,?, strftime('%s', ?))
my $size = 100; # records per Elasticsearch batch
# Figure out where we need to start fetching new releases
my $date = $db->selectrow_array('SELECT MAX(date) FROM releases');
if ($date) {
# subtract 15 minutes just in case something got added out of order
$date = DateTime->from_epoch(epoch => $date)
->subtract( minutes => 15 )
->iso8601 . 'Z';
} else {
$date = '1995-08-16T00:00:00.000Z'; # Happy birthday, CPAN!
}
# Start the scrolling query on MetaCPAN
my $es = Search::Elasticsearch->new(
cxn_pool => 'Static::NoPing',
nodes => 'api.metacpan.org:80',
handle_args => { agent => 'onceaweek/1 ' },
);
my $scroller = $es->scroll_helper(
index => 'v0',
type => 'release',
search_type => 'scan',
scroll => '2m',
size => $size,
body => {
fields => [qw(author archive date)],
query => { range => { date => { gte => $date } } },
},
);
# Insert records into the temporary database
while (my @hits = $scroller->next($size)) {
foreach my $hit (@hits) {
my $field = $hit->{fields};
$field->{archive} =~ s!^.*/!!; # remove directories
### say "@$field{qw(date author archive)}";
$addRelease->execute( @$field{qw(author archive date)} );
} # end foreach $hit in @hits
$tdb->commit;
sleep 2 unless $scroller->is_finished;
} # end while hits
}
#---------------------------------------------------------------------
# Prepare to transfer the records into the main db
#---------------------------------------------------------------------
sub getID
{
my ($cache, $get, $add, $name) = @_;
$cache->{$name} //= do {
my $id;
while (not defined( $id = $db->selectrow_array($get, undef, $name) )) {
$add->execute( $name );
}
$id;
};
} # end getID
#---------------------------------------------------------------------
my $getAuthor = $db->prepare(<<'');
SELECT author_num FROM authors WHERE author_id = ?
my $addAuthor = $db->prepare(<<'');
INSERT INTO authors (author_id) VALUES (?)
my $addRelease = $db->prepare(<<'');
INSERT OR IGNORE INTO releases
(author_num, filename, date) VALUES (?,?, ?)
my (%author);
my @author = (\%author, $getAuthor, $addAuthor);
#---------------------------------------------------------------------
# Transfer release data from temporary db to main db:
{
my $getReleases = $tdb->prepare(<<'');
SELECT author, filename, date FROM releases ORDER BY date
$getReleases->execute;
while (my $row = $getReleases->fetchrow_arrayref) {
$addRelease->execute( getID(@author, $row->[0]), @$row[1,2] );
} # end while hits
$tdb->disconnect;
}
#---------------------------------------------------------------------
# Update chains for authors with new releases:
my $getReleases = $db->prepare(<<'');
SELECT date FROM releases WHERE author_num = ? AND date > ? ORDER BY date
my $getAuthorInfo = $db->prepare(<<'');
SELECT con_longest_start, con_longest_length,
con_last_start, con_last_end, con_last_length,
con_active_weeks, con_total_releases,
hist_longest_start, hist_longest_length,
hist_last_start, hist_last_end, hist_last_length,
hist_active_weeks, hist_total_releases,
last_release
FROM authors WHERE author_num = ?
my $update = $db->prepare(<<'');
UPDATE authors SET
con_longest_start = ?, con_longest_length = ?,
con_last_start = ?, con_last_end = ?, con_last_length = ?,
con_active_weeks = ?, con_total_releases = ?,
hist_longest_start = ?, hist_longest_length = ?,
hist_last_start = ?, hist_last_end = ?, hist_last_length = ?,
hist_active_weeks = ?, hist_total_releases = ?,
last_release = ?
WHERE author_num = ?
my %seinfeld = (
con => DateTimeX::Seinfeld->new(
start_date => {qw(year 2012 month 1 day 1 time_zone UTC)},
increment => { weeks => 1 },
),
hist => DateTimeX::Seinfeld->new(
start_date => {qw(year 1995 month 8 day 13 time_zone UTC)},
increment => { weeks => 1 },
),
);
my $contest_start = $seinfeld{con}->start_date;
my $updated;
for my $aNum (values %author) {
my $author = $db->selectrow_hashref($getAuthorInfo, undef, $aNum);
my $dates = $db->selectcol_arrayref($getReleases, undef,
$aNum, $author->{last_release} // 0);
next unless @$dates;
for (@$dates,
@$author{qw(con_longest_start con_last_start con_last_end
hist_longest_start hist_last_start hist_last_end
last_release)}) {
$_ = DateTime->from_epoch(epoch => $_) if defined $_;
}
my @values;
for my $type (qw(con hist)) {
my $info;
if ($author->{"${type}_last_length"}) {
$info = {
longest => {
start_period => $author->{"${type}_longest_start"},
length => $author->{"${type}_longest_length"},
},
last => {
start_period => $author->{"${type}_last_start"},
end_period => $author->{"${type}_last_end"},
end_event => $author->{last_release},
length => $author->{"${type}_last_length"},
},
marked_periods => $author->{"${type}_active_weeks"},
};
} # end if author has existing chains
$info = $seinfeld{$type}->find_chains( $dates, $info );
push @values,
$info->{longest}{start_period}->epoch, $info->{longest}{length},
$info->{last}{start_period}->epoch, $info->{last}{end_period}->epoch,
$info->{last}{length},
$info->{marked_periods}, $author->{"${type}_total_releases"} + @$dates;
push @values, $info->{last}{end_event}->epoch if $type eq 'hist';
} # end foreach $type
++$updated;
$update->execute(@values, $aNum);
} # end for each $aNum in %author
$db->commit;
$db->disconnect;
exit( $updated ? 0 : 212 );