Skip to content

feat: allow unit param in file upload - #221

Merged
nhoening merged 4 commits into
mainfrom
feat/unit-param-in-file-upload
Aug 6, 2026
Merged

feat: allow unit param in file upload#221
nhoening merged 4 commits into
mainfrom
feat/unit-param-in-file-upload

Conversation

@nhoening

@nhoening nhoening commented Aug 4, 2026

Copy link
Copy Markdown
Member

The file upload (via post_sensor_data() and _post_sensor_data_file()) should allow to specify the unit parameter. The former accepts a unit param but only uses it for JSON upload.

This PR adds this missing ability.

Signed-off-by: Nicolas Höning <nicolas@seita.nl>
@nhoening nhoening self-assigned this Aug 4, 2026
@coveralls

coveralls commented Aug 4, 2026

Copy link
Copy Markdown

Coverage Report for CI Build 31124985627

Warning

Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes.
Quick fix: rebase this PR. Learn more →

Coverage increased (+0.2%) to 96.58%

Details

  • Coverage increased (+0.2%) from the base build.
  • Patch coverage: 15 of 15 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 731
Covered Lines: 706
Line Coverage: 96.58%
Coverage Strength: 9.66 hits per line

💛 - Coveralls

@nhoening
nhoening requested a review from BelhsanHmida August 5, 2026 07:45
BelhsanHmida and others added 3 commits August 6, 2026 18:23
Servers before v0.30.0 do not read a "unit" form field on the sensor data
upload endpoint. Their webargs location loader copies only the file and the
"belief-time-measured-instantly" field out of the form, so an unknown "unit"
never reaches marshmallow and no validation error is raised. Ingestion then
falls back to `fields.get("unit", sensor.unit)`, which makes the unit
conversion a no-op: the file's values are recorded as if they were already
in the sensor's unit, and the request still returns 200.

The result is silent data corruption (e.g. a 1000x error when uploading W
to a kW sensor) with no signal to the caller. Fail before sending anything
instead, but only when a unit is actually passed, so existing file uploads
are unaffected and incur no extra request.

Base versions are compared via _server_version_at_least so that pre-release
builds such as 0.30.0.dev5, which already expose the field, are accepted.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Dropping unit from json_params is right for the mutual-exclusivity check,
since a unit can now accompany a file upload. But that same list also picks
the error message, so a lone unit stopped counting as "something was passed"
and post_sensor_data(sensor_id=1, unit="MW") reported that neither mode was
chosen -- while a unit sat right there in the call. prior was already in
that position.

Keep unit and prior out of the mode decision, but let them select the
message, so the caller is told which parameters are missing instead. The
genuinely empty call is unaffected.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The guard leans on _server_version_at_least returning False for an unknown
version, which was the one uncovered branch in that helper. A server whose
/api/ response carries no flexmeasures_version key now has to be refused,
since it cannot be shown to honour the unit and would otherwise record the
file's values unconverted behind a 200.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

@BelhsanHmida BelhsanHmida 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.

Tested this works well!

I pushed two small fixes:

1. Version guard. The unit field only landed server-side in FM 0.30.0, and older servers don't reject it they silently ignore it and store the values as if they were already in the sensor's unit. I confirmed this: the same upload without a unit stores 5000 instead of 5, and still returns 200. So passing a unit to a pre-0.30 server gives you a silent 1000x error. Now it raises before anything is uploaded. Only kicks in when a unit is actually passed, so existing uploads are unaffected, and dev builds like 0.30.0.dev5 still pass.

2. Error message. Since unit left json_params, post_sensor_data(sensor_id=1, unit="MW") was answering "you provided neither" even though you clearly did pass something. Now it tells you which parameters are missing.

Also added a test for the case where the server reports no version at all.

@nhoening
nhoening merged commit 73fedf8 into main Aug 6, 2026
5 of 7 checks passed
@nhoening
nhoening deleted the feat/unit-param-in-file-upload branch August 6, 2026 18:25
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.

3 participants