1111 DurableExecutionInvocationOutput ,
1212 InvocationStatus ,
1313)
14- from aws_durable_execution_sdk_python .lambda_service import ErrorObject , OperationUpdate
14+ from aws_durable_execution_sdk_python .lambda_service import ErrorObject , OperationUpdate , OperationStatus
1515
1616from aws_durable_execution_sdk_python_testing .exceptions import (
1717 ExecutionAlreadyStartedException ,
@@ -99,6 +99,30 @@ def get_execution(self, execution_arn: str) -> Execution:
9999 msg : str = f"Execution { execution_arn } not found"
100100 raise ResourceNotFoundException (msg ) from e
101101
102+ @staticmethod
103+ def _get_execution_status (execution : Execution ) -> str :
104+ """Get execution status string."""
105+ if not execution .is_complete :
106+ return "RUNNING"
107+
108+ if not execution .result :
109+ return OperationStatus .FAILED .value
110+
111+ match execution .result .status :
112+ case InvocationStatus .SUCCEEDED :
113+ return OperationStatus .SUCCEEDED .value
114+ case InvocationStatus .FAILED :
115+ if execution .result .error and execution .result .error .type :
116+ error_type = execution .result .error .type .lower ()
117+ match error_type :
118+ case t if "timeout" in t :
119+ return OperationStatus .TIMED_OUT .value
120+ case t if "stop" in t :
121+ return OperationStatus .STOPPED .value
122+ return OperationStatus .FAILED .value
123+ case _:
124+ return OperationStatus .FAILED .value
125+
102126 def get_execution_details (self , execution_arn : str ) -> GetDurableExecutionResponse :
103127 """Get detailed execution information for web API response.
104128
@@ -112,21 +136,8 @@ def get_execution_details(self, execution_arn: str) -> GetDurableExecutionRespon
112136 ResourceNotFoundException: If execution does not exist
113137 """
114138 execution = self .get_execution (execution_arn )
115-
116- # Extract execution details from the first operation (EXECUTION type)
117139 execution_op = execution .get_operation_execution_started ()
118-
119- # Determine status based on execution state
120- if execution .is_complete :
121- if (
122- execution .result
123- and execution .result .status == InvocationStatus .SUCCEEDED
124- ):
125- status = "SUCCEEDED"
126- else :
127- status = "FAILED"
128- else :
129- status = "RUNNING"
140+ status = self ._get_execution_status (execution )
130141
131142 # Extract result and error from execution result
132143 result = None
@@ -162,99 +173,56 @@ def list_executions(
162173 function_version : str | None = None , # noqa: ARG002
163174 execution_name : str | None = None ,
164175 status_filter : str | None = None ,
165- time_after : str | None = None , # noqa: ARG002
166- time_before : str | None = None , # noqa: ARG002
176+ time_after : str | None = None ,
177+ time_before : str | None = None ,
167178 marker : str | None = None ,
168179 max_items : int | None = None ,
169180 reverse_order : bool = False , # noqa: FBT001, FBT002
170181 ) -> ListDurableExecutionsResponse :
171- """List executions with filtering and pagination.
172-
173- Args:
174- function_name: Filter by function name
175- function_version: Filter by function version
176- execution_name: Filter by execution name
177- status_filter: Filter by status (RUNNING, SUCCEEDED, FAILED)
178- time_after: Filter executions started after this time
179- time_before: Filter executions started before this time
180- marker: Pagination marker
181- max_items: Maximum items to return (default 50)
182- reverse_order: Return results in reverse chronological order
183-
184- Returns:
185- ListDurableExecutionsResponse: List of executions with pagination
186- """
187- # Get all executions from store
188- all_executions = self ._store .list_all ()
189-
190- # Apply filters
191- filtered_executions = []
192- for execution in all_executions :
193- # Filter by function name
194- if function_name and execution .start_input .function_name != function_name :
195- continue
196-
197- # Filter by execution name
198- if (
199- execution_name
200- and execution .start_input .execution_name != execution_name
201- ):
202- continue
203-
204- # Determine execution status
205- execution_status = "RUNNING"
206- if execution .is_complete :
207- if (
208- execution .result
209- and execution .result .status == InvocationStatus .SUCCEEDED
210- ):
211- execution_status = "SUCCEEDED"
212- else :
213- execution_status = "FAILED"
214-
215- # Filter by status
216- if status_filter and execution_status != status_filter :
217- continue
218-
219- # Convert to ExecutionSummary
220- execution_op = execution .get_operation_execution_started ()
221- execution_summary = ExecutionSummary (
222- durable_execution_arn = execution .durable_execution_arn ,
223- durable_execution_name = execution .start_input .execution_name ,
224- function_arn = f"arn:aws:lambda:us-east-1:123456789012:function:{ execution .start_input .function_name } " ,
225- status = execution_status ,
226- start_timestamp = execution_op .start_timestamp .timestamp ()
227- if execution_op .start_timestamp
228- else datetime .now (UTC ).timestamp (),
229- end_timestamp = execution_op .end_timestamp .timestamp ()
230- if execution_op .end_timestamp
231- else None ,
232- )
233- filtered_executions .append (execution_summary )
234-
235- # Sort by start date
236- filtered_executions .sort (key = lambda e : e .start_timestamp , reverse = reverse_order )
237-
238- # Apply pagination
239- if max_items is None :
240- max_items = 50
241-
242- start_index = 0
182+ """List executions with filtering and pagination."""
183+ # Convert marker to offset
184+ offset = 0
243185 if marker :
244186 try :
245- start_index = int (marker )
187+ offset = int (marker )
246188 except ValueError :
247- start_index = 0
189+ offset = 0
248190
249- end_index = start_index + max_items
250- paginated_executions = filtered_executions [start_index :end_index ]
191+ # Query store directly with parameters
192+ executions , next_marker = self ._store .query (
193+ function_name = function_name ,
194+ execution_name = execution_name ,
195+ status_filter = status_filter ,
196+ time_after = time_after ,
197+ time_before = time_before ,
198+ limit = max_items or 50 ,
199+ offset = offset ,
200+ reverse_order = reverse_order ,
201+ )
251202
252- next_marker = None
253- if end_index < len (filtered_executions ):
254- next_marker = str (end_index )
203+ # Convert to ExecutionSummary objects
204+ execution_summaries = []
205+ for execution in executions :
206+ execution_op = execution .get_operation_execution_started ()
207+ status = self ._get_execution_status (execution )
208+
209+ execution_summaries .append (
210+ ExecutionSummary (
211+ durable_execution_arn = execution .durable_execution_arn ,
212+ durable_execution_name = execution .start_input .execution_name ,
213+ function_arn = f"arn:aws:lambda:us-east-1:123456789012:function:{ execution .start_input .function_name } " ,
214+ status = status ,
215+ start_timestamp = execution_op .start_timestamp .timestamp ()
216+ if execution_op .start_timestamp
217+ else datetime .now (UTC ).timestamp (),
218+ end_timestamp = execution_op .end_timestamp .timestamp ()
219+ if execution_op .end_timestamp
220+ else None ,
221+ )
222+ )
255223
256224 return ListDurableExecutionsResponse (
257- durable_executions = paginated_executions , next_marker = next_marker
225+ durable_executions = execution_summaries , next_marker = next_marker
258226 )
259227
260228 def list_executions_by_function (
@@ -269,22 +237,7 @@ def list_executions_by_function(
269237 max_items : int | None = None ,
270238 reverse_order : bool = False , # noqa: FBT001, FBT002
271239 ) -> ListDurableExecutionsByFunctionResponse :
272- """List executions for a specific function.
273-
274- Args:
275- function_name: The function name to filter by
276- qualifier: Function qualifier/version
277- execution_name: Filter by execution name
278- status_filter: Filter by status (RUNNING, SUCCEEDED, FAILED)
279- time_after: Filter executions started after this time
280- time_before: Filter executions started before this time
281- marker: Pagination marker
282- max_items: Maximum items to return (default 50)
283- reverse_order: Return results in reverse chronological order
284-
285- Returns:
286- ListDurableExecutionsByFunctionResponse: List of executions for the function
287- """
240+ """List executions for a specific function."""
288241 # Use the general list_executions method with function_name filter
289242 list_response = self .list_executions (
290243 function_name = function_name ,
0 commit comments