-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.py
561 lines (460 loc) · 19.4 KB
/
util.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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
import json
import mimetypes
import os
import pkgutil
from collections.abc import Iterable
from contextlib import AbstractContextManager
from copy import deepcopy
from datetime import datetime, timezone
from functools import lru_cache
from io import BytesIO
from pathlib import Path
from uuid import uuid4
from typing import List, Optional, Set, Dict
import fastjsonschema
import requests
from frozendict import frozendict
from jsonschema.validators import Draft7Validator
from nmdc_schema.nmdc_schema_accepting_legacy_ids import Database as NMDCDatabase
from nmdc_schema.get_nmdc_view import ViewGetter
from pydantic import Field, BaseModel
from pymongo.database import Database as MongoDatabase
from pymongo.errors import OperationFailure
from toolz import merge, unique
from nmdc_runtime.api.core.util import sha256hash_from_file
from nmdc_runtime.api.models.object import DrsObjectIn
from typing_extensions import Annotated
def get_class_names_from_collection_spec(
spec: dict, prefix: Optional[str] = None
) -> List[str]:
"""
Returns the list of classes referenced by the `$ref` values in a JSON Schema snippet describing a collection,
applying an optional prefix to each class name.
>>> get_class_names_from_collection_spec({"items": {"foo": "#/$defs/A"}})
[]
>>> get_class_names_from_collection_spec({"items": {"$ref": "#/$defs/A"}})
['A']
>>> get_class_names_from_collection_spec({"items": {"$ref": "#/$defs/A"}}, "p:")
['p:A']
>>> get_class_names_from_collection_spec({"items": {"anyOf": "not-a-list"}})
[]
>>> get_class_names_from_collection_spec({"items": {"anyOf": []}})
[]
>>> get_class_names_from_collection_spec({"items": {"anyOf": [{"$ref": "#/$defs/A"}]}})
['A']
>>> get_class_names_from_collection_spec({"items": {"anyOf": [{"$ref": "#/$defs/A"}, {"$ref": "#/$defs/B"}]}})
['A', 'B']
>>> get_class_names_from_collection_spec({"items": {"anyOf": [{"$ref": "#/$defs/A"}, {"$ref": "#/$defs/B"}]}}, "p:")
['p:A', 'p:B']
"""
class_names = []
if "items" in spec:
# If the `items` dictionary has a key named `$ref`, get the single class name from it.
if "$ref" in spec["items"]:
ref_dict = spec["items"]["$ref"]
class_name = ref_dict.split("/")[-1] # e.g. `#/$defs/Foo` --> `Foo`
class_names.append(class_name)
# Else, if it has a key named `anyOf` whose value is a list, get the class name from each ref in the list.
elif "anyOf" in spec["items"] and isinstance(spec["items"]["anyOf"], list):
for element in spec["items"]["anyOf"]:
ref_dict = element["$ref"]
class_name = ref_dict.split("/")[-1] # e.g. `#/$defs/Foo` --> `Foo`
class_names.append(class_name)
# Apply the specified prefix, if any, to each class name.
if isinstance(prefix, str):
class_names = list(map(lambda name: f"{prefix}{name}", class_names))
return class_names
@lru_cache
def get_type_collections() -> dict:
"""Returns a dictionary mapping class names to Mongo collection names."""
mappings = {}
# Process the `items` dictionary of each collection whose name ends with `_set`.
for collection_name, spec in nmdc_jsonschema["properties"].items():
if collection_name.endswith("_set"):
class_names = get_class_names_from_collection_spec(spec, "nmdc:")
for class_name in class_names:
mappings[class_name] = collection_name
return mappings
def without_id_patterns(nmdc_jsonschema):
rv = deepcopy(nmdc_jsonschema)
for cls_, spec in rv["$defs"].items():
if "properties" in spec:
if "id" in spec["properties"]:
spec["properties"]["id"].pop("pattern", None)
return rv
@lru_cache
def get_nmdc_jsonschema_dict(enforce_id_patterns=True):
"""Get NMDC JSON Schema with materialized patterns (for identifier regexes)."""
d = json.loads(
BytesIO(
pkgutil.get_data("nmdc_schema", "nmdc_materialized_patterns.schema.json")
)
.getvalue()
.decode("utf-8")
)
return d if enforce_id_patterns else without_id_patterns(d)
@lru_cache
def get_nmdc_jsonschema_validator(enforce_id_patterns=True):
return fastjsonschema.compile(
get_nmdc_jsonschema_dict(enforce_id_patterns=enforce_id_patterns)
)
nmdc_jsonschema = get_nmdc_jsonschema_dict()
nmdc_jsonschema_validator = get_nmdc_jsonschema_validator()
nmdc_jsonschema_noidpatterns = get_nmdc_jsonschema_dict(enforce_id_patterns=False)
nmdc_jsonschema_validator_noidpatterns = get_nmdc_jsonschema_validator(
enforce_id_patterns=False
)
REPO_ROOT_DIR = Path(__file__).parent.parent
def put_object(filepath, url, mime_type=None):
if mime_type is None:
mime_type = mimetypes.guess_type(filepath)[0]
with open(filepath, "rb") as f:
return requests.put(url, data=f, headers={"Content-Type": mime_type})
def drs_metadata_for(filepath, base=None, timestamp=None):
"""given file path, get drs metadata
required: size, created_time, and at least one checksum.
"""
base = {} if base is None else base
if "size" not in base:
base["size"] = os.path.getsize(filepath)
if "created_time" not in base:
base["created_time"] = datetime.fromtimestamp(
os.path.getctime(filepath), tz=timezone.utc
)
if "checksums" not in base:
base["checksums"] = [
{"type": "sha256", "checksum": sha256hash_from_file(filepath, timestamp)}
]
if "mime_type" not in base:
base["mime_type"] = mimetypes.guess_type(filepath)[0]
if "name" not in base:
base["name"] = Path(filepath).name
return base
def drs_object_in_for(filepath, op_doc, base=None):
access_id = f'{op_doc["metadata"]["site_id"]}:{op_doc["metadata"]["object_id"]}'
drs_obj_in = DrsObjectIn(
**drs_metadata_for(
filepath,
merge(base or {}, {"access_methods": [{"access_id": access_id}]}),
)
)
return json.loads(drs_obj_in.json(exclude_unset=True))
def freeze(obj):
"""Recursive function for dict → frozendict, set → frozenset, list → tuple.
For example, will turn JSON data into a hashable value.
"""
try:
# See if the object is hashable
hash(obj)
return obj
except TypeError:
pass
if isinstance(obj, (dict, frozendict)):
return frozendict({k: freeze(obj[k]) for k in obj})
elif isinstance(obj, (set, frozenset)):
return frozenset({freeze(elt) for elt in obj})
elif isinstance(obj, (list, tuple)):
return tuple([freeze(elt) for elt in obj])
msg = "Unsupported type: %r" % type(obj).__name__
raise TypeError(msg)
def unfreeze(obj):
"""frozendict → dict, frozenset → set, tuple → list."""
if isinstance(obj, (dict, frozendict)):
return {k: unfreeze(v) for k, v in obj.items()}
elif isinstance(obj, (set, frozenset)):
return {unfreeze(elt) for elt in obj}
elif isinstance(obj, (list, tuple)):
return [unfreeze(elt) for elt in obj]
else:
return obj
def pluralize(singular, using, pluralized=None):
"""Pluralize a word for output.
>>> pluralize("job", 1)
'job'
>>> pluralize("job", 2)
'jobs'
>>> pluralize("datum", 2, "data")
'data'
"""
return (
singular
if using == 1
else (pluralized if pluralized is not None else f"{singular}s")
)
def iterable_from_dict_keys(d, keys):
for k in keys:
yield d[k]
def flatten(d):
"""Flatten a nested JSON-able dict into a flat dict of dotted-pathed keys."""
# assumes plain-json-able
d = json.loads(json.dumps(d))
# atomic values are already "flattened"
if not isinstance(d, (dict, list)):
return d
out = {}
for k, v in d.items():
if isinstance(v, list):
for i, elt in enumerate(v):
if isinstance(elt, dict):
for k_inner, v_inner in flatten(elt).items():
out[f"{k}.{i}.{k_inner}"] = v_inner
elif isinstance(elt, list):
raise ValueError("Can't handle lists in lists at this time")
else:
out[f"{k}.{i}"] = elt
elif isinstance(v, dict):
for kv, vv in v.items():
if isinstance(vv, dict):
for kv_inner, vv_inner in flatten(vv).items():
out[f"{k}.{kv}.{kv_inner}"] = vv_inner
elif isinstance(vv, list):
raise ValueError("Can't handle lists in sub-dicts at this time")
else:
out[f"{k}.{kv}"] = vv
else:
out[k] = v
return out
def find_one(k_v: dict, entities: Iterable[dict]):
"""Find the first entity with key-value pair k_v, if any?
>>> find_one({"id": "foo"}, [{"id": "foo"}])
True
>>> find_one({"id": "foo"}, [{"id": "bar"}])
False
"""
if len(k_v) > 1:
raise Exception("Supports only one key-value pair")
k = next(k for k in k_v)
return next((e for e in entities if k in e and e[k] == k_v[k]), None)
@lru_cache
def nmdc_activity_collection_names():
slots = []
view = ViewGetter().get_view()
acts = set(view.class_descendants("WorkflowExecutionActivity"))
acts -= {"WorkflowExecutionActivity"}
for slot in view.class_slots("Database"):
rng = getattr(view.get_slot(slot), "range", None)
if rng in acts:
slots.append(slot)
return slots
@lru_cache
def nmdc_schema_view():
return ViewGetter().get_view()
@lru_cache
def nmdc_database_collection_instance_class_names():
names = []
view = nmdc_schema_view()
all_classes = set(view.all_classes())
for slot in view.class_slots("Database"):
rng = getattr(view.get_slot(slot), "range", None)
if rng in all_classes:
names.append(rng)
return names
@lru_cache
def nmdc_database_collection_names():
names = []
view = nmdc_schema_view()
all_classes = set(view.all_classes())
for slot in view.class_slots("Database"):
rng = getattr(view.get_slot(slot), "range", None)
if rng in all_classes:
names.append(slot)
return names
def all_docs_have_unique_id(coll) -> bool:
first_doc = coll.find_one({}, ["id"])
if first_doc is None or "id" not in first_doc:
# short-circuit exit for empty collection or large collection via first-doc peek.
return False
total_count = coll.count_documents({})
return (
# avoid attempt to fetch large (>16mb) list of distinct IDs,
# a limitation of collection.distinct(). Use aggregation pipeline
# instead to compute on mongo server, using disk if necessary.
next(
coll.aggregate(
[{"$group": {"_id": "$id"}}, {"$count": "n_unique_ids"}],
allowDiskUse=True,
)
)["n_unique_ids"]
== total_count
)
def specialize_activity_set_docs(docs):
validation_errors = {}
type_collections = get_type_collections()
if "activity_set" in docs:
for doc in docs["activity_set"]:
doc_type = doc["type"]
try:
collection_name = type_collections[doc_type]
except KeyError:
msg = (
f"activity_set doc {doc.get('id', '<id missing>')} "
f"has type {doc_type}, which is not in NMDC Schema. "
"Note: Case is sensitive."
)
if "activity_set" in validation_errors:
validation_errors["activity_set"].append(msg)
else:
validation_errors["activity_set"] = [msg]
continue
if collection_name in docs:
docs[collection_name].append(doc)
else:
docs[collection_name] = [doc]
del docs["activity_set"]
return docs, validation_errors
# Define a mapping from collection name to a list of class names allowable for that collection's documents.
collection_name_to_class_names: Dict[str, List[str]] = {
collection_name: get_class_names_from_collection_spec(spec)
for collection_name, spec in nmdc_jsonschema["$defs"]["Database"][
"properties"
].items()
}
@lru_cache
def schema_collection_names_with_id_field() -> Set[str]:
"""
Returns the set of collection names with which _any_ of the associated classes contains an `id` field.
"""
target_collection_names = set()
for collection_name, class_names in collection_name_to_class_names.items():
for class_name in class_names:
if "id" in nmdc_jsonschema["$defs"][class_name].get("properties", {}):
target_collection_names.add(collection_name)
break
return target_collection_names
def ensure_unique_id_indexes(mdb: MongoDatabase):
"""Ensure that any collections with an "id" field have an index on "id"."""
candidate_names = (
set(mdb.list_collection_names()) | schema_collection_names_with_id_field()
)
for collection_name in candidate_names:
if collection_name.startswith("system."): # reserved by mongodb
continue
if (
collection_name in schema_collection_names_with_id_field()
or all_docs_have_unique_id(mdb[collection_name])
):
mdb[collection_name].create_index("id", unique=True)
class UpdateStatement(BaseModel):
q: dict
u: dict
upsert: bool = False
multi: bool = False
class DeleteStatement(BaseModel):
q: dict
limit: Annotated[int, Field(ge=0, le=1)] = 1
class OverlayDBError(Exception):
pass
class OverlayDB(AbstractContextManager):
"""Provides a context whereby a base Database is overlaid with a temporary one.
If you need to run basic simulations of updates to a base database,
you don't want to actually commit transactions to the base database.
For example, to insert or replace (matching on "id") many documents into a collection in order
to then validate the resulting total set of collection documents, an OverlayDB writes to
an overlay collection that "shadows" the base collection during a "find" query
(the "merge_find" method of an OverlayDB object): if a document with `id0` is found in the
overlay collection, that id is marked as "seen" and will not also be returned when
subsequently scanning the (unmodified) base-database collection.
Mongo "update" commands (as the "apply_updates" method) are simulated by first copying affected
documents from a base collection to the overlay, and then applying the updates to the overlay,
so that again, base collections are unmodified, and a "merge_find" call will produce a result
*as if* the base collection(s) were modified.
Mongo deletions (as the "delete" method) also copy affected documents from the base collection
to the overlay collection, and flag them using the "_deleted" field. In this way, a `merge_find`
call will match a relevant document given a suitable filter, and will mark the document's id
as "seen" *without* returning the document. Thus, the result is as if the document were deleted.
Usage:
````
with OverlayDB(mdb) as odb:
# do stuff, e.g. `odb.replace_or_insert_many(...)`
```
"""
def __init__(self, mdb: MongoDatabase):
self._bottom_db = mdb
self._top_db = self._bottom_db.client.get_database(f"overlay-{uuid4()}")
ensure_unique_id_indexes(self._top_db)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self._bottom_db.client.drop_database(self._top_db.name)
def replace_or_insert_many(self, coll_name, documents: list):
try:
self._top_db[coll_name].insert_many(documents)
except OperationFailure as e:
raise OverlayDBError(str(e.details))
def apply_updates(self, coll_name, updates: list):
"""prepare overlay db and apply updates to it."""
assert all(UpdateStatement(**us) for us in updates)
for update_spec in updates:
for bottom_doc in self._bottom_db[coll_name].find(update_spec["q"]):
self._top_db[coll_name].insert_one(bottom_doc)
try:
self._top_db.command({"update": coll_name, "updates": updates})
except OperationFailure as e:
raise OverlayDBError(str(e.details))
def delete(self, coll_name, deletes: list):
""" "apply" delete command by flagging docs in overlay database"""
assert all(DeleteStatement(**us) for us in deletes)
for delete_spec in deletes:
for bottom_doc in self._bottom_db[coll_name].find(
delete_spec["q"], limit=delete_spec.get("limit", 1)
):
bottom_doc["_deleted"] = True
self._top_db[coll_name].insert_one(bottom_doc)
def merge_find(self, coll_name, find_spec: dict):
"""Yield docs first from overlay and then from base db, minding deletion flags."""
# ensure projection of "id" and "_deleted"
if "projection" in find_spec:
proj = find_spec["projection"]
if isinstance(proj, dict):
proj = merge(proj, {"id": 1, "_deleted": 1})
elif isinstance(proj, list):
proj = list(unique(proj + ["id", "_deleted"]))
top_docs = self._top_db[coll_name].find(**find_spec)
bottom_docs = self._bottom_db[coll_name].find(**find_spec)
top_seen_ids = set()
for doc in top_docs:
if not doc.get("_deleted"):
yield doc
top_seen_ids.add(doc["id"])
for doc in bottom_docs:
if doc["id"] not in top_seen_ids:
yield doc
def validate_json(in_docs: dict, mdb: MongoDatabase):
validator = Draft7Validator(get_nmdc_jsonschema_dict())
docs = deepcopy(in_docs)
validation_errors = {}
known_coll_names = set(nmdc_database_collection_names())
for coll_name, coll_docs in docs.items():
if coll_name not in known_coll_names:
if coll_name == "@type" and coll_docs in ("Database", "nmdc:Database"):
continue
else:
validation_errors[coll_name] = [
f"'{coll_name}' is not a known schema collection name"
]
continue
errors = list(validator.iter_errors({coll_name: coll_docs}))
validation_errors[coll_name] = [e.message for e in errors]
if coll_docs:
if not isinstance(coll_docs, list):
validation_errors[coll_name].append("value must be a list")
elif not all(isinstance(d, dict) for d in coll_docs):
validation_errors[coll_name].append(
"all elements of list must be dicts"
)
if not validation_errors[coll_name]:
try:
with OverlayDB(mdb) as odb:
odb.replace_or_insert_many(coll_name, coll_docs)
except OverlayDBError as e:
validation_errors[coll_name].append(str(e))
if all(len(v) == 0 for v in validation_errors.values()):
# Second pass. Try instantiating linkml-sourced dataclass
in_docs.pop("@type", None)
try:
NMDCDatabase(**in_docs)
except Exception as e:
return {"result": "errors", "detail": str(e)}
return {"result": "All Okay!"}
else:
return {"result": "errors", "detail": validation_errors}