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

Modify carton_program_search to accept initial query #23

Merged
merged 4 commits into from
Jun 26, 2024
Merged
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
31 changes: 20 additions & 11 deletions python/valis/db/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

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.SDSSidStacked.select(peewee.fn.DISTINCT(vizdb.SDSSidStacked.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
Expand Down Expand Up @@ -744,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:
Expand Down
7 changes: 4 additions & 3 deletions python/valis/routes/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Loading