Skip to content

Commit

Permalink
Cache subject data assets with default ttl 2 mins
Browse files Browse the repository at this point in the history
  • Loading branch information
bjhardcastle committed Jul 11, 2024
1 parent b735450 commit c7f7f22
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/npc_lims/metadata/codeocean.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import os
import re
import time
import uuid
from collections.abc import Mapping, Sequence
from typing import Any, Literal, NamedTuple, TypedDict
Expand Down Expand Up @@ -119,14 +120,24 @@ def get_codeocean_client() -> aind_codeocean_api.CodeOceanClient:
)


def get_subject_data_assets(subject: str | int) -> tuple[DataAssetAPI, ...]:
def _get_ttl_hash(seconds=2 * 60) -> int:
"""Return the same value within `seconds` time period
From https://stackoverflow.com/a/55900800
"""
return round(time.time() / seconds)


@functools.cache
def get_subject_data_assets(subject: str | int, ttl_hash: int | None = None) -> tuple[DataAssetAPI, ...]:
"""
All assets associated with a subject ID.
Examples:
>>> assets = get_subject_data_assets(668759)
>>> assert len(assets) > 0
"""
del ttl_hash # only used for functools.cache
response = get_codeocean_client().search_all_data_assets(
query=f"subject id: {npc_session.SubjectRecord(subject)}"
)
Expand Down Expand Up @@ -157,7 +168,7 @@ def get_session_data_assets(
Any,
],
...,
] = get_subject_data_assets(session.subject)
] = get_subject_data_assets(session.subject, ttl_hash=_get_ttl_hash())
try:
pattern = get_codoecean_session_id(session)
except ValueError: # no raw data uploaded
Expand Down Expand Up @@ -227,7 +238,7 @@ def get_sessions_with_data_assets(
>>> sessions = get_sessions_with_data_assets(668759)
>>> assert len(sessions) > 0
"""
assets = get_subject_data_assets(subject)
assets = get_subject_data_assets(subject, ttl_hash=_get_ttl_hash())
sessions = set()
for asset in assets:
try:
Expand Down Expand Up @@ -367,7 +378,7 @@ def get_codoecean_session_id(
session = npc_session.SessionRecord(session)
data_assets = [
asset
for asset in get_subject_data_assets(session.subject)
for asset in get_subject_data_assets(session.subject, ttl_hash=_get_ttl_hash())
if (
asset["name"].startswith(f"ecephys_{session.subject}_{session.date}")
or asset["name"].startswith(f"behavior_{session.subject}_{session.date}")
Expand Down

0 comments on commit c7f7f22

Please sign in to comment.