Skip to content

perf: load only the data each asset chart type draws - #2790

Merged
Flix6x merged 10 commits into
mainfrom
perf/2789-chart-data-per-chart-type
Nov 3, 2026
Merged

Flix6x merged 10 commits into
mainfrom
perf/2789-chart-data-per-chart-type

Conversation

@Ahmad-Wahid

Copy link
Copy Markdown
Contributor

Description

Closes #2789.

On an asset with charge points, the Default chart and the Charge Points chart shared one chart data request, which loaded every sensor in sensors_to_show. Both renderers skip the "Charge Point sessions" graph in the Default chart, so its session sensors (six per charging session) were loaded and thrown away. The Charge Points chart likewise loaded everything, to draw the sessions and two companion graphs.

  • GET /api/v3_0/assets/<id>/chart_data accepts chart-type, as /chart already does, and returns only the data that chart type draws. GenericAsset.chart_sensors(chart_type) decides which sensors:
    • chart_for_multiple_sensors (Default): every sensor except those that appear only in the "Charge Point sessions" graph. A session sensor that is also in another graph stays.
    • chart_for_chargepoint_sessions: the session sensors, charge points power from "Power flow by type", and the "Prices" sensors. These are the same rules chart_for_chargepoint_sessions applies when drawing.
    • No chart type, or another one: every sensor, as before.
  • The asset page sends the selected chart type with each chart data request (including replay). Switching charts loads that chart's data, instead of redrawing the data loaded for the other chart.
  • The chart data cache no longer reuses records loaded for another chart type.
  • The chart's magic strings (the sessions title, the session sensor names, the companion titles, charge points power) are named once in belief_charts.py and used by both the chart and chart_sensors.
  • Added tests: test_asset_chart_data.py (the sensors per chart type, model and API) and two JS tests in test_chart_data_cache.py (the URL, and the cache per chart type).
  • Added changelog items in documentation/changelog.rst and documentation/api/change_log.rst. The OpenAPI specs are regenerated.

asset.chart(include_data=True) still inlines every sensor's data. The UI doesn't use it, so it stays out of this PR.

Look & Feel

No visible change. On an asset with charge points, the Default chart loads faster, because it no longer loads the charging-session data. Picking "Charge Points" loads that chart's data then, with the spinner, so the switch takes as long as one chart data request instead of being instant.

How to test

uv run pytest flexmeasures/api/v3_0/tests/test_asset_chart_data.py flexmeasures/ui/tests/js/test_chart_data_cache.py

With the chart_data change reverted, the chart_for_multiple_sensors and chart_for_chargepoint_sessions cases of test_chart_data_holds_only_what_the_chart_type_draws fail: every sensor comes back. I also ran the chart, asset API, sensor API and UI test files (543 tests), and they pass.

The template's chart switch is inline in graphs.html, so no test reaches it. To check it by hand: open an asset with a charge point child, watch the network tab, and switch between Default and Charge Points. Each switch should make one chart_data request with that chart's chart-type, and the Charge Points chart should look the same as before.

Further Improvements

  • The session sensors are recognised by name and unit (s), and the sessions graph by its title. That's what the charts already do, but a sensor or graph attribute would be sturdier.

Related Items

Closes #2789. Related to #2753, which makes each sensor's search cheaper; this PR loads fewer sensors.


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

🤖 Generated with Claude Code

On an asset with charge points, the Default chart and the Charge Points chart
shared one chart data request, which loaded every sensor in sensors_to_show.
The Default chart skips the "Charge Point sessions" graph, so its session
sensors were loaded and thrown away, and the Charge Points chart likewise
loaded everything to draw a few graphs.

GET /assets/<id>/chart_data now accepts chart-type and returns only the data
of the sensors that chart type draws (GenericAsset.chart_sensors). Without it,
it returns every sensor's data, as before. The asset page sends the selected
chart type, and loads that chart's data when the user switches charts; the
chart data cache no longer reuses records loaded for another chart type.

Closes #2789.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Ahmad-Wahid <ahmedwahid16101@gmail.com>
@read-the-docs-community

read-the-docs-community Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

