From 610f04dccb8f4ed6be001a14665c4ce231380d8d Mon Sep 17 00:00:00 2001 From: aschmere Date: Thu, 13 Jun 2024 22:14:51 +0200 Subject: [PATCH] feat: Enhance /process endpoint and optimize logging - Allow /process endpoint to function without requiring dataset or location parameters. - Fetch forecasts internally if no dataset is provided. - Use internal locations if no locations are provided. - Optimize logging configuration: - Introduce custom logging formatter for consistent log messages with timestamps. - Prevent duplicate log entries by disabling propagation to the root logger. bump version to 1.4.7 --- .DS_Store | Bin 6148 -> 6148 bytes dwd_global_rad_api_server/app.py | 46 ++++++++++++++++++-------- dwd_global_rad_api_server/config.yaml | 2 +- dwd_global_rad_api_server/main.py | 6 ++-- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/.DS_Store b/.DS_Store index a2c117795ecbd93ac928664204fc72bbc4b37a0f..ff4e6c13f749798df65ff0900bd4772b7fdcabf0 100644 GIT binary patch delta 24 gcmZoMXffE}%QE>a+pfvVY{r{ySx+%7X5kP30B{!v`2YX_ delta 96 zcmZoMXffE}%ObopsURn_xWvHV8Y2@k3o9EtJNx7WmS`PL&UgXI>S{w{BOL`}6Z2Xf zg=#}{b2A+Ub91BGT22m8Wqs?Q`0SkAy!`ITiR|)>J(FeG%r;xGo?_a}&heKY0LkJR A-~a#s diff --git a/dwd_global_rad_api_server/app.py b/dwd_global_rad_api_server/app.py index 8c59b41..ad9948d 100644 --- a/dwd_global_rad_api_server/app.py +++ b/dwd_global_rad_api_server/app.py @@ -11,24 +11,42 @@ # Instantiate main object objGlobalRadiation = dgr.GlobalRadiation() -# Configure logging -logging.basicConfig(level=logging.DEBUG) +# Configure logging with a custom formatter +class CustomFormatter(logging.Formatter): + def format(self, record): + log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' + formatter = logging.Formatter(log_format, datefmt='%Y-%m-%d %H:%M:%S') + return formatter.format(record) -@app.route('/process', methods=['POST']) -def process(): - if 'dataset' not in request.files or 'locations' not in request.form: - app.logger.error("Missing dataset or locations parameter") - return jsonify({'error': 'Missing dataset or locations parameter'}), 400 +# Remove the default Flask handlers if any +for handler in app.logger.handlers[:]: + app.logger.removeHandler(handler) +handler = logging.StreamHandler() +handler.setFormatter(CustomFormatter()) +app.logger.addHandler(handler) +app.logger.setLevel(logging.INFO) - dataset_file = request.files['dataset'] - locations = json.loads(request.form['locations']) +app.logger.propagate = False - try: - app.logger.info(f"Locations received: {locations}") - app.logger.info("Loading dataset") - ds = pickle.load(dataset_file) - app.logger.info("Dataset loaded successfully") +@app.route('/process', methods=['POST']) +def process(): + try: + if 'dataset' in request.files: + app.logger.info("Dataset provided in request") + dataset_file = request.files['dataset'] + ds = pickle.load(dataset_file) + else: + app.logger.info("No dataset provided, fetching internally") + objGlobalRadiation.fetch_forecasts() + ds = objGlobalRadiation.forecast_data.all_grid_forecasts + + if 'locations' in request.form: + app.logger.info("Locations provided in request") + locations = json.loads(request.form['locations']) + else: + app.logger.info("No locations provided, using internal locations") + locations = [{"lat": loc.latitude, "lon": loc.longitude} for loc in objGlobalRadiation.locations] app.logger.info("Creating animation") output_file = main.create_animation(ds, locations) diff --git a/dwd_global_rad_api_server/config.yaml b/dwd_global_rad_api_server/config.yaml index f802e1b..4966934 100644 --- a/dwd_global_rad_api_server/config.yaml +++ b/dwd_global_rad_api_server/config.yaml @@ -1,6 +1,6 @@ # https://developers.home-assistant.io/docs/add-ons/configuration#add-on-config name: DWD Global Rad Api Server -version: "1.4.6" +version: "1.4.7" slug: dwd_global_rad_api_server description: DWD Global Rad Api Server arch: diff --git a/dwd_global_rad_api_server/main.py b/dwd_global_rad_api_server/main.py index 40240f2..205f407 100644 --- a/dwd_global_rad_api_server/main.py +++ b/dwd_global_rad_api_server/main.py @@ -10,9 +10,9 @@ import logging # Set up logging configuration to show only warnings and errors -logging.basicConfig(level=logging.WARNING) -logging.getLogger('fiona').setLevel(logging.WARNING) -logging.getLogger('matplotlib').setLevel(logging.WARNING) +#logging.basicConfig(level=logging.WARNING) +#logging.getLogger('fiona').setLevel(logging.WARNING) +#logging.getLogger('matplotlib').setLevel(logging.WARNING) def create_animation(ds, custom_locations): lats = ds['lat'].values