Skip to content

Commit

Permalink
feat: 更改为批量查询 更改输入名称 #82
Browse files Browse the repository at this point in the history
  • Loading branch information
ZC-A committed Jan 14, 2025
1 parent 47c7803 commit 7ebf1df
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def inputs_format(self):
item_schema=ObjectItemSchema(
description=_("单个赋值关系"),
property_schemas={
"bk_assign_source_var": StringItemSchema(description=_("赋值变量或常量")),
"bk_assign_source_val": StringItemSchema(description=_("赋值变量或常量")),
"bk_assgin_target_var": StringItemSchema(description=_("被赋值变量")),
},
),
Expand All @@ -58,34 +58,46 @@ def plugin_execute(self, data, parent_data):
upsert_dict = {}
pipeline_id = self._runtime_attrs["root_pipeline_id"]
assign_list = data.get_one_of_inputs("bk_assignment_list")
# 构建目标变量集合
target_var_set = {"${{{}}}".format(assign["bk_assgin_target_var"]) for assign in assign_list}

try:
# 批量查询目标变量的上下文值
context_values = runtime.get_context_values(pipeline_id=pipeline_id, keys=target_var_set)
except ValueError as e:
self.logger.exception(str(e))
data.outputs.ex_data = str(e)
return False

# 构建一个字典以快速访问
context_dict = {cv.key: cv for cv in context_values}

for assign in assign_list:
# 循环处理表格中的内容 检查是否存在/类型问题后再统一事务批量执行
input_var = assign["bk_assign_source_var"]
input_val = assign["bk_assign_source_val"]
target_var = assign["bk_assgin_target_var"]
target_var_str = "${{{}}}".format(target_var)
context = runtime.get_context_values(pipeline_id=pipeline_id, keys={target_var_str})
context = context_dict.get(target_var_str)
if not context:
# 如果没有找到对应的全局变量则直接返回错误
err_msg = "target variable {} is not exist".format(target_var)
self.logger.exception(err_msg)
data.outputs.ex_data = err_msg
return False
context = context[0]

# 处理输入变量与目标变量的类型转换(输入常量时) 如果是相同类型则必然成功
try:
# 尝试将输入转换为目标变量类型
input_var = type(context.value)(input_var)
input_val = type(context.value)(input_val)
except (ValueError, TypeError):
err_msg = "input variable '{}' cannot be converted to the type of target variable '{}': {}".format(
input_var, target_var, type(context.value).__name__
input_val, target_var, type(context.value).__name__
)
self.logger.exception(err_msg)
data.outputs.ex_data = err_msg
return False

# 更新上下文并放入字典
context.value = input_var
context.value = input_val
upsert_dict[target_var_str] = context
runtime.upsert_plain_context_values(pipeline_id=pipeline_id, update=upsert_dict)
# 批量更新内容 事务原子操作
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
empty_text: gettext("赋值变量或常量与被赋值变量的映射列表,一行填写一个映射"),
columns: [
{
tag_code: "bk_assign_source_var",
tag_code: "bk_assign_source_val",
type: "textarea",
attrs: {
name: gettext("赋值变量或常量"),
Expand Down

0 comments on commit 7ebf1df

Please sign in to comment.