Skip to content

Commit

Permalink
Return all rows of parent data for a given sdss_id and catalogid (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox authored Aug 13, 2024
1 parent 8c18e81 commit b3f2385
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
6 changes: 5 additions & 1 deletion python/valis/db/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))


Expand Down
17 changes: 12 additions & 5 deletions python/valis/routes/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
Expand Down

0 comments on commit b3f2385

Please sign in to comment.