Skip to content

Commit

Permalink
用 WorkflowType 和 WorkflowStatus 两个 enum 替换WorkflowDict (hhyo#2344)
Browse files Browse the repository at this point in the history
* 用 WorkflowType 和 WorkflowStatus 两个 enum 替换WorkflowDict

* black
  • Loading branch information
LeoQuote authored and Yc Chen committed Nov 10, 2023
1 parent cd804a6 commit ef6ff20
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 228 deletions.
33 changes: 11 additions & 22 deletions common/utils/const.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: UTF-8 -*-
from django.db import models


class Const(object):
Expand All @@ -10,28 +10,17 @@ class Const(object):
}


class WorkflowDict:
# 工作流申请类型,1.query,2.SQL上线申请
workflow_type = {
"query": 1,
"query_display": "查询权限申请",
"sqlreview": 2,
"sqlreview_display": "SQL上线申请",
"archive": 3,
"archive_display": "数据归档申请",
}
class WorkflowType(models.IntegerChoices):
QUERY = 1, "查询权限申请"
SQL_REVIEW = 2, "SQL上线申请"
ARCHIVE = 3, "数据归档申请"

# 工作流状态,0.待审核 1.审核通过 2.审核不通过 3.审核取消
workflow_status = {
"audit_wait": 0,
"audit_wait_display": "待审核",
"audit_success": 1,
"audit_success_display": "审核通过",
"audit_reject": 2,
"audit_reject_display": "审核不通过",
"audit_abort": 3,
"audit_abort_display": "审核取消",
}

class WorkflowStatus(models.IntegerChoices):
WAITING = 0, "待审核"
PASSED = 1, "审核通过"
REJECTED = 2, "审核不通过"
ABORTED = 3, "审核取消"


class SQLTuning:
Expand Down
4 changes: 2 additions & 2 deletions common/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.contrib.auth.models import Group
from django.http import HttpResponse

from common.utils.const import WorkflowDict
from common.utils.const import WorkflowStatus
from common.utils.extend_json_encoder import ExtendJSONEncoder, ExtendJSONEncoderFTime
from sql.models import WorkflowAudit, WorkflowLog
from sql.utils.resource_group import user_groups
Expand Down Expand Up @@ -31,7 +31,7 @@ def lists(request):
# 只返回所在资源组当前待自己审核的数据
workflow_audit = WorkflowAudit.objects.filter(
workflow_title__icontains=search,
current_status=WorkflowDict.workflow_status["audit_wait"],
current_status=WorkflowStatus.WAITING,
group_id__in=group_ids,
current_audit__in=auth_group_ids,
)
Expand Down
24 changes: 9 additions & 15 deletions sql/archiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from django.urls import reverse
from django_q.tasks import async_task

from common.utils.const import WorkflowDict
from common.utils.const import WorkflowStatus, WorkflowType
from common.utils.extend_json_encoder import ExtendJSONEncoder
from common.utils.timer import FuncTimer
from sql.engines import get_engine
Expand Down Expand Up @@ -171,9 +171,7 @@ def archive_apply(request):

# 获取资源组和审批信息
res_group = ResourceGroup.objects.get(group_name=group_name)
audit_auth_groups = Audit.settings(
res_group.group_id, WorkflowDict.workflow_type["archive"]
)
audit_auth_groups = Audit.settings(res_group.group_id, WorkflowType.ARCHIVE)
if not audit_auth_groups:
return JsonResponse({"status": 1, "msg": "审批流程不能为空,请先配置审批流程", "data": {}})

