Skip to content

Commit

Permalink
updating route for getting db metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
havok2063 committed Feb 1, 2024
1 parent 4ce19bc commit aa2142d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion python/valis/db/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def get_pw_db(db_state=Depends(reset_db_state)):
# connect to the db, yield None since we don't need the db in peewee
db = connect_db(pdb, orm='peewee')
try:
yield None
yield db
finally:
if db:
db.close()
Expand Down
10 changes: 9 additions & 1 deletion python/valis/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,24 @@ class CartonModel(PeeweeBase):
run_on: Optional[datetime.datetime] = None


class PipeFiles(BaseModel):
""" Pydantic model for lists of files """
boss: Optional[str] = None
apogee: Optional[str] = None
astra: Optional[str] = None


class PipesModel(PeeweeBase):
""" Pydantic model for pipeline metadata """
boss: Optional[BossSpectrum] = None
apogee: Optional[dict] = None
astra: Optional[dict] = None
files: Optional[PipeFiles] = None


class DbMetadata(PeeweeBase):
""" Pydantic response model for the db metadata """
schema: str = Field(..., description='the database schema name')
dbschema: str = Field(..., description='the database schema name', alias='schema')
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')
Expand Down
16 changes: 13 additions & 3 deletions python/valis/routes/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import print_function, division, absolute_import

from collections import defaultdict
from typing import List, Union, Dict, Annotated
from fastapi import APIRouter, HTTPException, Depends, Query, Path
from fastapi_restful.cbv import cbv
Expand Down Expand Up @@ -42,6 +43,14 @@ def get_products(release: str = Depends(release), dm: SDSSDataModel = Depends(ge
return products.get(release, [])


def convert_metadata(data) -> dict:
""" Convert db metadata output to a dict of dicts """
mm = defaultdict(dict)
for i in data:
mm[i['schema']].update({i['column_name']: i})
return mm


class InfoModel(BaseModel):
""" Resposne model for info endpoint """
description: str = None
Expand Down Expand Up @@ -129,8 +138,9 @@ async def get_schema(self, name: Annotated[str, Path(description='The datamodel
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])
@router.get('/database', summary='Retrieve sdss5db database table and column metadata',
dependencies=[Depends(get_pw_db)],
response_model=Dict[str, Dict[str, 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()
return convert_metadata(get_db_metadata(schema=schema).dicts().iterator())

0 comments on commit aa2142d

Please sign in to comment.