Skip to content

Commit

Permalink
cherry picked prev branch changes onto edge
Browse files Browse the repository at this point in the history
  • Loading branch information
TamarZanzouri committed Aug 20, 2024
1 parent 00ae0d2 commit 0f2dc73
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 17 deletions.
8 changes: 8 additions & 0 deletions api/src/opentrons/protocol_engine/state/command_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ def get_all_commands(self) -> List[Command]:
for command_id in self._all_command_ids
]

def get_filtered_queue_ids(self, all_commands: bool) -> list[str]:
print(list(self.get_fixit_queue_ids()))
return [
i
for i in self._all_command_ids
if i not in list(self.get_fixit_queue_ids())
]

def get_all_ids(self) -> List[str]:
"""Get all command IDs."""
return self._all_command_ids
Expand Down
37 changes: 30 additions & 7 deletions api/src/opentrons/protocol_engine/state/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,19 +579,40 @@ def get_all(self) -> List[Command]:
"""
return self._state.command_history.get_all_commands()

def get_filtered_queue_ids(self, all_commands: bool) -> List[str]:
"""Get a list of all filtered commands in state.
Entries are returned in the order of first-added command to last-added command.
Replacing a command (to change its status, for example) keeps its place in the
ordering.
If all_commands is True, retunred list will contain all command intents.
If False, return list will contain only safe commands.
"""
return self._state.command_history.get_filtered_queue_ids(
all_commands=all_commands
)

def get_slice(
self,
cursor: Optional[int],
length: int,
self, cursor: Optional[int], length: int, all_commands: bool
) -> CommandSlice:
"""Get a subset of commands around a given cursor.
If the cursor is omitted, a cursor will be selected automatically
based on the currently running or most recently executed command.
"""
running_command = self._state.command_history.get_running_command()
queued_command_ids = self._state.command_history.get_queue_ids()
total_length = self._state.command_history.length()
command_ids = self._state.command_history.get_filtered_command_ids(
command_intents=[
CommandIntent.PROTOCOL,
CommandIntent.SETUP,
CommandIntent.FIXIT,
]
if include_fixit_commands
else [CommandIntent.PROTOCOL, CommandIntent.SETUP]
)
running_command = self._state.command_history.get_running_command()
queued_command_ids = self._state.command_history.get_queue_ids()
total_length = len(command_ids)

# TODO(mm, 2024-05-17): This looks like it's attempting to do the same thing
# as self.get_current(), but in a different way. Can we unify them?
Expand Down Expand Up @@ -620,7 +641,9 @@ def get_slice(
# start is inclusive, stop is exclusive
actual_cursor = max(0, min(cursor, total_length - 1))
stop = min(total_length, actual_cursor + length)
commands = self._state.command_history.get_slice(start=actual_cursor, stop=stop)
commands = self._state.command_history.get_slice(
start=actual_cursor, stop=stop, command_ids=command_ids
)

return CommandSlice(
commands=commands,
Expand Down
7 changes: 3 additions & 4 deletions api/src/opentrons/protocol_runner/run_orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,18 +257,17 @@ def get_current_command(self) -> Optional[CommandPointer]:
return self._protocol_engine.state_view.commands.get_current()

def get_command_slice(
self,
cursor: Optional[int],
length: int,
self, cursor: Optional[int], length: int, all_commands: bool
) -> CommandSlice:
"""Get a slice of run commands.
Args:
cursor: Requested index of first command in the returned slice.
length: Length of slice to return.
all_commands: Get all command intents.
"""
return self._protocol_engine.state_view.commands.get_slice(
cursor=cursor, length=length
cursor=cursor, length=length, all_commands=all_commands
)

def get_command_error_slice(
Expand Down
11 changes: 11 additions & 0 deletions api/tests/opentrons/protocol_engine/state/test_command_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ def test_get_all_commands(command_history: CommandHistory) -> None:
]


def test_get_all_filtered_commands(command_history: CommandHistory) -> None:
"""It should return a list of all commapnds."""
assert command_history.get_filtered_queue_ids(all_commands=False) == []
command_entry_1 = create_queued_command_entry()
command_entry_2 = create_queued_command_entry(index=1)
command_history._add("0", command_entry_1)
command_history._add("1", command_entry_2)
command_history._add_to_fixit_queue("1")
assert command_history.get_filtered_queue_ids(all_commands=False) == ["0"]


def test_get_all_ids(command_history: CommandHistory) -> None:
"""It should return a list of all command IDs."""
assert command_history.get_all_ids() == []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ def test_get_current() -> None:
def test_get_slice_empty() -> None:
"""It should return a slice from the tail if no current command."""
subject = get_command_view(commands=[])
result = subject.get_slice(cursor=0, length=2)
result = subject.get_slice(cursor=0, length=2, all_commands=True)

assert result == CommandSlice(commands=[], cursor=0, total_length=0)

Expand All @@ -918,15 +918,15 @@ def test_get_slice() -> None:

subject = get_command_view(commands=[command_1, command_2, command_3, command_4])

result = subject.get_slice(cursor=1, length=3)
result = subject.get_slice(cursor=1, length=3, all_commands=True)

assert result == CommandSlice(
commands=[command_2, command_3, command_4],
cursor=1,
total_length=4,
)

result = subject.get_slice(cursor=-3, length=10)
result = subject.get_slice(cursor=-3, length=10, all_commands=True)

assert result == CommandSlice(
commands=[command_1, command_2, command_3, command_4],
Expand All @@ -944,7 +944,7 @@ def test_get_slice_default_cursor_no_current() -> None:

subject = get_command_view(commands=[command_1, command_2, command_3, command_4])

result = subject.get_slice(cursor=None, length=3)
result = subject.get_slice(cursor=None, length=3, all_commands=True)

assert result == CommandSlice(
commands=[command_2, command_3, command_4],
Expand Down Expand Up @@ -975,7 +975,7 @@ def test_get_slice_default_cursor_failed_command() -> None:
failed_command=CommandEntry(index=2, command=command_3),
)

result = subject.get_slice(cursor=None, length=3)
result = subject.get_slice(cursor=None, length=3, all_commands=True)

assert result == CommandSlice(
commands=[command_3, command_4],
Expand All @@ -997,7 +997,7 @@ def test_get_slice_default_cursor_running() -> None:
running_command_id="command-id-3",
)

result = subject.get_slice(cursor=None, length=2)
result = subject.get_slice(cursor=None, length=2, all_commands=True)

assert result == CommandSlice(
commands=[command_3, command_4],
Expand Down

0 comments on commit 0f2dc73

Please sign in to comment.