Skip to content

Commit

Permalink
feat: CLI can delete multiple sensors at once (#734)
Browse files Browse the repository at this point in the history
This PR let's you delete multiple sensors with a single call to flexmeasures delete sensor by passing the --id option multiple times. I also tried to streamline the help messages a bit for CLI usages of the SensorIDField.


* feat: Support deleting multiple sensors with one call to the CLI.

Signed-off-by: F.N. Claessen <felix@seita.nl>

* docs: CLI changelog entry

Signed-off-by: F.N. Claessen <felix@seita.nl>

* bug:) trailing space

Signed-off-by: F.N. Claessen <felix@seita.nl>

* docs: changelog entry

Signed-off-by: F.N. Claessen <felix@seita.nl>

---------

Signed-off-by: F.N. Claessen <felix@seita.nl>
  • Loading branch information
Flix6x committed Jun 20, 2023
1 parent 3a54f3a commit 99cf03a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
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

0 comments on commit 99cf03a

Please sign in to comment.