@Ahmad-Wahid
Ahmad-Wahid requested review from Flix6x and a balanced review from Copilot October 31, 2026 21:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Active issues remain: the selector duplicates chart-title constants and chart-type changes can leave an active replay loop pushing stale data into the newly selected renderer.

2 open issues

Review effort: Lite

… graph titles

Switching between the asset charts reloaded the data without stopping a replay,
which would keep drawing the previous chart type's data; and stopping a replay
redraws the Vega-Lite chart, so the reload now waits for it.

The "Prices" and "Power flow by type" titles were spelled out again in
GenericAsset.chart_sensors; both now come from belief_charts.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Ahmad-Wahid <ahmedwahid16101@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The change still has an unresolved critical test-isolation issue that can contaminate other database tests, along with a documentation consistency nit.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Low severity Add punctuation to RST field descriptions

flexmeasures/​data/​models/​generic_assets.py:1009

The new public method's RST field lines do not end with punctuation, unlike the repository's docstring convention. Please terminate the :param and :returns descriptions so the docstring remains valid and consistent with the surrounding public APIs.

2 resolved since last review

Review effort: Lite

Co-authored-by: Ahmad-Wahid <59763365+Ahmad-Wahid@users.noreply.github.com>
Flix6x and others added 6 commits November 2, 2026 13:37
… case of their titles

The Vega-Lite Charge Points chart capitalizes graph titles before matching them,
but chart_sensors compared the raw titles,
so a graph titled "prices" or "power flow by type" was drawn without its data.
One helper in belief_charts.py now decides which sensors of a graph the chart draws,
used by both chart_for_chargepoint_sessions and chart_sensors.
The power-flow filter now also matches the capitalized title,
and no longer mutates the caller's sensors_to_show.
The fast chart matches companion titles the same way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UBBvKZkzbfEPKctwTN8SDf
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Per testing.instructions.md, modules using fresh_db end in _fresh_db.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UBBvKZkzbfEPKctwTN8SDf
Signed-off-by: F.N. Claessen <claessen@seita.nl>
The field is new, so no older client sends chart_type,
and legacy aliases are for renamed fields.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UBBvKZkzbfEPKctwTN8SDf
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UBBvKZkzbfEPKctwTN8SDf
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Also say that the Charge Points chart lists its session sensors first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UBBvKZkzbfEPKctwTN8SDf
Signed-off-by: F.N. Claessen <claessen@seita.nl>
…nstants

fast-chart.js and graphs.html kept their own copies of the graph titles and the power sensor name.
They now get them from CHARGE_POINT_CHART_NAMES in belief_charts.py, through a Jinja global,
so renaming one no longer needs edits in three places.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UBBvKZkzbfEPKctwTN8SDf
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Flix6x
Flix6x previously approved these changes Nov 2, 2026

@Flix6x Flix6x left a comment

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.

Approving: this does what #2789 asks. On a seeded hub (20 charge points, 7 days), the Default chart's data request drops from 377 SQL statements / 1.7–2.0 s to 17 / 0.1 s, and chart_data without chart-type returns the same as before. Copilot's three points (replay on chart switch, duplicated titles, the db fixture) were all real, and your fixes for them are right.

@Ahmad-Wahid I pushed six commits on top of yours (head d9bca97). Please review them and say if anything doesn't match your intent:

  • 9974e13 — one real bug. The Vega Charge Points chart capitalizes graph titles before matching (belief_charts.py), but chart_sensors compared raw titles. So a graph titled "prices" was drawn as "Prices" while its sensors were never loaded, and it showed up empty; the same for "power flow by type". Both sides now use one helper, charge_point_companion_graph / charge_point_chart_sensors in belief_charts.py, so they can't drift apart. A parametrized test over title cases checks that what the Vega spec filters through equals chart_sensors; it fails on the old code for the three lowercase cases. Side effects:
    • the old power-flow filter changed the caller's sensors_to_show in place (shallow copy); the helper returns a copy;
    • fast mode now also capitalizes before matching (isChargePointCompanionTitle, with a JS test), so lowercase "prices" / "power flow by type" graphs now appear in its Charge Points chart, where before they were not drawn at all.
  • 320bfb7: the test module is renamed to test_asset_chart_data_fresh_db.py, per the testing conventions.
  • b3a8fe3: dropped the chart_type alias on chart_data's new field. No older client sends it; the existing alias on /chart stays.
  • 4349787: an example on the chart-type field (OpenAPI spec regenerated).
  • 399ed9f: punctuation on the :param / :returns lines, as Copilot asked. :returns now says the Charge Points chart lists its session sensors first.
  • d9bca97: fast-chart.js no longer keeps its own copies of the title strings. CHARGE_POINT_CHART_NAMES in belief_charts.py reaches the page as a Jinja global and is passed to the fast chart.

