Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
130 changes: 108 additions & 22 deletions src/aws_durable_execution_sdk_python_testing/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
DurableExecutionInvocationOutput,
InvocationStatus,
)
from aws_durable_execution_sdk_python.lambda_service import ErrorObject, OperationUpdate
from aws_durable_execution_sdk_python.lambda_service import (
ErrorObject,
Operation,
OperationUpdate,
OperationStatus,
OperationType,
)

from aws_durable_execution_sdk_python_testing.exceptions import (
ExecutionAlreadyStartedException,
Expand All @@ -35,6 +41,8 @@
StartDurableExecutionInput,
StartDurableExecutionOutput,
StopDurableExecutionResponse,
TERMINAL_STATUSES,
EventCreationContext,
)
from aws_durable_execution_sdk_python_testing.model import (
Event as HistoryEvent,
Expand All @@ -59,8 +67,8 @@


class Executor(ExecutionObserver):
MAX_CONSECUTIVE_FAILED_ATTEMPTS = 5
RETRY_BACKOFF_SECONDS = 5
MAX_CONSECUTIVE_FAILED_ATTEMPTS: int = 5
RETRY_BACKOFF_SECONDS: int = 5

def __init__(
self,
Expand Down Expand Up @@ -420,20 +428,18 @@ def get_execution_state(
def get_execution_history(
self,
execution_arn: str,
include_execution_data: bool = False, # noqa: FBT001, FBT002, ARG002
reverse_order: bool = False, # noqa: FBT001, FBT002, ARG002
include_execution_data: bool = False, # noqa: FBT001, FBT002
reverse_order: bool = False, # noqa: FBT001, FBT002
marker: str | None = None,
max_items: int | None = None,
) -> GetDurableExecutionHistoryResponse:
"""Get execution history with events.

TODO: incomplete

Args:
execution_arn: The execution ARN
include_execution_data: Whether to include execution data in events
reverse_order: Return events in reverse chronological order
marker: Pagination marker
marker: Pagination marker (event_id)
max_items: Maximum items to return

Returns:
Expand All @@ -442,30 +448,110 @@ def get_execution_history(
Raises:
ResourceNotFoundException: If execution does not exist
"""
execution = self.get_execution(execution_arn) # noqa: F841
execution: Execution = self.get_execution(execution_arn)

# Generate events
all_events: list[HistoryEvent] = []
event_id: int = 1
ops: list[Operation] = execution.operations
updates: list[OperationUpdate] = execution.updates
updates_dict: dict[str, OperationUpdate] = {u.operation_id: u for u in updates}
durable_execution_arn: str = execution.durable_execution_arn
for op in ops:
# Step Operation can have PENDING status -> not included in History
operation_update: OperationUpdate | None = updates_dict.get(
op.operation_id, None
)

# Convert operations to events
# This is a simplified implementation - real implementation would need
# to generate proper event history from operations
events: list[HistoryEvent] = []
if op.status is OperationStatus.PENDING:
if (
op.operation_type is not OperationType.CHAINED_INVOKE
or op.start_timestamp is None
):
continue
context: EventCreationContext = EventCreationContext(
op,
event_id,
durable_execution_arn,
execution.start_input,
execution.result,
operation_update,
include_execution_data,
)
pending = HistoryEvent.create_chained_invoke_event_pending(context)
all_events.append(pending)
event_id += 1
if op.start_timestamp is not None:
context = EventCreationContext(
op,
event_id,
durable_execution_arn,
execution.start_input,
execution.result,
operation_update,
include_execution_data,
)
started = HistoryEvent.create_event_started(context)
all_events.append(started)
event_id += 1
if op.end_timestamp is not None and op.status in TERMINAL_STATUSES:
context = EventCreationContext(
op,
event_id,
durable_execution_arn,
execution.start_input,
execution.result,
operation_update,
include_execution_data,
)
finished = HistoryEvent.create_event_terminated(context)
all_events.append(finished)
event_id += 1

# Apply pagination
# Apply cursor-based pagination
if max_items is None:
max_items = 100

start_index = 0
# Handle pagination marker
if reverse_order:
all_events.reverse()
start_index: int = 0
if marker:
try:
start_index = int(marker)
marker_event_id: int = int(marker)
# Find the index of the first event with event_id >= marker
start_index = len(all_events)
for i, e in enumerate(all_events):
is_valid_page_start: bool = (
e.event_id < marker_event_id
if reverse_order
else e.event_id >= marker_event_id
)
if is_valid_page_start:
start_index = i
break
except ValueError:
start_index = 0

end_index = start_index + max_items
paginated_events = events[start_index:end_index]

next_marker = None
if end_index < len(events):
next_marker = str(end_index)
# Get paginated events
end_index: int = start_index + max_items
paginated_events: list[HistoryEvent] = all_events[start_index:end_index]

# Generate next marker
next_marker: str | None = None
if end_index < len(all_events):
if reverse_order:
# Next marker is the event_id of the last returned event
next_marker = (
str(paginated_events[-1].event_id) if paginated_events else None
)
else:
# Next marker is the event_id of the next event after the last returned
next_marker = (
str(all_events[end_index].event_id)
if end_index < len(all_events)
else None
)

return GetDurableExecutionHistoryResponse(
events=paginated_events, next_marker=next_marker
Expand Down
Loading