Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api, robot-server): get historical and current command errors #16697

Open
wants to merge 59 commits into
base: edge
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
282c99c
WIP intial schema and orchestrator changes
TamarZanzouri Nov 1, 2024
1bb8d7c
changed schema and imports to 8
TamarZanzouri Nov 1, 2024
77902c6
insert the errors to the DB
TamarZanzouri Nov 4, 2024
15db246
refactor into commands table
TamarZanzouri Nov 4, 2024
b88e722
select from commands
TamarZanzouri Nov 4, 2024
b6d33b6
linting and command error slice done
TamarZanzouri Nov 5, 2024
85eda17
added tests and fixed bugs
TamarZanzouri Nov 5, 2024
5bc0500
changed logic for current command errors
TamarZanzouri Nov 5, 2024
e423f37
removed failed_command_errors from state
TamarZanzouri Nov 5, 2024
e29d95f
refactor get command errors
TamarZanzouri Nov 5, 2024
c275c35
Merge branch 'edge' into EXEC-655-store-commands-error-list-in-db
TamarZanzouri Nov 6, 2024
a950cb8
migration and initialization fixes
TamarZanzouri Nov 6, 2024
55a809a
Update robot-server/robot_server/runs/run_store.py
TamarZanzouri Nov 6, 2024
1ccaba3
Merge branch 'edge' into EXEC-655-store-commands-error-list-in-db
TamarZanzouri Nov 6, 2024
9fa97ba
change get_run_errors to get count
TamarZanzouri Nov 6, 2024
f5fe016
Merge branch 'edge' into EXEC-655-store-commands-error-list-in-db
TamarZanzouri Nov 6, 2024
13c5b87
tables tests fixed
TamarZanzouri Nov 6, 2024
a88e95d
added test for historical run
TamarZanzouri Nov 6, 2024
6fea08a
Merge branch 'edge' into EXEC-655-store-commands-error-list-in-db
TamarZanzouri Nov 6, 2024
1bea28e
fixed persistance path
TamarZanzouri Nov 6, 2024
8f73bc1
fixed import from schema 7
TamarZanzouri Nov 7, 2024
c87dc65
rollback add_column change
TamarZanzouri Nov 7, 2024
5405ef3
append_failed_command_id when setting a failed command
TamarZanzouri Nov 7, 2024
f888364
append_failed_command_id when setting a failed command
TamarZanzouri Nov 7, 2024
e6aa375
moved test to command state
TamarZanzouri Nov 8, 2024
66856ac
changed index and lint
TamarZanzouri Nov 8, 2024
02e6806
WIP - command status
TamarZanzouri Nov 8, 2024
37a4aff
fixed failing tests
TamarZanzouri Nov 8, 2024
6c6257f
insert and get with command_status
TamarZanzouri Nov 8, 2024
56d12c4
translate to CommandStatusSQLEnum
TamarZanzouri Nov 8, 2024
c33745b
nips
TamarZanzouri Nov 12, 2024
635a5af
Update robot-server/tests/runs/test_run_store.py
TamarZanzouri Nov 12, 2024
a16636b
nits
TamarZanzouri Nov 12, 2024
a873369
batch edit
TamarZanzouri Nov 13, 2024
67b6d53
batch update without binding
TamarZanzouri Nov 13, 2024
4d00c99
test table with new index and migration bulk fix
TamarZanzouri Nov 13, 2024
38cd6ea
conditional update
TamarZanzouri Nov 13, 2024
1160174
map command status to sql command status
TamarZanzouri Nov 13, 2024
18ce80c
map command status to sql command status in migration
TamarZanzouri Nov 13, 2024
964fddc
map command status to sql command status in migration
TamarZanzouri Nov 13, 2024
5ad4d0d
add missing indexes
TamarZanzouri Nov 13, 2024
de02268
lint
TamarZanzouri Nov 13, 2024
085d4bb
change conversion into enum
TamarZanzouri Nov 13, 2024
378eec1
Merge branch 'edge' into EXEC-655-store-commands-error-list-in-db
TamarZanzouri Nov 13, 2024
ec64c12
fixing merge conflicts - WIP
TamarZanzouri Nov 14, 2024
b285c99
WIP - fix test_tables failing
TamarZanzouri Nov 14, 2024
3a29bfd
update test table
TamarZanzouri Nov 20, 2024
dca8269
Revert "update test table"
TamarZanzouri Nov 20, 2024
4d91d6f
Fix schema_8.py after merge.
SyntaxColoring Nov 14, 2024
fd78ad3
Fix up test_tables.py after merge.
SyntaxColoring Nov 14, 2024
b59601e
Remove attempts to fix prior migrations, for now.
SyntaxColoring Nov 20, 2024
6496d3c
Make command_status column nullable to match what the migration does.
SyntaxColoring Nov 20, 2024
11aa280
Remove enum constraint, for now.
SyntaxColoring Nov 20, 2024
5af324e
Merge branch 'edge' into EXEC-655-store-commands-error-list-in-db
SyntaxColoring Nov 20, 2024
6669e61
Update robot-server/robot_server/persistence/_migrations/v7_to_v8.py
TamarZanzouri Nov 21, 2024
ce3fbb3
Update robot-server/robot_server/runs/run_store.py
TamarZanzouri Nov 21, 2024
e0aacbd
select subquery and minor fixes
TamarZanzouri Nov 21, 2024
b10e8a2
fixed join logic and added logic to the tests
TamarZanzouri Nov 22, 2024
13fd3a8
fixed logic of cursor
TamarZanzouri Nov 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ def test_get_state_update_for_false_positive() -> None:


