Skip to content

Feat/sensor data source filtering#2083

Open
BelhsanHmida wants to merge 19 commits intocopilot/add-account-id-to-data-sourcefrom
feat/sensor-data-source-filtering
Open

Feat/sensor data source filtering#2083
BelhsanHmida wants to merge 19 commits intocopilot/add-account-id-to-data-sourcefrom
feat/sensor-data-source-filtering

Conversation

@BelhsanHmida
Copy link
Copy Markdown
Contributor

@BelhsanHmida BelhsanHmida commented Apr 2, 2026

Description

  • Add source_account_id filtering to GET /api/v3_0/sensors/<id>/data so sensor data can be filtered by the account linked to a data source.
  • Keep source as the existing data-source ID filter for backward compatibility.
  • Fix the OpenAPI/Swagger docs for the endpoint so the actual GET query parameters are exposed correctly.
  • Clarify the API docs for supported source filtering on this endpoint.
  • Add test coverage for account-based source filtering.
  • Updated documentation/changelog.rst

Look & Feel

  • Swagger/OpenAPI for GET /api/v3_0/sensors/<id>/data now shows resolution, source, and source_account_id as query parameters.

Example request:

GET /api/v3_0/sensors/1/data?start=2021-05-02T00:00:00+02:00&duration=PT1H20M&unit=m%C2%B3%2Fh&resolution=PT20M&source_account_id=2

How to test

Run:
pytest flexmeasures/api/v3_0/tests/test_sensor_data.py

Optionally inspect Swagger/OpenAPI and verify GET /api/v3_0/sensors//data shows:

  • resolution
  • source
  • source_account_id

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

Context:
- source filtering needs to support data sources linked to accounts

Change:
- add a reusable query criterion for filtering by source account ids
Context:
- account-based source filtering needs to reach belief queries from sensor search helpers

Change:
- add source_account_ids to the time-series search interfaces and pass it into source criteria generation
Context:
- the GET sensor data schema needs to expose account-linked source filtering

Change:
- add source_account_id to the sensor data schema and pass it into belief searches
- define a query schema for documenting the actual GET parameters
Context:
- the GET sensor data route was pointing Swagger at the wrong query schema

Change:
- switch the documented query schema to the GET-specific one
- clarify source and source_account_id in the endpoint docs
Context:
- the sensor data endpoint now accepts source_account_id for source filtering

Change:
- add a GET sensor data test that verifies account-linked sources are filtered correctly
Context:
- the notation docs should match the actual source filtering supported by the endpoint

Change:
- document source and source_account_id for GET sensor data
- clarify that source type filtering is not supported there
Context:
- the endpoint behavior and Swagger exposure changed in a user-visible way

Change:
- add a changelog line for source account filtering and Swagger query parameter exposure
Context:
- the sensor data schema and route docs changed the exposed GET query parameters

Change:
- regenerate the checked-in OpenAPI spec to match the current API surface
@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community bot commented Apr 2, 2026

Documentation build overview

📚 flexmeasures | 🛠️ Build #32143429 | 📁 Comparing 9b61895 against latest (1ce87c1)

  🔍 Preview build  

Show files changed (9 files in total): 📝 9 modified | ➕ 0 added | ➖ 0 deleted
File Status
changelog.html 📝 modified
genindex.html 📝 modified
_autosummary/flexmeasures.api.common.schemas.sensor_data.html 📝 modified
_autosummary/flexmeasures.api.v3_0.sensors.html 📝 modified
_autosummary/flexmeasures.data.models.time_series.html 📝 modified
_autosummary/flexmeasures.data.queries.utils.html 📝 modified
_autosummary/flexmeasures.data.services.data_sources.html 📝 modified
api/notation.html 📝 modified
api/v3_0.html 📝 modified

…-data-source-filtering

Signed-off-by: Mohamed Belhsan Hmida <149331360+BelhsanHmida@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

@Flix6x Flix6x left a comment

Choose a reason for hiding this comment

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

Preliminary review. Direction looks good.

The failing test in the pipeline is doing its job well, informing us of an oversight (that almost always trips us up when we extend schemas). In the long run, the UI schemas should be automatically derived from the API schemas. We should probably open an issue for that.

Comment on lines +271 to +276
class GetSensorDataQuerySchema(SensorDataTimingDescriptionSchema):
"""Document the actual query parameters for GET /sensors/<id>/data."""

resolution = DurationField(required=False)
source = SourceIdField(required=False)
source_account = AccountIdField(required=False, data_key="source_account_id")
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.

At first glance it seems redundant to define this again, given GetSensorDataSchema already exists. Maybe this new class should become a mixin for GetSensorDataSchema.

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.

No answer yet?

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.

Good point. I refactored the shared GET sensor-data filter fields into a mixin, so GetSensorDataSchema and GetSensorDataQuerySchema now reuse the same resolution / source / account definitions instead of duplicating them.

Comment on lines -608 to +610
schema: SensorDataTimingDescriptionSchema
schema: GetSensorDataQuerySchema
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.

Why?

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.

No answer yet?

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.

Originally, this switch was only there to make OpenAPI expose the actual GET query parameters (resolution, source, and now account), because SensorDataTimingDescriptionSchema did not include them. At that point the downside was that GetSensorDataQuerySchema duplicated fields already defined elsewhere, which indeed made it look redundant.

With the latest refactor, the shared GET filter fields now live in a mixin that both GetSensorDataSchema and GetSensorDataQuerySchema inherit from. So the reason for keeping GetSensorDataQuerySchema is now just that the route needs a query-only schema for OpenAPI, while the actual field definitions stay shared and in sync.

values = response.json["values"]
# The fixture also stores data from an accountless "Other source".
# Filtering by the user account should exclude those points.
assert all(a == b for a, b in zip(values, [91.5, 92.1, None, None]))
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.

Note to self: check what the test is really doing here. Cross-reference where in the fixtures these values are defined.

@BelhsanHmida BelhsanHmida requested a review from Flix6x April 7, 2026 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants