-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathutils.py
More file actions
261 lines (223 loc) · 9.83 KB
/
utils.py
File metadata and controls
261 lines (223 loc) · 9.83 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
"""Utilities for pushing metadata to SHARE/Trove
SHARE/Trove accepts metadata records as "indexcards" in turtle format: https://www.w3.org/TR/turtle/
"""
from http import HTTPStatus
import logging
from django.apps import apps
from django.db.models import Q, OuterRef, Subquery
from django.contrib.contenttypes.models import ContentType
from celery.utils.time import get_exponential_backoff_interval
import requests
from framework.celery_tasks import app as celery_app
from framework.celery_tasks.handlers import enqueue_task
from framework.encryption import ensure_bytes
from framework.sentry import log_exception
from osf.external.gravy_valet.exceptions import GVException
from osf.metadata.osf_gathering import (
OsfmapPartition,
pls_get_magic_metadata_basket,
)
from osf.metadata.serializers import get_metadata_serializer
from website import settings
logger = logging.getLogger(__name__)
def shtrove_ingest_url():
return f'{settings.SHARE_URL}trove/ingest'
def is_qa_resource(resource):
"""
QA puts tags and special titles on their project to stop them from appearing in the search results. This function
check if a resource is a 'QA resource' that should be indexed.
:param resource: should be Node/Registration/Preprint
:return:
"""
tags = set(resource.tags.all().values_list('name', flat=True))
has_qa_tags = bool(set(settings.DO_NOT_INDEX_LIST['tags']).intersection(tags))
has_qa_title = False
_title = getattr(resource, 'title', None)
if _title:
has_qa_title = any((_substring in _title) for _substring in settings.DO_NOT_INDEX_LIST['titles'])
return has_qa_tags or has_qa_title
def update_share(resource):
if not settings.SHARE_ENABLED:
return
if not hasattr(resource, 'guids'):
logger.error(f'update_share called on non-guid resource: {resource}')
return
_enqueue_update_share(resource)
def _enqueue_update_share(osfresource):
_osfguid_value = osfresource.guids.values_list('_id', flat=True).first()
if not _osfguid_value:
logger.warning(f'update_share skipping resource that has no guids: {osfresource}')
return
enqueue_task(task__update_share.s(_osfguid_value))
@celery_app.task(
bind=True,
acks_late=True,
max_retries=4,
retry_backoff=True,
)
def task__update_share(self, guid: str, is_backfill=False, osfmap_partition_name='MAIN'):
"""
Send SHARE/trove current metadata record(s) for the osf-guid-identified object
"""
_osfmap_partition = OsfmapPartition[osfmap_partition_name]
_osfid_instance = apps.get_model('osf.Guid').load(guid)
if _osfid_instance is None:
raise ValueError(f'unknown osfguid "{guid}"')
_resource = _osfid_instance.referent
_is_deletion = _should_delete_indexcard(_resource)
_resource.mark_indexing_failed()
try:
_response = (
pls_delete_trove_record(_resource, osfmap_partition=_osfmap_partition)
if _is_deletion
else pls_send_trove_record(
_resource,
is_backfill=is_backfill,
osfmap_partition=_osfmap_partition,
)
)
except GVException as e:
log_exception(e)
raise self.retry(exc=e)
try:
_response.raise_for_status()
except Exception as e:
log_exception(e)
if _response.status_code == HTTPStatus.TOO_MANY_REQUESTS:
retry_after = _response.headers.get('Retry-After')
try:
countdown = int(retry_after)
except (TypeError, ValueError):
retries = getattr(self.request, 'retries', 0)
countdown = get_exponential_backoff_interval(
factor=4,
retries=retries,
maximum=2 * 60,
full_jitter=True,
)
raise self.retry(exc=e, countdown=countdown)
if HTTPStatus(_response.status_code).is_server_error:
raise self.retry(exc=e)
else: # success response
_resource.mark_indexing_success()
if not _is_deletion:
# enqueue followup task for supplementary metadata
_next_partition = _next_osfmap_partition(_osfmap_partition)
if _next_partition is not None:
task__update_share.delay(
guid,
is_backfill=is_backfill,
osfmap_partition_name=_next_partition.name,
)
@celery_app.task
def task__reindex_failed_or_not_indexed_resource_into_share(resource_type: str, start_id: int = 0, chunk_count: int = 200, chunk_size: int = 500):
from osf.management.commands.recatalog_metadata import recatalog
queryset = get_not_indexed_guids_for_resource_with_no_indexed_guid(resource_type, first_guid=False)
# chunk count and chunk size up to discussion what will be better with Cloud Team
recatalog(queryset, start_id, chunk_count, chunk_size)
def get_not_indexed_guids_for_resource_with_no_indexed_guid(resource_type: str, first_guid: bool = True):
from osf.models import Guid, Registration, Preprint, Node, OSFUser
from addons.osfstorage.models import OsfStorageFile
common_not_indexed_public_resource_extract_query = (
Q(is_public=True) & Q(deleted__isnull=True) &
(Q(has_been_indexed=False) | Q(has_been_indexed__isnull=True))
)
resource_mapper = {
'projects': (Node, common_not_indexed_public_resource_extract_query, ('first_guid', 'date_last_indexed', 'title')),
'preprints': (Preprint, common_not_indexed_public_resource_extract_query & Q(is_published=True), ('first_guid', 'date_last_indexed', 'title')),
'registries': (Registration, common_not_indexed_public_resource_extract_query, ('first_guid', 'date_last_indexed', 'title')),
'users': (OSFUser, Q(is_active=True) & Q(deleted__isnull=True) & (Q(has_been_indexed=False) | Q(has_been_indexed__isnull=True)), ('first_guid', 'fullname', 'date_last_indexed')),
'files': (OsfStorageFile, Q(deleted__isnull=True), ('first_guid', 'name', 'date_last_indexed')),
}
resource_model, query, values_to_return = resource_mapper.get(resource_type, 'projects')
if first_guid:
model_content_type = ContentType.objects.get_for_model(resource_model)
first_guid_sq = Guid.objects.filter(
content_type=model_content_type,
object_id=OuterRef('pk'),
).order_by('created').values('_id')[:1]
return resource_model.objects.filter(query).annotate(first_guid=Subquery(first_guid_sq)).exclude(first_guid__isnull=True).values(*values_to_return)
return resource_model.objects.filter(query)
def pls_send_trove_record(osf_item, *, is_backfill: bool, osfmap_partition: OsfmapPartition):
try:
_iri = osf_item.get_semantic_iri()
except (AttributeError, ValueError):
raise ValueError(f'could not get iri for {osf_item}')
_basket = pls_get_magic_metadata_basket(osf_item)
_serializer = get_metadata_serializer(
format_key='turtle',
basket=_basket,
serializer_config={'osfmap_partition': osfmap_partition},
)
_serialized_record = _serializer.serialize()
_queryparams = {
'focus_iri': _iri,
'record_identifier': _shtrove_record_identifier(osf_item, osfmap_partition),
}
if is_backfill:
_queryparams['nonurgent'] = ''
if osfmap_partition.is_supplementary:
_queryparams['is_supplementary'] = ''
_expiration_date = osfmap_partition.get_expiration_date(_basket)
if _expiration_date is not None:
_queryparams['expiration_date'] = str(_expiration_date)
return requests.post(
shtrove_ingest_url(),
params=_queryparams,
headers={
'Content-Type': _serializer.mediatype,
**_shtrove_auth_headers(osf_item),
},
data=ensure_bytes(_serialized_record),
)
def pls_delete_trove_record(osf_item, osfmap_partition: OsfmapPartition):
return requests.delete(
shtrove_ingest_url(),
params={
'record_identifier': _shtrove_record_identifier(osf_item, osfmap_partition),
},
headers=_shtrove_auth_headers(osf_item),
)
def _shtrove_record_identifier(osf_item, osfmap_partition: OsfmapPartition):
_id = osf_item.guids.values_list('_id', flat=True).first()
return (
f'{_id}/{osfmap_partition.name}'
if osfmap_partition.is_supplementary
else _id
)
def _shtrove_auth_headers(osf_item):
_nonfile_item = (
osf_item.target
if hasattr(osf_item, 'target')
else osf_item
)
_access_token = (
_nonfile_item.provider.access_token
if getattr(_nonfile_item, 'provider', None) and _nonfile_item.provider.access_token
else settings.SHARE_API_TOKEN
)
return {'Authorization': f'Bearer {_access_token}'}
def _should_delete_indexcard(osf_item):
if getattr(osf_item, 'is_deleted', False) or getattr(osf_item, 'deleted', None):
return True
# if it quacks like BaseFileNode, look at .target instead
_containing_item = getattr(osf_item, 'target', None)
if _containing_item:
return not osf_item.should_update_search or _should_delete_indexcard(_containing_item)
return (
not _is_item_public(osf_item)
or getattr(osf_item, 'is_spam', False)
or is_qa_resource(osf_item)
)
def _is_item_public(guid_referent) -> bool:
if hasattr(guid_referent, 'verified_publishable'):
return guid_referent.verified_publishable # quacks like Preprint
return getattr(guid_referent, 'is_public', False) # quacks like AbstractNode
def _next_osfmap_partition(partition: OsfmapPartition) -> OsfmapPartition | None:
match partition:
case OsfmapPartition.MAIN:
return OsfmapPartition.SUPPLEMENT
case OsfmapPartition.SUPPLEMENT:
return OsfmapPartition.MONTHLY_SUPPLEMENT
case _:
return None