forked from adsabs/ADSOrcid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·360 lines (291 loc) · 12.6 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
#!/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 sys
import time
import argparse
import logging
import traceback
import requests
import warnings
from requests.packages.urllib3 import exceptions
warnings.simplefilter('ignore', exceptions.InsecurePlatformWarning)
from adsputils import setup_logging, get_date
from ADSOrcid import updater, tasks
from ADSOrcid.models import ClaimsLog, KeyValue, Records, AuthorInfo
app = tasks.app
logger = setup_logging('run.py')
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, basestring) 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:
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, basestring) 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)
orcidids = set()
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: # 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, basestring) 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: # 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 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('-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)