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

Adding get_target_list_by_mapper endpoint #14

Merged
merged 4 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
58 changes: 58 additions & 0 deletions python/valis/db/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,61 @@ def get_db_metadata(schema: str = None) -> peewee.ModelSelect:
if schema:
query = query.where(vizdb.DbMetadata.schema == schema)
return query

def get_paged_target_list_by_mapper(mapper: Union[str, int] = "MWM", page_number: int = 1, items_per_page: int = 10) -> peewee.ModelSelect:
""" Return a paged list of target rows, based on the mapper.

Return paginated and ordered target rows (of a particular mapper)
from the vizdb.SDSSidStacked table,
using the peewee ORM. We return the peewee ModelSelect
directly here so it can be easily combined with other queries,
if needed.

Parameters
----------
mapper : Union[str, int]
label or integer id (pk) of the mapper from targetdb.Mapper table.
page_number : int
Page number of the returned target rows.
items_per_page : int
Number of target rows displayed in the page.

Returns
-------
peewee.ModelSelect
the ORM query
"""

where_condition = targetdb.Mapper.pk == mapper if type(mapper) == int else targetdb.Mapper.label == mapper

return vizdb.SDSSidStacked.select()\
.where(\
vizdb.SDSSidStacked.sdss_id.in_(\
vizdb.SDSSidFlat.select(vizdb.SDSSidFlat.sdss_id)\
.join(targetdb.Target, \
on=(vizdb.SDSSidFlat.catalogid == targetdb.Target.catalogid))\
.join(targetdb.CartonToTarget, \
on=(targetdb.Target.pk == targetdb.CartonToTarget.target))\
.join(targetdb.Carton, \
on=(targetdb.Carton.pk == targetdb.CartonToTarget.carton))\
.join(targetdb.Mapper,
on=(targetdb.Mapper.pk == targetdb.Carton.mapper))\
.where(where_condition)\
.order_by(vizdb.SDSSidFlat.sdss_id)\
.paginate(page_number, items_per_page)\
)\
)\
.order_by(vizdb.SDSSidStacked.sdss_id)

def get_mappers() -> peewee.ModelSelect:
"""Return the list of all mapper names.

Return the list of all mapper name labels and their asscociated IDs,
using the peewee ORM. We return the peewee ModelSelect.

Returns
-------
peewee.ModelSelect
the ORM query
"""
return targetdb.Mapper.select().order_by(targetdb.Mapper.pk)
25 changes: 24 additions & 1 deletion python/valis/routes/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from valis.db.models import SDSSidStackedBase, SDSSidPipesBase
from valis.db.queries import (cone_search, append_pipes, carton_program_search,
carton_program_list, carton_program_map,
get_targets_by_sdss_id, get_targets_by_catalog_id)
get_targets_by_sdss_id, get_targets_by_catalog_id,
get_mappers, get_paged_target_list_by_mapper)

# convert string floats to proper floats
Float = Annotated[Union[float, str], BeforeValidator(lambda x: float(x) if x and isinstance(x, str) else x)]
Expand Down Expand Up @@ -48,6 +49,12 @@ class MainSearchResponse(BaseModel):
data: List[MainResponse] = Field(..., description='the list of query results')


class Mapper(BaseModel):
""" Mapper ID and name label """
pk: int = Field(..., description='the mapper id (primary key on the database)')
label: str = Field(..., description='the mapper name label')


router = APIRouter()


Expand Down Expand Up @@ -149,6 +156,12 @@ async def program_map(self):

return carton_program_map()

@router.get('/list/mappers', summary='Return list of all mappers',
response_model=List[Mapper], dependencies=[Depends(get_pw_db)])
async def get_mappers_list(self):
""" Return a list of all mappers """
return list(get_mappers())

@router.get('/carton-program', summary='Search for all SDSS targets within a carton or program',
response_model=List[SDSSidStackedBase], dependencies=[Depends(get_pw_db)])
async def carton_program(self,
Expand All @@ -161,3 +174,13 @@ async def carton_program(self,

return list(carton_program_search(name, name_type))

@router.get('/mapper', summary='Perform a search for SDSS targets based on the mapper',
response_model=List[SDSSidStackedBase], dependencies=[Depends(get_pw_db)])
async def get_target_list_by_mapper(self, mapper: str = Query(default="MWM", description='String (integer) that identifies the name label (id/primary key) of the mapper', example="MWM"),
page_number: int = Query(..., description='Page number of the returned items', gt=0, example=1),
items_per_page: int = Query(..., description='Number of items displayed in a page', gt=0, example=10)):
""" Return an ordered and paged list of targets based on the mapper."""
targets = get_paged_target_list_by_mapper(mapper, page_number, items_per_page)
return list(targets)


Loading