-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(api): handle performance-metrics package not existing (#14922)
# Overview Closes https://opentrons.atlassian.net/browse/EXEC-407 We do not want performance-metrics to be a required dependency for the publicly available opentrons package on PyPI. But we want to utilize performance-metrics when running the opentrons package on the robot itself. The issue is, that since the performance-metrics package uses decorators to track running code, the decorators will always need to exist. This PR handles the case where the performance-metrics package does not exist and injects stubs where necessary. # Changelog - Created the `SupportsTracking` mypy Protocol to define an interface both `RobotContextTracker` and the stubbed class, `StubbedTracker` implement - Added performance-metrics as a dev dependency so tests using performance-metrics can still run - Created `performance_helpers.py` - Contains `StubbedTracker` defininition - Handles loading `StubberTracker` if performance-metrics library fails to load - Provides `_get_robot_context_tracker` private singleton function for eventual public-facing functions to use. # Test Plan - Testing to ensure stubbed `track` decorator returns the decorated function unchanged - Validate singleton logic of _get_robot_context_tracker # Risk assessment Low, still not actually being used anywhere --------- Co-authored-by: Jethary Rader <[email protected]> Co-authored-by: Jamey Huffnagle <[email protected]>
- Loading branch information
1 parent
aae8a10
commit 0940e7c
Showing
14 changed files
with
1,176 additions
and
710 deletions.
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.