main
py 132 lines 4.41 KB
Raw
1 from fastapi import APIRouter
2 from fastapi import HTTPException
3 from fastapi import Security
4 from loguru import logger
5
6 from app.auth.utils import AuthHandler
7 from app.connectors.shuffle.schema.workflows import RequestWorkflowExecutionModel
8 from app.connectors.shuffle.schema.workflows import RequestWorkflowExecutionResponse
9 from app.connectors.shuffle.schema.workflows import WorkflowExecutionBodyModel
10 from app.connectors.shuffle.schema.workflows import WorkflowExecutionResponseModel
11 from app.connectors.shuffle.schema.workflows import WorkflowsResponse
12 from app.connectors.shuffle.services.workflows import execute_workflow
13 from app.connectors.shuffle.services.workflows import get_workflow_executions
14 from app.connectors.shuffle.services.workflows import get_workflows
15
16 shuffle_workflows_router = APIRouter()
17
18
19 async def validate_execution_id(workflow_id: str) -> bool:
20 """
21 Validate the execution ID.
22
23 Args:
24 workflow_id (str): The workflow ID.
25
26 Returns:
27 bool: True if the workflow ID is valid, False otherwise.
28 """
29 workflows = await get_workflows()
30 for workflow in workflows.workflows:
31 logger.info(f"Workflow ID: {workflow['id']}")
32 if workflow["id"] == workflow_id:
33 logger.info("Workflow validation successful")
34 return True
35 raise HTTPException(status_code=404, detail="Workflow not found")
36
37
38 @shuffle_workflows_router.get(
39 "",
40 response_model=WorkflowsResponse,
41 description="Get all workflows",
42 dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))],
43 )
44 async def get_all_workflows() -> WorkflowsResponse:
45 """
46 Retrieve all workflows.
47
48 Returns:
49 WorkflowsResponse: The response containing the list of workflows.
50 """
51 logger.info("Fetching all workflows")
52 return await get_workflows()
53
54
55 @shuffle_workflows_router.get(
56 "/executions",
57 response_model=WorkflowExecutionResponseModel,
58 description="Get all workflow executions",
59 dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))],
60 )
61 async def get_all_workflow_executions() -> WorkflowExecutionResponseModel:
62 """
63 Retrieve all workflow executions.
64
65 Returns:
66 WorkflowExecutionResponseModel: The response model containing the workflow executions.
67
68 Raises:
69 HTTPException: If no workflows are found.
70 """
71 logger.info("Fetching all workflow executions")
72
73 # Initialize an empty list for storing workflow details
74 workflow_details = []
75
76 # Get the workflow response by awaiting the asynchronous function get_workflows()
77 workflow_response = await get_all_workflows()
78
79 # Access the workflows attribute from the response
80 workflows = workflow_response.workflows
81
82 # Check if workflows is not None before proceeding
83 if workflows:
84 for workflow in workflows:
85 workflow_details.append(
86 {
87 "workflow_id": workflow["id"],
88 "workflow_name": workflow["name"],
89 "status": await get_workflow_executions(
90 WorkflowExecutionBodyModel(workflow_id=workflow["id"]),
91 ),
92 },
93 )
94 return WorkflowExecutionResponseModel(
95 success=True,
96 message="Successfully fetched workflow executions",
97 workflows=workflow_details,
98 )
99 else:
100 raise HTTPException(status_code=404, detail="No workflows found")
101
102
103 @shuffle_workflows_router.post(
104 "/execute",
105 response_model=RequestWorkflowExecutionResponse,
106 description="Execute a workflow",
107 dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))],
108 )
109 async def execute_workflow_request(
110 workflow_execution_body: RequestWorkflowExecutionModel,
111 ) -> RequestWorkflowExecutionResponse:
112 """
113 Execute a workflow.
114
115 Args:
116 workflow_execution_body (WorkflowExecutionBodyModel): The workflow execution body model.
117
118 Returns:
119 RequestWorkflowExecutionResponse: The response model containing the workflow executions.
120
121 Raises:
122 HTTPException: If the workflow is not found.
123 """
124 logger.info(f"Executing workflow with ID: {workflow_execution_body.workflow_id}")
125
126 await validate_execution_id(workflow_execution_body.workflow_id)
127
128 return RequestWorkflowExecutionResponse(
129 success=True,
130 message="Successfully executed workflow",
131 data=await execute_workflow(workflow_execution_body),
132 )