排程管理及排程记录查看功能
This commit is contained in:
1 parent
3b31867319
commit
08776feef2
28 files changed
+3040
-8
No files matched your search
@@ -0,0 +1,129 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.orm import joinedload
|
||||
|
||||
from app import db
|
||||
from app.models import ScheduleRun, ScheduleTask
|
||||
|
||||
|
||||
def serialize_task(task: ScheduleTask, *, last_status: str | None = None) -> dict:
|
||||
return {
|
||||
'id': task.id,
|
||||
'schedule_no': task.schedule_no,
|
||||
'name': task.name,
|
||||
'schedule_type': task.schedule_type,
|
||||
'owner_user_id': task.owner_user_id,
|
||||
'owner_username': task.owner.username if task.owner else None,
|
||||
'cron_expr': task.cron_expr,
|
||||
'wait_seconds': task.wait_seconds,
|
||||
'dify_agent_id': task.dify_agent_id,
|
||||
'platform_api_path': task.platform_api_path,
|
||||
'notify_user_ids_json': task.notify_user_ids_json or [],
|
||||
'notify_enabled': bool(task.notify_enabled),
|
||||
'enabled': bool(task.enabled),
|
||||
'next_run_at': task.next_run_at.isoformat() if task.next_run_at else None,
|
||||
'last_run_at': task.last_run_at.isoformat() if task.last_run_at else None,
|
||||
'last_status': last_status,
|
||||
'created_at': task.created_at.isoformat() if task.created_at else None,
|
||||
'updated_at': task.updated_at.isoformat() if task.updated_at else None,
|
||||
}
|
||||
|
||||
|
||||
def serialize_run(run: ScheduleRun) -> dict:
|
||||
return {
|
||||
'id': run.id,
|
||||
'run_no': run.run_no,
|
||||
'schedule_task_id': run.schedule_task_id,
|
||||
'schedule_name': run.task.name if run.task else None,
|
||||
'trigger_type': run.trigger_type,
|
||||
'triggered_by_user_id': run.triggered_by_user_id,
|
||||
'started_at': run.started_at.isoformat() if run.started_at else None,
|
||||
'finished_at': run.finished_at.isoformat() if run.finished_at else None,
|
||||
'duration_ms': run.duration_ms,
|
||||
'status': run.status,
|
||||
'total_count': run.total_count,
|
||||
'success_count': run.success_count,
|
||||
'failed_count': run.failed_count,
|
||||
'skipped_count': run.skipped_count,
|
||||
'api_request_json': run.api_request_json,
|
||||
'api_response_json': run.api_response_json,
|
||||
'response_summary_json': run.response_summary_json,
|
||||
'error_message': run.error_message,
|
||||
'excel_file_name': run.excel_file_name,
|
||||
'excel_file_size': run.excel_file_size,
|
||||
'mail_sent': bool(run.mail_sent),
|
||||
'mail_sent_at': run.mail_sent_at.isoformat() if run.mail_sent_at else None,
|
||||
'mail_error': run.mail_error,
|
||||
'created_at': run.created_at.isoformat() if run.created_at else None,
|
||||
}
|
||||
|
||||
|
||||
def list_tasks() -> list[dict]:
|
||||
latest_run_subquery = (
|
||||
db.session.query(
|
||||
ScheduleRun.schedule_task_id.label('task_id'),
|
||||
func.max(ScheduleRun.id).label('last_run_id'),
|
||||
)
|
||||
.group_by(ScheduleRun.schedule_task_id)
|
||||
.subquery()
|
||||
)
|
||||
rows = (
|
||||
db.session.query(ScheduleTask, ScheduleRun.status.label('last_status'))
|
||||
.options(joinedload(ScheduleTask.owner))
|
||||
.outerjoin(latest_run_subquery, latest_run_subquery.c.task_id == ScheduleTask.id)
|
||||
.outerjoin(ScheduleRun, ScheduleRun.id == latest_run_subquery.c.last_run_id)
|
||||
.order_by(ScheduleTask.created_at.desc())
|
||||
.all()
|
||||
)
|
||||
return [serialize_task(task, last_status=last_status) for task, last_status in rows]
|
||||
|
||||
|
||||
def list_runs(
|
||||
*,
|
||||
task_id: Optional[int] = None,
|
||||
owner_user_id: Optional[int] = None,
|
||||
notify_user_id: Optional[int] = None,
|
||||
schedule_name: Optional[str] = None,
|
||||
status: Optional[str] = None,
|
||||
started_after: Optional[datetime] = None,
|
||||
started_before: Optional[datetime] = None,
|
||||
limit: int = 100,
|
||||
) -> list[dict]:
|
||||
query = db.session.query(ScheduleRun).options(joinedload(ScheduleRun.task)).join(ScheduleTask, ScheduleTask.id == ScheduleRun.schedule_task_id)
|
||||
if task_id is not None:
|
||||
query = query.filter(ScheduleRun.schedule_task_id == task_id)
|
||||
if schedule_name:
|
||||
query = query.filter(ScheduleTask.name.ilike(f'%{schedule_name}%'))
|
||||
if status:
|
||||
query = query.filter(ScheduleRun.status == status)
|
||||
if started_after is not None:
|
||||
query = query.filter(ScheduleRun.started_at >= started_after)
|
||||
if started_before is not None:
|
||||
query = query.filter(ScheduleRun.started_at < started_before)
|
||||
if owner_user_id is not None:
|
||||
query = query.filter(ScheduleTask.owner_user_id == owner_user_id)
|
||||
if notify_user_id is not None:
|
||||
query = query.filter(
|
||||
(ScheduleTask.owner_user_id == notify_user_id)
|
||||
| ScheduleTask.notify_user_ids_json.cast(db.String).ilike(f'%{notify_user_id}%')
|
||||
)
|
||||
runs = query.order_by(ScheduleRun.created_at.desc()).limit(max(1, min(limit, 500))).all()
|
||||
return [serialize_run(r) for r in runs]
|
||||
|
||||
|
||||
def get_run(run_id: int) -> ScheduleRun | None:
|
||||
return db.session.query(ScheduleRun).options(joinedload(ScheduleRun.task)).filter(ScheduleRun.id == run_id).first()
|
||||
|
||||
|
||||
def can_view_run(run: ScheduleRun, user_id: int) -> bool:
|
||||
task = run.task
|
||||
if not task:
|
||||
return False
|
||||
if task.owner_user_id == user_id:
|
||||
return True
|
||||
notify = task.notify_user_ids_json or []
|
||||
return int(user_id) in {int(x) for x in notify if str(x).isdigit()}
|
||||
Reference in new issue
Block a user