Skip to content

Draw the suggested graphs for an asset without sensors_to_show, and let [] mean no graphs - #2953

Open
Flix6x wants to merge 7 commits into
mainfrom
sensors-to-show-null
Open

Flix6x wants to merge 7 commits into
mainfrom
sensors-to-show-null

Conversation

@Flix6x

@Flix6x Flix6x commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Problem

An asset whose graphs were never chosen shows no graphs on its graph page, while the chart data still loads two of its sensors, and the Edit Graphs dialogue shows no graphs either.

  • GenericAsset.sensors_to_show was nullable=False, default=[], so "never chosen" and "no graphs" were the same value.
  • validate_sensors_to_show suggests graphs whenever not self.sensors_to_show, but _suggest_default_sensors_to_show returned the old top-level form ({"title": None, "sensors": [...]}), which chart_for_multiple_sensors skips since feat: Adapting flex-config to SensorsToShowSchema #1904 (v0.32), as it only reads plots. The chart data (which flattens either form) still loads the two suggested sensors.
  • The dialogue (asset_graph.html) reads the raw asset.sensors_to_show, so it starts empty.

Confirmed on main: an asset with two kW sensors and the default [] gets [{'title': None, 'sensors': [<Sensor 1>, <Sensor 2>]}] from validate_sensors_to_show(), no rows in its Vega-Lite spec, and both sensors in its chart data.

What changes

  1. sensors_to_show is nullable, with NULL as the default: NULL means no graphs were chosen, so graphs are suggested, and [] means no graphs. A migration turns every stored [] into NULL (none of them was a deliberate choice, as [] meant "suggest" until now) and drops the '[]' server default; the downgrade turns NULL back into [].
  2. validate_sensors_to_show suggests only when sensors_to_show is None, and returns the suggestions in the current plots form, so the Vega-Lite builder draws them: the first two sensors in one graph if they share a unit, or a graph each if not, as before. With suggest_default_sensors=False (the sensor status overview in sensors.py), NULL gives no graphs. A new GenericAsset.suggested_sensors_to_show() gives the same suggestions with sensor IDs, in the stored form.
  3. The Edit Graphs dialogue starts from the suggested graphs when sensors_to_show is NULL, with a note that marks them as suggested; Save refuses to store them unedited (a toast says there is nothing to save), and any edit makes them the user's own to save. Removing every graph and saving stores [], which shows no graphs. Once graphs have been chosen, a small muted "Use suggested graphs" link under them (after a confirm) stores NULL again and reloads, so the asset is back to suggested graphs without the API.
  4. API: POST/PATCH /api/v3_0/assets accept sensors_to_show: null to reset to suggested graphs (it was refused as "may not be null"), and keep [] as no graphs; the field now has a description in the OpenAPI spec, and the audit log says "not set, so graphs are suggested" or "no graphs". flexmeasures show asset prints "Not set (graphs are suggested)" rather than failing on NULL, and the graph page's legend count no longer fails on NULL.
  5. Docs (views/asset-data.rst), the main changelog (bugfix, plus the migration in the upgrade warning) and the API changelog (null accepted; assets that had [] now return "null").

Not changed: when deleting a sensor or asset prunes every graph from another asset's sensors_to_show, or copying an asset drops them all, the result stays [], which now means no graphs (it used to mean suggested graphs).

How to test

  • pytest flexmeasures/data/tests/test_suggested_sensors_to_show.py: NULL draws the suggested graphs (the Vega-Lite spec has one row with both sensors for a shared unit, a row each otherwise), [] draws none, and a new asset has NULL. These fail on main.
  • pytest flexmeasures/data/tests/test_sensors_to_show_nullable_migration.py: the upgrade turns [] into NULL and keeps chosen graphs, the downgrade turns NULL back into [] and restores NOT NULL with its default.
  • pytest flexmeasures/api/v3_0/tests/test_assets_api.py -k no_graphs_and_back (PATCH [] and null, stored as SQL NULL) and pytest flexmeasures/ui/tests/test_asset_crud.py -k graphs_page (the dialogue starts from the suggestions only for NULL, and offers "Use suggested graphs" only once graphs were chosen).
  • pytest flexmeasures/data/tests/test_jsonb_columns.py: every nullable JSON column stores None as SQL NULL (none_as_null=True).
  • By hand: flexmeasures db upgrade, then open the graph page of an asset with sensors and no chosen graphs: two suggested graphs show, and Edit Graphs lists them under a "Suggested graphs" note. Save without edits stores nothing; remove every graph and save, and the page shows no graphs; click "Use suggested graphs" under the graphs in Edit Graphs (or PATCH the asset with {"sensors_to_show": null}) to get the suggestions back.

🤖 Generated with Claude Code

Flix6x and others added 4 commits December 2, 2026 15:41
sensors_to_show becomes nullable: NULL means no graphs were chosen, so graphs are suggested, and [] means no graphs.
A migration turns every stored [] into NULL (until now, [] meant "suggest"), and the downgrade turns NULL back into [].

