-
Notifications
You must be signed in to change notification settings - Fork 1
14 - Support Dynamic Labels #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f8ad4a9
modified README
tuanhungngyn 8696f82
added LabelStateTracker
tuanhungngyn 24769ca
implementing dynamic labels
tuanhungngyn 01cf2f4
fixed linting
tuanhungngyn e816beb
fixed mcap labels
tuanhungngyn 1f30b64
added cdr functioniality
tuanhungngyn 47f71f9
linting
tuanhungngyn e341239
fixed docstring grammar, adjusted utils function
tuanhungngyn ab89614
refactoring code with Null Object
tuanhungngyn cafce4b
added unit tests, config test checks
tuanhungngyn 781a737
removed try except in max update
tuanhungngyn c6359d0
added integration test, fixed topic subscription for state, added lab…
tuanhungngyn c44247e
minor error prone fixes
tuanhungngyn 94f61ab
modified changelog
tuanhungngyn 016300b
reformatting
tuanhungngyn 8dc0766
fixed tests, moved init of updaters to LabelStateTracker init
tuanhungngyn 6be2b8c
reformatting
tuanhungngyn 688b32e
added dynamic label cdr_test
tuanhungngyn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| # Copyright 2025 ReductSoftware UG | ||
| # | ||
| # Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| # of this software and associated documentation files (the "Software"), to deal | ||
| # in the Software without restriction, including without limitation the rights | ||
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| # copies of the Software, and to permit persons to whom the Software is | ||
| # furnished to do so, subject to the following conditions: | ||
| # | ||
| # The above copyright notice and this permission notice shall be included in | ||
| # all copies or substantial portions of the Software. | ||
| # | ||
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
| # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| # THE SOFTWARE. | ||
|
|
||
| """Label state tracker for dynamic labels.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from .models import LabelMode, LabelTopicConfig, PipelineConfig | ||
| from .utils import extract_field | ||
|
|
||
|
|
||
| class LabelStateTracker: | ||
| """Class for tracking dynamic label state.""" | ||
|
|
||
| def __init__(self, cfg: PipelineConfig, logger=None): | ||
| """Initialize a LabelStateTracker instance.""" | ||
| self._configs: dict[str, LabelTopicConfig] = { | ||
| label_cfg.topic: label_cfg for label_cfg in cfg.labels | ||
| } | ||
| self.logger = logger | ||
| self._updaters: dict[str, callable] = {} | ||
|
|
||
| for topic, label_cfg in self._configs.items(): | ||
| if label_cfg.mode is LabelMode.LAST: | ||
| self._updaters[topic] = self._update_last | ||
| elif label_cfg.mode is LabelMode.FIRST: | ||
| self._updaters[topic] = self._update_first | ||
| else: | ||
| self._updaters[topic] = self._update_max | ||
|
|
||
| self._values: dict[str, Any] = {} | ||
|
|
||
| def update(self, topic_name, msg): | ||
| """Update label state from a single incoming message.""" | ||
| cfg = self._configs.get(topic_name) | ||
| if cfg is None: | ||
| if self.logger: | ||
| self.logger.info( | ||
| "Cannot read config for topic " f"'{topic_name}'. Returning ..." | ||
| ) | ||
| return | ||
|
|
||
| updater = self._updaters[topic_name] | ||
|
|
||
| for label_name, field_path in cfg.fields.items(): | ||
| value = extract_field(msg, field_path) | ||
| updater(label_name, value) | ||
|
|
||
| def _update_last(self, label_key: str, value: Any): | ||
| """Use the most recent message (default).""" | ||
| self._values[label_key] = value | ||
|
|
||
| def _update_first(self, label_key: str, value: Any): | ||
| """Use the first message of the current file.""" | ||
| if label_key not in self._values: | ||
| self._values[label_key] = value | ||
|
|
||
| def _update_max(self, label_key: str, value: Any): | ||
| """Use the maximum value across all messages in the file.""" | ||
| if label_key not in self._values: | ||
| self._values[label_key] = value | ||
| return | ||
|
|
||
| if value > self._values[label_key]: | ||
| self._values[label_key] = value | ||
|
|
||
| def get_labels(self) -> dict[str, str]: | ||
| """Return current labels for writing.""" | ||
| return {k: str(v) for k, v in self._values.items()} | ||
|
|
||
|
|
||
| class NullLabelStateTracker(LabelStateTracker): | ||
| """Null object for LabelStateTracker.""" | ||
|
|
||
| def __init__(self): | ||
| """Initialize the null object without configuration.""" | ||
| pass | ||
|
|
||
| def update(self, topic_name, msg): | ||
| """Do nothing on update, as there is no state to track.""" | ||
| pass | ||
|
|
||
| def get_labels(self) -> dict[str, str]: | ||
| """Return the default/empty state.""" | ||
| return {} |
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
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
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.