-
Notifications
You must be signed in to change notification settings - Fork 13
Improve progress bar by tracking record metrics #184
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
9 commits
Select commit
Hold shift + click to select a range
7a2f720
Add AppContext for tracking record reader metrics
yunzheng 80e6f7d
Introduce match_record_with_context helper function
yunzheng 1ff3911
Linting
yunzheng a8bc36f
Move helper function to context.py
yunzheng 2f96e95
f-string consistency
yunzheng d341fc4
Update flow/record/context.py
yunzheng dd70e27
Remove unneeded whitespace changes
yunzheng cef470e
Apply suggestions from code review (consistency)
yunzheng 55c91fb
Add missing ruff format lint check to tox.ini
yunzheng 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
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 |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import sys | ||
| from contextlib import contextmanager | ||
| from contextvars import ContextVar | ||
| from dataclasses import dataclass | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Generator | ||
|
|
||
| from flow.record import Record | ||
| from flow.record.selector import Selector | ||
|
|
||
| APP_CONTEXT: ContextVar[AppContext] = ContextVar("APP_CONTEXT") | ||
|
|
||
|
|
||
| def get_app_context() -> AppContext: | ||
| """Retrieve the application context, creating it if it does not exist. | ||
|
|
||
| Returns: | ||
| The application context. | ||
| """ | ||
| if (ctx := APP_CONTEXT.get(None)) is None: | ||
| ctx = AppContext() | ||
| APP_CONTEXT.set(ctx) | ||
| return ctx | ||
|
|
||
|
|
||
| @contextmanager | ||
| def fresh_app_context() -> Generator[AppContext, None, None]: | ||
| """Create a fresh application context for the duration of the with block.""" | ||
| token = APP_CONTEXT.set(AppContext()) | ||
| try: | ||
| yield APP_CONTEXT.get() | ||
| finally: | ||
| APP_CONTEXT.reset(token) | ||
|
|
||
|
|
||
| # Use slots=True on dataclass for better performance which requires Python 3.10 or later. | ||
| # This can be removed when we drop support for Python 3.9. | ||
yunzheng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if sys.version_info >= (3, 10): | ||
| app_dataclass = dataclass(slots=True) # novermin | ||
| else: | ||
| app_dataclass = dataclass | ||
|
|
||
|
|
||
| @app_dataclass | ||
| class AppContext: | ||
| """Context for the application, holding metrics like amount of processed records.""" | ||
|
|
||
| read: int = 0 | ||
| matched: int = 0 | ||
| unmatched: int = 0 | ||
| source_count: int = 0 | ||
| source_total: int = 0 | ||
|
|
||
|
|
||
| def match_record_with_context(record: Record, selector: Selector | None, context: AppContext) -> bool: | ||
| """Return True if ``record`` matches the ``selector``, also keeps track of relevant metrics in ``context``. | ||
| If selector is None, it will always return True. | ||
|
|
||
| When calling this function, it also increases the ``context.read`` property. | ||
|
|
||
| Arguments: | ||
| record: The record to match against the selector. | ||
| selector: The selector to use for matching. | ||
| context: The context in which the record is being matched. | ||
|
|
||
| Returns: | ||
| True if record matches the selector, or if selector is None | ||
| """ | ||
| context.read += 1 | ||
| if selector is None or selector.match(record): | ||
| context.matched += 1 | ||
| return True | ||
| context.unmatched += 1 | ||
| return False | ||
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.
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.