Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion nmdc_runtime/api/endpoints/lib/linked_instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@

"""

from datetime import timedelta
from typing import Literal, Any

from bson import ObjectId
from pymongo.collection import Collection as MongoCollection
from pymongo.database import Database as MongoDatabase
from toolz import merge

from nmdc_runtime.api.core.util import hash_from_str
from nmdc_runtime.api.core.util import hash_from_str, now
from nmdc_runtime.api.db.mongo import get_mongo_db
from nmdc_runtime.util import get_class_name_to_collection_names_map, nmdc_schema_view


Expand All @@ -35,6 +37,17 @@ def temp_linked_instances_collection_name(ids: list[str], types: list[str]) -> s
return f"_runtime.tmp.linked_instances.{hash_from_ids_and_types(ids=ids,types=types)}.{ObjectId()}"


def drop_stale_temp_linked_instances_collections():
"""Drop any temporary linked-instances collections that were generated earlier than one day ago."""
mdb = get_mongo_db()
one_day_ago = now() - timedelta(days=1)
for collection_name in mdb.list_collection_names(
filter={"name": {"$regex": r"^_runtime.tmp.linked_instances\..*"}}
):
if ObjectId(collection_name.split(".")[-1]).generation_time < one_day_ago:
mdb.drop_collection(collection_name)


def gather_linked_instances(
alldocs_collection: MongoCollection,
ids: list[str],
Expand Down
6 changes: 5 additions & 1 deletion nmdc_runtime/api/endpoints/nmdcschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import List, Dict, Annotated

import pymongo
from fastapi import APIRouter, Depends, HTTPException, Path, Query
from fastapi import APIRouter, Depends, HTTPException, Path, Query, BackgroundTasks
from pydantic import AfterValidator
from refscan.lib.helpers import (
get_collection_names_from_schema,
Expand All @@ -13,6 +13,7 @@
from nmdc_runtime.api.endpoints.lib.linked_instances import (
gather_linked_instances,
hydrated,
drop_stale_temp_linked_instances_collections,
)
from nmdc_runtime.config import IS_LINKED_INSTANCES_ENDPOINT_ENABLED
from nmdc_runtime.minter.config import typecodes
Expand Down Expand Up @@ -127,6 +128,7 @@ def get_nmdc_database_collection_stats(
)
)
def get_linked_instances(
background_tasks: BackgroundTasks,
ids: Annotated[
list[str],
Query(
Expand Down Expand Up @@ -222,6 +224,7 @@ class definition ([linkml:ClassDefinition](https://w3id.org/linkml/ClassDefiniti
[nmdc:Sample](https://w3id.org/nmdc/Sample), etc. -- may be given.
If no value for `types` is given, then all [nmdc:NamedThing](https://w3id.org/nmdc/NamedThing)s are returned.
"""
background_tasks.add_task(drop_stale_temp_linked_instances_collections)
if page_token is not None:
rv = list_resources(
req=ListRequest(page_token=page_token, max_page_size=max_page_size), mdb=mdb
Expand All @@ -237,6 +240,7 @@ class definition ([linkml:ClassDefinition](https://w3id.org/linkml/ClassDefiniti
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Some IDs not found: {ids_not_found}.",
)

types = types or ["nmdc:NamedThing"]
types_possible = set([f"nmdc:{name}" for name in nmdc_schema_view().all_classes()])
types_not_found = list(set(types) - types_possible)
Expand Down
Loading