-
Notifications
You must be signed in to change notification settings - Fork 4
Add support for dispatch_ids and queries filters
#213
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
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
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 | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||
| from frequenz.api.dispatch.v1.dispatch_pb2 import ( | ||||||||||||||||||||||||||||||||||||||||||||||
| CreateMicrogridDispatchResponse, | ||||||||||||||||||||||||||||||||||||||||||||||
| DeleteMicrogridDispatchRequest, | ||||||||||||||||||||||||||||||||||||||||||||||
| DispatchFilter, | ||||||||||||||||||||||||||||||||||||||||||||||
| GetMicrogridDispatchRequest, | ||||||||||||||||||||||||||||||||||||||||||||||
| GetMicrogridDispatchResponse, | ||||||||||||||||||||||||||||||||||||||||||||||
| ListMicrogridDispatchesRequest, | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -178,6 +179,46 @@ def _filter_dispatch( | |||||||||||||||||||||||||||||||||||||||||||||
| if dispatch.dry_run != _filter.is_dry_run: | ||||||||||||||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if len(_filter.dispatch_ids) > 0 and dispatch.id not in map( | ||||||||||||||||||||||||||||||||||||||||||||||
| DispatchId, _filter.dispatch_ids | ||||||||||||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if not FakeService._matches_query(_filter, dispatch): | ||||||||||||||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| @staticmethod | ||||||||||||||||||||||||||||||||||||||||||||||
| def _matches_query(filter_: DispatchFilter, dispatch: Dispatch) -> bool: | ||||||||||||||||||||||||||||||||||||||||||||||
| """Check if a dispatch matches the query.""" | ||||||||||||||||||||||||||||||||||||||||||||||
| # Two cases: | ||||||||||||||||||||||||||||||||||||||||||||||
| # - query starts with # and is interpretable as int: filter by id | ||||||||||||||||||||||||||||||||||||||||||||||
| # - otherwise: filter by exact match in 'type' field | ||||||||||||||||||||||||||||||||||||||||||||||
| # Multiple queries use OR logic - dispatch must match at least one | ||||||||||||||||||||||||||||||||||||||||||||||
| if len(filter_.queries) > 0: | ||||||||||||||||||||||||||||||||||||||||||||||
| matches_any_query = False | ||||||||||||||||||||||||||||||||||||||||||||||
| for query in filter_.queries: | ||||||||||||||||||||||||||||||||||||||||||||||
| if query.startswith("#"): | ||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||
| int_id = int(query[1:]) | ||||||||||||||||||||||||||||||||||||||||||||||
| except ValueError: | ||||||||||||||||||||||||||||||||||||||||||||||
| # not an int, interpret as exact type match (without the #) | ||||||||||||||||||||||||||||||||||||||||||||||
| if query[1:] == dispatch.type: | ||||||||||||||||||||||||||||||||||||||||||||||
| matches_any_query = True | ||||||||||||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+203
to
+209
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make the error detection more fine-grained:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||
| query_id = DispatchId(int_id) | ||||||||||||||||||||||||||||||||||||||||||||||
| if dispatch.id == query_id: | ||||||||||||||||||||||||||||||||||||||||||||||
| matches_any_query = True | ||||||||||||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||||||||||||
| elif query == dispatch.type: | ||||||||||||||||||||||||||||||||||||||||||||||
| matches_any_query = True | ||||||||||||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if not matches_any_query: | ||||||||||||||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| async def CreateMicrogridDispatch( | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, super minor and irrelevant, but if you can call
iter()on it, it is already an iterable, right? Why do you need to call it explicitly? It doesn't hurt to do so, so just out of curiosity...Oh, wait, I just looked at
client.list()and it takes an iterator instead of an iterable, I see. I think we could change it in the future toIterable, anyIteratoris iterable too, so it should work. It is pretty annoying not being able to pass a collection (iterable) directly tolist()and having to explicitly convert to an iterator.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#223