From 868f483cd7239ef55e574be9bf74338589df6fd1 Mon Sep 17 00:00:00 2001 From: Felix Claessen <30658763+Flix6x@users.noreply.github.com> Date: Thu, 6 Jan 2022 19:45:13 +0100 Subject: [PATCH] Skip autoscheduling if none of the posted values represent a state change (#303) Stop autoscheduling when API calls save nothing new to the database, thereby saving redundant computation. Signed-off-by: F.N. Claessen --- flexmeasures/api/common/utils/api_utils.py | 7 +++++-- flexmeasures/data/utils.py | 6 ++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/flexmeasures/api/common/utils/api_utils.py b/flexmeasures/api/common/utils/api_utils.py index e793c5117..2c5463df7 100644 --- a/flexmeasures/api/common/utils/api_utils.py +++ b/flexmeasures/api/common/utils/api_utils.py @@ -365,13 +365,16 @@ def save_and_enqueue( ) # Only enqueue forecasting jobs upon successfully saving new data - if status[:7] == "success": + if status[:7] == "success" and status != "success_but_nothing_new": enqueue_forecasting_jobs(forecasting_jobs) # Pick a response if status == "success": return request_processed() - elif status == "success_with_unchanged_beliefs_skipped": + elif status in ( + "success_with_unchanged_beliefs_skipped", + "success_but_nothing_new", + ): return already_received_and_successfully_processed() return invalid_replacement() diff --git a/flexmeasures/data/utils.py b/flexmeasures/data/utils.py index 228f7d566..cb36a2357 100644 --- a/flexmeasures/data/utils.py +++ b/flexmeasures/data/utils.py @@ -80,6 +80,7 @@ def save_to_db( :returns: status string, one of the following: - 'success': all beliefs were saved - 'success_with_unchanged_beliefs_skipped': not all beliefs represented a state change + - 'success_but_nothing_new': no beliefs represented a state change """ # Convert to list @@ -89,6 +90,7 @@ def save_to_db( timed_values_list = data status = "success" + values_saved = 0 for timed_values in timed_values_list: if timed_values.empty: @@ -124,6 +126,10 @@ def save_to_db( if current_app.config.get("FLEXMEASURES_MODE", "") != "play" else True, ) + values_saved += len(timed_values) # Flush to bring up potential unique violations (due to attempting to replace beliefs) db.session.flush() + + if values_saved == 0: + status = "success_but_nothing_new" return status