Skip to content

Commit

Permalink
feat: Enhance /process endpoint and optimize logging
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
aschmere committed Jun 13, 2024
1 parent 6c5cc3f commit 610f04d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
Binary file modified .DS_Store
Binary file not shown.
46 changes: 32 additions & 14 deletions dwd_global_rad_api_server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion dwd_global_rad_api_server/config.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
6 changes: 3 additions & 3 deletions dwd_global_rad_api_server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 610f04d

Please sign in to comment.