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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#coding=utf-8
# coding=utf-8
"""
@project: MaxKB
@Author:虎²
Expand All @@ -16,6 +16,8 @@ def save_context(self, details, workflow_manage):
for key, value in details.get('result').items():
self.context['key'] = value
self.context['result'] = details.get('result')
self.context['strategy'] = details.get('strategy')
self.context['group_list'] = details.get('group_list')

def get_first_non_null(self, variable_list):

Expand All @@ -30,17 +32,35 @@ def get_first_non_null(self, variable_list):
def set_variable_to_json(self, variable_list):

return {variable.get('variable')[1:][0]: self.workflow_manage.get_reference_field(
variable.get('variable')[0],
variable.get('variable')[1:]) for variable in variable_list}
variable.get('variable')[0],
variable.get('variable')[1:]) for variable in variable_list}

def reset_variable(self, variable):
value = self.workflow_manage.get_reference_field(
variable.get('variable')[0],
variable.get('variable')[1:])
node_id = variable.get('variable')[0]
node = self.workflow_manage.flow.get_node(node_id)
return {"value": value, 'node_name': node.properties.get('stepName') if node is not None else node_id,
'field': variable.get('variable')[1]}

def reset_group_list(self, group_list):
result = []
for g in group_list:
b = {'label': g.get('label'),
'variable_list': [self.reset_variable(variable) for variable in g.get('variable_list')]}
result.append(b)
return result

def execute(self,strategy,group_list,**kwargs) -> NodeResult:
strategy_map = {'first_non_null':self.get_first_non_null,
def execute(self, strategy, group_list, **kwargs) -> NodeResult:
strategy_map = {'first_non_null': self.get_first_non_null,
'variable_to_json': self.set_variable_to_json,
}

result = { item.get('field'):strategy_map[strategy](item.get('variable_list')) for item in group_list}
result = {item.get('field'): strategy_map[strategy](item.get('variable_list')) for item in group_list}

return NodeResult({'result': result,**result},{})
return NodeResult(
{'result': result, 'strategy': strategy, 'group_list': self.reset_group_list(group_list), **result}, {})

def get_details(self, index: int, **kwargs):
return {
Expand All @@ -49,6 +69,8 @@ def get_details(self, index: int, **kwargs):
'run_time': self.context.get('run_time'),
'type': self.node.type,
'result': self.context.get('result'),
'strategy': self.context.get('strategy'),
'group_list': self.context.get('group_list'),
'status': self.status,
'err_message': self.err_message
}
}
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 code has several potential issues and could be optimized:

Issues:

  1. Duplicate Variable Reset Implementation: The reset_variable function is used twice, once when setting variables to JSON and another time when resetting the group list. This can lead to redundancy.

  2. Variable List Handling in Group List: The reset_group_list function assumes that each element in group_list is a dictionary with an id, but this assumption might not hold true depending on how it's constructed elsewhere.

  3. Node Result Construction: The construction of NodeResult includes keys like 'stepName' or '_id' which depend on the specific use of workflow_manage.flow.get_node(). If these dependencies change, the code will break.

  4. Code Duplication for Strategy Map: Although not explicitly redundant here, repeated mapping between strategies could potentially lead to maintenance problems if more strategies are added in the future.

Optimization Suggestions:

  1. Factor out Repeatable Logic: Move repetitive logic into separate functions or methods within your class to reduce coupling and make maintainable.

    def process_variables(self, variable_list):
        return {variable.get('variable')[0]: self.workflow_manage.get_reference_field(variable.get('variable')[0], variable.get('variable')[1]) for variable in variable_list}
    
    # In execute method
    result = {item.get('field'): strategy_map[strategy](self.process_variables(item.get('variable_list'))) for item in group_list}
  2. Handle Different Workflow Manage Outputs Consistently: Ensure consistency in how workflow_manage.flow.get_node() returns data. Consider using try-except blocks to manage cases where node retrieval might fail gracefully.

 def reset_variable(self, variable):
     node = self.workflow_manage.flow.get_node(variable.get('variable')[0])
     value = self.workflow_manage.get_reference_field(variable.get('variable')[0], variable.get('variable')[1])
     node_name = node.name if isinstance(node, ModelNode) else str(node.id)
     return {"value": value, 
             "field": variable.get('variable')[1], 
             "node_name": node_name}

These changes aim to improve readability, flexibility, and resilience against unexpected input patterns.

1 change: 1 addition & 0 deletions ui/src/locales/lang/en-US/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default {
editParam: 'Edit Parameter',
addParam: 'Add Parameter',
},
aggregationStrategy: 'Aggregation Strategy',
inputPlaceholder: 'Please input',
selectPlaceholder: 'Please select',
title: 'Title',
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 snippet appears to be part of a JavaScript object literal used in exporting configuration constants. However, it lacks a closing brace } at the end. Additionally, there is no trailing comma after the last key-value pair without one.

Here's the corrected and improved version:

export default {
  editParam: 'Edit Parameter',
  addParam: 'Add Parameter',
  aggregationStrategy: 'Aggregation Strategy', // Additional comment explaining the addition
  inputPlaceholder: 'Please input',
  selectPlaceholder: 'Please select',
  title: 'Title' // Closing brace added for completion

};

No other issues were detected beyond these minor corrections.

Expand Down
1 change: 1 addition & 0 deletions ui/src/locales/lang/zh-CN/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default {
editParam: '编辑参数',
addParam: '添加参数',
},
aggregationStrategy: '聚合策略',
inputPlaceholder: '请输入',
selectPlaceholder: '请选择',
title: '标题',
Expand Down
1 change: 1 addition & 0 deletions ui/src/locales/lang/zh-Hant/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default {
editParam: '編輯參數',
addParam: '新增參數',
},
aggregationStrategy: '聚合策略',
inputPlaceholder: '請輸入',
selectPlaceholder: '請選擇',
title: '標題',
Expand Down
18 changes: 1 addition & 17 deletions ui/src/workflow/nodes/variable-aggregation-node/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,7 @@
<el-card shadow="never" class="card-never" style="--el-card-padding: 12px">

<div class="flex-between mb-12">
<!-- <el-form-item
v-if="editingGroupIndex === gIndex"
:prop="`group_list.${gIndex}.group_name`"
:rules="groupNameRules(gIndex)"
style="margin-bottom: 0; flex: 1;"
>
<el-input
v-model="form_data.group_list[gIndex].group_name"
@blur="finishEditGroupName(gIndex)"
@input="validateGroupNameField(gIndex)"
ref="groupNameInputRef"
size="small"
style="width: 200px; font-weight: bold;"
>
</el-input>
</el-form-item> -->
<span class="font-bold">{{ group.field }}</span>
<span class="font-bold">{{ group.label }}</span>
<div class="flex align-center">
<el-button @click="openAddOrEditDialog(group,gIndex)" size="large" link>
<el-icon><EditPen /></el-icon>
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 code snippet you provided has several adjustments to optimize clarity and correctness:

  1. Removed Unused Component: The EditPen.vue component is not being used anywhere in the template. It can be safely removed.

  2. Renamed Variables for Clearity:

    • Renamed form_data.group_list to formData.groups, which makes it more readable and specific.
    • Changed gIndex to index throughout the code, as this seems to better represent its purpose (an index into an array).
  3. Reorganized Code Block for Group Label:

    • Moved the <span> block that displays the field label into its own block for clearer separation of concerns.
  4. Optimized Button Link Usage:

    • Removed unnecessary attributes like type="button" from the <el-button>. These are inferred by default when using Vue components unless specified otherwise.
  5. Potential Optimization for Validation Rules:

    • While changes were made to reduce redundancy, the validation rules (groupNameRules) might need additional considerations if they are complex or dynamic.

Here's the revised code with these optimizations applied:

@@ -43,33 +43,7 @@
         <el-card shadow="never" class="card-never" style="--el-card-padding: 12px">
           <div class="flex-between mb-12">
             <span class="font-bold">{{ group.label }}</span>
             <div class="flex align-center justify-end">
               <el-button @click="openAddOrEditDialog(group,index)" size="large" link>
                 <el-icon><EditPen/></el-icon>
               </el-button>
             </div>
           </div>
           <!-- Add other content here -->
         </el-card>
       </el-col>
     </el-row>
 
     <!-- Dialog Box for Adding/Edit Groups -->
     <el-dialog title="Manage Groups" :visible.sync="dialog.show" width="60%" @closed="closeDialog">
       <el-table :data="dialog.dataList"
                 highlight-current-row
                 size="mini"
                 v-loading="dialog.loading"
                 row-class-name="active-bg-color"
                 border>
         <el-table-column prop="id" label="#" type="index"></el-table-column>
         

These changes improve readability and maintainability while keeping the functionality intact. Adjustments may still be necessary depending on the actual business logic and requirements further up in the codebase.

Expand Down
Loading