Skip to content

Feat: Cleanup stale assets in graphs and configs#2210

Open
joshuaunity wants to merge 9 commits into
mainfrom
feat/stale-asset-references-in-graphs
Open

Feat: Cleanup stale assets in graphs and configs#2210
joshuaunity wants to merge 9 commits into
mainfrom
feat/stale-asset-references-in-graphs

Conversation

@joshuaunity
Copy link
Copy Markdown
Contributor

@joshuaunity joshuaunity commented May 29, 2026

Description

  • Ignore stale asset references in sensors_to_show when resolving chart inputs, so chart and chart_data do not fail if a referenced asset was deleted
  • Make sensors_to_show flattening resilient by skipping invalid asset plot references instead of raising validation errors
  • Clean stale asset references from other assets’ sensors_to_show during asset deletion, with audit-log entries for traceability
  • Harden graph modal rendering so unavailable asset plots are shown as Not available instead of breaking the full plot list
  • Improve asset fetch error handling in frontend utils so missing assets fail predictably and can be handled gracefully by callers
  • Add regression tests for:
    • missing asset references in chart data generation
    • stale asset-reference cleanup on asset delete
  • Added changelog item in documentation/changelog.rst

Look & Feel

  • API behavior improvement:
    • Before: chart endpoints could return UNPROCESSABLE_ENTITY when sensors_to_show referenced a deleted asset
    • After: valid plots still render; stale asset references are ignored
  • Graph modal behavior improvement:
    • Before: one failing asset lookup could stop rendering of plot cards
    • After: available plots are still shown, stale ones are marked as Not available with a warning banner
  • UI helper behavior:
    • getAsset in flexmeasures/ui/static/js/ui-utils.js now throws descriptive errors on non-2xx responses, enabling robust fallback rendering

How to test

  1. Run focused regression tests:
    • .venv/bin/python -m pytest flexmeasures/api/v3_0/tests/test_assets_api.py -k delete_asset_cleans_stale_asset_references_in_sensors_to_show
  2. Manual API/UI scenario:
    • Create an asset graph config where sensors_to_show contains an asset plot reference
    • Delete the referenced asset
    • Open the graph page and confirm:
      • chart still loads for remaining valid plots
      • stale plot is shown as Not available in graph modal
      • warning banner appears about unavailable saved plots
  3. Verify cleanup on delete:
    • Delete an asset referenced by another asset’s sensors_to_show
    • Confirm reference is removed from persisted sensors_to_show
    • Confirm audit log entry notes stale asset reference removal

Further Improvements

  • Add a dedicated UI integration test for graph modal unavailable-plot rendering path
  • Add optional auto-repair action in graph modal to remove all unavailable references in one click
  • Consider a lightweight migration/check command to proactively detect stale asset references across existing data

Related Items


Sign-off

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on code under GPL or other license that is incompatible with FlexMeasures

joshuaunity and others added 2 commits May 29, 2026 12:47
…d improving error handling for asset fetching

Signed-off-by: joshuaunity <oghenerobojosh01@gmail.com>
Ignore stale asset references in sensors_to_show for usability and automatic pruning when deleting assets.

Signed-off-by: JDev <45713692+joshuaunity@users.noreply.github.com>
@joshuaunity joshuaunity self-assigned this May 29, 2026
@joshuaunity joshuaunity added bug Something isn't working Data UI labels May 29, 2026
…r stale asset references

Signed-off-by: joshuaunity <oghenerobojosh01@gmail.com>
…exMeasures/flexmeasures into feat/stale-asset-references-in-graphs
…h database context

Signed-off-by: joshuaunity <oghenerobojosh01@gmail.com>
… deletion

Signed-off-by: joshuaunity <oghenerobojosh01@gmail.com>
@nhoening nhoening added this to the 1.0.0 milestone Jun 1, 2026
@joshuaunity joshuaunity requested a review from nhoening June 2, 2026 07:34
Copy link
Copy Markdown
Member

@nhoening nhoening left a comment

Choose a reason for hiding this comment

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

I re-did the initial setup, and the deleted asset reference was removed befire the graph page could even get in trouble, nice.

I need some examples in two utils, as they are not easy to grasp without. Then I know better what I approve.

Also, I ran into a separate but related problem: When a config setting is removed from its asset after it was added to some sensor_to_show, we also get an error when that graph dialogue is opened. Is that better to do in a follow-up PR?

Comment thread flexmeasures/data/services/generic_assets.py Outdated
Comment thread flexmeasures/data/services/generic_assets.py Outdated
…amples

Signed-off-by: Joshua Edward <oghenerobojosh01@gmail.com>
Copy link
Copy Markdown
Member

@nhoening nhoening left a comment

Choose a reason for hiding this comment

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

Thanks for the examples.

Please check failing tests, it might just have to do with quotes:

Expected:
    [{"title": "Power", "plots": [{"sensor": 1}]}, {"title": "Price", "plots": [{"sensor": 3}]}]
Got:
    [{'title': 'Power', 'plots': [{'sensor': 1}]}, {'title': 'Price', 'plots': [{'sensor': 3}]}]

And do check the diff - do you know why we are deleting a lot of code in flexmeasures/data/tests/test_belief_charts.py? That seems unrelated to this PR.

try:
asset = GenericAssetIdField().deserialize(asset_id)
except ValidationError:
if self.allow_missing_asset_flex_field:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I can't find this field anywhere else in this PR - is this still relevant?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it's in ln 440 in /data/models/generic_assets.py

This was implemented before the removal of the stale asset feature was completed. The goal for this is so that the graph page doesn't crash in the case of a stale asset. It's like a second net to safeguard the UI in a case where an asset somehow didnt get cleaned up.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah thanks.
Do you recall why we let it default to True?

Comment thread flexmeasures/ui/templates/assets/asset_graph.html
…r consistency

Signed-off-by: joshuaunity <oghenerobojosh01@gmail.com>
Signed-off-by: joshuaunity <oghenerobojosh01@gmail.com>
@joshuaunity joshuaunity requested a review from nhoening June 5, 2026 07:37
Copy link
Copy Markdown
Member

@nhoening nhoening left a comment

Choose a reason for hiding this comment

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

This looks okay to me, I just have questions.
We can probably merge this today :)

asset_id: int, asset_name: str | None = None
) -> int:
"""Remove references to a deleted asset in sensors_to_show across assets."""
vars_json = sa.func.jsonb_build_object("aid", asset_id)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What is "aid"?
Maybe add a comment so I can understand the way you find candidates better.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

3/4 of the changes in this file are just reformatting, is that correct?

I believe you still have a formatter in your editor that has a different opinion and makes changes by itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working Data UI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Graph more stable against deleted assets

2 participants