validate_sensors_to_show suggests only when sensors_to_show is None,
and returns the suggestions in the plots form, which the Vega-Lite builder draws
(it skipped the old top-level "sensors" form, so the chart showed no graphs while its data loaded the suggested sensors).
suggested_sensors_to_show gives the same suggestions with sensor IDs, for the graph editor.
`flexmeasures show asset` and the graph page's legend count no longer fail on NULL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
… suggested graphs

POST and PATCH /api/v3_0/assets accepted [] but refused null ("may not be null").
Now null resets sensors_to_show, so graphs are suggested, while [] still means no graphs.
The audit log says which of the two was set, and the field is described in the OpenAPI spec.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
…sensors_to_show

When sensors_to_show is not set, the Edit Graphs dialogue starts from the graphs the page suggests, under a note that marks them as suggested,
rather than from no graphs.
Save stores them only once the user edits them, and removing every graph and saving stores [], which shows no graphs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
… and [] none

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
@Flix6x Flix6x added this to the 1.2.0 milestone Dec 2, 2026
@Flix6x Flix6x added bug Something isn't working API Data UI upgrading labels Dec 2, 2026
@Flix6x Flix6x self-assigned this Dec 2, 2026
@read-the-docs-community

read-the-docs-community Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

📖 6 files modified


Preview · build overview · flexmeasures · built Dec. 5, 2026, 11:06 a.m. UTC

Without none_as_null, setting sensors_to_show to None (PATCH or POST with null, or copying an asset
whose graphs are suggested) stored the JSON value null, which the migration's IS NULL misses:
the downgrade left those assets with null rather than [].
The migration now also handles a JSON null both ways.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>

@Flix6x Flix6x left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Self-review, at head 59466bc (on today's main).

What holds

  • An asset whose graphs were never chosen draws its suggested graphs again: one graph for two sensors sharing a unit, a graph each otherwise. On main its chart has no rows, while the chart data still loads both sensors.
  • [] now means no graphs, and its chart data loads nothing. A new asset, made in code or by POST without sensors_to_show, starts unset.
  • Edit Graphs starts from the suggested graphs only when nothing was chosen, and every edit (titles, y-axis, plots) makes them the user's own to save.
  • The new tests guard the fix: the 8 cases in test_suggested_sensors_to_show.py fail on main. The asset API, UI and flexmeasures show tests pass.

Fixed in this PR since (pushed after this review)

  1. null was stored as the JSON value null, not as SQL NULL. The column's JSONB type stores None as JSON null unless none_as_null=True. Only a column left unset at insert was SQL NULL. So PATCH/POST with null, and copying an asset whose graphs are suggested, stored JSON null. The app reads both as None, but the downgrade's WHERE sensors_to_show IS NULL missed these assets. They kept null under NOT NULL, and flexmeasures show asset then failed on them in the downgraded version. The column now uses JSONB(none_as_null=True). The migration also turns a JSON null into SQL NULL on upgrade, and into [] on downgrade. The API test now checks the stored SQL NULL, and the migration test has a JSON-null row. A new test checks that every nullable JSON/JSONB column in FlexMeasures uses none_as_null=True, so a future column can't repeat this. sensors_to_show is the only one today.
  2. No way back to suggested graphs in the UI. Once graphs were saved, going back to suggested graphs took a PATCH with null. The Edit Graphs dialogue now has a subtle Use suggested graphs option for that, shown only when graphs were chosen.

Worth knowing

  • #2943 also adds a migration on d5b8f3a1c962, so whichever of the two merges second needs its down_revision moved.
  • Deleting the only sensor in an asset's graphs now leaves no graphs, rather than suggested ones, as the description says. No graphs were drawn in that case on main either.

Flix6x and others added 2 commits December 5, 2026 12:02
A guard against the sensors_to_show bug coming back in another column:
without none_as_null, None is stored as the JSON value null, which an IS NULL query misses.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Once graphs have been chosen for an asset, a small muted link under the graphs (after a confirm) sets sensors_to_show to null,
so the asset shows suggested graphs again, without needing the API.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
@Flix6x

Flix6x commented Dec 5, 2026

Copy link
Copy Markdown
Member Author

Follow-ups of the review, done:

  • push-fix, store an unset sensors_to_show as SQL NULL: pushed 9549077 (JSONB(none_as_null=True); the migration handles a JSON null both ways).
  • push-fix, guard test that every nullable JSON/JSONB column uses none_as_null=True: pushed 592e0d8 (flexmeasures/data/tests/test_jsonb_columns.py; fails at 59466bc, passes now).
  • push-fix, a subtle "Use suggested graphs" link in Edit Graphs: pushed b121b36 (small muted link under the graphs, shown only once graphs were chosen; after a confirm it saves null and reloads; UI test, docs, changelog and this PR's description updated).
  • merge: not done yet. CI is green on b121b36 (all checks, Read the Docs included) and feat: say whether an automation's runs can start, in one status field #2943 has not merged, so the migration parent is still right. But the PR is blocked on a required approving review, and the --admin squash-merge that would override that was refused on this machine. Left for @Flix6x: approve or squash-merge it yourself (gh pr merge 2953 -R FlexMeasures/flexmeasures --squash --admin).

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant