From 10fb95694d341d0375cfaebc5b33aa5bf75703e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20S=C3=A1nchez-Gallego?= Date: Mon, 12 Aug 2024 09:19:14 -0700 Subject: [PATCH] Return all rows of parent data for a given sdss_id and catalogid --- python/valis/db/queries.py | 6 +++++- python/valis/routes/target.py | 17 ++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/python/valis/db/queries.py b/python/valis/db/queries.py index 37383f2..1e8b2ee 100644 --- a/python/valis/db/queries.py +++ b/python/valis/db/queries.py @@ -696,7 +696,7 @@ def get_catalog_sources(sdss_id: int) -> peewee.ModelSelect: order_by(cat.Catalog.version.desc()) -def get_parent_catalog_data(sdss_id: int, catalog: str) -> peewee.ModelSelect: +def get_parent_catalog_data(sdss_id: int, catalog: str, catalogid: int | None = None) -> peewee.ModelSelect: """Returns parent catalog data for a given target.""" SID = cat.SDSS_ID_To_Catalog @@ -707,9 +707,13 @@ def get_parent_catalog_data(sdss_id: int, catalog: str) -> peewee.ModelSelect: ParentModel = cat.database.models[fqtn] + cid_condition = (SID.catalogid == catalogid) if catalogid is not None else True + return (SID.select(SID.sdss_id, SID.catalogid, ParentModel) + .distinct(SID.sdss_id, SID.catalogid) .join(ParentModel) .where(SID.sdss_id == sdss_id) + .where(cid_condition) .order_by(SID.catalogid)) diff --git a/python/valis/routes/target.py b/python/valis/routes/target.py index f799439..c049f00 100644 --- a/python/valis/routes/target.py +++ b/python/valis/routes/target.py @@ -198,24 +198,31 @@ async def get_catalogs(self, sdss_id: int = Path(title="The sdss_id of the targe @router.get('/parents/{catalog}/{sdss_id}', dependencies=[Depends(get_pw_db)], - response_model=ParentCatalogModel, + response_model=list[ParentCatalogModel], responses={400: {'description': 'Invalid input sdss_id or catalog'}}, summary='Retrieve parent catalog information for a taget by sdss_id') async def get_parents(self, catalog: Annotated[str, Path(description='The parent catalog to search', example='gaia_dr3_source')], sdss_id: Annotated[int, Path(description='The sdss_id of the target to get', - example=129055990)]): - """Return parent catalog information for a given sdss_id """ + example=129047350)], + catalogid: Annotated[int, Query(description='Restrict the list of returned entries to this catalogid', + example=63050396587194280)]=None): + """Return parent catalog information for a given sdss_is. + + Returns a list of mappings for each set of parent catalogs associated + with the catalogid and sdss_id. + + """ try: - result = get_parent_catalog_data(sdss_id, catalog).dicts() + result = get_parent_catalog_data(sdss_id, catalog, catalogid=catalogid).dicts() if len(result) == 0: raise ValueError(f'No parent catalog data found for sdss_id {sdss_id}') except Exception as e: raise HTTPException(status_code=400, detail=f'Error: {e}') - return result[0] + return result @router.get('/cartons/{sdss_id}', summary='Retrieve carton information for a target sdss_id', dependencies=[Depends(get_pw_db)],