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 endpoint for getting an sdss_id list from search value. #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
40 changes: 40 additions & 0 deletions python/valis/db/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,46 @@ def get_paged_target_list_by_mapper(mapper: MapperName = MapperName.MWM, page_nu
.paginate(page_number, items_per_page)


def get_paged_sdss_id_list(search_integer: int, page_number: int = 1, items_per_page: int = 10) -> peewee.ModelSelect:
""" Return a paged list of sdss_id values.
Return paginated and ordered sdss_id column values (based on an input search value)
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
----------
search_integer : int
Integer that matches the starting digits of the returned sdss_id values.
page_number : int
Page number of the returned sdss_id values.
items_per_page : int
Number of sdss_id values displayed in the page.
Returns
-------
peewee.ModelSelect
the ORM query
"""

max_sdss_id = vizdb.SDSSidStacked.select(peewee.fn.MAX(vizdb.SDSSidStacked.sdss_id)).scalar()

max_num_digits = len(str(max_sdss_id))
num_search_digits = len(str(search_integer))
max_i = max_num_digits - num_search_digits + 1

where_condition = (False)
for i in range(0, max_i):
min_id = int(search_integer * 10**(i))
max_id = int((search_integer + 1) * 10**(i))
where = ((vizdb.SDSSidStacked.sdss_id >= min_id) & (vizdb.SDSSidStacked.sdss_id < max_id))
where_condition = where_condition | where

return vizdb.SDSSidStacked.select(vizdb.SDSSidStacked.sdss_id)\
.where(where_condition)\
.order_by(vizdb.SDSSidStacked.sdss_id)\
.paginate(page_number, items_per_page)


def starfields(model: peewee.ModelSelect) -> peewee.NodeList:
""" Return the peewee star fields

Expand Down
12 changes: 11 additions & 1 deletion python/valis/routes/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
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_obs, get_paged_target_list_by_mapper)
get_targets_obs, get_paged_target_list_by_mapper,
get_paged_sdss_id_list)
from sdssdb.peewee.sdss5db import database

# convert string floats to proper floats
Expand Down Expand Up @@ -142,6 +143,15 @@ async def catalog_id_search(self, catalog_id: Annotated[int, Query(description='

return list(get_targets_by_catalog_id(catalog_id))

@router.get('/list/sdssid', summary='Return a paged list of sdss_id values from input search value',
response_model=List[int], dependencies=[Depends(get_pw_db)])
async def sdss_id_list(self, search_integer: Annotated[int, Query(description='Integer that matches the starting digits of the returned sdss_id values', ge=1, example=1)] = 1,
page_number: Annotated[int, Query(description='Page number of the returned items', gt=0, example=1)] = 1,
items_per_page: Annotated[int, Query(description='Number of items displayed in a page', gt=0, example=10)] = 10):
""" Return an ordered and paged list of sdss_id values from input search value """
sdss_ids = get_paged_sdss_id_list(search_integer, page_number, items_per_page)
return list(sdss_ids.scalars())

@router.get('/list/cartons', summary='Return a list of all cartons',
response_model=list, dependencies=[Depends(get_pw_db)])
async def cartons(self):
Expand Down
Loading