Expand All @@ -195,16 +193,14 @@ def archive_apply(request):
mode=mode,
no_delete=no_delete,
sleep=sleep,
status=WorkflowDict.workflow_status["audit_wait"],
status=WorkflowStatus.WAITING,
state=False,
user_name=user.username,
user_display=user.display,
)
archive_id = archive_info.id
# 调用工作流插入审核信息
audit_result, audit_detail = Audit.add(
WorkflowDict.workflow_type["archive"], archive_id
)
audit_result, audit_detail = Audit.add(WorkflowType.ARCHIVE, archive_id)
except Exception as msg:
logger.error(traceback.format_exc())
result["status"] = 1
Expand All @@ -213,7 +209,7 @@ def archive_apply(request):
result = audit_result
# 消息通知
workflow_audit = Audit.detail_by_workflow_id(
workflow_id=archive_id, workflow_type=WorkflowDict.workflow_type["archive"]
workflow_id=archive_id, workflow_type=WorkflowType.ARCHIVE
)
async_task(
notify_for_audit,
Expand Down Expand Up @@ -250,7 +246,7 @@ def archive_audit(request):
with transaction.atomic():
workflow_audit = Audit.detail_by_workflow_id(
workflow_id=archive_id,
workflow_type=WorkflowDict.workflow_type["archive"],
workflow_type=WorkflowType.ARCHIVE,
)
audit_id = workflow_audit.audit_id

Expand All @@ -262,9 +258,7 @@ def archive_audit(request):
ArchiveConfig(
id=archive_id,
status=audit_status,
state=True
if audit_status == WorkflowDict.workflow_status["audit_success"]
else False,
state=True if audit_status == WorkflowStatus.PASSED else False,
).save(update_fields=["status", "state"])
except Exception as msg:
logger.error(traceback.format_exc())
Expand Down Expand Up @@ -298,11 +292,11 @@ def add_archive_task(archive_ids=None):
archive_cnf_list = ArchiveConfig.objects.filter(
id__in=archive_ids,
state=True,
status=WorkflowDict.workflow_status["audit_success"],
status=WorkflowStatus.PASSED,
)
else:
archive_cnf_list = ArchiveConfig.objects.filter(
state=True, status=WorkflowDict.workflow_status["audit_success"]
state=True, status=WorkflowStatus.PASSED
)

# 添加task任务
Expand Down
18 changes: 8 additions & 10 deletions sql/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from django.utils.translation import gettext as _
from mirage.crypto import Crypto

from common.utils.const import WorkflowStatus, WorkflowType


class ResourceGroup(models.Model):
"""
Expand Down Expand Up @@ -304,10 +306,6 @@ class Meta:
verbose_name_plural = "SQL工单内容"


workflow_type_choices = ((1, _("sql_query")), (2, _("sql_review")))
workflow_status_choices = ((0, "待审核"), (1, "审核通过"), (2, "审核不通过"), (3, "审核取消"))


class WorkflowAudit(models.Model):
"""
工作流审核状态表
Expand All @@ -317,13 +315,13 @@ class WorkflowAudit(models.Model):
group_id = models.IntegerField("组ID")
group_name = models.CharField("组名称", max_length=100)
workflow_id = models.BigIntegerField("关联业务id")
workflow_type = models.IntegerField("申请类型", choices=workflow_type_choices)
workflow_type = models.IntegerField("申请类型", choices=WorkflowType.choices)
workflow_title = models.CharField("申请标题", max_length=50)
workflow_remark = models.CharField("申请备注", default="", max_length=140, blank=True)
audit_auth_groups = models.CharField("审批权限组列表", max_length=255)
current_audit = models.CharField("当前审批权限组", max_length=20)
next_audit = models.CharField("下级审批权限组", max_length=20)
current_status = models.IntegerField("审核状态", choices=workflow_status_choices)
current_status = models.IntegerField("审核状态", choices=WorkflowStatus.choices)
create_user = models.CharField("申请人", max_length=30)
create_user_display = models.CharField("申请人中文名", max_length=50, default="")
create_time = models.DateTimeField("申请时间", auto_now_add=True)
Expand All @@ -349,7 +347,7 @@ class WorkflowAuditDetail(models.Model):
audit_id = models.IntegerField("审核主表id")
audit_user = models.CharField("审核人", max_length=30)
audit_time = models.DateTimeField("审核时间")
audit_status = models.IntegerField("审核状态", choices=workflow_status_choices)
audit_status = models.IntegerField("审核状态", choices=WorkflowStatus.choices)
remark = models.CharField("审核备注", default="", max_length=1000)
sys_time = models.DateTimeField("系统时间", auto_now=True)

Expand All @@ -371,7 +369,7 @@ class WorkflowAuditSetting(models.Model):
audit_setting_id = models.AutoField(primary_key=True)
group_id = models.IntegerField("组ID")
group_name = models.CharField("组名称", max_length=100)
workflow_type = models.IntegerField("审批类型", choices=workflow_type_choices)
workflow_type = models.IntegerField("审批类型", choices=WorkflowType.choices)
audit_auth_groups = models.CharField("审批权限组列表", max_length=255)
create_time = models.DateTimeField(auto_now_add=True)
sys_time = models.DateTimeField(auto_now=True)
Expand Down Expand Up @@ -446,7 +444,7 @@ class QueryPrivilegesApply(models.Model):
),
default=0,
)
status = models.IntegerField("审核状态", choices=workflow_status_choices)
status = models.IntegerField("审核状态", choices=WorkflowStatus.choices)
audit_auth_groups = models.CharField("审批权限组列表", max_length=255)
create_time = models.DateTimeField(auto_now_add=True)
sys_time = models.DateTimeField(auto_now=True)
Expand Down Expand Up @@ -720,7 +718,7 @@ class ArchiveConfig(models.Model):
no_delete = models.BooleanField("是否保留源数据")
sleep = models.IntegerField("归档limit行后的休眠秒数", default=1)
status = models.IntegerField(
"审核状态", choices=workflow_status_choices, blank=True, default=1
"审核状态", choices=WorkflowStatus.choices, blank=True, default=1
)
state = models.BooleanField("是否启用归档", default=True)
user_name = models.CharField("申请人", max_length=30, blank=True, default="")
Expand Down
24 changes: 12 additions & 12 deletions sql/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from django.contrib.auth.models import Group