Not done: a JS test for the chart-switch code. It's inline in graphs.html, tied to the template's state, and the JS tests' README says such logic needs to move into a module under static/js first. That would be a follow-up, not this PR.

Tests, on a sandbox: 424 passed across the related modules, JS tests included.

🤖 Generated with Claude Code

https://claude.ai/code/session_01UBBvKZkzbfEPKctwTN8SDf

…e only

Its values are the names of the chart functions, which public chart type names replace in v1.2.0 (#2795),
so the official API makes no promise about it yet: the OpenAPI specs leave it out, as they do beliefs-after,
and the API changelog no longer announces it. The endpoint still accepts it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UBBvKZkzbfEPKctwTN8SDf
Signed-off-by: F.N. Claessen <claessen@seita.nl>
@Flix6x Flix6x added this to the 1.1.0 milestone Nov 2, 2026
@Flix6x

Flix6x commented Nov 2, 2026

Copy link
Copy Markdown
Member

@Ahmad-Wahid We'd like this PR in v1.1.0, so I set the milestone and pushed one more commit (5249165).

chart-type on chart_data is now left out of the API docs, the same way beliefs-after already is. The endpoint still accepts it, and the asset page keeps using it. I dropped the API changelog line for it too. The main changelog line about the faster charts stays.

The reason: the parameter's values are the names of the chart functions (chart_for_multiple_sensors, chart_for_chargepoint_sessions). In v1.2.0, public chart type names will replace them (#2795). Leaving the parameter undocumented until then means the official API makes no promise about those names. A new test checks that the published specs leave it out.

Could you have a look at this commit and my earlier ones? Then we can merge.

🤖 Generated with Claude Code

https://claude.ai/code/session_01UBBvKZkzbfEPKctwTN8SDf

@Ahmad-Wahid

Copy link
Copy Markdown
Contributor Author

@Flix6x Thanks, I went through all seven of your commits (up to 5249165), and they match what I meant this PR to do.

#2801 landed on main after this PR's last CI run, so I also tested this branch merged with current main. It merges cleanly, and 528 tests pass: the chart data tests, the chart models, the asset API, the sensor chart types, and the UI and JS tests.

Your approval was dismissed by the later push, so it needs another one before it can merge.

🤖 Generated with Claude Code

@Flix6x Flix6x left a comment

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.

Great!

@Flix6x
Flix6x merged commit 66efc23 into main Nov 3, 2026
13 checks passed
@Flix6x
Flix6x deleted the perf/2789-chart-data-per-chart-type branch November 3, 2026 08:04
Flix6x added a commit that referenced this pull request Nov 3, 2026
Conflicts resolved as follows, keeping #2790's behaviour:
belief_charts.py keeps CHARGE_POINT_CHART_NAMES and charge_point_chart_sensors from #2790, next to the registry's public charge_point_session_sensors and its companion graphs without sensors left out;
GenericAsset.chart_sensors keeps its rules, but takes the public chart type names, which the asset page sends since the registry (the legacy names are mapped to them, any other name still gets all sensors);
graphs.html imports both fastChartDrawsChartType and isChargePointCompanionTitle, and checks the belief tooltip by the public chart type names;
and test_fast_chart.py keeps both sides' tests, handing the Charge Points chart test the chart's names, which the fast chart needs since #2790.

chart-type on chart_data stays out of the API docs, and its description now names the public chart types;
its test now also asks for the chart types by the public names the asset page sends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UBBvKZkzbfEPKctwTN8SDf
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Asset charts load charge point session data in the Default view, which doesn't draw it

4 participants