-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(performance-metrics): separate storage logic from tracker (#…
…14982) # Overview Closes https://opentrons.atlassian.net/browse/EXEC-415 - Pulls out storage logic from tracker into MetricsStore object - Defines MetricsMetadata to store information about the MetricsStore object # Test Plan - Modified file storage test in performance-metrics to use the new classes. - Loads file content and validates it - Modified test_track_analysis test in api/test_cli - Loads file content and validates it # Changelog - Create MetricsMetadata in shared data which defines where to store data, what to call it, and the headers - Create MetricsStore which handles creating any necessary directories or files and then storing data to the files - Remove storage logic from RobotContextTracker then instantiate MetricsStore and call to its methods to handle data storage # Review requests None # Risk assessment Low
- Loading branch information
1 parent
ff84cb1
commit ce1e64a
Showing
7 changed files
with
229 additions
and
92 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
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
37 changes: 37 additions & 0 deletions
37
performance-metrics/src/performance_metrics/metrics_store.py
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,37 @@ | ||
"""Interface for storing performance metrics data to a CSV file.""" | ||
|
||
import csv | ||
import typing | ||
from opentrons_shared_data.performance.dev_types import MetricsMetadata | ||
from performance_metrics.datashapes import SupportsCSVStorage | ||
|
||
T = typing.TypeVar("T", bound=SupportsCSVStorage) | ||
|
||
|
||
class MetricsStore(typing.Generic[T]): | ||
"""Dataclass to store data for tracking robot context.""" | ||
|
||
def __init__(self, metadata: MetricsMetadata) -> None: | ||
"""Initialize the metrics store.""" | ||
self.metadata = metadata | ||
self._data: typing.List[T] = [] | ||
|
||
def add(self, context_data: T) -> None: | ||
"""Add data to the store.""" | ||
self._data.append(context_data) | ||
|
||
def setup(self) -> None: | ||
"""Set up the data store.""" | ||
self.metadata.storage_dir.mkdir(parents=True, exist_ok=True) | ||
self.metadata.data_file_location.touch(exist_ok=True) | ||
self.metadata.headers_file_location.touch(exist_ok=True) | ||
self.metadata.headers_file_location.write_text(",".join(self.metadata.headers)) | ||
|
||
def store(self) -> None: | ||
"""Clear the stored data and write it to the storage file.""" | ||
stored_data = self._data.copy() | ||
self._data.clear() | ||
rows_to_write = [context_data.csv_row() for context_data in stored_data] | ||
with open(self.metadata.data_file_location, "a") as storage_file: | ||
writer = csv.writer(storage_file) | ||
writer.writerows(rows_to_write) |
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
52 changes: 52 additions & 0 deletions
52
performance-metrics/tests/performance_metrics/test_metrics_store.py
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,52 @@ | ||
"""Tests for the metrics store.""" | ||
|
||
from pathlib import Path | ||
from time import sleep | ||
|
||
from opentrons_shared_data.performance.dev_types import RobotContextState | ||
from performance_metrics.datashapes import RawContextData | ||
from performance_metrics.robot_context_tracker import RobotContextTracker | ||
|
||
# Corrected times in seconds | ||
STARTING_TIME = 0.001 | ||
CALIBRATING_TIME = 0.002 | ||
ANALYZING_TIME = 0.003 | ||
RUNNING_TIME = 0.004 | ||
SHUTTING_DOWN_TIME = 0.005 | ||
|
||
|
||
async def test_storing_to_file(tmp_path: Path) -> None: | ||
"""Tests storing the tracked data to a file.""" | ||
robot_context_tracker = RobotContextTracker(tmp_path, should_track=True) | ||
|
||
@robot_context_tracker.track(state=RobotContextState.STARTING_UP) | ||
def starting_robot() -> None: | ||
sleep(STARTING_TIME) | ||
|
||
@robot_context_tracker.track(state=RobotContextState.CALIBRATING) | ||
def calibrating_robot() -> None: | ||
sleep(CALIBRATING_TIME) | ||
|
||
@robot_context_tracker.track(state=RobotContextState.ANALYZING_PROTOCOL) | ||
def analyzing_protocol() -> None: | ||
sleep(ANALYZING_TIME) | ||
|
||
starting_robot() | ||
calibrating_robot() | ||
analyzing_protocol() | ||
|
||
robot_context_tracker.store() | ||
|
||
with open(robot_context_tracker._store.metadata.data_file_location, "r") as file: | ||
lines = file.readlines() | ||
assert len(lines) == 3, "All stored data should be written to the file." | ||
|
||
split_lines: list[list[str]] = [line.strip().split(",") for line in lines] | ||
assert all( | ||
RawContextData.from_csv_row(line) for line in split_lines | ||
), "All lines should be valid RawContextData instances." | ||
|
||
with open(robot_context_tracker._store.metadata.headers_file_location, "r") as file: | ||
headers = file.readlines() | ||
assert len(headers) == 1, "Header should be written to the headers file." | ||
assert tuple(headers[0].strip().split(",")) == RawContextData.headers() |
Oops, something went wrong.