-
Notifications
You must be signed in to change notification settings - Fork 7
/
run.py
executable file
·462 lines (374 loc) · 16.7 KB
/
run.py
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
#!/usr/bin/env python
"""
"""
__author__ = 'rca'
__maintainer__ = 'rca'
__copyright__ = 'Copyright 2015'
__version__ = '1.0'
__email__ = '[email protected]'
__status__ = 'Production'
__credit__ = ['J. Elliott']
__license__ = 'MIT'
import os
import sys
import time
import json
import argparse
import logging
import traceback
import warnings
from urllib3 import exceptions
warnings.simplefilter('ignore', exceptions.InsecurePlatformWarning)
from adsputils import get_date
from adsmsg import OrcidClaims
from ADSOrcid import updater, tasks
from ADSOrcid.models import ClaimsLog, KeyValue, Records, AuthorInfo
# ============================= INITIALIZATION ==================================== #
from adsputils import setup_logging, load_config
proj_home = os.path.realpath(os.path.dirname(__file__))
config = load_config(proj_home=proj_home)
logger = setup_logging('run.py', proj_home=proj_home,
level=config.get('LOGGING_LEVEL', 'INFO'),
attach_stdout=config.get('LOG_STDOUT', False))
app = tasks.app
# =============================== FUNCTIONS ======================================= #
def reindex_claims(since=None, orcid_ids=None, **kwargs):
"""
Re-runs all claims, both from the pipeline and
from the orcid-service storage.
:param: since - RFC889 formatted string
:type: str
:return: no return
"""
if orcid_ids:
for oid in orcid_ids:
tasks.task_index_orcid_profile.delay({'orcidid': oid, 'force': True})
if not since:
print('Done (just the supplied orcidids)')
return
logging.captureWarnings(True)
if not since or isinstance(since, str) and since.strip() == "":
with app.session_scope() as session:
kv = session.query(KeyValue).filter_by(key='last.reindex').first()
if kv is not None:
since = kv.value
else:
since = '1974-11-09T22:56:52.518001Z'
from_date = get_date(since)
orcidids = set()
logger.info('Loading records since: {0}'.format(from_date.isoformat()))
# first re-check our own database (replay the logs)
with app.session_scope() as session:
for author in session.query(AuthorInfo.orcidid.distinct().label('orcidid')).all():
orcidid = author.orcidid
if orcidid and orcidid.strip() != "":
try:
changed = updater.reindex_all_claims(app, orcidid, since=from_date.isoformat(), ignore_errors=True)
if len(changed):
orcidids.add(orcidid)
tasks.task_index_orcid_profile.delay({'orcidid': orcidid, 'force': True})
except Exception as e:
print('Error processing: {0}'.format(orcidid))
traceback.print_exc()
continue
if len(orcidids) % 100 == 0:
print('Done replaying {0} profiles'.format(len(orcidids)))
print('Now harvesting orcid profiles...')
# then get all new/old orcidids from orcid-service
all_orcids = set(updater.get_all_touched_profiles(app, from_date.isoformat()))
orcidids = all_orcids.difference(orcidids)
from_date = get_date()
for orcidid in orcidids:
try:
tasks.task_index_orcid_profile.delay({'orcidid': orcidid, 'force': True})
except: # potential backpressure (we are too fast)
time.sleep(2)
print('Conn problem, retrying...', orcidid)
tasks.task_index_orcid_profile.delay({'orcidid': orcidid, 'force': True})
with app.session_scope() as session:
kv = session.query(KeyValue).filter_by(key='last.reindex').first()
if kv is None:
kv = KeyValue(key='last.reindex', value=from_date.isoformat())
session.add(kv)
else:
kv.value = from_date.isoformat()
session.commit()
print('Done')
logger.info('Done submitting {0} orcid ids.'.format(len(orcidids)))
def repush_claims(since=None, orcid_ids=None, **kwargs):
"""
Re-pushes all recs that were added since date 'X'
to the output (i.e. forwards them onto the Solr queue)
:param: since - RFC889 formatted string
:type: str
:return: no return
"""
if orcid_ids:
for oid in orcid_ids:
tasks.task_index_orcid_profile.delay({'orcidid': oid, 'force': False})
if not since:
print('Done (just the supplied orcidids)')
return
logging.captureWarnings(True)
if not since or isinstance(since, str) and since.strip() == "":
with app.session_scope() as session:
kv = session.query(KeyValue).filter_by(key='last.repush').first()
if kv is not None:
since = kv.value
else:
since = '1974-11-09T22:56:52.518001Z'
from_date = get_date(since)
logger.info('Re-pushing records since: {0}'.format(from_date.isoformat()))
num_bibcodes = 0
with app.session_scope() as session:
for rec in session.query(Records) \
.filter(Records.updated >= from_date) \
.order_by(Records.updated.asc()) \
.all():
data = rec.toJSON()
try:
tasks.task_output_results.delay({'bibcode': data['bibcode'], 'authors': data['authors'], 'claims': data['claims']})
except Exception as e: # potential backpressure (we are too fast)
time.sleep(2)
print('Conn problem, retrying ', data['bibcode'])
tasks.task_output_results.delay({'bibcode': data['bibcode'], 'authors': data['authors'], 'claims': data['claims']})
num_bibcodes += 1
with app.session_scope() as session:
kv = session.query(KeyValue).filter_by(key='last.repush').first()
if kv is None:
kv = KeyValue(key='last.repush', value=get_date())
session.add(kv)
else:
kv.value = get_date()
session.commit()
logger.info('Done processing {0} orcid ids.'.format(num_bibcodes))
def refetch_orcidids(since=None, orcid_ids=None, **kwargs):
"""
Gets all orcidids that were updated since time X.
:param: since - RFC889 formatted string
:type: str
:return: no return
"""
if orcid_ids:
for oid in orcid_ids:
tasks.task_index_orcid_profile({'orcidid': oid, 'force': False})
if not since:
print('Done (just the supplied orcidids)')
return
logging.captureWarnings(True)
if not since or isinstance(since, str) and since.strip() == "":
with app.session_scope() as session:
kv = session.query(KeyValue).filter_by(key='last.refetch').first()
if kv is not None:
since = kv.value
else:
since = '1974-11-09T22:56:52.518001Z'
from_date = get_date(since)
logger.info('Re-fetching orcidids updated since: {0}'.format(from_date.isoformat()))
# then get all new/old orcidids from orcid-service
orcidids = set(updater.get_all_touched_profiles(app, from_date.isoformat()))
from_date = get_date()
for orcidid in orcidids:
try:
tasks.task_index_orcid_profile.delay({'orcidid': orcidid, 'force': False})
except Exception as e: # potential backpressure (we are too fast)
time.sleep(2)
print('Conn problem, retrying...', orcidid)
tasks.task_index_orcid_profile.delay({'orcidid': orcidid, 'force': False})
with app.session_scope() as session:
kv = session.query(KeyValue).filter_by(key='last.refetch').first()
if kv is None:
kv = KeyValue(key='last.refetch', value=from_date.isoformat())
session.add(kv)
else:
kv.value = from_date.isoformat()
session.commit()
print('Done')
logger.info('Done submitting {0} orcid ids.'.format(len(orcidids)))
def reprocess_bibcodes(bibcodes, force=False):
"""
Checks that the stored length of the claims matches the length of the authors.
Reprocesses the relevant ORCID IDs if the lengths do not match.
:param bibcodes: Bibcodes to check (list)
:param force: Force the length of the ORCID array to be rebuilt (boolean)
:return: no return
"""
orcids_to_process = set()
logging.captureWarnings(True)
if type(bibcodes) != list:
if type(bibcodes) == str:
bibcodes = [bibcodes]
else:
raise TypeError('Bibcodes must be list or string')
for bibcode in bibcodes:
logger.debug('Reprocessing bibcode {}'.format(bibcode))
update = False
metadata = app.retrieve_metadata(bibcode, search_identifiers=True)
# make sure we're using the canonical bibcode
bibc = metadata.get('bibcode')
author_list = metadata.get('author', [])
# fetch existing record; creates one if necessary
rec = app.retrieve_record(bibc, author_list)
claims = rec.get('claims', {})
# reprocess any ORCID IDs that are in arrays of the wrong length
fld_names = ['verified', 'unverified']
for f in fld_names:
f_claims = claims.get(f, [])
if f_claims and len(author_list) != f_claims:
logger.debug('{0} claims length does not match author length for bibcode {1}, reprocessing'.
format(f, bibcode))
orcids = set(f_claims)
orcids -= {'-'}
orcids_to_process = orcids_to_process.union(orcids)
if force or not orcids:
# force is on or length is non-zero but wrong but there are no ORCIDs in it to reprocess,
# so rebuild manually
# note that this is fine to do even if there are valid orcid claims here because those will get
# rebuilt in the reindex task at the end of this process
claims[f] = ['-'] * len(author_list)
update = True
if update:
with app.session_scope() as session:
r = session.query(Records).filter_by(bibcode=bibc).first()
r.claims = json.dumps(claims)
r.updated = get_date()
session.commit()
# if there are no claims, we need to push the update to master manually
msg = OrcidClaims(authors=rec.get('authors'), bibcode=rec['bibcode'],
verified=claims.get('verified', []),
unverified=claims.get('unverified', [])
)
tasks.task_output_results.delay(msg)
for orcidid in orcids_to_process:
try:
tasks.task_index_orcid_profile.delay({'orcidid': orcidid, 'force': True})
except Exception as e: # potential backpressure (we are too fast)
time.sleep(2)
logger.info('Connection problem when trying to process {}, retrying...'.format(orcidid))
tasks.task_index_orcid_profile.delay({'orcidid': orcidid, 'force': True})
logger.info('Done processing the given bibcodes')
def print_kvs():
"""Prints the values stored in the KeyValue table."""
print('Key, Value from the storage:')
print('-' * 80)
with app.session_scope() as session:
for kv in session.query(KeyValue).order_by('key').all():
print(kv.key, kv.value)
def show_api_diagnostics(orcid_ids=None, bibcodes=None, ):
"""
Prints various responses that we receive from our API.
"""
print('API_ENDPOINT', app.conf.get('API_ENDPOINT', None))
print('API_SOLR_QUERY_ENDPOINT', app.conf.get('API_SOLR_QUERY_ENDPOINT', None))
print('API_ORCID_EXPORT_PROFILE', app.conf.get('API_ORCID_EXPORT_PROFILE', None))
print('API_ORCID_UPDATES_ENDPOINT', app.conf.get('API_ORCID_UPDATES_ENDPOINT', None))
if orcid_ids:
for o in orcid_ids:
print(o)
print('DB Model', app.retrieve_orcid(o))
print('=' * 80 + '\n')
print('Author info', app.harvest_author_info(o))
print('=' * 80 + '\n')
print('Public orcid profile', app.get_public_orcid_profile(o))
print('=' * 80 + '\n')
print('ADS Orcid Profile', app.get_ads_orcid_profile(o))
print('=' * 80 + '\n')
print('Harvested Author Info', app.retrieve_orcid(o))
print('=' * 80 + '\n')
orcid_present, updated, removed = app.get_claims(o,
app.conf.get('API_TOKEN'),
app.conf.get('API_ORCID_EXPORT_PROFILE') % o,
orcid_identifiers_order=app.conf.get('ORCID_IDENTIFIERS_ORDER', {'bibcode': 9, '*': -1})
)
print('All of orcid', len(orcid_present), orcid_present)
print('In need of update', len(updated), updated)
print('In need of removal', len(removed), removed)
print('=' * 80 + '\n')
else:
print('If you want to see what I see for authors, give me some orcid ids')
if bibcodes:
for b in bibcodes:
print(b, app.retrieve_metadata(b))
else:
print('If you want to see what I see give me some bibcodes')
if orcid_ids:
print('=' * 80 + '\n')
print('Now submitting ORCiD for processing')
for o in orcid_ids:
m = {'orcidid': o}
print('message=%s, taskid=%s' % (m, tasks.task_index_orcid_profile.delay(m)))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Process user input.')
parser.add_argument('-r',
'--reindex_claims',
dest='reindex_claims',
action='store_true',
help='Reindex claims')
parser.add_argument('-u',
'--repush_claims',
dest='repush_claims',
action='store_true',
help='Re-push claims')
parser.add_argument('-f',
'--refetch_orcidids',
dest='refetch_orcidids',
action='store_true',
help='Gets all orcidids changed since X (as discovered from ads api) and sends them to the queue.')
parser.add_argument('-e',
'--reprocess_bibcodes',
dest='reprocess_bibcodes',
action='store_true',
help='Reprocesses all claims for the given bibcodes, confirms claim array lengths')
parser.add_argument('-m',
'--force',
dest='force',
action='store_true',
default=False,
help='Force rebuilding the length of the ORCID array when reprocessing a bibcode')
parser.add_argument('-s',
'--since',
dest='since_date',
action='store',
default=None,
help='Starting date for reindexing')
parser.add_argument('-o',
'--oid',
dest='orcid_ids',
action='store',
default=None,
help='Comma delimited list of orcid-ids to re-index (use with refetch orcidids)')
parser.add_argument('-b',
'--bibcodes',
dest='bibcodes',
action='store',
default=None,
help='Comma delimited list of bibcodes (for diagnostics)')
parser.add_argument('-k',
'--kv',
dest='kv',
action='store_true',
default=False,
help='Show current values of KV store')
parser.add_argument('-d',
'--diagnose',
dest='diagnose',
action='store_true',
default=False,
help='Show me what you would do with ORCiDs/bibcodes')
args = parser.parse_args()
if args.orcid_ids:
args.orcid_ids = [x.strip() for x in args.orcid_ids.split(',')]
if args.bibcodes:
args.bibcodes = [x.strip() for x in args.bibcodes.split(',')]
if args.kv:
print_kvs()
if args.diagnose:
show_api_diagnostics(args.orcid_ids or ['0000-0003-3041-2092'], args.bibcodes or ['2015arXiv150305881C'])
if args.reindex_claims:
reindex_claims(args.since_date, args.orcid_ids)
elif args.repush_claims:
repush_claims(args.since_date, args.orcid_ids)
elif args.refetch_orcidids:
refetch_orcidids(args.since_date, args.orcid_ids)
elif args.reprocess_bibcodes:
reprocess_bibcodes(args.bibcodes, args.force)