From c62868e69e2d45214d2392988fae1fef8b178b9e Mon Sep 17 00:00:00 2001 From: Phil Owen <19691521+PhillipsOwen@users.noreply.github.com> Date: Mon, 1 May 2023 16:21:52 -0400 Subject: [PATCH] adding the retrieval of catalog member data for PSC --- src/common/pg_impl.py | 18 ++++++------ src/server.py | 67 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 69 insertions(+), 16 deletions(-) diff --git a/src/common/pg_impl.py b/src/common/pg_impl.py index f73307d..82efaf5 100644 --- a/src/common/pg_impl.py +++ b/src/common/pg_impl.py @@ -50,6 +50,7 @@ def get_terria_map_catalog_data(self, **kwargs): """ gets the catalog data for the terria map UI + :param **kwargs :return: """ # init the return @@ -121,24 +122,23 @@ def get_obs_station_data(self, **kwargs): # return the data return observations_list - def get_run_prop_urls(self, source_type, run_date, end_date) -> dict: + def get_catalog_member_records(self, **kwargs) -> dict: """ - gets the image urls in the run props for a source type (ASGS, ECFLOW, etc. + gets the apsviz catalog member record for the run id and project code passed. the SP default + record count returned can be overridden. - :param source_type: - :param run_date: - :param end_date: + :param **kwargs :return: """ - # init the return ret_val: dict = {} - # create the sql - sql: str = f"SELECT public.get_run_prop_urls(_source_type := '{source_type}', _run_date := '{run_date}', _end_date := '{end_date}');" + # create the sql. note we are appending a '%' wildcard to get all products for this run + sql: str = f"SELECT public.get_catalog_member_records(_run_id := {kwargs['run_id']}, _project_code := {kwargs['project_code']}, " \ + f"_limit := {kwargs['limit']});" # get the layer list - ret_val = self.exec_sql('asgs', sql) + ret_val = self.exec_sql('apsviz', sql) # return the data return ret_val diff --git a/src/server.py b/src/server.py index 61955b8..2851829 100644 --- a/src/server.py +++ b/src/server.py @@ -50,7 +50,7 @@ async def get_terria_map_catalog_data(grid_type: Union[str, None] = Query(defaul storm_name: Union[str, None] = Query(default=None), cycle: Union[str, None] = Query(default=None), advisory_number: Union[str, None] = Query(default=None), run_date: Union[str, None] = Query(default=None), end_date: Union[str, None] = Query(default=None), project_code: Union[str, None] = Query(default=None), - product_type: Union[str, None] = Query(default=None), limit: Union[int, None] = Query(default=4)) -> json: + product_type: Union[str, None] = Query(default=None), limit: Union[int, None] = Query(default=7)) -> json: """ Gets the json formatted terria map UI catalog data.
Note: Leave filtering params empty if not desired. @@ -65,7 +65,7 @@ async def get_terria_map_catalog_data(grid_type: Union[str, None] = Query(defaul
   end_date: Filter by the data between the run date and end date
   project_code: Filter by the project code
   product_type: Filter by the product type -
   limit: Limit the number of catalog records returned (default is 4) +
   limit: Limit the number of catalog records returned in days (default is 7) """ # pylint: disable=unused-argument # pylint: disable=too-many-arguments @@ -120,7 +120,7 @@ async def get_terria_map_catalog_data_file(file_name: Union[str, None] = Query(d storm_name: Union[str, None] = Query(default=None), cycle: Union[str, None] = Query(default=None), advisory_number: Union[str, None] = Query(default=None), run_date: Union[str, None] = Query(default=None), end_date: Union[str, None] = Query(default=None), project_code: Union[str, None] = Query(default=None), - product_type: Union[str, None] = Query(default=None), limit: Union[int, None] = Query(default=4)) -> json: + product_type: Union[str, None] = Query(default=None), limit: Union[int, None] = Query(default=7)) -> json: """ Returns the json formatted terria map UI catalog data in a file specified.
Note: Leave filtering params empty if not desired. @@ -136,7 +136,7 @@ async def get_terria_map_catalog_data_file(file_name: Union[str, None] = Query(d
   end_date: Filter by the data between the run date and end date
   project_code: Filter by the project code
   product_type: Filter by the product type -
   limit: Limit the number of catalog records returned (default is 4) +
   limit: Limit the number of catalog records returned in days (default is 7) """ # pylint: disable=unused-argument # pylint: disable=too-many-arguments @@ -172,7 +172,7 @@ async def get_terria_map_catalog_data_file(file_name: Union[str, None] = Query(d ret_val: dict = db_info.get_terria_map_catalog_data(**kwargs) # check the return, no catalog data gets not found warning - if ret_val['catalog'] is None : + if ret_val['catalog'] is None: # set a warning message ret_val = {'Warning': 'No data found using the filter criteria selected.'} @@ -266,6 +266,59 @@ def get_obs_station_data(station_name: Union[str, None] = Query(default=None), s return PlainTextResponse(content=ret_val, status_code=status_code, media_type="text/plain") +@APP.get('/get_catalog_member_records', status_code=200, response_model=None) +async def get_catalog_member_records(run_id: Union[str, None] = Query(default=None), project_code: Union[str, None] = Query(default=None), + limit: Union[int, None] = Query(default=4)) -> json: + """ + Gets the json formatted catalog member data. +
Note: Leave filtering params empty if not desired. +
   run_id: Filter by the name of the ASGS grid. Leaving this empty will result in getting the latest records. +
   project_code: Filter by the project code. +
   limit: limit the number of records returned. only applicable when run_id is empty. + """ + # init the returned data and html status code + ret_val: dict = {} + status_code: int = 200 + + try: + logger.debug('Input params - run_id: %s, project_code: %s, limit: %s', run_id, project_code, limit) + + # init the kwargs variable + kwargs: dict = {} + + # create the param list + params: list = ['run_id', 'project_code', 'limit'] + + # if we get a run id add on a wildcard for the search + if run_id is not None: + run_id += '%' + + # loop through the SP params passed in + for param in params: + # add this parm to the list + kwargs.update({param: 'null' if not locals()[param] else f"'{locals()[param]}'"}) + + # try to make the call for records + ret_val: dict = db_info.get_catalog_member_records(**kwargs) + + # check the return + if ret_val == -1: + ret_val = {'Warning': 'No data found using the filter criteria selected.'} + + except Exception: + # return a failure message + ret_val = {'Error': 'Exception detected trying to get the catalog member data.'} + + # log the exception + logger.exception(ret_val) + + # set the status to a server error + status_code = 500 + + # return to the caller + return JSONResponse(content=ret_val, status_code=status_code, media_type="application/json") + + @APP.get('/get_pulldown_data', status_code=200, response_model=None) async def get_pulldown_data(grid_type: Union[str, None] = Query(default=None), event_type: Union[str, None] = Query(default=None), instance_name: Union[str, None] = Query(default=None), met_class: Union[str, None] = Query(default=None), @@ -320,8 +373,8 @@ async def get_pulldown_data(grid_type: Union[str, None] = Query(default=None), e # if PSC output is requested elif psc_output: # collect the choices - choices_data: dict = {'model': ['nhc', 'gfs'], 'storm': ret_val['storm_names'], - 'mesh': ret_val['grid_types'], 'advisory': ret_val['advisory_numbers'], 'ensembleMember': ret_val['event_types'], + choices_data: dict = {'model': ['nhc', 'gfs'], 'storm': ret_val['storm_names'], 'mesh': ret_val['grid_types'], + 'advisory': ret_val['advisory_numbers'], 'ensembleMember': ret_val['event_types'], 'metric': ret_val['product_types'], 'cycle': ret_val['cycles'], 'datetime': ret_val['run_dates']} # create a new dict for return