• 老广's avatar
    Asset meta (#3539) · e1919d0a
    老广 authored
    - 更改了资产表单,影响
      - 资产创建和更新
    - 增加了资产平台数据库,影响
      - 平台创建更新和删除
    - 更改了资产的platform字段,又一个字符字段,改为一个外键,影响 
      - 资产创建和更新
      - 资产连接 [windows,linux]
      - 测试连接等ansible任务
      - 自动化云导入
    - 更改了资产的序列化器,影响
      - 资产创建更新列表
    - 统一了树列表基础模板,影响
      - 资产列表页,权限列表页,vault页,资产收集页
    - 统一了导入导出组件,影响
      - 资产导入导出
      - 用户导入导出
      - 用户组导入导出
      - 系统用户导入导出
      - 管理用户导入导出
      - vault导出导出
      - 收集用户列表导入导出
    - 修改用户更新密码信号,影响
      - 修改用户密码产生的改密日志
    
    - 新增Model instance序列化工具函数,影响
      - 操作日志生成
    - 修改api mixin,新增 serializer_classes字段,serializer_classes = {"default": "", "display": "", "list": .., "other_action": ""}, 根据用户请求的方式返回不同的serializer_class,影响
    
      - 用户的viewset
      - 资产权限的viewset
    - 统一系统配置中的tab切换
    - 统一没有nav的页面,影响
      - 重置密码
      - 忘记密码
      - 重置中设置密码
      - 独立的message页面
    - 修改用户组列表页,不再返还用户组下的用户,仅有数量
    - 组织的一些方法变为layzproperty,避免重复计算
    - 修改用户组详情页,影响
      - 用户组增加删除用户
    
      
    Unverified
    e1919d0a
signals_handlers.py 2.52 KB
# -*- coding: utf-8 -*-
#
import re
import os
import logging
from collections import defaultdict
from django.conf import settings
from django.dispatch import receiver
from django.core.signals import request_finished
from django.db import connection
from django.conf import LazySettings
from django.db.utils import ProgrammingError, OperationalError

from jumpserver.utils import get_current_request

from .local import thread_local
from .signals import django_ready

pattern = re.compile(r'FROM `(\w+)`')
logger = logging.getLogger("jumpserver.common")
DEBUG_DB = os.environ.get('DEBUG_DB', '0') == '1'


class Counter:
    def __init__(self):
        self.counter = 0
        self.time = 0

    def __gt__(self, other):
        return self.counter > other.counter

    def __lt__(self, other):
        return self.counter < other.counter

    def __eq__(self, other):
        return self.counter == other.counter


def on_request_finished_logging_db_query(sender, **kwargs):
    queries = connection.queries
    counters = defaultdict(Counter)
    for query in queries:
        if not query['sql'] or not query['sql'].startswith('SELECT'):
            continue
        tables = pattern.findall(query['sql'])
        table_name = ''.join(tables)
        time = query['time']
        counters[table_name].counter += 1
        counters[table_name].time += float(time)
        counters['total'].counter += 1
        counters['total'].time += float(time)

    counters = sorted(counters.items(), key=lambda x: x[1])
    if not counters:
        return
    method = 'GET'
    path = '/Unknown'
    current_request = get_current_request()
    if current_request:
        method = current_request.method
        path = current_request.get_full_path()
    logger.debug(">>> [{}] {}".format(method, path))
    for name, counter in counters:
        logger.debug("Query {:3} times using {:.2f}s {}".format(
            counter.counter, counter.time, name)
        )


def on_request_finished_release_local(sender, **kwargs):
    thread_local.__release_local__()


if settings.DEBUG and DEBUG_DB:
    request_finished.connect(on_request_finished_logging_db_query)
else:
    request_finished.connect(on_request_finished_release_local)


@receiver(django_ready)
def monkey_patch_settings(sender, **kwargs):
    def monkey_patch_getattr(self, name):
        val = getattr(self._wrapped, name)
        if callable(val):
            val = val()
        return val

    try:
        LazySettings.__getattr__ = monkey_patch_getattr
    except (ProgrammingError, OperationalError):
        pass