From b5549d8cc60d286417aea54d1dffc12861e198bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20H=C3=B6ning?= Date: Sat, 4 Feb 2023 14:28:00 +0100 Subject: [PATCH] Backport #589: Fix 588: CLI command add schedule for-storage not working without --as-job (#589) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * when deleting prognoses, only delete forecasting and scheduling jobs (which affects all sensors) if we have no sensor_id Signed-off-by: Nicolas Höning * Fix parameters for make_schedule in add schedule for-storage CLI command Signed-off-by: Nicolas Höning * add a docstring Signed-off-by: Nicolas Höning --------- Signed-off-by: Nicolas Höning --- flexmeasures/cli/data_add.py | 5 ++--- flexmeasures/data/scripts/data_gen.py | 20 +++++++++++++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/flexmeasures/cli/data_add.py b/flexmeasures/cli/data_add.py index a0fd4bce9..6e41b412a 100755 --- a/flexmeasures/cli/data_add.py +++ b/flexmeasures/cli/data_add.py @@ -1000,7 +1000,6 @@ def add_schedule_for_storage( roundtrip_efficiency = roundtrip_efficiency.magnitude / 100.0 scheduling_kwargs = dict( - sensor=power_sensor, start=start, end=end, belief_time=server_now(), @@ -1019,11 +1018,11 @@ def add_schedule_for_storage( }, ) if as_job: - job = create_scheduling_job(**scheduling_kwargs) + job = create_scheduling_job(sensor=power_sensor, **scheduling_kwargs) if job: print(f"New scheduling job {job.id} has been added to the queue.") else: - success = make_schedule(**scheduling_kwargs) + success = make_schedule(sensor_id=power_sensor.id, **scheduling_kwargs) if success: print("New schedule is stored.") diff --git a/flexmeasures/data/scripts/data_gen.py b/flexmeasures/data/scripts/data_gen.py index 25cfc86bf..9da2b336c 100644 --- a/flexmeasures/data/scripts/data_gen.py +++ b/flexmeasures/data/scripts/data_gen.py @@ -298,14 +298,23 @@ def depopulate_prognoses( db: SQLAlchemy, sensor_id: Optional[id] = None, ): + """ + Delete all prognosis data (with an horizon > 0). + This affects forecasts as well as schedules. + + Pass a sensor ID to restrict to data on one sensor only. + + If no sensor is specified, this function also deletes forecasting and scheduling jobs. + (Doing this only for jobs which forecast/schedule one sensor is not implemented and also tricky.) + """ click.echo( "Deleting (time series) forecasts and schedules data from the database %s ..." % db.engine ) - # Clear all jobs - num_forecasting_jobs_deleted = app.queues["forecasting"].empty() - num_scheduling_jobs_deleted = app.queues["scheduling"].empty() + if not sensor_id: + num_forecasting_jobs_deleted = app.queues["forecasting"].empty() + num_scheduling_jobs_deleted = app.queues["scheduling"].empty() # Clear all forecasts (data with positive horizon) query = db.session.query(TimedBelief).filter( @@ -315,8 +324,9 @@ def depopulate_prognoses( query = query.filter(TimedBelief.sensor_id == sensor_id) num_forecasts_deleted = query.delete() - click.echo("Deleted %d Forecast Jobs" % num_forecasting_jobs_deleted) - click.echo("Deleted %d Schedule Jobs" % num_scheduling_jobs_deleted) + if not sensor_id: + click.echo("Deleted %d Forecast Jobs" % num_forecasting_jobs_deleted) + click.echo("Deleted %d Schedule Jobs" % num_scheduling_jobs_deleted) click.echo("Deleted %d forecasts (ex-ante beliefs)" % num_forecasts_deleted)