Skip to content

Commit

Permalink
cleanup and logic fix
Browse files Browse the repository at this point in the history
  • Loading branch information
TamarZanzouri committed Aug 14, 2024
1 parent 4f29706 commit e93f539
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 45 deletions.
33 changes: 14 additions & 19 deletions api/src/opentrons/protocol_engine/state/command_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,6 @@ def get_all_ids(self) -> List[str]:
"""Get all command IDs."""
return self._all_command_ids

def get_filtered_slice(
self, start: int, stop: int, all_commands: bool
) -> List[Command]:
queued_ids = self.get_filtered_queue_ids(
[CommandIntent.PROTOCOL, CommandIntent.SETUP, CommandIntent.FIXIT]
if all_commands
else [CommandIntent.PROTOCOL, CommandIntent.SETUP]
)
commands = list(queued_ids)[start:stop]
return [self._commands_by_id[command].command for command in commands]

def get_slice(
self, start: int, stop: int, command_ids: Optional[list[str]] = None
) -> List[Command]:
Expand Down Expand Up @@ -141,21 +130,27 @@ def get_running_command(self) -> Optional[CommandEntry]:
else:
return self._commands_by_id[self._running_command_id]

def get_filtered_queue_ids(
def get_filtered_command_ids(
self,
command_intents: list[CommandIntent] = [
CommandIntent.PROTOCOL,
CommandIntent.SETUP,
],
) -> OrderedSet[str]:
queued_ids = self._queued_command_ids
) -> List[str]:
filtered_command = self._commands_by_id
if CommandIntent.FIXIT not in command_intents:
queued_ids = self._queued_command_ids.__sub__(
other=self.get_fixit_queue_ids()
)
filtered_command = {
key: val
for key, val in self._commands_by_id.items()
if val.command.intent != CommandIntent.FIXIT
}
if CommandIntent.SETUP not in command_intents:
queued_ids = queued_ids.__sub__(other=self.get_setup_queue_ids())
return queued_ids
filtered_command = {
key: val
for key, val in filtered_command.items()
if val.command.intent != CommandIntent.SETUP
}
return list(filtered_command.keys())

def get_queue_ids(self) -> OrderedSet[str]:
"""Get the IDs of all queued protocol commands, in FIFO order."""
Expand Down
29 changes: 5 additions & 24 deletions api/src/opentrons/protocol_engine/state/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,26 +579,6 @@ def get_all(self) -> List[Command]:
"""
return self._state.command_history.get_all_commands()

def get_filtered_queue_ids(self, all_commands: bool) -> OrderedSet[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(
command_intents=[
CommandIntent.SETUP,
CommandIntent.PROTOCOL,
CommandIntent.FIXIT,
]
if all_commands
else [CommandIntent.SETUP, CommandIntent.PROTOCOL]
)

def get_slice(
self, cursor: Optional[int], length: int, all_commands: bool
) -> CommandSlice:
Expand All @@ -607,7 +587,7 @@ def get_slice(
If the cursor is omitted, a cursor will be selected automatically
based on the currently running or most recently executed command.
"""
queued_command_ids = self._state.command_history.get_filtered_queue_ids(
command_ids = self._state.command_history.get_filtered_command_ids(
command_intents=[
CommandIntent.PROTOCOL,
CommandIntent.SETUP,
Expand All @@ -616,7 +596,7 @@ def get_slice(
if all_commands
else [CommandIntent.PROTOCOL, CommandIntent.SETUP]
)
total_length = len(queued_command_ids)
total_length = len(command_ids)

if cursor is None:
current_cursor = self.get_current()
Expand All @@ -638,8 +618,9 @@ def get_slice(
# start is inclusive, stop is exclusive
actual_cursor = max(0, min(cursor_index, total_length - 1))
stop = min(total_length, actual_cursor + length)
commands = self._state.command_history.get_filtered_slice(
start=actual_cursor, stop=stop, all_commands=all_commands

commands = self._state.command_history.get_slice(
start=actual_cursor, stop=stop, command_ids=command_ids
)

return CommandSlice(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ 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 commands without fixit commands."""
assert list(command_history.get_filtered_queue_ids()) == []
assert list(command_history.get_filtered_command_ids()) == []
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_queue("0")
command_history._add_to_queue("1")
command_history._add_to_fixit_queue("1")
assert list(command_history.get_filtered_queue_ids()) == ["0"]
assert list(command_history.get_filtered_command_ids()) == ["0"]


def test_get_all_ids(command_history: CommandHistory) -> None:
Expand Down

0 comments on commit e93f539

Please sign in to comment.