Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EcephysSessions from SLIMS #289

Merged
merged 4 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ server = [
'fastapi==0.115.0',
'uvicorn[standard]==0.31.0',
'python-dateutil',
'aind-slims-api==0.1.15',
'aind-slims-api==0.1.17',
'azure-identity==1.15.0'
]

Expand Down
47 changes: 37 additions & 10 deletions src/aind_metadata_service/response_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
ViralMaterial,
)
from aind_data_schema.core.rig import Rig
from aind_data_schema.core.session import Session
from aind_data_schema.core.subject import Subject
from aind_metadata_mapper.core import JobResponse
from fastapi import Response
Expand All @@ -35,6 +36,7 @@
ProtocolInformation,
Instrument,
Rig,
Session,
)


Expand Down Expand Up @@ -89,37 +91,42 @@ def no_data_found_error_response(cls):
message="No Data Found.",
)

@staticmethod
def _validate_model(model) -> Optional[str]:
"""Helper method to validate a model and return validation errors."""
validation_error = None
try:
model.__class__.model_validate(model.model_dump())
except ValidationError as e:
validation_error = repr(e)
except (AttributeError, ValueError, KeyError) as oe:
validation_error = repr(oe)
return validation_error

def _map_data_response( # noqa: C901
self, validate: bool = True
) -> Union[Response, JSONResponse]:
"""Map ModelResponse with StatusCodes.DB_RESPONDED to a JSONResponse.
Perform validations, bypasses validation if flag is set to False."""

if len(self.aind_models) == 0:
status_code = StatusCodes.NO_DATA_FOUND.value
content_data = None
message = "No Data Found."

elif len(self.aind_models) == 1:
aind_model = self.aind_models[0]
content_data = jsonable_encoder(
json.loads(aind_model.model_dump_json())
)
if validate:
validation_error = None
try:
aind_model.__class__.model_validate(
aind_model.model_dump()
)
except ValidationError as e:
validation_error = repr(e)
except (AttributeError, ValueError, KeyError) as oe:
validation_error = repr(oe)
validation_error = self._validate_model(aind_model)
if validation_error:
status_code = StatusCodes.INVALID_DATA.value
message = f"Validation Errors: {validation_error}"
else:
status_code = StatusCodes.VALID_DATA.value
message = "Valid Model."
# if validate flag is False
else:
status_code = StatusCodes.UNPROCESSIBLE_ENTITY.value
message = (
Expand All @@ -132,13 +139,33 @@ def _map_data_response( # noqa: C901
"There was an error retrieving records from one or more of"
" the databases."
)

else:
status_code = StatusCodes.MULTIPLE_RESPONSES.value
message = "Multiple Items Found."
content_data = [
jsonable_encoder(json.loads(model.model_dump_json()))
for model in self.aind_models
]

if validate:
# Validate each model and accumulate errors
validation_errors = []
for model in self.aind_models:
error = self._validate_model(model)
print(error)
if error:
validation_errors.append(error)

if validation_errors:
message += (
f" Validation Errors: {', '.join(validation_errors)}"
)
else:
message += " All Models Valid."
else:
message += " Models have not been validated."

return JSONResponse(
status_code=status_code,
content=({"message": message, "data": content_data}),
Expand Down
11 changes: 10 additions & 1 deletion src/aind_metadata_service/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
SharePointClient,
SharepointSettings,
)
from aind_metadata_service.slims.client import SlimsHandler, SlimsSettings
from aind_metadata_service.smartsheet.client import (
SmartSheetClient,
SmartsheetSettings,
Expand All @@ -36,7 +37,6 @@
)
from aind_metadata_service.tars.client import AzureSettings, TarsClient
from aind_metadata_service.tars.mapping import TarsResponseHandler
from aind_metadata_service.slims.client import SlimsSettings, SlimsHandler

SMARTSHEET_FUNDING_ID = os.getenv("SMARTSHEET_FUNDING_ID")
SMARTSHEET_FUNDING_TOKEN = os.getenv("SMARTSHEET_API_TOKEN")
Expand Down Expand Up @@ -122,6 +122,15 @@ async def retrieve_rig(rig_id):
return model_response.map_to_json_response(validate=False)


@app.get("/ecephys_sessions_by_subject/{subject_id}")
async def retrieve_sessions(subject_id):
"""Retrieves sessions from slims"""
model_response = await run_in_threadpool(
slims_client.get_sessions_model_response, subject_id=subject_id
)
return model_response.map_to_json_response()


@app.get("/protocols/{protocol_name}")
async def retrieve_protocols(
protocol_name,
Expand Down
31 changes: 28 additions & 3 deletions src/aind_metadata_service/slims/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@

from aind_data_schema.core.instrument import Instrument
from aind_data_schema.core.rig import Rig
from aind_slims_api import SlimsClient
from aind_slims_api.exceptions import SlimsRecordNotFound
from aind_slims_api.models.instrument import SlimsInstrumentRdrc
from aind_slims_api.operations.ecephys_session import fetch_ecephys_sessions
from pydantic import Extra, Field, SecretStr
from pydantic_settings import BaseSettings
from requests.models import Response
from slims.criteria import equals

from aind_metadata_service.client import StatusCodes
from aind_metadata_service.response_handler import ModelResponse
from aind_slims_api.exceptions import SlimsRecordNotFound
from aind_slims_api import SlimsClient
from aind_slims_api.models.instrument import SlimsInstrumentRdrc
from aind_metadata_service.slims.mapping import SlimsSessionMapper


class SlimsSettings(BaseSettings):
Expand Down Expand Up @@ -114,3 +116,26 @@ def get_rig_model_response(self, input_id) -> ModelResponse:
except Exception as e:
logging.error(repr(e))
return ModelResponse.internal_server_error_response()

def get_sessions_model_response(self, subject_id: str) -> ModelResponse:
"""
Fetches sessions for a given subject ID from SLIMS.
"""
try:
sessions = fetch_ecephys_sessions(
subject_id=subject_id, client=self.client
)
if sessions:
mapper = SlimsSessionMapper()
mapped_sessions = mapper.map_sessions(sessions, subject_id)
return ModelResponse(
aind_models=mapped_sessions,
status_code=StatusCodes.DB_RESPONDED,
)
else:
return ModelResponse.no_data_found_error_response()
except SlimsRecordNotFound:
return ModelResponse.no_data_found_error_response()
except Exception as e:
logging.error(repr(e))
return ModelResponse.internal_server_error_response()
Loading
Loading