-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
pg_downloads.sql
225 lines (212 loc) · 10.5 KB
/
pg_downloads.sql
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
-- model_enclosures:purge/3
CREATE OR REPLACE FUNCTION purge_download(
"d_user" TEXT,
"d_slug" TEXT,
"d_name" TEXT
) RETURNS void AS $$
DECLARE
"d_enclosure" TEXT;
BEGIN
SELECT "url" INTO d_enclosure
FROM enclosure_torrents
JOIN torrents USING (info_hash)
JOIN enclosures USING (url)
JOIN user_feeds USING (feed)
WHERE "user"=d_user AND "slug"=d_slug AND "name"=d_name;
DELETE FROM enclosure_torrents WHERE "url"=d_enclosure;
END;
$$ LANGUAGE plpgsql;
CREATE TYPE download AS (
"user" TEXT,
"slug" TEXT,
"feed" TEXT,
"item" TEXT,
"enclosure" TEXT,
"feed_title" TEXT,
"feed_public" BOOL,
"info_hash" BYTEA,
"name" TEXT,
"size" BIGINT,
"type" TEXT,
"title" TEXT,
"lang" TEXT,
"summary" TEXT,
"published" TIMESTAMP,
"homepage" TEXT,
"payment" TEXT,
"image" TEXT,
"downloaded" BIGINT
);
-- TODO: rm dups
CREATE OR REPLACE FUNCTION get_most_downloaded(
INT, INT, INT
) RETURNS SETOF download AS $$
SELECT user_feeds."user", user_feeds."slug", user_feeds."feed",
enclosures.item, enclosures.url AS enclosure,
COALESCE(user_feeds.title, feeds.title) AS feed_title, user_feeds."public" AS feed_public,
torrents.info_hash, torrents.name, torrents.size, enclosures.type,
feed_items.title, feed_items.lang, feed_items.summary, feed_items.published, feed_items.homepage, feed_items.payment, feed_items.image,
COALESCE(downloaded_stats.downloaded, 0) AS "downloaded"
FROM (SELECT info_hash, downloaded
FROM downloaded_stats
ORDER BY (CASE WHEN $3 <= 1 THEN downloaded1
WHEN $3 <= 7 THEN downloaded7
WHEN $3 <= 30 THEN downloaded30
ELSE downloaded
END) DESC
LIMIT $1 OFFSET $2
) AS downloaded_stats
JOIN torrents USING (info_hash)
JOIN enclosure_torrents USING (info_hash)
JOIN enclosures USING (url)
JOIN feed_items ON (enclosures.feed=feed_items.feed AND enclosures.item=feed_items.id)
JOIN feeds ON (feed_items.feed=feeds.url)
JOIN user_feeds ON (feed_items.feed=user_feeds.feed)
WHERE user_feeds."public";
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION get_torrent_download(
BYTEA
) RETURNS SETOF download AS $$
SELECT *
FROM (SELECT user_feeds."user", user_feeds."slug", user_feeds."feed",
enclosures.item, enclosures.url AS enclosure,
COALESCE(user_feeds.title, feeds.title) AS feed_title, user_feeds."public" AS feed_public,
torrents.info_hash, torrents.name, torrents.size, enclosures.type,
feed_items.title, feed_items.lang, feed_items.summary, feed_items.published, feed_items.homepage, feed_items.payment, feed_items.image,
COALESCE(downloaded_stats.downloaded, 0) AS "downloaded"
FROM torrents
LEFT JOIN downloaded_stats USING (info_hash)
JOIN enclosure_torrents USING (info_hash)
JOIN enclosures USING (url)
JOIN feed_items ON (enclosures.feed=feed_items.feed AND enclosures.item=feed_items.id)
JOIN feeds ON (feed_items.feed=feeds.url)
JOIN user_feeds ON (feed_items.feed=user_feeds.feed)
WHERE torrents.info_hash=$1
AND user_feeds."public"
) AS s
ORDER BY downloaded DESC;
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION get_recent_downloads(
INT, INT
) RETURNS SETOF download AS $$
SELECT *
FROM (SELECT user_feeds."user", user_feeds."slug", user_feeds."feed",
enclosures.item, enclosures.url AS enclosure,
COALESCE(user_feeds.title, feeds.title) AS feed_title, user_feeds."public" AS feed_public,
torrents.info_hash, torrents.name, torrents.size, enclosures.type,
feed_items.title, feed_items.lang, feed_items.summary, feed_items.published, feed_items.homepage, feed_items.payment, feed_items.image,
COALESCE(downloaded_stats.downloaded, 0) AS "downloaded"
FROM (SELECT feed, id, title, lang, summary, published, homepage, payment, image
FROM feed_items
ORDER BY published DESC
LIMIT $1 OFFSET $2
) AS feed_items
JOIN enclosures ON (feed_items.feed=enclosures.feed AND feed_items.id=enclosures.item)
JOIN enclosure_torrents ON (enclosures.url=enclosure_torrents.url)
JOIN torrents ON (enclosure_torrents.info_hash=torrents.info_hash)
JOIN feeds ON (feed_items.feed=feeds.url)
JOIN user_feeds ON (feed_items.feed=user_feeds.feed)
LEFT JOIN downloaded_stats ON (enclosure_torrents.info_hash=downloaded_stats.info_hash)
WHERE user_feeds."public"
) AS s
ORDER BY published DESC;
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION get_recent_downloads(
INT, INT, TEXT
) RETURNS SETOF download AS $$
SELECT *
FROM (SELECT user_feeds."user", user_feeds."slug", user_feeds."feed",
enclosures.item, enclosures.url AS enclosure,
COALESCE(user_feeds.title, feeds.title) AS feed_title, user_feeds."public" AS feed_public,
torrents.info_hash, torrents.name, torrents.size, enclosures.type,
feed_items.title, feed_items.lang, feed_items.summary, feed_items.published, feed_items.homepage, feed_items.payment, feed_items.image,
COALESCE(downloaded_stats.downloaded, 0) AS "downloaded"
FROM (SELECT feed, id, title, lang, summary, published, homepage, payment, image
FROM feed_items
WHERE feed=$3
ORDER BY published DESC
LIMIT $1 OFFSET $2
) AS feed_items
JOIN enclosures ON (feed_items.feed=enclosures.feed AND feed_items.id=enclosures.item)
JOIN enclosure_torrents ON (enclosures.url=enclosure_torrents.url)
JOIN torrents ON (enclosure_torrents.info_hash=torrents.info_hash)
JOIN feeds ON (feed_items.feed=feeds.url)
JOIN user_feeds ON (feed_items.feed=user_feeds.feed)
LEFT JOIN downloaded_stats ON (enclosure_torrents.info_hash=downloaded_stats.info_hash)
) AS s
ORDER BY published DESC;
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION get_user_recent_downloads(
INT, INT, TEXT
) RETURNS SETOF download AS $$
SELECT *
FROM (SELECT user_feeds."user", user_feeds."slug", user_feeds."feed",
enclosures.item, enclosures.url AS enclosure,
COALESCE(user_feeds.title, feeds.title) AS feed_title, user_feeds."public" AS feed_public,
torrents.info_hash, torrents.name, torrents.size, enclosures.type,
feed_items.title, feed_items.lang, feed_items.summary, feed_items.published, feed_items.homepage, feed_items.payment, feed_items.image,
COALESCE(downloaded_stats.downloaded, 0) AS "downloaded"
FROM (SELECT feed, id, title, lang, summary, published, homepage, payment, image
FROM feed_items
WHERE feed IN (SELECT feed FROM user_feeds WHERE "user"=$3)
ORDER BY published DESC
LIMIT $1 OFFSET $2
) AS feed_items
JOIN enclosures ON (feed_items.feed=enclosures.feed AND feed_items.id=enclosures.item)
JOIN enclosure_torrents ON (enclosures.url=enclosure_torrents.url)
JOIN torrents ON (enclosure_torrents.info_hash=torrents.info_hash)
JOIN feeds ON (feed_items.feed=feeds.url)
JOIN user_feeds ON (feed_items.feed=user_feeds.feed)
LEFT JOIN downloaded_stats ON (enclosure_torrents.info_hash=downloaded_stats.info_hash)
WHERE user_feeds."public"
) AS s
ORDER BY published DESC;
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION get_enclosure_downloads(
TEXT
) RETURNS SETOF download AS $$
SELECT user_feeds."user", user_feeds."slug", user_feeds."feed",
enclosures.item, enclosures.url AS enclosure,
COALESCE(user_feeds.title, feeds.title) AS feed_title, user_feeds."public" AS feed_public,
torrents.info_hash, torrents.name, torrents.size, enclosures.type,
feed_items.title, feed_items.lang, feed_items.summary, feed_items.published, feed_items.homepage, feed_items.payment, feed_items.image,
COALESCE(downloaded_stats.downloaded, 0) AS "downloaded"
FROM (SELECT url, info_hash FROM enclosure_torrents
WHERE url=$1 AND LENGTH(info_hash)=20
LIMIT 100) AS enclosure_torrents
JOIN torrents USING (info_hash)
JOIN enclosures USING (url)
JOIN feed_items ON (enclosures.feed=feed_items.feed AND enclosures.item=feed_items.id)
JOIN feeds ON (feed_items.feed=feeds.url)
JOIN user_feeds ON (feed_items.feed=user_feeds.feed)
LEFT JOIN downloaded_stats ON (enclosure_torrents.info_hash=downloaded_stats.info_hash);
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION get_guid_downloads(
TEXT
) RETURNS SETOF download AS $$
SELECT user_feeds."user", user_feeds."slug", user_feeds."feed",
enclosures.item, enclosures.url AS enclosure,
COALESCE(user_feeds.title, feeds.title) AS feed_title, user_feeds."public" AS feed_public,
torrents.info_hash, torrents.name, torrents.size, enclosures.type,
feed_items.title, feed_items.lang, feed_items.summary, feed_items.published, feed_items.homepage, feed_items.payment, feed_items.image,
COALESCE(downloaded_stats.downloaded, 0) AS "downloaded"
FROM (SELECT feed, item, url, type FROM enclosures
WHERE guid=$1
LIMIT 100) AS enclosures
JOIN enclosure_torrents USING (url)
JOIN torrents USING (info_hash)
JOIN feed_items ON (enclosures.feed=feed_items.feed AND enclosures.item=feed_items.id)
JOIN feeds ON (feed_items.feed=feeds.url)
JOIN user_feeds ON (feed_items.feed=user_feeds.feed)
LEFT JOIN downloaded_stats ON (enclosure_torrents.info_hash=downloaded_stats.info_hash)
WHERE LENGTH(enclosure_torrents.info_hash)=20;
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION get_torrent_guids(
BYTEA
) RETURNS SETOF TEXT AS $$
SELECT DISTINCT enclosures.guid AS "guid"
FROM enclosure_torrents
JOIN enclosures USING (url)
WHERE enclosure_torrents.info_hash=$1
AND enclosures.guid IS NOT NULL
$$ LANGUAGE SQL;