Skip to content

Implemented unit conversion support for KeyValueStoreSolver#30

Open
tombonfert wants to merge 10 commits into
mainfrom
feature/unit_conversion
Open

Implemented unit conversion support for KeyValueStoreSolver#30
tombonfert wants to merge 10 commits into
mainfrom
feature/unit_conversion

Conversation

@tombonfert
Copy link
Copy Markdown
Collaborator

@tombonfert tombonfert commented May 21, 2026

  • Added unit_conversion_table parameter to MeasurementDBConfig and updated related methods to handle unit conversions.
  • Modified load_blob methods across various cache classes to accept uses_alias parameter for compatibility with unit conversion logic.
  • Updated SolverConfig to include unit conversion mappings and properties.
  • Introduced unit conversion tests to validate functionality and ensure correct behavior when using aliased selectors.
  • Enhanced documentation and comments for clarity on unit conversion processes.

Summary

Changes

Test Plan

  • Unit tests added/updated
  • Manual testing completed
  • Documentation updated (if applicable)

Checklist

  • Code follows project style guidelines
  • Self-review completed
  • No new linter warnings introduced

- Added `unit_conversion_table` parameter to `MeasurementDBConfig` and updated related methods to handle unit conversions.
- Modified `load_blob` methods across various cache classes to accept `uses_alias` parameter for compatibility with unit conversion logic.
- Updated `SolverConfig` to include unit conversion mappings and properties.
- Introduced unit conversion tests to validate functionality and ensure correct behavior when using aliased selectors.
- Enhanced documentation and comments for clarity on unit conversion processes.
@codecov
Copy link
Copy Markdown

codecov Bot commented May 21, 2026

Codecov Report

❌ Patch coverage is 95.34884% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.28%. Comparing base (778ef1f) to head (d4bde55).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...ne/analyze/query/solvers/key_value_store_solver.py 94.93% 3 Missing and 1 partial ⚠️
src/impulse_query_engine/measurement_db.py 60.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #30      +/-   ##
==========================================
+ Coverage   86.03%   86.28%   +0.24%     
==========================================
  Files          52       52              
  Lines        4061     4177     +116     
  Branches      483      498      +15     
==========================================
+ Hits         3494     3604     +110     
- Misses        473      477       +4     
- Partials       94       96       +2     
Files with missing lines Coverage Δ
..._engine/analyze/metadata/time_series_expression.py 73.68% <100.00%> (ø)
..._query_engine/analyze/query/solvers/blob_solver.py 41.81% <100.00%> (ø)
...query_engine/analyze/query/solvers/delta_solver.py 73.88% <100.00%> (ø)
..._query_engine/analyze/query/solvers/empty_cache.py 88.88% <100.00%> (ø)
...query_engine/analyze/query/solvers/series_cache.py 80.00% <100.00%> (ø)
...uery_engine/analyze/query/solvers/solver_config.py 100.00% <100.00%> (ø)
src/impulse_reporting/config/config_parser.py 97.33% <100.00%> (+0.01%) ⬆️
src/impulse_reporting/core/report.py 88.73% <ø> (ø)
src/impulse_query_engine/measurement_db.py 78.66% <60.00%> (-1.34%) ⬇️
...ne/analyze/query/solvers/key_value_store_solver.py 92.76% <94.93%> (+0.81%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- Added `unit_conversion_table` parameter to relevant configuration sections, detailing its role in converting time-series values during `solve()`.
- Enhanced documentation across multiple files to clarify the usage of `source_unit` and `target_unit` columns in the `channel_mapping` table.
- Updated `KeyValueStoreSolver` documentation to reflect changes in unit conversion handling and its integration with aliased selectors.
- Improved clarity on the configuration and functionality of unit conversion within the impulse framework.
…tation and code

- Introduced `JoinKey` class to define custom join keys for alias resolution between `channel_mapping` and `channel_metrics`.
- Updated `ChannelMappingConfig` to include an optional `join_keys` attribute, allowing for flexible alias-resolution configurations.
- Enhanced `SolverConfig` to support the new `ChannelMappingConfig` and its properties.
- Improved documentation to clarify the usage of `join_keys` and internal column names related to channel mapping.
- Added unit tests to validate the behavior of configurable join keys and their integration with the `KeyValueStoreSolver`.
- Removed the field validator for channel mapping coercion, simplifying the handling of `ChannelMappingConfig`.
- Updated tests to ensure compatibility with the new configuration, replacing instances of `TableConfig` with `ChannelMappingConfig` where applicable.
- Enhanced unit tests to validate the behavior of the updated channel mapping configuration and its integration with the `KeyValueStoreSolver`.
- Modified the `unit_conversion.csv` file to add an `is_base` column, indicating whether each unit is a base unit for its group.
- Updated entries for speed and rotation units to reflect their base status, enhancing clarity for unit conversion processes.
@tombonfert tombonfert marked this pull request as ready for review May 23, 2026 15:10
@tombonfert tombonfert requested a review from a team as a code owner May 23, 2026 15:10

agg_exprs = [F.flatten(F.collect_list("selector_ids")).alias("selector_ids")]
if has_unit_cols:
agg_exprs.append(F.first(source_unit_col, ignorenulls=True).alias(source_unit_col))
Copy link
Copy Markdown
Collaborator

@adefabian adefabian May 24, 2026

Choose a reason for hiding this comment

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

What happens if we have multiple aliases on the same physical channel with different target_units?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

yep this would be a problem. :)
I've implemented a fix now, which uses collect_set to collect available source and target units for a physical channel. In case there are > 1 source/target units available, we are raising a ValueError.

This means, right now we do not support different aliases resolving to the same physical channel, which have different source/target units defined. If this becomes a requirement in the future, the solution is to map the conversion factor to selector_id rather than to (container_id, channel_id), i..e. physical channel


channels_df = channels_df.withColumn(
factor_col,
F.col("_src_factor") / F.col("_tgt_factor"),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

currently we dont check if the value != 0 should we add something so we raise an error early on?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I added validation checks for the unit conversion table in _validate_unit_conversion_table

…tion

- Updated the `KeyValueStoreSolver` to propagate effective `source_unit` and `target_unit` during query resolution, ensuring correct unit conversions based on `channel_metrics` and `channel_mapping`.
- Enhanced documentation to clarify the behavior of `source_unit` and `target_unit`, including their precedence and fallback mechanisms.
- Added new unit tests to validate the effective unit resolution logic, ensuring accurate conversions and fallback scenarios.
- Refactored related code for improved clarity and maintainability.
- Updated the `KeyValueStoreSolver` to validate that each `(container_id, channel_id)` pair has at most one distinct `source_unit` and one distinct `target_unit`, preventing silent mis-conversions.
- Added error handling to raise a `ValueError` when conflicting unit conversions are detected for aliased selectors, providing detailed information about the conflicts.
- Enhanced documentation to clarify the constraints on unit conversions and the behavior of the solver when encountering conflicting aliases.
- Introduced new unit tests to validate the conflict detection logic and ensure correct error handling for unit conversion scenarios.
- Introduced a new method `_validate_unit_conversion_table` to ensure that the `conversion_factor` in the unit conversion table is a positive non-null number, raising a `ValueError` for invalid entries.
- Updated the `_compute_conversion_factors` method to call the validation method, ensuring that any malformed data is caught early in the processing pipeline.
- Added comprehensive unit tests to validate the new error handling for zero, negative, and null conversion factors, providing clear feedback on invalid rows.
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