Files
assist/src/core/performance_config.py
Jeason c2a5bcbc94 fix: 清除所有硬编码配置
1. Redis 连接:redis_manager.py 和 system_optimizer.py 的 IP/端口/密码改为从 unified_config 读取(不再硬编码 43.134.68.207/123456)
2. 性能配置:performance_config.py 改为从 system_settings.json 读取,硬编码值仅作 fallback
3. 默认管理员密码:从环境变量 ADMIN_PASSWORD 读取,默认 admin123
2026-04-07 10:23:35 +08:00

66 lines
2.3 KiB
Python

# -*- coding: utf-8 -*-
"""
性能优化配置
从 system_settings.json 读取,硬编码值仅作为 fallback
"""
import os
import json
import logging
logger = logging.getLogger(__name__)
# 默认值
_DEFAULTS = {
"database": {"pool_size": 20, "max_overflow": 30, "pool_recycle": 1800, "pool_timeout": 10},
"cache": {"default_ttl": 60, "max_memory_size": 2000, "conversation_ttl": 60, "workorder_ttl": 30, "monitoring_ttl": 30},
"query": {"default_limit": 100, "conversations_limit": 1000, "workorders_limit": 100, "monitoring_limit": 1000},
"api": {"timeout": 10, "retry_count": 3, "batch_size": 50},
"monitoring": {"interval": 60, "slow_query_threshold": 1.0, "performance_log_enabled": True}
}
def _load():
try:
path = os.path.join('data', 'system_settings.json')
if os.path.exists(path):
with open(path, 'r', encoding='utf-8') as f:
return json.load(f).get('performance', {})
except Exception as e:
logger.debug(f"加载性能配置失败: {e}")
return {}
class PerformanceConfig:
"""性能配置类 — 优先从配置文件读取"""
@classmethod
def _get(cls, section, key):
cfg = _load()
return cfg.get(section, {}).get(key, _DEFAULTS.get(section, {}).get(key))
@classmethod
def get_database_config(cls):
cfg = _load().get('database', {})
return {**_DEFAULTS['database'], **cfg}
@classmethod
def get_cache_config(cls):
cfg = _load().get('cache', {})
return {**_DEFAULTS['cache'], **cfg}
@classmethod
def get_query_config(cls):
cfg = _load().get('query', {})
return {**_DEFAULTS['query'], **cfg}
@classmethod
def get_api_config(cls):
cfg = _load().get('api', {})
return {**_DEFAULTS['api'], **cfg}
# 向后兼容的类属性(读取时动态获取)
DATABASE_POOL_SIZE = property(lambda self: self._get('database', 'pool_size'))
CACHE_DEFAULT_TTL = property(lambda self: self._get('cache', 'default_ttl'))
CACHE_MAX_MEMORY_SIZE = property(lambda self: self._get('cache', 'max_memory_size'))
QUERY_LIMIT_DEFAULT = property(lambda self: self._get('query', 'default_limit'))
API_TIMEOUT = property(lambda self: self._get('api', 'timeout'))
MONITORING_INTERVAL = property(lambda self: self._get('monitoring', 'interval'))