from common.config import SysConfig
from common.utils.const import WorkflowDict
from common.utils.const import WorkflowStatus, WorkflowType
from common.utils.sendmsg import MsgSender
from sql.models import (
QueryPrivilegesApply,
Expand Down Expand Up @@ -162,8 +162,8 @@ def render_audit(self):
)
# workflow content, 即申请通过后要执行什么东西
# 执行的 SQL 语句, 授权的范围
if workflow_type == WorkflowDict.workflow_type["query"]:
workflow_type_display = WorkflowDict.workflow_type["query_display"]
if workflow_type == WorkflowType.QUERY:
workflow_type_display = WorkflowType.QUERY.label
workflow_detail = QueryPrivilegesApply.objects.get(apply_id=workflow_id)
instance = workflow_detail.instance.instance_name
db_name = workflow_detail.db_list
Expand All @@ -178,8 +178,8 @@ def render_audit(self):
workflow_content += (
f"""授权截止时间:{auth_ends_at}\n结果集:{workflow_detail.limit_num}\n"""
)
elif workflow_type == WorkflowDict.workflow_type["sqlreview"]:
workflow_type_display = WorkflowDict.workflow_type["sqlreview_display"]
elif workflow_type == WorkflowType.SQL_REVIEW:
workflow_type_display = WorkflowType.SQL_REVIEW.label
workflow_detail = SqlWorkflow.objects.get(pk=workflow_id)
instance = workflow_detail.instance.instance_name
db_name = workflow_detail.db_name
Expand All @@ -188,8 +188,8 @@ def render_audit(self):
"\n",
workflow_detail.sqlworkflowcontent.sql_content[0:500].replace("\r", ""),
)
elif workflow_type == WorkflowDict.workflow_type["archive"]:
workflow_type_display = WorkflowDict.workflow_type["archive_display"]
elif workflow_type == WorkflowType.ARCHIVE:
workflow_type_display = WorkflowType.ARCHIVE.label
workflow_detail = ArchiveConfig.objects.get(pk=workflow_id)
instance = workflow_detail.src_instance.instance_name
db_name = workflow_detail.src_db_name
Expand All @@ -201,7 +201,7 @@ def render_audit(self):
else:
raise Exception("工单类型不正确")
# 渲染提醒内容, 包括工单的所有信息, 申请人, 审批流等
if status == WorkflowDict.workflow_status["audit_wait"]: # 申请阶段
if status == WorkflowStatus.WAITING: # 申请阶段
msg_title = "[{}]新的工单申请#{}".format(workflow_type_display, audit_id)
# 接收人,发送给该资源组内对应权限组所有的用户
auth_group_names = Group.objects.get(id=self.audit.current_audit).name
Expand All @@ -228,7 +228,7 @@ def render_audit(self):
workflow_url,
workflow_content,
)
elif status == WorkflowDict.workflow_status["audit_success"]: # 审核通过
elif status == WorkflowStatus.PASSED: # 审核通过
msg_title = "[{}]工单审核通过#{}".format(workflow_type_display, audit_id)
# 接收人,仅发送给申请人
msg_to = [Users.objects.get(username=self.audit.create_user)]
Expand All @@ -244,7 +244,7 @@ def render_audit(self):
workflow_url,
workflow_content,
)
elif status == WorkflowDict.workflow_status["audit_reject"]: # 审核驳回
elif status == WorkflowStatus.REJECTED: # 审核驳回
msg_title = "[{}]工单被驳回#{}".format(workflow_type_display, audit_id)
# 接收人,仅发送给申请人
msg_to = [Users.objects.get(username=self.audit.create_user)]
Expand All @@ -257,7 +257,7 @@ def render_audit(self):
workflow_url,
re.sub("[\r\n\f]{2,}", "\n", self.audit_detail.remark),
)
elif status == WorkflowDict.workflow_status["audit_abort"]: # 审核取消,通知所有审核人
elif status == WorkflowStatus.ABORTED: # 审核取消,通知所有审核人
msg_title = "[{}]提交人主动终止工单#{}".format(workflow_type_display, audit_id)
# 接收人,发送给该资源组内对应权限组所有的用户
auth_group_names = [
Expand Down Expand Up @@ -293,7 +293,7 @@ def render_execute(self):
base_url=base_url, audit_id=audit_id
)
msg_title = (
f"[{WorkflowDict.workflow_type['sqlreview_display']}]工单"
f"[{WorkflowType.SQL_REVIEW.label}]工单"
f"{self.workflow.get_status_display()}#{audit_id}"
)
preview = re.sub(
Expand Down
18 changes: 8 additions & 10 deletions sql/query_privileges.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from django_q.tasks import async_task

from common.config import SysConfig
from common.utils.const import WorkflowDict
from common.utils.const import WorkflowStatus, WorkflowType
from common.utils.extend_json_encoder import ExtendJSONEncoder
from sql.engines.goinception import GoInceptionEngine
from sql.models import QueryPrivilegesApply, QueryPrivileges, Instance, ResourceGroup
Expand Down Expand Up @@ -267,15 +267,13 @@ def query_priv_apply(request):
title=title,
group_id=group_id,
group_name=group_name,
audit_auth_groups=Audit.settings(
group_id, WorkflowDict.workflow_type["query"]
),
audit_auth_groups=Audit.settings(group_id, WorkflowType.QUERY),
user_name=user.username,
user_display=user.display,
instance=ins,
priv_type=int(priv_type),
valid_date=valid_date,
status=WorkflowDict.workflow_status["audit_wait"],
status=WorkflowStatus.WAITING,
limit_num=limit_num,
)
if int(priv_type) == 1:
Expand All @@ -288,7 +286,7 @@ def query_priv_apply(request):
apply_id = applyinfo.apply_id

