summaryrefslogtreecommitdiff
path: root/lib/Multi/APIDump.pm
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2014-08-21 09:29:55 +0200
committerYorhel <git@yorhel.nl>2014-08-21 09:29:55 +0200
commita49faf37b12a326f232420089b2557ca957b2f43 (patch)
tree9f3bb3481959c8d5fa6796ae8acb67690a2d5f62 /lib/Multi/APIDump.pm
parentffd93bc792f7bb43f3a4bb054b402d90f309e094 (diff)
API dump: Add daily votes dump
Diffstat (limited to 'lib/Multi/APIDump.pm')
-rw-r--r--lib/Multi/APIDump.pm35
1 files changed, 34 insertions, 1 deletions
diff --git a/lib/Multi/APIDump.pm b/lib/Multi/APIDump.pm
index b9723e67..3441225a 100644
--- a/lib/Multi/APIDump.pm
+++ b/lib/Multi/APIDump.pm
@@ -17,12 +17,13 @@ sub spawn {
my $p = shift;
POE::Session->create(
package_states => [
- $p => [qw| _start shutdown tags_gen tags_write traits_gen traits_write writejson|],
+ $p => [qw| _start shutdown tags_gen tags_write traits_gen traits_write writejson votes_gen votes_write|],
],
heap => {
regenerate_interval => 86400, # daily min.
tagsfile => "$VNDB::ROOT/www/api/tags.json.gz",
traitsfile => "$VNDB::ROOT/www/api/traits.json.gz",
+ votesfile => "$VNDB::ROOT/www/api/votes.gz",
@_,
},
);
@@ -33,6 +34,7 @@ sub _start {
$_[KERNEL]->alias_set('apidump');
$_[KERNEL]->yield('tags_gen');
$_[KERNEL]->delay(traits_gen => 10);
+ $_[KERNEL]->delay(votes_gen => 20);
$_[KERNEL]->sig(shutdown => 'shutdown');
}
@@ -40,6 +42,7 @@ sub _start {
sub shutdown {
$_[KERNEL]->delay('tags_gen');
$_[KERNEL]->delay('traits_gen');
+ $_[KERNEL]->delay('votes_gen');
$_[KERNEL]->alias_remove('apidump');
}
@@ -113,4 +116,34 @@ sub writejson {
$file, $sqltime, $wt, (-s $file)/1024, scalar @$data);
}
+sub votes_gen {
+ $_[KERNEL]->alarm(votes_gen => int((time+3)/$_[HEAP]{regenerate_interval}+1)*$_[HEAP]{regenerate_interval});
+
+ $_[KERNEL]->post(pg => query => q{
+ SELECT vv.vid||' '||vv.uid||' '||vv.vote as l
+ FROM votes vv
+ JOIN users u ON u.id = vv.uid
+ JOIN vn v ON v.id = vv.vid
+ WHERE NOT v.hidden
+ AND NOT u.ign_votes
+ AND NOT EXISTS(SELECT 1 FROM users_prefs up WHERE up.uid = u.id AND key = 'hide_list')
+ }, undef, 'votes_write');
+}
+
+
+sub votes_write {
+ my($res, $sqltime) = @_[ARG1,ARG3];
+ my $ws = time;
+
+ my $file = $_[HEAP]{votesfile};
+ open my $f, '>:gzip:utf8', "$file~" or die "Writing $file: $!";
+ printf $f "%s\n", $_->{l} for (@$res);
+ close $f;
+ rename "$file~", $file or die "Renaming $file: $!";
+
+ my $wt = time-$ws;
+ $_[KERNEL]->call(core => log => 'Wrote %s in %.2fs query + %.2fs write, size: %.1fkB, items: %d.',
+ $file, $sqltime, $wt, (-s $file)/1024, scalar @$res);
+}
+
1;