Skip to content

Commit

Permalink
Skip autoscheduling if none of the posted values represent a state ch…
Browse files Browse the repository at this point in the history
…ange (#303)

Stop autoscheduling when API calls save nothing new to the database, thereby saving redundant computation.

Signed-off-by: F.N. Claessen <felix@seita.nl>
  • Loading branch information
Flix6x committed Jan 6, 2022
1 parent cc0ffa1 commit 868f483
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions flexmeasures/api/common/utils/api_utils.py
Expand Up @@ -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()

Expand Down
6 changes: 6 additions & 0 deletions flexmeasures/data/utils.py
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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

0 comments on commit 868f483

Please sign in to comment.