Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: 下拉框变量移除远程数据源选项 #20 #21

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
81 changes: 40 additions & 41 deletions bkflow/pipeline_plugins/query/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,54 @@

import logging

import requests
from django.conf import settings
from django.http import JsonResponse
from django.utils.translation import ugettext_lazy as _

logger = logging.getLogger("root")


def variable_select_source_data_proxy(request):
"""
@summary: 获取下拉框源数据的通用接口
@summary: 获取下拉框源数据的通用接口,暂时关闭该接口
@param request:
@return:
"""
url = request.GET.get("url")
try:
response = requests.get(url=url, verify=False, timeout=10)
except Exception as e:
logger.exception("variable select get data from url[url={url}] raise error: {error}".format(url=url, error=e))
text = _("请求数据异常: {error}").format(error=e)
data = [{"text": text, "value": ""}]
return JsonResponse(data, safe=False)
return JsonResponse([{"text": "text1", "value": "value1"}])

try:
data = response.json()
except Exception:
try:
content = response.content.decode(response.encoding)
logger.exception(
"variable select get data from url[url={url}] is not a valid JSON: {data}".format(
url=url, data=content[:500]
)
)
except Exception:
logger.exception("variable select get data from url[url={url}] data is not a valid JSON".format(url=url))
text = _("返回数据格式错误,不是合法 JSON 格式")
data = [{"text": text, "value": ""}]
return JsonResponse(data, safe=False)

# 支持开发者对远程数据源数据配置处理函数,进行再处理
post_process_function = getattr(settings, "REMOTE_SOURCE_DATA_TRANSFORM_FUNCTION", None)
if post_process_function and callable(post_process_function):
try:
data = post_process_function(data)
except Exception as e:
logger.exception(
"variable select transforming data from remote resource url[url={url}] "
"raise error: {error}".format(url=url, error=e)
)
text = _("远程数据源数据转换失败: {error}").format(error=e)
data = [{"text": text, "value": ""}]
return JsonResponse(data, safe=False)
# url = request.GET.get("url")
# try:
# response = requests.get(url=url, verify=False, timeout=10)
# except Exception as e:
# logger.exception("variable select get data from url[url={url}] raise error: {error}".format(url=url, error=e))
# text = _("请求数据异常: {error}").format(error=e)
# data = [{"text": text, "value": ""}]
# return JsonResponse(data, safe=False)
#
# try:
# data = response.json()
# except Exception:
# try:
# content = response.content.decode(response.encoding)
# logger.exception(
# "variable select get data from url[url={url}] is not a valid JSON: {data}".format(
# url=url, data=content[:500]
# )
# )
# except Exception:
# logger.exception("variable select get data from url[url={url}] data is not a valid JSON".format(url=url))
# text = _("返回数据格式错误,不是合法 JSON 格式")
# data = [{"text": text, "value": ""}]
# return JsonResponse(data, safe=False)
#
# # 支持开发者对远程数据源数据配置处理函数,进行再处理
# post_process_function = getattr(settings, "REMOTE_SOURCE_DATA_TRANSFORM_FUNCTION", None)
# if post_process_function and callable(post_process_function):
# try:
# data = post_process_function(data)
# except Exception as e:
# logger.exception(
# "variable select transforming data from remote resource url[url={url}] "
# "raise error: {error}".format(url=url, error=e)
# )
# text = _("远程数据源数据转换失败: {error}").format(error=e)
# data = [{"text": text, "value": ""}]
# return JsonResponse(data, safe=False)
34 changes: 18 additions & 16 deletions bkflow/pipeline_plugins/static/variables/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
hookable: true,
items: [{name: gettext("自定义"), value: "0"}, {name: gettext("远程数据源"), value: "1"}],
value: "0",
hidden: true,
validation: [
{
type: "required"
Expand All @@ -29,7 +30,7 @@
attrs: {
name: gettext("选项"),
hookable: true,
placeholder: gettext('请输入数据源信息,自定义数据源格式为 [{"text": "", "value": ""}...],若是远程数据源则填写返回该格式数据的 URL'),
placeholder: gettext('请输入数据源信息,自定义数据源格式为 [{"text": "", "value": ""}...]'),
validation: [
{
type: "required"
Expand Down Expand Up @@ -73,10 +74,11 @@
let remote_url = "";
let items = [];
let placeholder = '';
if (metaConfig.datasource === "1") {
remote_url = $.context.get('site_url') + 'api/plugin_query/variable_select_source_data_proxy/?url=' + metaConfig.items_text;
remote = true;
} else {
// if (metaConfig.datasource === "1") {
// remote_url = $.context.get('site_url') + 'api/plugin_query/variable_select_source_data_proxy/?url=' + metaConfig.items_text;
// remote = true;
// }
if (metaConfig.datasource === "0") {
try {
items = JSON.parse(metaConfig.items_text);
} catch (err) {
Expand All @@ -92,16 +94,16 @@
let multiple = false;
let default_val = metaConfig.default || '';

if (metaConfig.type === "1") {
multiple = true;
default_val = [];
if (metaConfig.default) {
let vals = metaConfig.default.split(',');
for (let i in vals) {
default_val.push(vals[i].trim());
}
}
}
// if (metaConfig.type === "1") {
// multiple = true;
// default_val = [];
// if (metaConfig.default) {
// let vals = metaConfig.default.split(',');
// for (let i in vals) {
// default_val.push(vals[i].trim());
// }
// }
// }
return {
tag_code: this.tag_code,
type: "select",
Expand All @@ -115,7 +117,7 @@
remote_url: remote_url,
placeholder: placeholder,
remote_data_init: function (data) {
return data
return data;
},
validation: [
{
Expand Down
Loading