Skip to content

Commit

Permalink
Backport #589: Fix 588: CLI command add schedule for-storage not work…
Browse files Browse the repository at this point in the history
…ing without --as-job (#589)

* 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 <nicolas@seita.nl>

* Fix parameters for make_schedule in add schedule for-storage CLI command

Signed-off-by: Nicolas Höning <nicolas@seita.nl>

* add a docstring

Signed-off-by: Nicolas Höning <nicolas@seita.nl>

---------

Signed-off-by: Nicolas Höning <nicolas@seita.nl>
  • Loading branch information
nhoening authored and Flix6x committed Feb 4, 2023
1 parent ba74eec commit b5549d8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
5 changes: 2 additions & 3 deletions flexmeasures/cli/data_add.py
Expand Up @@ -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(),
Expand All @@ -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.")

Expand Down
20 changes: 15 additions & 5 deletions flexmeasures/data/scripts/data_gen.py
Expand Up @@ -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(
Expand All @@ -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)


Expand Down

0 comments on commit b5549d8

Please sign in to comment.