Skip to content

Commit

Permalink
add --offspring flag to command flexmeasures delete beliefs
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Garcia Reolid <victor@seita.nl>
  • Loading branch information
victorgarcia98 committed Mar 7, 2024
1 parent 2f3ea42 commit d2336ec
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
15 changes: 14 additions & 1 deletion flexmeasures/cli/data_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,13 @@ def delete_prognoses(
required=False,
help="Remove beliefs about events ending at this datetime. Follow up with a timezone-aware datetime in ISO 6801 format.",
)
@click.option("--offspring", type=bool, required=False, default=False, is_flag=True)
def delete_beliefs( # noqa: C901
generic_assets: list[GenericAsset],
sensors: list[Sensor],
start: datetime | None = None,
end: datetime | None = None,
offspring: bool = False,
):
"""Delete all beliefs recorded on a given sensor (or on sensors of a given asset)."""

Expand All @@ -271,6 +273,8 @@ def delete_beliefs( # noqa: C901
abort("Passing both sensors and assets at the same time is not supported.")
if start is not None and end is not None and start > end:
abort("Start should not exceed end.")
if offspring and len(generic_assets) == 0:
abort("Must pass at least one asset when the offspring option is employed.")

# Time window filter
event_filters = []
Expand All @@ -284,9 +288,18 @@ def delete_beliefs( # noqa: C901
if sensors:
entity_filters += [TimedBelief.sensor_id.in_([sensor.id for sensor in sensors])]
if generic_assets:

# get the offspring of all generic assets
generic_assets_offspring = []

for asset in generic_assets:
generic_assets_offspring.extend(asset.offspring)

entity_filters += [
TimedBelief.sensor_id == Sensor.id,
Sensor.generic_asset_id.in_([asset.id for asset in generic_assets]),
Sensor.generic_asset_id.in_(
[asset.id for asset in generic_assets_offspring + list(generic_assets)]
),
]

# Create query
Expand Down
9 changes: 9 additions & 0 deletions flexmeasures/data/models/generic_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ def asset_type(self) -> GenericAssetType:
),
)

@property
def offspring(self) -> list[GenericAsset]:
offspring = []

for child in self.child_assets:
offspring.extend(child.offspring)

return offspring + self.child_assets

@property
def location(self) -> tuple[float, float] | None:
location = (self.latitude, self.longitude)
Expand Down

0 comments on commit d2336ec

Please sign in to comment.