You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
perf: load only the data each asset chart type draws - #2790
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
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>
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.
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.
… 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>
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.
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.
… 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>
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>
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.
…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>
@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.
@Flix6x Thanks, I went through all seven of your commits (up to 5249165), and they match what I meant this PR to do.
9974e13: a good catch. Having the chart and chart_sensors share charge_point_companion_graph is better than my two copies, and it also stops the chart from changing the caller's sensors_to_show.
d9bca97: passing CHARGE_POINT_CHART_NAMES to the fast chart removes the last copy of the titles in JS.
The test rename to _fresh_db, the dropped alias, the example and the docstring punctuation are all fine.
#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.
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_dataacceptschart-type, as/chartalready 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 powerfrom "Power flow by type", and the "Prices" sensors. These are the same ruleschart_for_chargepoint_sessionsapplies when drawing.charge points power) are named once inbelief_charts.pyand used by both the chart andchart_sensors.test_asset_chart_data.py(the sensors per chart type, model and API) and two JS tests intest_chart_data_cache.py(the URL, and the cache per chart type).documentation/changelog.rstanddocumentation/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
With the
chart_datachange reverted, thechart_for_multiple_sensorsandchart_for_chargepoint_sessionscases oftest_chart_data_holds_only_what_the_chart_type_drawsfail: 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 onechart_datarequest with that chart'schart-type, and the Charge Points chart should look the same as before.Further Improvements
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
🤖 Generated with Claude Code