Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 499 allow showing sensor data from other assets in the same acc…
…ount (#500) Let the asset page also show sensor data from other assets that belong to the same account. * Make it possible to show sensor data from other assets in the same account Signed-off-by: F.N. Claessen <felix@seita.nl> * Expand docstring Signed-off-by: F.N. Claessen <felix@seita.nl> * changelog entry Signed-off-by: F.N. Claessen <felix@seita.nl> * Refactor util function to get sensors for a given account (Account, id or name), and avoid duplicate queries of a known account Signed-off-by: F.N. Claessen <felix@seita.nl> * Sensors on a given asset are a subset of sensors on assets with the same owner Signed-off-by: F.N. Claessen <felix@seita.nl> * Limit query to requested sensor ids (instead of returning all sensors from the same account, which may be a lot) Signed-off-by: F.N. Claessen <felix@seita.nl> * flake8 Signed-off-by: F.N. Claessen <felix@seita.nl> * Remove redundant if statement Signed-off-by: F.N. Claessen <felix@seita.nl> * Refactor: rename function Signed-off-by: F.N. Claessen <felix@seita.nl> * Repurpose function by making it mandatory to pass at least one account, or None for public sensors (which makes get_public_sensors obsolete) Signed-off-by: F.N. Claessen <felix@seita.nl> * Partially revert repurposing function: passing accounts is optional again, and rename filter argument Signed-off-by: F.N. Claessen <felix@seita.nl> * Allow to filter sensors by name Signed-off-by: F.N. Claessen <felix@seita.nl> * Explicit parameter for including sensors of public assets Signed-off-by: F.N. Claessen <felix@seita.nl> * Must pass a list of accounts Signed-off-by: F.N. Claessen <felix@seita.nl> * The more common use case is looking up sensors for a single account Signed-off-by: F.N. Claessen <felix@seita.nl> * Rename variable Signed-off-by: F.N. Claessen <felix@seita.nl> * fix variable order Signed-off-by: F.N. Claessen <felix@seita.nl> Signed-off-by: F.N. Claessen <felix@seita.nl>
- Loading branch information
Showing
4 changed files
with
43 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,43 @@ | ||
from __future__ import annotations | ||
|
||
from werkzeug.exceptions import NotFound | ||
import sqlalchemy as sa | ||
|
||
from flexmeasures import Sensor, Account | ||
from flexmeasures.data.models.generic_assets import GenericAsset | ||
|
||
|
||
def get_sensors( | ||
account_name: str | None = None, | ||
account: Account | list[Account], | ||
include_public_assets: bool = False, | ||
sensor_id_allowlist: list[int] | None = None, | ||
sensor_name_allowlist: list[str] | None = None, | ||
) -> list[Sensor]: | ||
"""Return a list of Sensor objects. | ||
"""Return a list of Sensor objects that belong to the given account, and/or public sensors. | ||
:param account_name: optionally, filter by account name. | ||
:param account: select only sensors from this account (or list of accounts) | ||
:param include_public_assets: if True, include sensors that belong to a public asset | ||
:param sensor_id_allowlist: optionally, allow only sensors whose id is in this list | ||
:param sensor_name_allowlist: optionally, allow only sensors whose name is in this list | ||
""" | ||
sensor_query = Sensor.query | ||
|
||
if account_name is not None: | ||
account = Account.query.filter(Account.name == account_name).one_or_none() | ||
if not account: | ||
raise NotFound(f"There is no account named {account_name}!") | ||
sensor_query = ( | ||
sensor_query.join(GenericAsset) | ||
.filter(Sensor.generic_asset_id == GenericAsset.id) | ||
.filter(GenericAsset.owner == account) | ||
) | ||
|
||
return sensor_query.all() | ||
|
||
|
||
def get_public_sensors(sensor_ids: list[int] | None = None) -> list[Sensor]: | ||
"""Return a list of Sensor objects that belong to a public asset. | ||
:param sensor_ids: optionally, filter by sensor id. | ||
""" | ||
sensor_query = ( | ||
Sensor.query.join(GenericAsset) | ||
.filter(Sensor.generic_asset_id == GenericAsset.id) | ||
.filter(GenericAsset.account_id.is_(None)) | ||
if isinstance(account, list): | ||
account_ids = [account.id for account in account] | ||
else: | ||
account_ids = [account.id] | ||
sensor_query = sensor_query.join(GenericAsset).filter( | ||
Sensor.generic_asset_id == GenericAsset.id | ||
) | ||
if sensor_ids: | ||
sensor_query = sensor_query.filter(Sensor.id.in_(sensor_ids)) | ||
if include_public_assets: | ||
sensor_query = sensor_query.filter( | ||
sa.or_( | ||
GenericAsset.account_id.in_(account_ids), | ||
GenericAsset.account_id.is_(None), | ||
) | ||
) | ||
else: | ||
sensor_query = sensor_query.filter(GenericAsset.account_id.in_(account_ids)) | ||
if sensor_id_allowlist: | ||
sensor_query = sensor_query.filter(Sensor.id.in_(sensor_id_allowlist)) | ||
if sensor_name_allowlist: | ||
sensor_query = sensor_query.filter(Sensor.name.in_(sensor_name_allowlist)) | ||
return sensor_query.all() |