Skip to content

Commit

Permalink
Adds new route for database metadata (#9)
Browse files Browse the repository at this point in the history
* adding db metadata call

* adding model descriptions
  • Loading branch information
havok2063 authored Dec 17, 2023
1 parent 26ebc6b commit 4ce19bc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
10 changes: 9 additions & 1 deletion python/valis/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,12 @@ class PipesModel(PeeweeBase):
astra: Optional[dict] = None



class DbMetadata(PeeweeBase):
""" Pydantic response model for the db metadata """
schema: str = Field(..., description='the database schema name')
table_name: str = Field(..., description='the database table name')
column_name: str = Field(..., description='the database column name')
display_name: str = Field(..., description='a human-readable display name for the column')
description: str = Field(..., description='a description of the database column')
unit: Optional[str] = Field(None, description='the unit if any for the database column')
sql_type: Optional[str] = Field(None, description='the data type of the column')
22 changes: 22 additions & 0 deletions python/valis/db/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,25 @@ def get_target_cartons(sdss_id: int) -> peewee.ModelSelect:
join(targetdb.CartonToTarget).join(targetdb.Carton).where(vizdb.SDSSidFlat.sdss_id == sdss_id).\
order_by(targetdb.Carton.run_on, vizdb.SDSSidFlat.catalogid)


def get_db_metadata(schema: str = None) -> peewee.ModelSelect:
""" Get the sdss5db database metadata
Get the sdss5db database table and column metadata.
By default returns all schema, but a specific one can be
specified with the ``schema`` keyword.
Parameters
----------
schema : str, optional
the database schema name, by default None
Returns
-------
peewee.ModelSelect
the output query
"""
query = vizdb.DbMetadata.select()
if schema:
query = query.where(vizdb.DbMetadata.schema == schema)
return query
25 changes: 19 additions & 6 deletions python/valis/routes/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from __future__ import print_function, division, absolute_import

from typing import List, Union, Dict
from typing import List, Union, Dict, Annotated
from fastapi import APIRouter, HTTPException, Depends, Query, Path
from fastapi_restful.cbv import cbv
from fastapi_restful.enums import StrEnum
Expand All @@ -22,6 +22,9 @@
SDSSDataModel = None
Phases = Surveys = Releases = Tags = ProductModel = SchemaModel = None

from valis.db.db import get_pw_db
from valis.db.models import DbMetadata
from valis.db.queries import get_db_metadata
from valis.routes.base import Base, release
from valis.routes.auth import set_auth

Expand Down Expand Up @@ -91,7 +94,7 @@ async def get_surveys(self, dm: SDSSDataModel = Depends(get_datamodel)) -> dict:
return {'surveys': dm.surveys.model_dump()}

@router.get("/tags", summary='Get metadata on SDSS software tags', response_model=TagModel, response_model_exclude_unset=True)
async def get_tags(self, group: TagGroup = Query(None, description='group the tags by release or survey'),
async def get_tags(self, group: Annotated[TagGroup, Query(description='group the tags by release or survey')] = None,
dm: SDSSDataModel = Depends(get_datamodel)) -> dict:
""" Retrieve a dictionary of SDSS software tags """
if group == 'release':
Expand All @@ -101,23 +104,33 @@ async def get_tags(self, group: TagGroup = Query(None, description='group the ta
else:
return {'tags': dm.tags.model_dump()}

@router.get("/products", summary='Get a list of SDSS data products', dependencies=[Depends(set_auth)], response_model=ProductResponse)
@router.get("/products", summary='Get a list of SDSS data products', dependencies=[Depends(set_auth)],
response_model=ProductResponse)
async def list_products(self, prods: list = Depends(get_products)) -> dict:
""" Get a list of SDSS data products that have defined SDSS datamodels """
return {'products': [p.name for p in prods]}

@router.get("/products/{name}", summary='Retrieve a datamodel for an SDSS product', dependencies=[Depends(set_auth)], response_model=ProductModel)
async def get_product(self, name: str = Path(..., description='The datamodel file species name', example='sdR'), prods: list = Depends(get_products)) -> dict:
@router.get("/products/{name}", summary='Retrieve a datamodel for an SDSS product', dependencies=[Depends(set_auth)],
response_model=ProductModel)
async def get_product(self, name: Annotated[str, Path(description='The datamodel file species name', example='sdR')],
prods: list = Depends(get_products)) -> dict:
""" Get the JSON datamodel for an SDSS data product """
product = [i for i in prods if i.name == name]
if not product:
raise HTTPException(status_code=400, detail=f'{name} not found a valid SDSS data product for release {self.release}')
return product[0].get_content(by_alias=True)

@router.get("/schema/{name}", summary='Retrieve the datamodel schema for an SDSS product', dependencies=[Depends(set_auth)])#, response_model=SchemaModel)
async def get_schema(self, name: str = Path(..., description='The datamodel file species name', example='sdR'), prods: list = Depends(get_products)) -> dict:
async def get_schema(self, name: Annotated[str, Path(description='The datamodel file species name', example='sdR')],
prods: list = Depends(get_products)) -> dict:
""" Get the Pydantic schema describing an SDSS product """
product = [i for i in prods if i.name == name]
if not product:
raise HTTPException(status_code=400, detail=f'{name} not found a valid SDSS data product for release {self.release}')
return product[0].get_schema()

@router.get('/database', summary='Retrieve sdss5db database table and column metadata', dependencies=[Depends(get_pw_db)],
response_model=List[DbMetadata])
async def get_dbmetadata(self, schema: Annotated[str, Query(description='The sdss5db database schema name', example='targetdb')] = None):
""" Get the sdss5db database table and column metadata """
return get_db_metadata(schema=schema).dicts().iterator()

0 comments on commit 4ce19bc

Please sign in to comment.