Skip to content

Commit

Permalink
refactor: relocate dict processing - Work in progress
Browse files Browse the repository at this point in the history
Signed-off-by: joshuaunity <[email protected]>
  • Loading branch information
joshuaunity committed Oct 4, 2024
1 parent ab9c0f6 commit 66bfecb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
11 changes: 6 additions & 5 deletions documentation/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ FlexMeasures Changelog
v0.24.0 | October XX, 2024
============================

.. warning:: Upgrading to this version requires running ``flexmeasures db upgrade`` (you can create a backup first with ``flexmeasures db-ops dump``).


New features
-------------
* The data chart on the asset page splits up its color-coded sensor legend when showing more than 7 sensors, becoming a legend per subplot [see `PR #1176 <https://github.com/FlexMeasures/flexmeasures/pull/1176>`_ and `PR #1193 <https://github.com/FlexMeasures/flexmeasures/pull/1193>`_
* Speed up loading the users page, by making the pagination backend-based and adding support for that in the API [see `PR #1160 <https://github.com/FlexMeasures/flexmeasures/pull/1160>`]
* The data chart on the asset page splits up its color-coded sensor legend when showing more than 7 sensors, becoming a legend per subplot [see `PR #1176 <https://github.com/FlexMeasures/flexmeasures/pull/1176>`_ and `PR #1193 <https://github.com/FlexMeasures/flexmeasures/pull/1193>`_]
* Speed up loading the users page, by making the pagination backend-based and adding support for that in the API [see `PR #1160 <https://github.com/FlexMeasures/flexmeasures/pull/1160>`_]
* X-axis labels in CLI plots show datetime values in a readable and informative format [see `PR #1172 <https://github.com/FlexMeasures/flexmeasures/pull/1172>`_]

Infrastructure / Support
----------------------

* For MacOS developers, install HiGHS solver automatically [see `PR #1187 <https://github.com/FlexMeasures/flexmeasures/pull/1187>`_]
* Add dedicated ``sensors_to_show`` field to asset model and logic to migrate data from parent source(attributes field) [see `PR #1200 <https://github.com/FlexMeasures/flexmeasures/pull/1200>`_]

Bugfixes
-----------
Expand All @@ -28,13 +32,10 @@ v0.23.0 | September 18, 2024

.. note:: Read more on these features on `the FlexMeasures blog <https://flexmeasures.io/023-data-insights-and-white-labelling/>`_.

.. warning:: Upgrading to this version requires running ``flexmeasures db upgrade`` (you can create a backup first with ``flexmeasures db-ops dump``).

New features
-------------
* New chart type on sensor page: histogram [see `PR #1143 <https://github.com/FlexMeasures/flexmeasures/pull/1143>`_]
* Add basic sensor info to sensor page [see `PR #1115 <https://github.com/FlexMeasures/flexmeasures/pull/1115>`_]
* Add dedicated ``sensors_to_show`` field to asset model and logic to migrate data from parent source(attributes field) [see `PR #1200 <https://github.com/FlexMeasures/flexmeasures/pull/1200>`_]
* Add `Statistics` table on the sensor page and also add `api/v3_0/sensors/<id>/stats` endpoint to get sensor statistics [see `PR #1116 <https://github.com/FlexMeasures/flexmeasures/pull/1116>`_]
* Support adding custom titles to the graphs on the asset page, by extending the ``sensors_to_show`` format [see `PR #1125 <https://github.com/FlexMeasures/flexmeasures/pull/1125>`_ and `PR #1177 <https://github.com/FlexMeasures/flexmeasures/pull/1177>`_]
* Support zoom-in action on the asset and sensor charts [see `PR #1130 <https://github.com/FlexMeasures/flexmeasures/pull/1130>`_]
Expand Down
7 changes: 4 additions & 3 deletions flexmeasures/data/models/generic_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ def validate_sensors_to_show(
This function ensures that the 'sensors_to_show' attribute:
1. Follows the latest format, even if the data in the database uses an older format.
2. Contains only sensors that the user has access to (based on the current asset, account, or public availability).
3. Returns a list of dictionaries where each dictionary contains either a single sensor or a group of sensors with an optional title.
Steps:
- The function deserializes the 'sensors_to_show' data from the database, ensuring that older formats are parsed correctly.
Expand Down Expand Up @@ -223,7 +222,7 @@ def validate_sensors_to_show(
else:
self.sensors_to_show = []

if not self.has_attribute("sensors_to_show"):
if not self.sensors_to_show:
sensors_to_show = self.sensors[:2]
if (
len(sensors_to_show) == 2
Expand All @@ -234,7 +233,7 @@ def validate_sensors_to_show(
# Otherwise, show separately
return [{"title": None, "sensors": [sensor]} for sensor in sensors_to_show]

sensor_ids_to_show = self.get_attribute("sensors_to_show")
sensor_ids_to_show = self.sensors_to_show
# Import the schema for validation
from flexmeasures.data.schemas.generic_assets import SensorsToShowSchema

Expand Down Expand Up @@ -581,6 +580,7 @@ def chart(
"""
processed_sensors_to_show = self.validate_sensors_to_show()
sensors = flatten_unique(processed_sensors_to_show)

for sensor in sensors:
sensor.sensor_type = sensor.get_attribute("sensor_type", sensor.name)

Expand Down Expand Up @@ -656,6 +656,7 @@ def search_beliefs(
bdf_dict = {}
if sensors is None:
sensors = self.sensors

for sensor in sensors:
bdf_dict[sensor] = sensor.search_beliefs(
event_starts_after=event_starts_after,
Expand Down
3 changes: 3 additions & 0 deletions flexmeasures/ui/crud/assets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ def expunge_asset():
**{
**asset_data,
**{"attributes": json.loads(asset_data.get("attributes", "{}"))},
**{
"sensors_to_show": json.loads(asset_data.get("sensors_to_show", []))
},
}
) # TODO: use schema?
if "generic_asset_type_id" in asset_data:
Expand Down
9 changes: 1 addition & 8 deletions flexmeasures/ui/crud/assets/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations
import json

from flask import url_for, current_app, request
from flask_classful import FlaskView, route
from flask_security import login_required, current_user
Expand Down Expand Up @@ -115,13 +115,6 @@ def get(self, id: str, **kwargs):
get_asset_response = InternalApi().get(url_for("AssetAPI:fetch_one", id=id))
asset_dict = get_asset_response.json()

# set sensors to show to list from string, this is not currently being used on the frontend from my knowledge
# it may be better popped of instead, but will leave it hear for now
if asset_dict.get("sensors_to_show") and not isinstance(
asset_dict.get("sensors_to_show"), list
):
asset_dict["sensors_to_show"] = json.loads(asset_dict["sensors_to_show"])

asset = process_internal_api_response(asset_dict, int(id), make_obj=True)

asset_form = AssetForm()
Expand Down

0 comments on commit 66bfecb

Please sign in to comment.