From 7a4cf1efceb67c2f621f7016d722fabcb634439d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20S=C3=A1nchez-Gallego?= Date: Wed, 12 Jun 2024 16:47:40 -0600 Subject: [PATCH 1/3] Modify carton_program_search to accept initial query --- python/valis/db/queries.py | 29 +++++++++++++++++++---------- python/valis/routes/query.py | 7 ++++--- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/python/valis/db/queries.py b/python/valis/db/queries.py index 83ba379..6f4108b 100644 --- a/python/valis/db/queries.py +++ b/python/valis/db/queries.py @@ -243,7 +243,9 @@ def carton_program_map(key: str = 'program') -> dict: return mapping -def carton_program_search(name: str, name_type: str) -> peewee.ModelSelect: +def carton_program_search(name: str, + name_type: str, + query: peewee.ModelSelect | None = None) -> peewee.ModelSelect: """ Perform a search on either carton or program Parameters @@ -252,22 +254,29 @@ def carton_program_search(name: str, name_type: str) -> peewee.ModelSelect: Either the carton name or the program name name_type: str Which type you are searching on, either 'carton' or 'program' + query : ModelSelect + An initial query to extend. If ``None``, a new query with all the unique + ``sdss_id``s is created (note: this is probably a bad idea). Returns ------- peewee.ModelSelect the ORM query """ - model = vizdb.SDSSidFlat.select(peewee.fn.DISTINCT(vizdb.SDSSidFlat.sdss_id))\ - .join(targetdb.Target, - on=(targetdb.Target.catalogid == vizdb.SDSSidFlat.catalogid))\ - .join(targetdb.CartonToTarget)\ - .join(targetdb.Carton)\ - .where(getattr(targetdb.Carton, name_type) == name) - return vizdb.SDSSidStacked.select().join( - model, on=(model.c.sdss_id == vizdb.SDSSidStacked.sdss_id) - ) + if query is None: + query = vizdb.SDSSidFlat.select(peewee.fn.DISTINCT(vizdb.SDSSidFlat.sdss_id)) + + query = (query.join( + vizdb.SDSSidFlat, + on=(vizdb.SDSSidFlat.sdss_id == vizdb.SDSSidStacked.sdss_id)) + .join(targetdb.Target, + on=(targetdb.Target.catalogid == vizdb.SDSSidFlat.catalogid)) + .join(targetdb.CartonToTarget) + .join(targetdb.Carton) + .where(getattr(targetdb.Carton, name_type) == name)) + + return query def get_targets_obs(release: str, obs: str, spectrograph: str) -> peewee.ModelSelect: """ Return all targets with spectra from a given observatory diff --git a/python/valis/routes/query.py b/python/valis/routes/query.py index 13675b4..31e42df 100644 --- a/python/valis/routes/query.py +++ b/python/valis/routes/query.py @@ -92,9 +92,10 @@ async def main_search(self, body: SearchModel): query = get_targets_by_sdss_id(body.id) # build the program/carton query - elif body.program or body.carton: - query = carton_program_search(body.program or body.carton, 'program' if body.program else 'carton') - + if body.program or body.carton: + query = carton_program_search(body.program or body.carton, + 'program' if body.program else 'carton', + query=query) # append query to pipes query = append_pipes(query) From 11ed193a084121b7529bfca9a22a55d422dd9280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20S=C3=A1nchez-Gallego?= Date: Wed, 12 Jun 2024 16:51:12 -0600 Subject: [PATCH 2/3] Remove comment --- python/valis/db/queries.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/valis/db/queries.py b/python/valis/db/queries.py index 6f4108b..0522c5e 100644 --- a/python/valis/db/queries.py +++ b/python/valis/db/queries.py @@ -256,7 +256,7 @@ def carton_program_search(name: str, Which type you are searching on, either 'carton' or 'program' query : ModelSelect An initial query to extend. If ``None``, a new query with all the unique - ``sdss_id``s is created (note: this is probably a bad idea). + ``sdss_id``s is created. Returns ------- From 03e0ec05a4fa49bcaa9b87f903b15086cc6d6b62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20S=C3=A1nchez-Gallego?= Date: Tue, 25 Jun 2024 19:13:56 -0700 Subject: [PATCH 3/3] Initial query should be SDSSidStacked instead of SDSSidFlat --- python/valis/db/queries.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/valis/db/queries.py b/python/valis/db/queries.py index 0522c5e..6ea11ce 100644 --- a/python/valis/db/queries.py +++ b/python/valis/db/queries.py @@ -265,7 +265,7 @@ def carton_program_search(name: str, """ if query is None: - query = vizdb.SDSSidFlat.select(peewee.fn.DISTINCT(vizdb.SDSSidFlat.sdss_id)) + query = vizdb.SDSSidStacked.select(peewee.fn.DISTINCT(vizdb.SDSSidStacked.sdss_id)) query = (query.join( vizdb.SDSSidFlat, @@ -753,7 +753,7 @@ def get_paged_target_list_by_mapper(mapper: MapperName = MapperName.MWM, page_nu peewee.ModelSelect the ORM query """ - + if mapper is MapperName.MWM: where_condition = vizdb.SDSSidToPipes.in_apogee == True elif mapper is MapperName.BHM: