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

Issue 499 allow showing sensor data from other assets in the same account #500

Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions documentation/changelog.rst
Expand Up @@ -9,6 +9,7 @@ New features
-------------

* Hit the replay button to replay what happened, available on the sensor and asset pages [see `PR #463 <http://www.github.com/FlexMeasures/flexmeasures/pull/463>`_]
* The asset page also allows to show sensor data from other assets that belong to the same account [see `PR #500 <http://www.github.com/FlexMeasures/flexmeasures/pull/500>`_]

Bugfixes
-----------
Expand Down
12 changes: 10 additions & 2 deletions flexmeasures/data/models/generic_assets.py
Expand Up @@ -431,17 +431,25 @@ def search_beliefs(
def sensors_to_show(self) -> List["Sensor"]: # noqa F821
"""Sensors to show, as defined by the sensors_to_show attribute.

Sensors to show are defined as a list of sensor ids, which
is set by the "sensors_to_show" field of the asset's "attributes" column.
Valid sensors either belong to the asset itself, to other assets in the same account,
or to public assets.


Defaults to two of the asset's sensors.
"""
if not self.has_attribute("sensors_to_show"):
return self.sensors[:2]

from flexmeasures.data.services.sensors import get_public_sensors
from flexmeasures.data.services.sensors import get_sensors, get_public_sensors

sensor_ids = self.get_attribute("sensors_to_show")
sensor_map = {
sensor.id: sensor
for sensor in self.sensors + get_public_sensors(sensor_ids)
for sensor in self.sensors
+ get_sensors(self.account_id)
+ get_public_sensors(sensor_ids)
if sensor.id in sensor_ids
}

Expand Down
11 changes: 11 additions & 0 deletions flexmeasures/data/services/sensors.py
Expand Up @@ -7,14 +7,25 @@


def get_sensors(
Flix6x marked this conversation as resolved.
Show resolved Hide resolved
account_id: int | None = None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we should keep this to one argument.

If it's int: query for account ID.
If it's string: query for name.
Otherwise, it's an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Your idea also gave me the idea to avoid some duplicate queries to the Account table, and to filter the query of the sensors table.

account_name: str | None = None,
) -> list[Sensor]:
"""Return a list of Sensor objects.

:param account_id: optionally, filter by account id.
:param account_name: optionally, filter by account name.
"""
sensor_query = Sensor.query

if account_id is not None:
account = Account.query.filter_by(id=account_id).one_or_none()
if not account:
raise NotFound(f"There is no account with id {account_id}!")
sensor_query = (
sensor_query.join(GenericAsset)
.filter(Sensor.generic_asset_id == GenericAsset.id)
.filter(GenericAsset.owner == account)
)
if account_name is not None:
account = Account.query.filter(Account.name == account_name).one_or_none()
if not account:
Expand Down