Skip to content

Commit

Permalink
Implement /target/parents endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Aug 7, 2024
1 parent 5fbd882 commit 415eb89
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ httpx = "^0.24.0"
astroquery = "^0.4.6"
pandas = "^1.5.3"
SQLAlchemy = "^1.4.35"
sdssdb = "^0.12.3"
sdssdb = "^0.12.4"
deepmerge = "^1.1.1"
fuzzy-types = "^0.1.3"
sdss-solara = {git = "https://github.com/sdss/sdss_solara.git", rev = "main", optional = true}
Expand Down
12 changes: 11 additions & 1 deletion python/valis/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ class CatalogModel(PeeweeBase):
parallax: Optional[float] = None


class ParentCatalogModel(PeeweeBase):
"""Pydantic model for parent catalog information """

sdss_id: int
catalogid: int
catalog: str
field_name: str
parent_catalog_id: int | str


class CatalogResponse(CatalogModel, SDSSidFlatBase):
""" Response model for source catalog and sdss_id information """
pass
Expand Down Expand Up @@ -243,4 +253,4 @@ class MapperName(str, Enum):
"""Mapper names"""
MWM: str = 'MWM'
BHM: str = 'BHM'
LVM: str = 'LVM'
LVM: str = 'LVM'
27 changes: 27 additions & 0 deletions python/valis/db/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,33 @@ def get_catalog_sources(sdss_id: int) -> peewee.ModelSelect:
join(s, on=(s.c.catalogid == cat.Catalog.catalogid)).order_by(cat.Catalog.version.desc())


def get_parent_catalog_data(sdss_id: int, catalog: str) -> peewee.ModelSelect:
"""Returns parent catalog data for a given target."""

SID = cat.SDSS_ID_To_Catalog

cat_field: peewee.Field | None = None
cat_pk_name: str | None = None
for field in SID._meta.fields.values():
if '__' not in field.name:
continue
if field.name.split('__')[0] == catalog:
cat_field = field
cat_pk_name = field.name.split('__')[1]
break

if cat_field is None or cat_pk_name is None:
raise ValueError(f'Catalog {catalog} not found in SDSS_ID_To_Catalog table.')

return (SID.select(SID.sdss_id,
SID.catalogid,
peewee.Value(catalog).alias('catalog'),
peewee.Value(cat_pk_name).alias('field_name'),
field.alias('parent_catalog_id'))
.where(SID.sdss_id == sdss_id)
.order_by(SID.catalogid))


def get_target_cartons(sdss_id: int) -> peewee.ModelSelect:
""" Get the carton info for a target sdss_id
Expand Down
25 changes: 23 additions & 2 deletions python/valis/routes/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

from valis.routes.base import Base
from valis.db.queries import (get_target_meta, get_a_spectrum, get_catalog_sources,
get_target_cartons, get_target_pipeline)
get_parent_catalog_data, get_target_cartons,
get_target_pipeline)
from valis.db.db import get_pw_db
from valis.db.models import CatalogResponse, CartonModel, PipesModel, SDSSModel
from valis.db.models import CatalogResponse, CartonModel, ParentCatalogModel, PipesModel, SDSSModel

router = APIRouter()

Expand Down Expand Up @@ -183,6 +184,26 @@ async def get_catalogs(self, sdss_id: int = Path(title="The sdss_id of the targe
""" Return catalog information for a given sdss_id """
return get_catalog_sources(sdss_id).dicts().iterator()

@router.get('/parents/{parent_catalog}/{sdss_id}',
dependencies=[Depends(get_pw_db)],
response_model=ParentCatalogModel,
summary='Retrieve parent catalog information for a taget by sdss_id')
async def get_parents(self,
parent_catalog: str = Path(title='The parent catalog to search',
example='gaia_dr3_source'),
sdss_id = Path(title='The sdss_id of the target to get',
example=129055990)):
"""Return parent catalog information for a given sdss_id """

try:
result = get_parent_catalog_data(sdss_id, parent_catalog).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=500, detail=f'Error: {e}')

return result[0]

@router.get('/cartons/{sdss_id}', summary='Retrieve carton information for a target sdss_id',
dependencies=[Depends(get_pw_db)],
response_model=List[CartonModel],
Expand Down

0 comments on commit 415eb89

Please sign in to comment.