68 lines
3.0 KiB
Python
68 lines
3.0 KiB
Python
"""源级自动采集设置;页面、周期维护共用,不修改进程环境配置。"""
|
|||
|
|
|
||
|
|
from sqlalchemy import inspect, select, update
|
||
|
|
from sqlalchemy.exc import IntegrityError
|
||
|
|
|
||
|
|
from app.core.exceptions import BadRequestError, ConflictError
|
||
|
|
from app.models import TPlusAutomationConfig
|
||
|
|
from app.services.authorization_service import require_permission
|
||
|
|
from app.services.tplus import task_service
|
||
|
|
|
||
|
|
|
||
|
|
def configuration(session, settings):
|
||
|
|
from app.services.tplus.automation import source_identity
|
||
|
|
|
||
|
|
result = {"enabled": settings.t_plus_automation_enabled, "version": 0,
|
||
|
|
"editable": False, "configuration_origin": "ENVIRONMENT"}
|
||
|
|
try:
|
||
|
|
key, _database = source_identity(settings)
|
||
|
|
except RuntimeError:
|
||
|
|
return result
|
||
|
|
if not inspect(session.connection()).has_table(TPlusAutomationConfig.__tablename__):
|
||
|
|
return result
|
||
|
|
result["editable"] = True
|
||
|
|
row = session.scalar(select(TPlusAutomationConfig).where(
|
||
|
|
TPlusAutomationConfig.source_key == key
|
||
|
|
).execution_options(populate_existing=True))
|
||
|
|
if row is not None:
|
||
|
|
result.update(enabled=bool(row.enabled), version=row.version, configuration_origin="SAVED")
|
||
|
|
return result
|
||
|
|
|
||
|
|
|
||
|
|
def save(session, payload, actor):
|
||
|
|
from app.services.tplus.automation import source_identity
|
||
|
|
|
||
|
|
require_permission(session, actor.user, "tplus:collect")
|
||
|
|
settings = task_service.get_settings()
|
||
|
|
try:
|
||
|
|
key, database = source_identity(settings)
|
||
|
|
except RuntimeError:
|
||
|
|
raise BadRequestError("请先配置可用的 T+ 数据源") from None
|
||
|
|
if not inspect(session.connection()).has_table(TPlusAutomationConfig.__tablename__):
|
||
|
|
raise BadRequestError("自动采集设置尚未升级,请联系维护人员")
|
||
|
|
data = {"enabled": payload.enabled, "expected_version": payload.expected_version}
|
||
|
|
with task_service._audit(session, actor, "修改自动采集", data):
|
||
|
|
row = session.scalar(select(TPlusAutomationConfig).where(TPlusAutomationConfig.source_key == key))
|
||
|
|
if row is None:
|
||
|
|
if payload.expected_version != 0:
|
||
|
|
raise ConflictError()
|
||
|
|
session.add(TPlusAutomationConfig(source_key=key, source_database=database,
|
||
|
|
enabled=int(payload.enabled), version=1, updated_by=actor.user.id))
|
||
|
|
try:
|
||
|
|
session.commit()
|
||
|
|
except IntegrityError:
|
||
|
|
session.rollback()
|
||
|
|
raise ConflictError() from None
|
||
|
|
else:
|
||
|
|
changed = session.execute(update(TPlusAutomationConfig).where(
|
||
|
|
TPlusAutomationConfig.source_key == key,
|
||
|
|
TPlusAutomationConfig.version == payload.expected_version,
|
||
|
|
).values(enabled=int(payload.enabled), version=payload.expected_version + 1,
|
||
|
|
updated_by=actor.user.id))
|
||
|
|
if changed.rowcount != 1:
|
||
|
|
session.rollback()
|
||
|
|
raise ConflictError()
|
||
|
|
session.commit()
|
||
|
|
from app.services.tplus.automation import overview
|
||
|
|
|
||
|
|
return overview(session, settings=settings)
|