def test_get_errors_slice_empty() -> None:
"""It should return a slice from the tail if no current command."""
"""It should return an empty error list."""
subject = CommandStore(
config=_make_config(),
error_recovery_policy=_placeholder_error_recovery_policy,
Expand All @@ -1126,11 +1126,6 @@ def test_get_errors_slice() -> None:

subject_view = CommandView(subject.state)

# error_1 = ErrorOccurrence.construct(id="error-id-1") # type: ignore[call-arg]
# error_2 = ErrorOccurrence.construct(id="error-id-2") # type: ignore[call-arg]
# error_3 = ErrorOccurrence.construct(id="error-id-3") # type: ignore[call-arg]
# error_4 = ErrorOccurrence.construct(id="error-id-4") # type: ignore[call-arg]

queue_1 = actions.QueueCommandAction(
request=commands.WaitForResumeCreate(
params=commands.WaitForResumeParams(), key="command-key-1"
Expand Down
53 changes: 42 additions & 11 deletions robot-server/robot_server/persistence/_migrations/v7_to_v8.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Summary of changes from schema 7:

- Adds a new command_error to store the commands error in the commands table
- Adds a new command_status to store the commands status in the commands table
"""

import json
Expand All @@ -13,7 +14,7 @@

import sqlalchemy

from ..database import sql_engine_ctx, sqlite_rowid
from ..database import sql_engine_ctx
from ..tables import schema_8
from .._folder_migrator import Migration

Expand All @@ -38,7 +39,7 @@ def migrate(self, source_dir: Path, dest_dir: Path) -> None:
with ExitStack() as exit_stack:
dest_engine = exit_stack.enter_context(sql_engine_ctx(dest_db_file))

schema_8.metadata.create_all(dest_engine)
# schema_8.metadata.create_all(dest_engine)

dest_transaction = exit_stack.enter_context(dest_engine.begin())

Expand Down Expand Up @@ -72,10 +73,10 @@ def add_column(
def _migrate_command_table_with_new_command_error_col_and_command_status(
dest_transaction: sqlalchemy.engine.Connection,
) -> None:
"""Add a new 'command_intent' column to run_command_table table."""
"""Add a new 'command_error' and 'command_status' column to run_command_table table."""
commands_table = schema_8.run_command_table
select_commands = sqlalchemy.select(commands_table).order_by(sqlite_rowid)

select_commands = sqlalchemy.select(commands_table)
commands_to_update = []
for row in dest_transaction.execute(select_commands).all():
TamarZanzouri marked this conversation as resolved.
Show resolved Hide resolved
data = json.loads(row.command)
new_command_error = (
Expand All @@ -85,12 +86,42 @@ def _migrate_command_table_with_new_command_error_col_and_command_status(
else json.dumps(data["error"])
)
# parse json as enum
new_command_status = CommandStatusSQLEnum(data["status"])
new_command_status = _convert_commands_status_to_sql_command_status(
data["status"]
)
commands_to_update.append(
{
"_id": row.row_id,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Does "_id" really need to have that underscore? Or is it only there because that's how the column was named in the example code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think that as long as we dont use a defined prop name we are good

"command_error": new_command_error,
"command_status": new_command_status,
}
)

if len(commands_to_update) > 0:
update_commands = (
sqlalchemy.update(schema_8.run_command_table)
.where(schema_8.run_command_table.c.row_id == row.row_id)
.values(command_error=new_command_error, command_status=new_command_status)
sqlalchemy.update(commands_table)
.where(commands_table.c.row_id == sqlalchemy.bindparam("_id"))
.values(
{
"command_error": sqlalchemy.bindparam("command_error"),
"command_status": sqlalchemy.bindparam("command_status"),
}
)
)

dest_transaction.execute(update_commands)
dest_transaction.execute(update_commands, commands_to_update)


def _convert_commands_status_to_sql_command_status(
status: str,
) -> CommandStatusSQLEnum:
match status:
case "queued":
return CommandStatusSQLEnum.QUEUED
case "running":
return CommandStatusSQLEnum.RUNNING
case "failed":
return CommandStatusSQLEnum.FAILED
case "succeeded":
return CommandStatusSQLEnum.SUCCEEDED
case _:
assert False, "command status is unknown"
7 changes: 4 additions & 3 deletions robot-server/robot_server/persistence/tables/schema_8.py
TamarZanzouri marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ class CommandStatusSQLEnum(enum.Enum):
sqlalchemy.Column(
"run_id", sqlalchemy.String, sqlalchemy.ForeignKey("run.id"), nullable=False
),
# command_index in commands enumeration
sqlalchemy.Column("index_in_run", sqlalchemy.Integer, nullable=False),
sqlalchemy.Column("command_id", sqlalchemy.String, nullable=False),
sqlalchemy.Column("command", sqlalchemy.String, nullable=False),
Expand Down Expand Up @@ -240,12 +241,12 @@ class CommandStatusSQLEnum(enum.Enum):
"index_in_run",
unique=True,
),
# TODO(tz, 12-11-2024): change index to command_error when partial index is supported.
sqlalchemy.Index(
"ix_run_run_id_index_in_run_command_status", # An arbitrary name for the index.
"ix_run_run_id_command_status_index_in_run", # An arbitrary name for the index.
"run_id",
"index_in_run",
"command_status",
"index_in_run",
unique=True,
),
)

Expand Down
20 changes: 19 additions & 1 deletion robot-server/robot_server/runs/run_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ def update_run_state(
"command_error": pydantic_to_json(command.error)
if command.error
else None,
"command_status": CommandStatusSQLEnum(command.status.value),
"command_status": _convert_commands_status_to_sql_command_status(
command.status.value
),
},
)

Expand Down Expand Up @@ -812,3 +814,19 @@ def _convert_state_to_sql_values(
"_updated_at": utc_now(),
"run_time_parameters": pydantic_list_to_json(run_time_parameters),
}


def _convert_commands_status_to_sql_command_status(
status: str,
) -> CommandStatusSQLEnum:
match status:
case "queued":
return CommandStatusSQLEnum.QUEUED
case "running":
return CommandStatusSQLEnum.RUNNING
case "failed":
return CommandStatusSQLEnum.FAILED
case "succeeded":
return CommandStatusSQLEnum.SUCCEEDED
case _:
assert False, "command status is unknown"
TamarZanzouri marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion robot-server/tests/persistence/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
CREATE INDEX ix_protocol_protocol_kind ON protocol (protocol_kind)
""",
"""
CREATE INDEX ix_run_run_id_index_in_run_command_status ON run_command (run_id, index_in_run, command_status)
CREATE UNIQUE INDEX ix_run_run_id_command_status_index_in_run ON run_command (run_id, command_status, index_in_run)
""",
"""
CREATE INDEX ix_run_command_command_intent ON run_command (command_intent)
Expand Down
5 changes: 2 additions & 3 deletions robot-server/tests/runs/test_run_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def subject(

@pytest.fixture
def protocol_commands() -> List[pe_commands.Command]:
"""Get a StateSummary value object."""
"""Get protocol commands list."""
return [
pe_commands.WaitForResume(
id="pause-1",
Expand Down Expand Up @@ -102,7 +102,7 @@ def protocol_commands() -> List[pe_commands.Command]:

@pytest.fixture
def protocol_commands_errors() -> List[pe_commands.Command]:
"""Get a StateSummary value object."""
"""Get protocol commands errors list."""
return [
pe_commands.WaitForResume(
id="pause-1",
Expand Down Expand Up @@ -360,7 +360,6 @@ async def test_update_run_state_command_with_errors(
length=len(protocol_commands_errors),
cursor=0,
)
print(command_errors_result)

assert command_errors_result.commands_errors == [
item.error for item in protocol_commands_errors
Expand Down
Loading