You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
2.3 KiB
65 lines
2.3 KiB
"""
|
|
AmazingData 数据服务平台 - 配置服务
|
|
"""
|
|
|
|
from typing import List, Optional, Dict, Any
|
|
from sqlalchemy.orm import Session
|
|
from backend.models.tables import SystemConfig
|
|
from backend.config import settings
|
|
|
|
|
|
class ConfigService:
|
|
"""配置服务"""
|
|
|
|
@staticmethod
|
|
def get_all_configs(db: Session) -> List[SystemConfig]:
|
|
"""获取所有配置"""
|
|
return db.query(SystemConfig).all()
|
|
|
|
@staticmethod
|
|
def get_config(db: Session, key: str) -> Optional[SystemConfig]:
|
|
"""获取单个配置"""
|
|
return db.query(SystemConfig).filter(SystemConfig.config_key == key).first()
|
|
|
|
@staticmethod
|
|
def get_config_value(db: Session, key: str, default: str = None) -> str:
|
|
"""获取配置值"""
|
|
config = db.query(SystemConfig).filter(SystemConfig.config_key == key).first()
|
|
return config.config_value if config else default
|
|
|
|
@staticmethod
|
|
def update_config(db: Session, key: str, value: str) -> Optional[SystemConfig]:
|
|
"""更新配置"""
|
|
config = db.query(SystemConfig).filter(SystemConfig.config_key == key).first()
|
|
if config:
|
|
config.config_value = value
|
|
db.commit()
|
|
db.refresh(config)
|
|
return config
|
|
|
|
@staticmethod
|
|
def batch_update_configs(db: Session, configs: Dict[str, str]) -> bool:
|
|
"""批量更新配置"""
|
|
try:
|
|
for key, value in configs.items():
|
|
config = db.query(SystemConfig).filter(SystemConfig.config_key == key).first()
|
|
if config:
|
|
config.config_value = value
|
|
db.commit()
|
|
return True
|
|
except Exception:
|
|
db.rollback()
|
|
return False
|
|
|
|
@staticmethod
|
|
def get_amazing_data_config(db: Session) -> Dict[str, Any]:
|
|
"""获取 AmazingData 配置"""
|
|
return {
|
|
"username": ConfigService.get_config_value(db, "amazing_data_username", settings.AMAZING_DATA_USERNAME),
|
|
"password": ConfigService.get_config_value(db, "amazing_data_password", settings.AMAZING_DATA_PASSWORD),
|
|
"host": ConfigService.get_config_value(db, "amazing_data_host", settings.AMAZING_DATA_HOST),
|
|
"port": int(ConfigService.get_config_value(db, "amazing_data_port", str(settings.AMAZING_DATA_PORT))),
|
|
}
|
|
|
|
|
|
config_service = ConfigService() |