This repository was archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathendpoints.py
More file actions
507 lines (442 loc) · 15.2 KB
/
endpoints.py
File metadata and controls
507 lines (442 loc) · 15.2 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
"""Functions linked directly to functionality called from API endpoints.
"""
from datetime import datetime
import bottle
import config
import db
import helpers
import models
def get_categories(connection,repo='all'):
"""Fetches a list of all known preprint categories.
bioRxiv separates all papers into categories (or "collections"), such
as "bioinformatics", "genomics", etc. This function lists all the ones
we've pulled from the site so far.
Arguments:
- connection: a Connection object with an active database session
Returns:
- a list of strings, one for each collection
"""
results = []
query = """
SELECT DISTINCT collection
FROM articles
WHERE collection IS NOT NULL
"""
if repo != 'all':
query += " AND repo=%s"
params = (repo,)
else:
params = ()
query += " ORDER BY collection"
categories = connection.read(query, params)
for cat in categories:
if len(cat) > 0:
results.append(cat[0])
return results
def paper_query(q, categories, timeframe, metric, page, page_size, repo, version, connection):
"""Returns a list of the most downloaded papers that meet a given set of constraints.
Arguments:
- connection: a database Connection object.
- q: A search string to compare against article abstracts,
titles and author names. (Title matches are weighted more heavily.)
- categories: A list of categories the results can be in.
- timeframe: A description of the range of dates on which to
base the rankings (i.e. "alltime" or "lastmonth")
- metric: Which article-level statistic to use when sorting results
- page: Which page of the results to display (0-indexed)
- page_size: How many entries should be returned
Returns:
- An list of Article objects that meet the search criteria, sorted by the
specified metric in descending order.
"""
# HACK: Because there are so many possible combinations for which
# parameters need to be passed to the database for this query,
# it's much easier to say that every query needs a "repo" parameter
# rather than having lots of nested options in which some of them
# are performed without any repository clause.
if repo == 'all':
repo = ['biorxiv','medrxiv']
else:
repo = [repo]
# We build two queries, 'select' and 'countselect': one to get the
# current page of results, and one to figure out the total number
# of results
select = "SELECT "
if metric == "downloads":
select += "r.downloads"
elif metric == "twitter":
select += "SUM(r.count)"
select += ", a.id, a.url, a.title, a.abstract, a.collection, a.posted, a.doi, a.repo"
countselect = "SELECT COUNT(DISTINCT a.id)"
params = (repo,)
query = ""
if q != "": # if there's a text search specified
params = (q,repo)
query += f' FROM {config.db["schema"]}.articles AS a INNER JOIN {config.db["schema"]}.'
if metric == "twitter":
query += "crossref_daily"
elif metric == "downloads":
query_times = {
"alltime": "alltime_ranks",
"ytd": "ytd_ranks",
"lastmonth": "month_ranks",
}
query += query_times[timeframe]
if metric == "twitter":
query += " AS r ON r.doi=a.doi"
elif metric == "downloads":
query += " AS r ON r.article=a.id"
if q != "":
# backwards compatibility for text search
if version == 1:
query += """, plainto_tsquery(%s) query,
coalesce(setweight(a.title_vector, 'A') || setweight(a.abstract_vector, 'C') || setweight(a.author_vector, 'D')) totalvector
"""
else:
query += """, websearch_to_tsquery(%s) query,
coalesce(setweight(a.title_vector, 'A') || setweight(a.abstract_vector, 'C') || setweight(a.author_vector, 'D')) totalvector
"""
# add a WHERE clause if we need one:
# (all-time twitter stats don't require it)
query += f" WHERE a.repo=ANY(%s)"
if metric == "downloads" or (metric == "twitter" and timeframe != "alltime") or len(categories) > 0:
query += " AND "
if metric == "downloads":
query += "r.downloads > 0"
if q != "" or len(categories) > 0:
query += " AND "
if q != "":
query += "query @@ totalvector "
if len(categories) > 0 or (metric == "twitter" and timeframe != "alltime"):
query += " AND "
if len(categories) > 0:
query += "collection=ANY(%s)"
if q != "":
params = (q,repo,categories)
else:
params = (repo,categories)
if metric == "twitter" and timeframe != "alltime":
query += " AND "
if metric == "twitter" and timeframe != "alltime":
query += "r.source_date > now() - interval "
query_times = {
"day": 2,
"week": 7,
"month": 30,
"year": 365
}
query += f"'{query_times[timeframe]} days' "
# this is the last piece of the query we need for the one
# that counts the total number of results
countselect += query
resp = connection.read(countselect, params)
total = resp[0][0]
# continue building the query to get the full list of results:
if metric == "twitter":
query += " GROUP BY a.id"
query += " ORDER BY "
if metric == "downloads":
query += "r.rank ASC"
elif metric == "twitter":
query += "SUM(r.count) DESC"
query += f" LIMIT {page_size}"
if page > 0:
query += f" OFFSET {page * page_size}"
query += ";"
select += query
result = connection.read(select, params)
results = [models.SearchResultArticle(a, connection) for a in result]
return results, total
def author_rankings(connection, category=""):
"""Fetches a list of authors with the most cumulative downloads.
Arguments:
- connection: a database Connection object.
- category: (Optionally) a single collection to base download rankings on.
Returns:
- A list of Author objects that meet the search criteria.
"""
if category == "": # all time, all categories
table = "author_ranks"
where = ""
params = ()
else:
table = "author_ranks_category"
where = "WHERE r.category=%s"
params = (category,)
query = f"""
SELECT a.id, a.name, r.rank, r.downloads, r.tie
FROM authors AS a
INNER JOIN {config.db["schema"]}.{table} r ON a.id=r.author
{where}
ORDER BY r.rank
LIMIT {config.author_ranks_limit}
"""
authors = connection.read(query, params)
return [models.SearchResultAuthor(*a) for a in authors]
def author_details(author_id, connection):
"""Returns information about a single author, including a list of
all their papers.
Arguments:
- connection: a database Connection object.
- author_id: the Rxivist-issued ID of the author being queried.
Returns:
- An Author object containing information about that
author's publications and contact info.
"""
result = models.Author(author_id)
result.GetInfo(connection)
return result
def paper_details(article_id, connection):
"""Returns information about a single paper.
Arguments:
- connection: a database Connection object.
- article_id: the ID given to the author being queried.
Returns:
- A Paper object containing details about the paper and
its authors.
"""
result = models.ArticleDetails(article_id, connection)
return result
def paper_downloads(a_id, connection):
"""Returns time-series data about how many
times a paper's webpage and PDF have been downloaded.
Arguments:
- connection: a database Connection object.
- a_id: the Rxivist-issued ID given to the paper being queried.
Returns:
- A list of months and the download stats for each month
"""
result = models.Article(a_id)
result.GetTraffic(connection)
return {
"query": {
"id": a_id
},
"results": [{"month": x.month, "year": x.year, "downloads": x.downloads, "views": x.views} for x in result.traffic]
}
def get_distribution(category, metric, connection):
"""Returns time-series data about how many
times a paper's webpage and PDF have been downloaded.
Arguments:
- connection: a database Connection object.
- a_id: the Rxivist-issued ID given to the paper being queried.
Returns:
- A list of months and the download stats for each month
"""
# "category" param can be either "author" or "paper"
# "metric" param is (right now) limited to just "downloads"
if category == "paper":
category = "alltime"
data = connection.read("SELECT bucket, count FROM download_distribution WHERE category=%s ORDER BY bucket", (category,))
results = [(entry[0], entry[1]) for entry in data]
averages = {}
for avg in ["mean", "median"]:
cat = f"{category}_{avg}"
answer = connection.read("SELECT count FROM download_distribution WHERE category=%s", (cat,))
averages[avg] = answer[0][0]
return results, averages
def top_year(year, connection):
resp = connection.read("""
SELECT SUM(t.pdf) as downloads, t.article, a.url,
a.title, a.abstract, a.collection, a.posted, a.doi
FROM article_traffic t
INNER JOIN articles a ON t.article=a.id
WHERE t.year = %s
AND a.posted >= '%s-01-01'
AND a.posted <= '%s-12-31'
GROUP BY 2,3,4,5,6,7,8
ORDER BY 1 DESC
LIMIT 25
""", (year,year,year))
if len(resp) == 0:
return []
results = [models.SearchResultArticle(a, connection) for a in resp]
return results
def summary_stats(connection, category=None):
"""Returns time-series data reflecting how many submissions and downloads
were recorded in each month.
Arguments:
- connection: a database Connection object.
- category: *tk NOOP whether to restrict the query to only a single collection.
Returns:
- A dictionary containing collections, each of which stores either the submission
or download numbers for a single month.
"""
results = {
'submissions': [],
'downloads': []
}
# Submissions:
# The reason this is so complicated is because bioRxiv has more
# months of submissions, but we want an entry for each month.
# figure out the most recent month, so we know when to stop:
data = connection.read(f"SELECT MAX(EXTRACT(YEAR FROM posted)) FROM {config.db['schema']}.articles")
maxyear = int(data[0][0])
data = connection.read(f"SELECT MAX(EXTRACT(MONTH FROM posted)) FROM {config.db['schema']}.articles WHERE EXTRACT(YEAR FROM posted) = %s", (maxyear,))
maxmonth = int(data[0][0])
for repo in ['biorxiv','medrxiv']:
repodata = {}
for year in range(2013, maxyear + 1):
repodata[year] = {}
for month in range(1,13):
if year == 2013 and month < 11:
continue # nothing before nov 2013
if year == maxyear and month > maxmonth:
break
repodata[year][month] = 0
data = connection.read("""
SELECT EXTRACT(MONTH FROM posted)::int AS month,
EXTRACT(YEAR FROM posted)::int AS year, COUNT(id) AS submissions
FROM prod.articles
WHERE posted IS NOT NULL
AND repo=%s
GROUP BY year, month
ORDER BY year, month;
""",(repo,))
for entry in data:
repodata[entry[1]][entry[0]] = entry[2]
monthdata = []
for year, yeardata in repodata.items():
for month, count in yeardata.items():
monthdata.append({
'month': month,
'year': year,
'count': count,
})
results['submissions'].append({
'label': repo,
'data': monthdata
})
# Downloads:
# Don't show the previous month's download numbers until X days after the
# month ends, since those numbers won't be updated for every paper until
# after the month ends
current = datetime.now().day
adjust = 1 if current > config.summary_download_age else 2
if maxmonth > adjust:
maxmonth -= adjust
else:
maxmonth += 12 - adjust
maxyear -= 1
for repo in ['biorxiv','medrxiv']:
repodata = {}
for year in range(2013, maxyear + 1):
repodata[year] = {}
for month in range(1,13):
if year == 2013 and month < 11:
continue # nothing before nov 2013
if year == maxyear and month > maxmonth:
break
repodata[year][month] = 0
data = connection.read("""
SELECT t.month, t.year, sum(t.pdf) AS downloads
FROM prod.article_traffic t
INNER JOIN prod.articles a ON t.article=a.id
WHERE repo=%s
GROUP BY year, month
ORDER BY year, month
""",(repo,))
for entry in data:
# skip results outside the range we want
if entry[1] > maxyear:
continue
if entry[1] == maxyear and entry[0] > maxmonth:
continue
repodata[entry[1]][entry[0]] = entry[2]
monthdata = []
for year, yeardata in repodata.items():
for month, count in yeardata.items():
monthdata.append({
'month': month,
'year': year,
'count': count,
})
if year == maxyear and month == maxmonth:
break
results['downloads'].append({
'label': repo,
'data': monthdata
})
return results
def site_stats(connection):
"""Returns a (very) brief summary of the information indexed by Rxivist. More of
a data hygiene report than anything.
Arguments:
- connection: a database Connection object.
Returns:
- A dict with the total indexed papers and authors
"""
# Counting up how many of each entity we have
resp = connection.read("SELECT COUNT(id) FROM articles;")
if len(resp) != 1 or len(resp[0]) != 1:
paper_count = 0
else:
paper_count = resp[0][0]
resp = connection.read("SELECT COUNT(id) FROM authors;")
if len(resp) != 1 or len(resp[0]) != 1:
author_count = 0
else:
author_count = resp[0][0]
resp = connection.read("SELECT COUNT(id) FROM articles WHERE abstract IS NULL;")
if len(resp) != 1 or len(resp[0]) != 1:
no_abstract = 0
else:
no_abstract = resp[0][0]
resp = connection.read("SELECT COUNT(id) FROM articles WHERE posted IS NULL;")
if len(resp) != 1 or len(resp[0]) != 1:
no_posted = 0
else:
no_posted = resp[0][0]
outdated = {}
resp = connection.read("SELECT collection, COUNT(id) FROM articles WHERE last_crawled < now() - interval %s GROUP BY collection ORDER BY collection;", (config.outdated_limit,))
if len(resp) > 0:
for entry in resp:
if len(entry) < 2:
continue # something fishy with this entry
outdated[entry[0]] = entry[1]
resp = connection.read("SELECT COUNT(id) FROM articles WHERE collection IS NULL;")
if len(resp) != 1 or len(resp[0]) != 1:
no_category = 0
else:
no_category = resp[0][0]
resp = connection.read(f"""
SELECT COUNT(id)
FROM (
SELECT
a.id, COUNT(w.author) AS authors
FROM {config.db["schema"]}.articles a
LEFT JOIN {config.db["schema"]}.article_authors w ON w.article=a.id
GROUP BY a.id
ORDER BY authors
) AS authorcount
WHERE authors=0;
""")
if len(resp) != 1 or len(resp[0]) != 1:
no_authors = 0
else:
no_authors = resp[0][0]
resp = connection.read(f"""
SELECT COUNT(id)
FROM (
SELECT a.id, COUNT(z.article) AS num
FROM prod.authors a
LEFT JOIN prod.article_authors z ON a.id=z.author
GROUP BY 1
ORDER BY 2 DESC
) AS asdf
WHERE num = 0
""")
if len(resp) != 1 or len(resp[0]) != 1:
no_papers = 0
else:
no_papers = resp[0][0]
return {
"papers_indexed": paper_count,
"authors_indexed": author_count,
"missing_abstract": no_abstract,
"missing_date": no_posted,
"outdated_count": outdated,
"missing_authors": no_authors,
"missing_category": no_category,
"authors_no_papers": no_papers
}