Skip to content
Merged
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
25 changes: 16 additions & 9 deletions apps/application/serializers/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ def get_base_node_work_flow(work_flow):
return None


def hand_node(node, update_tool_map):
if node.get('type') == 'tool-lib-node':
tool_lib_id = (node.get('properties', {}).get('node_data', {}).get('tool_lib_id') or '')
node.get('properties', {}).get('node_data', {})['tool_lib_id'] = update_tool_map.get(tool_lib_id,
tool_lib_id)
if node.get('type') == 'search-knowledge-node':
node.get('properties', {}).get('node_data', {})['knowledge_id_list'] = []


class MKInstance:

def __init__(self, application: dict, function_lib_list: List[dict], version: str, tool_list: List[dict]):
Expand Down Expand Up @@ -348,9 +357,9 @@ def get_query_set(self, instance: Dict, workspace_manage: bool, is_x_pack_ee: bo
application_query_set = application_query_set.order_by("-create_time")

resource_and_folder_query_set = QuerySet(WorkspaceUserResourcePermission).filter(
auth_target_type="APPLICATION",
workspace_id=workspace_id,
user_id=user_id)
auth_target_type="APPLICATION",
workspace_id=workspace_id,
user_id=user_id)

return {'application_query_set': application_query_set,
'workspace_user_resource_permission_query_set': resource_and_folder_query_set,
Expand Down Expand Up @@ -582,12 +591,10 @@ def to_tool(tool, workspace_id, user_id):
def to_application(application, workspace_id, user_id, update_tool_map, folder_id):
work_flow = application.get('work_flow')
for node in work_flow.get('nodes', []):
if node.get('type') == 'tool-lib-node':
tool_lib_id = (node.get('properties', {}).get('node_data', {}).get('tool_lib_id') or '')
node.get('properties', {}).get('node_data', {})['tool_lib_id'] = update_tool_map.get(tool_lib_id,
tool_lib_id)
if node.get('type') == 'search-knowledge-node':
node.get('properties', {}).get('node_data', {})['knowledge_id_list'] = []
hand_node(node, update_tool_map)
if node.get('type') == 'loop-node':
for n in node.get('properties', {}).get('node_data', {}).get('loop_body', {}).get('nodes', []):
hand_node(n, update_tool_map)
return Application(id=uuid.uuid7(),
user_id=user_id,
name=application.get('name'),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The provided code appears to be part of an application that processes workflows. It includes a hand_node function to handle certain types of workflow nodes and updates them according to predefined rules. Additionally, it contains methods within the MKInstance class for querying resources and folders.

Review:

  1. Functionality Consistency: The hand_node function handles two specific types of nodes:

    • tool-lib-node: Updates the 'tool_lib_id'.
    • search-knowledge-node: Clears the 'knowledge_id_list'.
  2. Loop Node Handling: If the node type is "loop-node", the loop body also needs handling, but this was not done in the given code snippet. Consider adding logic to recurse through nested loops.

  3. Variable Naming: Use consistent variable names to improve readability. For example, consider renaming update_tool_map to tool_mapping.

  4. Error Handling: Add error handling around accessing dictionary items to prevent runtime errors, especially when dealing with optional keys (or ''). Consider using getattr or default parameter values.

  5. Performance Optimization:

    • Ensure there are no unnecessary list operations inside loops.
    • Avoid redundant method calls where possible.
  6. Documentation: Provide docstrings for all functions and classes to help other developers understand their purpose and usage.

Here's a revised version incorporating some suggested changes:

def hand_node(node, tool_mapping):
    """Handles specific node types."""
    if node.get('type') == 'tool-lib-node':
        tool_lib_id = node.get('properties', {}).get('node_data', {}).get('tool_lib_id', '')        
        # Update tool_lib_id using the mapping (if available)
        if tool_lib_id in tool_mapping:
            node['properties']['node_data'].update({'tool_lib_id': tool_mapping[tool_lib_id]})
    
    if node.get('type') == 'search-knowledge-node':
        node['properties']['node_data']['knowledge_id_list'] = []

class MKInstance:
    """Class responsible for managing instances of applications."""

    def __init__(self, application: dict, function_lib_list: List[dict], version: str, tool_list: List[dict]):
        self.application = application
        self.function_lib_list = function_lib_list
        self.version = version
        self.tool_list = tool_list

    def get_query_set(self, instance: Dict, workspace_manage: bool, is_x_pack_ee: bool) -> Tuple[QuerySet, QuerySet]:
        """Gets query sets based on instance details."""
        application_query_set = ...
        resource_and_folder_query_set = ...

        return {
            'application_query_set': application_query_set,
            'workspace_user_resource_permission_query_set': resource_and_folder_query_set
        }

    def to_tool(tool, workspace_id, user_id):
        ...  # Existing implementation

    def to_application(application, workspace_id, user_id, tool_mapping, folder_id=None) -> Application:
        """Converts application data to a new application instance."""
        work_flow = application.get('work_flow')

        for node in work_flow.get('nodes', []):
            hand_node(node, tool_mapping)

            # Handle nested.loop-body nodes
            loop_body_nodes = node.get('properties', {}).get('node_data', {}).get('loop_body', {}).get('nodes', [])
            for n in loop_body_nodes:
                hand_node(n, tool_mapping)

        return Application(id=uuid.uuid7(), 
                            user_id=user_id, 
                            name=application.get('name'), 
                            # Add other necessary fields here
                        )

This revision improves clarity, adds consistency, and addresses potential optimizations.

Expand Down
Loading