diff --git a/common/templates/config.html b/common/templates/config.html index 93af8eeb1f..ba511c2f04 100755 --- a/common/templates/config.html +++ b/common/templates/config.html @@ -304,6 +304,29 @@
SQL上线
+
+ +
+ +
+
+
+ +
+ +
+
@@ -1096,18 +1119,7 @@

其他配置

key="announcement_content" value="{{ config.announcement_content }}" placeholder="公告内容"> - -
- -
- -
- +
@@ -1159,29 +1171,30 @@
当前审批流程:" + phases[i] + "" - $("#notify_phase_control").append(phase) - } - } else { - for (let i=0;i= 0) { - phase = "" - } else { - phase = "" - } - $("#notify_phase_control").append(phase) - } + function setupSelectPicker(selector, configValue, defaultPhases) { + let $select = $(selector); + let cfgArr = configValue ? configValue.split(',') : defaultPhases; + $select.empty(); + + defaultPhases.forEach(phase => { + let isSelected = cfgArr.includes(phase) ? ' selected' : ''; + let option = ``; + $select.append(option); + }); + + $select.selectpicker('render'); + $select.selectpicker('refresh'); } - $("#notify_phase_control").selectpicker('render'); - $("#notify_phase_control").selectpicker('refresh'); + + // notify_phase_control参数处理 + let notifyConfig = "{{ config.notify_phase_control }}"; + let notifyPhases = ['Apply', 'Pass', 'Execute', 'Cancel']; + setupSelectPicker("#notify_phase_control", notifyConfig, notifyPhases); + + // query_low_peak_query参数处理 + let queryConfig = "{{ config.query_low_peak_query }}"; + let queryPhases = ['DML', 'DDL']; + setupSelectPicker("#query_low_peak_query", queryConfig, queryPhases); // api_user_whitelist参数处理 let api_config = "{{ config.api_user_whitelist }}"; @@ -1304,7 +1317,6 @@
当前审批流程:当前审批流程:= endTime) { + alert('业务低峰时段时间范围错误,结束时间必须大于开始时间'); + return ; + } + } var sys_config = $("#div-system-config"); var configs = []; sys_config.find('[key]').each( diff --git a/sql/sql_workflow.py b/sql/sql_workflow.py index b812be7c51..d8c77cb644 100644 --- a/sql/sql_workflow.py +++ b/sql/sql_workflow.py @@ -24,6 +24,7 @@ can_timingtask, can_cancel, can_execute, + on_query_low_peak_time_ddl, on_correct_time_period, can_view, can_rollback, @@ -306,6 +307,18 @@ def execute(request): "errMsg": "不在可执行时间范围内,如果需要修改执行时间请重新提交工单!" } return render(request, "error.html", context) + sys_config = SysConfig() + if ( + not request.user.is_superuser + and on_query_low_peak_time_ddl(workflow_id) is False + ): + periods = sys_config.get("query_low_peak", "") + peak_action = sys_config.get("query_low_peak_query", "") + context = { + "errMsg": "管理员设置了业务低峰期时间范围:%s,你只能在业务低峰时间范围执行%s工单操作!" + % (periods, peak_action) + } + return render(request, "error.html", context) # 获取审核信息 audit_id = Audit.detail_by_workflow_id( workflow_id=workflow_id, workflow_type=WorkflowType.SQL_REVIEW diff --git a/sql/utils/sql_review.py b/sql/utils/sql_review.py index 78296247f6..0404594013 100644 --- a/sql/utils/sql_review.py +++ b/sql/utils/sql_review.py @@ -41,6 +41,36 @@ def can_execute(user, workflow_id): return result +def on_query_low_peak_time_ddl(workflow_id, run_date=None): + """ + 判断是否是ddl,ddl必须在业务低峰期执行,包括人工执行和定时执行 + :param workflow_id: + :param run_date: + :return: + """ + config = SysConfig() + workflow_detail = SqlWorkflow.objects.get(id=workflow_id) + result = True + ctime = run_date or datetime.datetime.now() + run_time = f"{ctime.hour:02}:{ctime.minute:02}" + syntax_type = workflow_detail.syntax_type + periods = config.get("query_low_peak", "") + peak_action = config.get("query_low_peak_query", "") + + def is_without_peak_periods(run_time, periods): + for period in periods.split(","): + start, end = period.split("-") + if start <= run_time <= end: + return True # 如果 run_time 在当前时间段内,直接返回 True + return False # 只有当 run_time 不在任何时间段内时,才返回 False + + if "DML" in peak_action and syntax_type == 2: + return is_without_peak_periods(run_time, periods) + if "DDL" in peak_action and syntax_type == 1: + return is_without_peak_periods(run_time, periods) + return result + + def on_correct_time_period(workflow_id, run_date=None): """ 判断是否在可执行时间段内,包括人工执行和定时执行 diff --git a/sql_api/api_workflow.py b/sql_api/api_workflow.py index d834a5af30..954b10471f 100644 --- a/sql_api/api_workflow.py +++ b/sql_api/api_workflow.py @@ -26,7 +26,12 @@ from sql.notify import notify_for_audit, notify_for_execute from sql.query_privileges import _query_apply_audit_call_back from sql.utils.resource_group import user_groups -from sql.utils.sql_review import can_cancel, can_execute, on_correct_time_period +from sql.utils.sql_review import ( + can_cancel, + can_execute, + on_correct_time_period, + on_query_low_peak_time_ddl, +) from sql.utils.tasks import del_schedule from sql.utils.workflow_audit import Audit, get_auditor, AuditException from .filters import WorkflowFilter, WorkflowAuditFilter @@ -340,6 +345,19 @@ def post(self, request): "errors": "不在可执行时间范围内,如果需要修改执行时间请重新提交工单!" } ) + sys_config = SysConfig() + if ( + not request.user.is_superuser + and on_query_low_peak_time_ddl(workflow_id) is False + ): + periods = sys_config.get("query_low_peak", "") + peak_action = sys_config.get("query_low_peak_query", "") + raise serializers.ValidationError( + { + "errMsg": "管理员设置了业务低峰期时间范围:%s,你只能在业务低峰时间范围执行%s工单操作!" + % (periods, peak_action) + } + ) # 获取审核信息 audit_id = Audit.detail_by_workflow_id(