summaryrefslogtreecommitdiff
path: root/lib/Multi/Feed.pm
blob: c97d87d8f78fe458432f1ac877045875d34b21cf (plain)
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

#
#  Multi::Feed  -  Generates and updates Atom feeds
#

package Multi::Feed;

use strict;
use warnings;
use POE;
use XML::Writer;
use POSIX 'strftime';
use Time::HiRes 'time';
use VNDBUtil 'bb2html';


sub spawn {
  my $p = shift;
  POE::Session->create(
    package_states => [
      $p => [qw| _start shutdown generate write_atom log_stats |],
    ],
    heap => {
      regenerate_interval => 900, # 15 min.
      stats_interval => 86400, # daily
      debug => 0,
      @_,
      stats => {}, # key = feed, value = [ count, total, max ]
    },
  );
}


sub _start {
  $_[KERNEL]->alias_set('feed');
  $_[KERNEL]->yield('generate');
  $_[KERNEL]->alarm(log_stats => int((time+3)/$_[HEAP]{stats_interval}+1)*$_[HEAP]{stats_interval});
  $_[KERNEL]->sig(shutdown => 'shutdown');
}


sub shutdown {
  $_[KERNEL]->delay('generate');
  $_[KERNEL]->delay('log_stats');
  $_[KERNEL]->alias_remove('feed');
}


sub generate {
  $_[KERNEL]->alarm(generate => int((time+3)/$_[HEAP]{regenerate_interval}+1)*$_[HEAP]{regenerate_interval});

  # announcements
  $_[KERNEL]->post(pg => query => q{
    SELECT '/t'||t.id AS id, t.title, extract('epoch' from tp.date) AS published,
       extract('epoch' from tp.edited) AS updated, u.username, u.id AS uid, tp.msg AS summary
     FROM threads t
     JOIN threads_posts tp ON tp.tid = t.id AND tp.num = 1
     JOIN threads_boards tb ON tb.tid = t.id AND tb.type = 'an'
     JOIN users u ON u.id = tp.uid
    WHERE NOT t.hidden
    ORDER BY t.id DESC
    LIMIT ?}, [ $VNDB::S{atom_feeds}{announcements}[0] ], 'write_atom', 'announcements'
  );

  # changes
  $_[KERNEL]->post(pg => query => q{
    SELECT '/'||c.type||COALESCE(vr.vid, rr.rid, pr.pid)||'.'||c.rev AS id,
       COALESCE(vr.title, rr.title, pr.name) AS title, extract('epoch' from c.added) AS updated,
       u.username, u.id AS uid, c.comments AS summary
    FROM changes c
     LEFT JOIN vn_rev vr ON c.type = 'v' AND c.id = vr.id
     LEFT JOIN releases_rev rr ON c.type = 'r' AND c.id = rr.id
     LEFT JOIN producers_rev pr ON c.type = 'p' AND c.id = pr.id
     JOIN users u ON u.id = c.requester
    WHERE c.requester <> 1
    ORDER BY c.id DESC
    LIMIT ?}, [ $VNDB::S{atom_feeds}{changes}[0] ], 'write_atom', 'changes'
  );

  # posts (this query isn't all that fast)
  $_[KERNEL]->post(pg => query => q{
    SELECT '/t'||t.id||'.'||tp.num AS id, t.title||' (#'||tp.num||')' AS title, extract('epoch' from tp.date) AS published,
       extract('epoch' from tp.edited) AS updated, u.username, u.id AS uid, tp.msg AS summary
     FROM threads_posts tp
     JOIN threads t ON t.id = tp.tid
     JOIN users u ON u.id = tp.uid
    WHERE NOT tp.hidden AND NOT t.hidden
    ORDER BY tp.date DESC
    LIMIT ?}, [ $VNDB::S{atom_feeds}{posts}[0] ], 'write_atom', 'posts'
  );
}


sub write_atom { # num, res, feed, time
  my $r = $_[ARG1];
  my $feed = $_[ARG2];

  my $start = time;

  my $updated = 0;
  for(@$r) {
    $updated = $_->{published} if $_->{published} && $_->{published} > $updated;
    $updated = $_->{updated} if $_->{updated} && $_->{updated} > $updated;
  }

  my $data;
  my $x = XML::Writer->new(OUTPUT => \$data, DATA_MODE => 1, DATA_INDENT => 2);
  $x->xmlDecl('UTF-8');
  $x->startTag(feed => xmlns => 'http://www.w3.org/2005/Atom', 'xml:lang' => 'en', 'xml:base' => $VNDB::S{url}.'/');
  $x->dataElement(title => $VNDB::S{atom_feeds}{$feed}[1]);
  $x->dataElement(updated => datetime($updated));
  $x->dataElement(id => $VNDB::S{url}.$VNDB::S{atom_feeds}{$feed}[2]);
  $x->emptyTag(link => rel => 'self', type => 'application/atom+xml', href => "$VNDB::S{url}/feeds/$feed.atom");
  $x->emptyTag(link => rel => 'alternate', type => 'text/html', href => $VNDB::S{atom_feeds}{$feed}[2]);

  for(@$r) {
    $x->startTag('entry');
    $x->dataElement(id => $VNDB::S{url}.$_->{id});
    $x->dataElement(title => $_->{title});
    $x->dataElement(updated => $_->{updated}?datetime($_->{updated}):datetime($_->{published}));
    $x->dataElement(published => datetime($_->{published})) if $_->{published};
    if($_->{username}) {
      $x->startTag('author');
      $x->dataElement(name => $_->{username});
      $x->dataElement(uri => '/u'.$_->{uid}) if $_->{uid};
      $x->endTag('author');
    }
    $x->emptyTag(link => rel => 'alternate', type => 'text/html', href => $_->{id});
    $x->dataElement(summary => bb2html($_->{summary}, 200), type => 'html') if $_->{summary};
    $x->endTag('entry');
  }

  $x->endTag('feed');

  open my $f, '>:utf8', "$VNDB::ROOT/www/feeds/$feed.atom" || die $!;
  print $f $data;
  close $f;

  $_[HEAP]{debug} && $_[KERNEL]->call(core => log => 'Wrote %s.atom (%d entries, sql:%4dms, perl:%4dms)',
    $feed, scalar(@$r), $_[ARG3]*1000, (time-$start)*1000);

  $_[HEAP]{stats}{$feed} = [ 0, 0, 0 ] if !$_[HEAP]{stats}{$feed};
  my $time = ((time-$start)+$_[ARG3])*1000;
  $_[HEAP]{stats}{$feed}[0]++;
  $_[HEAP]{stats}{$feed}[1] += $time;
  $_[HEAP]{stats}{$feed}[2] = $time if $_[HEAP]{stats}{$feed}[2] < $time;
}


sub log_stats {
  $_[KERNEL]->alarm(log_stats => int((time+3)/$_[HEAP]{stats_interval}+1)*$_[HEAP]{stats_interval});

  for (keys %{$_[HEAP]{stats}}) {
    my $v = $_[HEAP]{stats}{$_};
    next if !$v->[0];
    $_[KERNEL]->call(core => log => 'Stats summary for %s.atom: total:%5dms, avg:%4dms, max:%4dms, size: %.1fkB',
      $_, $v->[1], $v->[1]/$v->[0], $v->[2], (-s "$VNDB::ROOT/www/feeds/$_.atom")/1024);
  }
  $_[HEAP]{stats} = {};
}


# non-POE helper function
sub datetime {
  strftime('%Y-%m-%dT%H:%M:%SZ', gmtime shift);
}


1;