Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: CLI can delete multiple sensors at once #734

Merged
merged 5 commits into from Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions documentation/changelog.rst
Expand Up @@ -9,6 +9,8 @@ v0.15.0 | July XX, 2023
New features
-------------

* Allow deleting multiple sensors with a single call to ``flexmeasures delete sensor`` by passing the ``--id`` option multiple times [see `PR #734 <https://www.github.com/FlexMeasures/flexmeasures/pull/734>`_]

Bugfixes
-----------

Expand Down
5 changes: 5 additions & 0 deletions documentation/cli/change_log.rst
Expand Up @@ -4,6 +4,11 @@
FlexMeasures CLI Changelog
**********************

since v0.15.0 | July XX, 2023
=================================

* Allow deleting multiple sensors with a single call to ``flexmeasures delete sensor`` by passing the ``--id`` option multiple times.

since v0.14.0 | June 15, 2023
=================================

Expand Down
4 changes: 2 additions & 2 deletions flexmeasures/cli/data_add.py
Expand Up @@ -374,7 +374,7 @@ def add_source(name: str, model: str, version: str, source_type: str):
"sensor",
required=True,
type=SensorIdField(),
help="Sensor to which the beliefs pertain.",
help="Record the beliefs under this sensor. Follow up with the sensor's ID. ",
)
@click.option(
"--source",
Expand Down Expand Up @@ -1165,7 +1165,7 @@ def add_schedule_for_storage(
"sensor",
type=SensorIdField(),
required=True,
help="ID of the sensor used to save the report."
help="Sensor used to save the report. Follow up with the sensor's ID. "
" If needed, use `flexmeasures add sensor` to create a new sensor first.",
)
@click.option(
Expand Down
22 changes: 15 additions & 7 deletions flexmeasures/cli/data_delete.py
Expand Up @@ -307,18 +307,26 @@ def delete_nan_beliefs(sensor_id: int | None = None):
@with_appcontext
@click.option(
"--id",
"sensor",
"sensors",
type=SensorIdField(),
required=True,
help="Delete a single sensor and its (time series) data. Follow up with the sensor's ID.",
multiple=True,
help="Delete a sensor and its (time series) data. Follow up with the sensor's ID. "
"This argument can be given multiple times",
)
def delete_sensor(
sensor: Sensor,
sensors: list[Sensor],
):
"""Delete a sensor and all beliefs about it."""
n = TimedBelief.query.filter(TimedBelief.sensor_id == sensor.id).delete()
db.session.delete(sensor)
click.confirm(f"Delete {sensor.__repr__()}, along with {n} beliefs?", abort=True)
"""Delete sensors and their (time series) data."""
n = TimedBelief.query.filter(
TimedBelief.sensor_id.in_(sensor.id for sensor in sensors)
).delete()
for sensor in sensors:
db.session.delete(sensor)
click.confirm(
f"Delete {', '.join(sensor.__repr__() for sensor in sensors)}, along with {n} beliefs?",
abort=True,
)
db.session.commit()


Expand Down