Skip to content

Commit

Permalink
restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
TamarZanzouri committed Aug 2, 2024
1 parent 8ce0aa8 commit 6e94b90
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 21 deletions.
3 changes: 2 additions & 1 deletion api/src/opentrons/protocol_engine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
CommandType,
CommandIntent,
)
from .state import State, StateView, StateSummary, CommandSlice, CommandPointer, Config
from .state import State, StateView, StateSummary, CommandSlice, CommandPointer, Config, CommandErrorSlice
from .plugins import AbstractPlugin

from .types import (
Expand Down Expand Up @@ -81,6 +81,7 @@
"State",
"StateView",
"CommandSlice",
"CommandErrorSlice",
"CommandPointer",
# public value interfaces and models
"LabwareOffset",
Expand Down
2 changes: 2 additions & 0 deletions api/src/opentrons/protocol_engine/state/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
CommandState,
CommandView,
CommandSlice,
CommandErrorSlice,
CommandPointer,
)
from .command_history import CommandEntry
Expand Down Expand Up @@ -39,6 +40,7 @@
"CommandState",
"CommandView",
"CommandSlice",
"CommandErrorSlice",
"CommandPointer",
"CommandEntry",
# labware state and values
Expand Down
8 changes: 8 additions & 0 deletions api/src/opentrons/protocol_engine/state/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ class CommandSlice:
cursor: int
total_length: int

@dataclass(frozen=True)
class CommandErrorSlice:
"""A subset of all commands errors in state."""

commands_errors: List[ErrorOccurrence]
cursor: int
total_length: int


@dataclass(frozen=True)
class CommandPointer:
Expand Down
13 changes: 10 additions & 3 deletions robot-server/robot_server/runs/router/commands_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ async def get_run_commands_error(
description="The maximum number of command errors in the list to return.",
),
run_data_manager: RunDataManager = Depends(get_run_data_manager),
) -> PydanticResponse[SimpleBody[ResponseList[pe_errors.ErrorOccurrence]]]:
) -> PydanticResponse[SimpleMultiBody[ResponseList[pe_errors.ErrorOccurrence]]]:
"""Get a summary of a set of command errors in a run.
Arguments:
Expand All @@ -472,9 +472,16 @@ async def get_run_commands_error(
except RunNotCurrentError as e:
raise RunStopped(detail=str(e)).as_error(status.HTTP_409_CONFLICT) from e

commands_errors_result = ResponseList.construct(__root__=command_error_slice)
meta = MultiBodyMeta(
cursor=command_error_slice.cursor,
totalLength=command_error_slice.total_length,
)

commands_errors_result = ResponseList.construct(
__root__=command_error_slice.commands_errors
)
return await PydanticResponse.create(
content=SimpleBody.construct(data=commands_errors_result),
content=SimpleMultiBody.construct(data=commands_errors_result, meta=meta),
status_code=status.HTTP_200_OK,
)

Expand Down
3 changes: 2 additions & 1 deletion robot-server/robot_server/runs/run_data_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
LabwareOffsetCreate,
StateSummary,
CommandSlice,
CommandErrorSlice,
CommandPointer,
Command,
)
Expand Down Expand Up @@ -383,7 +384,7 @@ def get_commands_slice(

def get_command_error_slice(
self, run_id: str, cursor: Optional[int], length: int
) -> List[ErrorOccurrence]:
) -> CommandErrorSlice:
"""Get a slice of run commands.
Args:
Expand Down
3 changes: 2 additions & 1 deletion robot-server/robot_server/runs/run_orchestrator_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
LabwareOffsetCreate,
StateSummary,
CommandSlice,
CommandErrorSlice,
CommandPointer,
Command,
CommandCreate,
Expand Down Expand Up @@ -339,7 +340,7 @@ def get_command_error_slice(
self,
cursor: Optional[int],
length: int,
) -> List[ErrorOccurrence]:
) -> CommandErrorSlice:
"""Get a slice of run commands error.
Args:
Expand Down
29 changes: 16 additions & 13 deletions robot-server/tests/runs/router/test_commands_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

from opentrons.protocol_engine import (
CommandSlice,
CommandErrorSlice,
CommandPointer,
CommandNote,
commands as pe_commands,
errors as pe_errors,
)

from robot_server.errors.error_responses import ApiError
from robot_server.service.json_api import MultiBodyMeta, ResponseList
from robot_server.service.json_api import MultiBodyMeta, SimpleMultiBody

from robot_server.runs.command_models import (
RequestModelWithCommandCreate,
Expand Down Expand Up @@ -456,13 +457,17 @@ async def test_get_run_commands_errors(
),
)

command_error_slice = CommandErrorSlice(
cursor=1, total_length=3, commands_errors=[error]
)

decoy.when(
mock_run_data_manager.get_command_error_slice(
run_id="run-id",
cursor=None,
length=42,
)
).then_return([error])
).then_return(command_error_slice)

result = await get_run_commands_error(
runId="run-id",
Expand All @@ -471,17 +476,15 @@ async def test_get_run_commands_errors(
pageLength=42,
)

assert result.content.data == ResponseList.construct(
__root__=[
pe_errors.ErrorOccurrence(
id="error-id",
errorType="PrettyBadError",
createdAt=datetime(year=2024, month=4, day=4),
detail="Things are not looking good.",
)
]
)
# assert result.content.meta == MultiBodyMeta(cursor=1, totalLength=3)
assert result.content.data == [
pe_errors.ErrorOccurrence(
id="error-id",
errorType="PrettyBadError",
createdAt=datetime(year=2024, month=4, day=4),
detail="Things are not looking good.",
)
]
assert result.content.meta == MultiBodyMeta(cursor=1, totalLength=3)
assert result.status_code == 200


Expand Down
9 changes: 7 additions & 2 deletions robot-server/tests/runs/test_run_data_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
commands,
types as pe_types,
CommandSlice,
CommandErrorSlice,
CommandPointer,
ErrorOccurrence,
LoadedLabware,
Expand Down Expand Up @@ -869,14 +870,18 @@ def test_get_commands_errors_slice_current_run(
ErrorOccurrence.construct(id="error-id") # type: ignore[call-arg]
]

command_error_slice = CommandErrorSlice(
cursor=1, total_length=3, commands_errors=expected_commands_errors_result
)

decoy.when(mock_run_orchestrator_store.current_run_id).then_return("run-id")
decoy.when(mock_run_orchestrator_store.get_command_error_slice(1, 2)).then_return(
expected_commands_errors_result
command_error_slice
)

result = subject.get_command_error_slice("run-id", 1, 2)

assert expected_commands_errors_result == result
assert command_error_slice == result


def test_get_commands_slice_from_db_run_not_found(
Expand Down

0 comments on commit 6e94b90

Please sign in to comment.