diff --git a/python/valis/db/queries.py b/python/valis/db/queries.py index c539306..a112296 100644 --- a/python/valis/db/queries.py +++ b/python/valis/db/queries.py @@ -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 diff --git a/python/valis/routes/query.py b/python/valis/routes/query.py index 13675b4..f70c347 100644 --- a/python/valis/routes/query.py +++ b/python/valis/routes/query.py @@ -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 @@ -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):