refactor(backend): 重构后端admin和schedule部分
This commit is contained in:
1 parent
9f5493185d
commit
4a14960e01
74 files changed
+5937
-3644
No files matched your search
@@ -0,0 +1,104 @@
|
||||
"""
|
||||
backend/app/services/schedule/import_service.py
|
||||
|
||||
用途:排程任务 JSON 批量导入(upsert by schedule_no)。
|
||||
依赖:models、scheduler_service、sql_config。
|
||||
逻辑:由 api/schedule/tasks.py import 路由调用。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from app import db
|
||||
from app.core.errors import ValidationError
|
||||
from app.models import ScheduleTask
|
||||
from app.services.schedule.scheduler_service import create_task, update_task
|
||||
from app.services.schedule.sql_config import upsert_schedule_task_config
|
||||
|
||||
|
||||
def import_schedules_from_payload(schedules: list[Any], *, actor_id: int | None) -> dict[str, Any]:
|
||||
"""
|
||||
批量导入排程 JSON。
|
||||
Args:
|
||||
schedules: 导出文件中的 schedules 数组。
|
||||
actor_id: 当前管理员 user id。
|
||||
Returns:
|
||||
ok、imported、skipped、errors(最多 20 条)。
|
||||
Raises:
|
||||
ValidationError: schedules 非 list。
|
||||
"""
|
||||
if not isinstance(schedules, list):
|
||||
raise ValidationError('Invalid import file: schedules missing')
|
||||
|
||||
imported = 0
|
||||
skipped = 0
|
||||
errors: list[str] = []
|
||||
for item in schedules:
|
||||
if not isinstance(item, dict):
|
||||
skipped += 1
|
||||
continue
|
||||
try:
|
||||
name = str(item.get('name') or '').strip()
|
||||
cron_expr = str(item.get('cron_expr') or '').strip()
|
||||
owner_user_id = int(item.get('owner_user_id'))
|
||||
if not name or not cron_expr:
|
||||
raise ValueError('name or cron_expr missing')
|
||||
existing = None
|
||||
schedule_no = str(item.get('schedule_no') or '').strip()
|
||||
if schedule_no:
|
||||
existing = db.session.query(ScheduleTask).filter_by(schedule_no=schedule_no).first()
|
||||
task_payload = {
|
||||
'name': name,
|
||||
'schedule_type': str(item.get('schedule_type') or 'platform_app'),
|
||||
'owner_user_id': owner_user_id,
|
||||
'cron_expr': cron_expr,
|
||||
'schedule_timezone': item.get('schedule_timezone'),
|
||||
'wait_seconds': int(item.get('wait_seconds') or 0),
|
||||
'dify_agent_id': item.get('dify_agent_id'),
|
||||
'platform_api_path': item.get('platform_api_path'),
|
||||
'notify_user_ids_json': item.get('notify_user_ids_json')
|
||||
if isinstance(item.get('notify_user_ids_json'), list)
|
||||
else [],
|
||||
'notify_enabled': bool(item.get('notify_enabled', True)),
|
||||
'enabled': bool(item.get('enabled', True)),
|
||||
'data_source_type': str(item.get('data_source_type') or 'database'),
|
||||
'kb_sync_enabled': bool(item.get('kb_sync_enabled', False)),
|
||||
'kb_group_id': item.get('kb_group_id'),
|
||||
}
|
||||
cfg = item.get('config') if isinstance(item.get('config'), dict) else None
|
||||
if existing:
|
||||
update_task(existing, payload=task_payload, actor_id=actor_id)
|
||||
existing.data_source_type = task_payload['data_source_type']
|
||||
if cfg:
|
||||
upsert_schedule_task_config(existing.id, cfg)
|
||||
else:
|
||||
task = create_task(
|
||||
name=name,
|
||||
schedule_type=task_payload['schedule_type'],
|
||||
owner_user_id=owner_user_id,
|
||||
cron_expr=cron_expr,
|
||||
schedule_timezone=task_payload['schedule_timezone'],
|
||||
wait_seconds=task_payload['wait_seconds'],
|
||||
dify_agent_id=task_payload['dify_agent_id'],
|
||||
platform_api_path=task_payload['platform_api_path'],
|
||||
notify_user_ids_json=task_payload['notify_user_ids_json'],
|
||||
notify_enabled=task_payload['notify_enabled'],
|
||||
enabled=task_payload['enabled'],
|
||||
actor_id=actor_id,
|
||||
)
|
||||
task.data_source_type = task_payload['data_source_type']
|
||||
task.kb_sync_enabled = task_payload['kb_sync_enabled']
|
||||
task.kb_group_id = (
|
||||
int(task_payload['kb_group_id'])
|
||||
if task_payload.get('kb_group_id') not in (None, '', 0, '0')
|
||||
else None
|
||||
)
|
||||
db.session.commit()
|
||||
if cfg:
|
||||
upsert_schedule_task_config(task.id, cfg)
|
||||
imported += 1
|
||||
except Exception as exc:
|
||||
errors.append(str(exc))
|
||||
skipped += 1
|
||||
db.session.commit()
|
||||
return {'ok': True, 'imported': imported, 'skipped': skipped, 'errors': errors[:20]}
|
||||
Reference in new issue
Block a user