# 调用工作流插入审核信息,查询权限申请workflow_type=1
audit_result = Audit.add(WorkflowDict.workflow_type["query"], apply_id)
audit_result = Audit.add(WorkflowType.QUERY, apply_id)
if audit_result["status"] == 0:
# 更新业务表审核状态,判断是否插入权限信息
_query_apply_audit_call_back(
Expand All @@ -302,7 +300,7 @@ def query_priv_apply(request):
result = audit_result
# 消息通知
workflow_audit = Audit.detail_by_workflow_id(
workflow_id=apply_id, workflow_type=WorkflowDict.workflow_type["query"]
workflow_id=apply_id, workflow_type=WorkflowType.QUERY
)
async_task(
notify_for_audit,
Expand Down Expand Up @@ -440,7 +438,7 @@ def query_priv_audit(request):
try:
with transaction.atomic():
audit_id = Audit.detail_by_workflow_id(
workflow_id=apply_id, workflow_type=WorkflowDict.workflow_type["query"]
workflow_id=apply_id, workflow_type=WorkflowType.QUERY
).audit_id

# 调用工作流接口审核
Expand All @@ -450,7 +448,7 @@ def query_priv_audit(request):

# 按照审核结果更新业务表审核状态
audit_detail = Audit.detail(audit_id)
if audit_detail.workflow_type == WorkflowDict.workflow_type["query"]:
if audit_detail.workflow_type == WorkflowType.QUERY:
# 更新业务表审核状态,插入权限信息
_query_apply_audit_call_back(
audit_detail.workflow_id, audit_result["data"]["workflow_status"]
Expand Down Expand Up @@ -578,7 +576,7 @@ def _query_apply_audit_call_back(apply_id, workflow_status):
apply_info.status = workflow_status
apply_info.save()
# 审核通过插入权限信息,批量插入,减少性能消耗
if workflow_status == WorkflowDict.workflow_status["audit_success"]:
if workflow_status == WorkflowStatus.PASSED:
apply_queryset = QueryPrivilegesApply.objects.get(apply_id=apply_id)
# 库权限

Expand Down
Loading

0 comments on commit ef6ff20

Please sign in to comment.