diff --git a/README.md b/README.md index 3f09639ac4476c16882812d0ef833bba67cf0404..fbd5b3935bfea19ececea4cffd62669737a98997 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,9 @@ * http://localhost:8000/adex-fastapi/skyviews/ * http://localhost:8000/adex-fastapi/skyviews_rectangle/?ra_min=40&ra_max=50&dec_min=25&dec_max=35&limit=1000 + * http://localhost:8000/adex-fastapi/docs + * http://localhost:8000/adex-fastapi/redoc + ### sdc-dev (test environment) * https://sdc-dev.astron.nl/adex-fastapi/skyviews/ * https://sdc-dev.astron.nl/adex-fastapi/skyviews_rectangle/?ra_min=40&ra_max=50&dec_min=25&dec_max=35&limit=1000 diff --git a/database/crud.py b/database/crud.py index 5064cb4fbbad755b3d20823a549e818b5ca271bf..9e9389fb0974e5512fabf31c4f9883826d5f487f 100644 --- a/database/crud.py +++ b/database/crud.py @@ -17,3 +17,31 @@ def get_skyviews_rectangle(db: Session, ra_min: float = 0.0, ra_max: float = 1.0 ).limit(limit).all() print("retrieved "+str(len(list)) + ' dataproducts') return list + +@timeit +def get_skyviews_dataproducts( + db: Session, + ra_min: float = 0.0, ra_max: float = 1.0, + dec_min: float = 0.0, dec_max: float = 1.0, + limit: int = 1000, + collections: list = None, + beams : list = None, + levels : list = None, + dp_types : list = None, + dp_subtypes: list = None): + + # https://www.tutorialspoint.com/sqlalchemy/sqlalchemy_orm_using_query.htm + # https://docs.sqlalchemy.org/en/14/orm/query.html + list = db.query(SkyView).filter( + SkyView.ra > ra_min, + SkyView.ra < ra_max, + SkyView.dec > dec_min, + SkyView.dec < dec_max, + SkyView.collection.in_(collections), + #SkyView.beam.in_(beams), + SkyView.level.in_(levels), + SkyView.dataproduct_type.in_(dp_types), + SkyView.dataproduct_subtype.in_(dp_subtypes), + ).limit(limit).all() + print("retrieved "+str(len(list)) + ' dataproducts') + return list \ No newline at end of file diff --git a/main.py b/main.py index 107325487cc614cc17e3fc6544ba3e7ea5cef596..3ba56cade57866668304dfd893ed91f808492972 100644 --- a/main.py +++ b/main.py @@ -10,6 +10,9 @@ app = FastAPI( title="ADEX backend", description="ADEX backend FastAPI", version="0.0.1", + docs_url='/adex-fastapi/docs', + redoc_url='/adex-fastapi/redoc', + openapi_url='/adex-fastapi/openapi.json', contact={ "name": "Nico Vermaas", "email": "vermaas@astron.nl", diff --git a/routers/skyviews.py b/routers/skyviews.py index 29b7abc1f086133d5b1942ce8169f5aaf77d53bc..9f9e368c9fe41900b84005bdd528310dcfc3ec40 100644 --- a/routers/skyviews.py +++ b/routers/skyviews.py @@ -1,4 +1,4 @@ -from typing import List +from typing import List, Union from fastapi import APIRouter, Query, Depends from sqlalchemy.orm import Session @@ -15,6 +15,13 @@ def get_db(): finally: db.close() +def get_all_dp_types(): + return ['image','cube','timeSeries','visibility'] + +def get_all_dp_subtypes(): + return ['uncalibratedVisibility','lineCube','continuumCube','beamCube','continuumChunk','polarizationCube', + 'pulsarTimingTimeSeries','compressedUncalibratedVisibility','splitttedUncalibratedVisibility', + 'calibratedImage','calibratedVisibility','continuumMF','polarisationImage'] # http://127.0.0.1:8000/skyviews/ # http://127.0.0.1:8000/skyviews/?skip=100&limit=100 @@ -25,9 +32,60 @@ async def get_skyviews(skip: int = 0, limit: int = 1000, db: Session = Depends(g # http://localhost:8000/adex-fastapi/skyviews_rectangle/?ra_min=40&ra_max=50&dec_min=25&dec_max=35&limit=1000 @router.get("/skyviews_rectangle/", tags=["skyviews"], response_model=List[schemas.SkyView]) -async def get_skyviews_rectangle(ra_min: float = 0.0, ra_max: float = 1.0, - dec_min: float = 0.0, dec_max: float = 1.0, - limit: int = 1000, db: Session = Depends(get_db)): +async def get_skyviews_rectangle(db: Session = Depends(get_db), + ra_min: float = 0.0, ra_max: float = 1.0, dec_min: float = 0.0, dec_max: float = 1.0, + limit: int = 1000): items = crud.get_skyviews_rectangle(db, ra_min=ra_min, ra_max=ra_max, dec_min=dec_min, dec_max=dec_max, limit=limit) return items +# http://localhost:8000/adex-fastapi/skyviews_dataproducts/?ra_min=40&ra_max=50&dec_min=25&dec_max=35&limit=1000&dp_types=image,ube +@router.get("/skyviews_dataproducts/", tags=["skyviews"], response_model=List[schemas.SkyView]) +async def get_skyviews_dataproducts(db: Session = Depends(get_db), + ra_min: float = 0.0, ra_max: float = 1.0, dec_min: float = 0.0, dec_max: float = 1.0, + collections: str = None, + beams: str = None, + levels : str = None, + dp_types: str = None, + dp_subtypes: str = None, + limit: int = 1000): + + # TODO: get rid of these hardcoded values + # no values should result in something like 'all', + # but I don't know how to do that in SQLAlchemy query filters + + try: + collections_list = collections.split(',') + except: + collections_list = ['apertif-imaging','apertif-timeseries'] + + try: + beams_list = beams.split(',') + except: + beams_list = [0] + + try: + levels_list = levels.split(',') + except: + levels_list = [0,1,2] + + try: + dp_types_list = dp_types.split(',') + except: + # if no dp_types are given, assume 'image' (better than nothing) + dp_types_list = get_all_dp_types() + + try: + dp_subtypes_list = dp_subtypes.split(',') + except: + # if no dp_types are given, assume 'image' (better than nothing) + dp_subtypes_list = get_all_dp_subtypes() + + items = crud.get_skyviews_dataproducts( + db, ra_min=ra_min, ra_max=ra_max, dec_min=dec_min, dec_max=dec_max, + collections=collections_list, + beams=beams_list, + levels=levels_list, + dp_types=dp_types_list, + dp_subtypes=dp_subtypes_list, limit=limit) + + return items