mirror of https://github.com/jumpserver/jumpserver
perf: Refactor OperateLogStore separator logic for database compatibility (#13424)
Co-authored-by: feng <1304903146@qq.com> Co-authored-by: feng626 <57284900+feng626@users.noreply.github.com>pull/13426/head
parent
6c59888d77
commit
32966b260a
|
@ -1,5 +1,4 @@
|
||||||
# ~*~ coding: utf-8 ~*~
|
# ~*~ coding: utf-8 ~*~
|
||||||
from django.conf import settings
|
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from audits.models import OperateLog
|
from audits.models import OperateLog
|
||||||
|
@ -7,22 +6,9 @@ from perms.const import ActionChoices
|
||||||
|
|
||||||
|
|
||||||
class OperateLogStore(object):
|
class OperateLogStore(object):
|
||||||
|
# 使用 Unicode 单元分隔符\u001f,替代旧的分隔符\0 PostgreSQL 数据库不支持\0
|
||||||
@staticmethod
|
SEP = '\u001f'
|
||||||
def get_separator():
|
OLD_SEP = '\0'
|
||||||
"""
|
|
||||||
用不可见字符分割前后数据
|
|
||||||
根据数据库引擎类型返回适当的分隔符。
|
|
||||||
PostgreSQL 数据库使用 Unicode 单元分隔符 '\u001f',
|
|
||||||
其他数据库使用空字符 '\0' 作为分隔符。
|
|
||||||
"""
|
|
||||||
db_engine = settings.DB_ENGINE
|
|
||||||
if db_engine == 'postgresql':
|
|
||||||
return '\u001f'
|
|
||||||
else:
|
|
||||||
return '\0'
|
|
||||||
|
|
||||||
SEP = get_separator()
|
|
||||||
|
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
self.model = OperateLog
|
self.model = OperateLog
|
||||||
|
@ -35,6 +21,22 @@ class OperateLogStore(object):
|
||||||
def ping(timeout=None):
|
def ping(timeout=None):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def split_value(cls, value):
|
||||||
|
"""
|
||||||
|
Attempt to split the string using the new separator.
|
||||||
|
If it fails, attempt to split using the old separator.
|
||||||
|
If both fail, return the original string and an empty string.
|
||||||
|
|
||||||
|
:param value: The string to split
|
||||||
|
:return: The split parts (before, after)
|
||||||
|
"""
|
||||||
|
for sep in (cls.SEP, cls.OLD_SEP):
|
||||||
|
parts = value.split(sep, 1)
|
||||||
|
if len(parts) == 2:
|
||||||
|
return parts[0], parts[1]
|
||||||
|
return value, ''
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def convert_before_after_to_diff(cls, before, after):
|
def convert_before_after_to_diff(cls, before, after):
|
||||||
if not isinstance(before, dict):
|
if not isinstance(before, dict):
|
||||||
|
@ -57,7 +59,7 @@ class OperateLogStore(object):
|
||||||
return before, after
|
return before, after
|
||||||
|
|
||||||
for k, v in diff.items():
|
for k, v in diff.items():
|
||||||
before_value, after_value = v.split(cls.SEP, 1)
|
before_value, after_value = cls.split_value(v)
|
||||||
before[k], after[k] = before_value, after_value
|
before[k], after[k] = before_value, after_value
|
||||||
return before, after
|
return before, after
|
||||||
|
|
||||||
|
@ -74,11 +76,11 @@ class OperateLogStore(object):
|
||||||
diff_list = list()
|
diff_list = list()
|
||||||
handler = cls._get_special_handler(op_log.resource_type)
|
handler = cls._get_special_handler(op_log.resource_type)
|
||||||
for k, v in op_log.diff.items():
|
for k, v in op_log.diff.items():
|
||||||
before, after = v.split(cls.SEP, 1)
|
before_value, after_value = cls.split_value(v)
|
||||||
diff_list.append({
|
diff_list.append({
|
||||||
'field': _(k),
|
'field': _(k),
|
||||||
'before': handler(k, before) if before else _('empty'),
|
'before': handler(k, before_value) if before_value else _('empty'),
|
||||||
'after': handler(k, after) if after else _('empty'),
|
'after': handler(k, after_value) if after_value else _('empty'),
|
||||||
})
|
})
|
||||||
return diff_list
|
return diff_list
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue