Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions documentation/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Bugfixes
* Clean up stale sensor references from ``flex-config`` and ``sensors_to_show`` when deleting a sensor, using JSONB queries to find affected assets before pruning those references [see `PR #2106 <https://www.github.com/FlexMeasures/flexmeasures/pull/2106>`_]
* Standardize resolution formatting across API endpoints for consistent response payloads [see `PR #2152 <https://www.github.com/FlexMeasures/flexmeasures/pull/2152>`_]
* Make the auth check for CLI commands work with ``flask``, too, instead of only with the ``flexmeasures`` alias [see `PR #2169 <https://www.github.com/FlexMeasures/flexmeasures/pull/2169>`_]
* Fix ``flexmeasures show asset`` for assets whose ``sensors_to_show`` uses the standardized ``plots`` schema [see `PR #2189 <https://www.github.com/FlexMeasures/flexmeasures/pull/2189>`_]
* Distinguish data sources with duplicate names in chart legends without showing source IDs [see `PR #2185 <https://www.github.com/FlexMeasures/flexmeasures/pull/2185>`_]
* Fix broken API endpoint links in Read the Docs output by post-processing both known API index layouts and rewriting ``(id)`` anchors [see `PR #1753 <https://www.github.com/FlexMeasures/flexmeasures/pull/1753>`_]

Expand Down
43 changes: 37 additions & 6 deletions flexmeasures/cli/data_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,7 @@ def show_generic_asset(asset):
(
asset.generic_asset_type.name,
asset.location,
"".join(
[
f"{graph['title']}: {graph['sensors']} \n"
for graph in standardized_sensors_to_show
]
),
_format_sensors_to_show(standardized_sensors_to_show),
"".join([f"{k}: {v}\n" for k, v in asset.attributes.items()]),
asset.external_id,
)
Expand Down Expand Up @@ -330,6 +325,42 @@ def show_generic_asset(asset):
)


def _format_sensors_to_show(standardized_sensors_to_show: list[dict]) -> str:
formatted_graphs = []
for graph in standardized_sensors_to_show:
if not isinstance(graph, dict):
formatted_graphs.append(f"{graph} \n")
continue
title = graph.get("title") or "No Title"
formatted_plots = [_format_sensor_plot(plot) for plot in graph.get("plots", [])]
if not formatted_plots:
formatted_plots = ["[]"]
formatted_graphs.append(f"{title}: {', '.join(formatted_plots)} \n")
return "".join(formatted_graphs)


def _format_sensor_plot(plot: dict) -> str:
if not isinstance(plot, dict):
return str(plot)
if "sensor" in plot:
return str(plot["sensor"])
if "sensors" in plot:
return str(plot["sensors"])
if "asset" in plot:
asset_text = f"asset={plot['asset']}"
reference_keys = [
f"{key}={plot[key]}"
for key in ("flex-model", "flex-context")
if key in plot
]
return (
f"{asset_text} ({', '.join(reference_keys)})"
if reference_keys
else asset_text
)
return str(plot)


@fm_show_data.command("data-sources")
@with_appcontext
@click.option(
Expand Down
38 changes: 38 additions & 0 deletions flexmeasures/cli/tests/test_data_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,44 @@ def test_show_asset(app, fresh_db, setup_generic_assets_fresh_db):
assert result.exit_code == 1 # command raises a click.Abort Exception


def test_show_asset_with_standardized_sensors_to_show(
app, fresh_db, setup_generic_assets_fresh_db
):
from flexmeasures.cli.data_show import show_generic_asset

asset = setup_generic_assets_fresh_db["test_wind_turbine"]
asset.sensors_to_show = [{"title": "Power", "plots": [{"sensors": [432, 433]}]}]
fresh_db.session.flush()

runner = app.test_cli_runner()
result = runner.invoke(show_generic_asset, ["--id", asset.id])

assert "Power: [432, 433]" in result.output
assert "KeyError" not in result.output
assert result.exit_code == 1 # command raises a click.Abort Exception


def test_format_sensors_to_show_supports_asset_plots():
from flexmeasures.cli.data_show import _format_sensors_to_show

formatted_sensors_to_show = _format_sensors_to_show(
[
{
"title": "Storage",
"plots": [
{"asset": 12, "flex-model": "soc-min"},
{"asset": 13, "flex-context": "consumption-price"},
{"unexpected": "plot"},
],
}
]
)

assert "Storage: asset=12 (flex-model=soc-min)" in formatted_sensors_to_show
assert "asset=13 (flex-context=consumption-price)" in formatted_sensors_to_show
assert "{'unexpected': 'plot'}" in formatted_sensors_to_show


def test_show_forecasters(app, db):
from flexmeasures.cli.data_show import list_forecasters

Expand Down
Loading