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

Return all rows of parent data for a given sdss_id and catalogid #43

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading