-
Notifications
You must be signed in to change notification settings - Fork 178
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
chore(api): handle performance-metrics package not existing #14922
Merged
DerekMaggio
merged 15 commits into
edge
from
EXEC-407-handle-performance-metrics-package-not-existing
Apr 17, 2024
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8a9325d
chore: conditionally import time functions based on os
DerekMaggio ece7c7d
test: testy tests
DerekMaggio 4f8b810
chore: formatting
DerekMaggio 2ef8fbd
fix: only call once
DerekMaggio 48001ac
fix(protocol-designer): moveLabware newLocation error accounts for cu…
jerader 6a222c1
fix(app): Handle Unsafe Move to Plunger during Drop-Tip (#14910)
mjhuff 5df1b03
chore: define the shape of something that can be tracked
DerekMaggio 640ca28
chore: fix imports
DerekMaggio 2185bf7
chore: expose performance metrics
DerekMaggio ad3e12d
chore: use performance metrics
DerekMaggio 66b8c9f
feat: add performance helpers to setup performance-metrics
DerekMaggio 86e7c60
Merge remote-tracking branch 'origin/edge' into EXEC-407-handle-perfo…
DerekMaggio 098da31
chore: formatting
DerekMaggio f59a0b1
chore: docstring
DerekMaggio 65176b7
feat(api): add analysis context tracking (#14928)
DerekMaggio 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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,76 @@ | ||
"""Performance helpers for tracking robot context.""" | ||
|
||
from pathlib import Path | ||
from opentrons_shared_data.performance.dev_types import ( | ||
SupportsTracking, | ||
F, | ||
RobotContextState, | ||
) | ||
from opentrons_shared_data.robot.dev_types import RobotTypeEnum | ||
from typing import Callable, Type | ||
from opentrons.config import ( | ||
feature_flags as ff, | ||
get_performance_metrics_data_dir, | ||
robot_configs, | ||
) | ||
|
||
|
||
_should_track = ff.enable_performance_metrics( | ||
RobotTypeEnum.robot_literal_to_enum(robot_configs.load().model) | ||
) | ||
|
||
|
||
def _handle_package_import() -> Type[SupportsTracking]: | ||
"""Handle the import of the performance_metrics package. | ||
|
||
If the package is not available, return a stubbed tracker. | ||
""" | ||
try: | ||
from performance_metrics import RobotContextTracker | ||
|
||
return RobotContextTracker | ||
except ImportError: | ||
return StubbedTracker | ||
|
||
|
||
package_to_use = _handle_package_import() | ||
_robot_context_tracker: SupportsTracking | None = None | ||
|
||
|
||
class StubbedTracker(SupportsTracking): | ||
"""A stubbed tracker that does nothing.""" | ||
|
||
def __init__(self, storage_location: Path, should_track: bool) -> None: | ||
"""Initialize the stubbed tracker.""" | ||
pass | ||
|
||
def track(self, state: RobotContextState) -> Callable[[F], F]: | ||
"""Return the function unchanged.""" | ||
|
||
def inner_decorator(func: F) -> F: | ||
"""Return the function unchanged.""" | ||
return func | ||
|
||
return inner_decorator | ||
|
||
def store(self) -> None: | ||
"""Do nothing.""" | ||
pass | ||
|
||
|
||
def _get_robot_context_tracker() -> SupportsTracking: | ||
"""Singleton for the robot context tracker.""" | ||
global _robot_context_tracker | ||
if _robot_context_tracker is None: | ||
# TODO: replace with path lookup and should_store lookup | ||
_robot_context_tracker = package_to_use( | ||
get_performance_metrics_data_dir(), _should_track | ||
) | ||
return _robot_context_tracker | ||
|
||
|
||
def track_analysis(func: F) -> F: | ||
"""Track the analysis of a protocol.""" | ||
return _get_robot_context_tracker().track(RobotContextState.ANALYZING_PROTOCOL)( | ||
func | ||
) |
This file contains 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 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,28 @@ | ||
"""Tests for performance_helpers.""" | ||
|
||
from pathlib import Path | ||
from opentrons_shared_data.performance.dev_types import RobotContextState | ||
from opentrons.util.performance_helpers import ( | ||
StubbedTracker, | ||
_get_robot_context_tracker, | ||
) | ||
|
||
|
||
def test_return_function_unchanged() -> None: | ||
"""Test that the function is returned unchanged when using StubbedTracker.""" | ||
tracker = StubbedTracker(Path("/path/to/storage"), True) | ||
|
||
def func_to_track() -> None: | ||
pass | ||
|
||
assert ( | ||
tracker.track(RobotContextState.ANALYZING_PROTOCOL)(func_to_track) | ||
is func_to_track | ||
) | ||
|
||
|
||
def test_singleton_tracker() -> None: | ||
"""Test that the tracker is a singleton.""" | ||
tracker = _get_robot_context_tracker() | ||
tracker2 = _get_robot_context_tracker() | ||
assert tracker is tracker2 |
This file contains 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 |
---|---|---|
@@ -1 +1,5 @@ | ||
"""Opentrons performance metrics library.""" | ||
|
||
from .robot_context_tracker import RobotContextTracker | ||
|
||
__all__ = ["RobotContextTracker"] |
This file contains 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 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 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 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.
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.
Should I check opentrons.config.IS_ROBOT here?
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.
nah I think this is totally fine