完善后台任务中心历史摘要视图

- 为系统任务和插件调度补充批量历史摘要查询,支持最近成功时间、最近失败原因与累计成功失败次数

- 任务列表接口合并内存运行态与数据库日志态,服务重启后后台仍可回看最近执行结果

- 系统任务页与插件调度页新增健康状态、历史执行摘要与插件调度快捷启停入口

- 更新工程优化文档,记录 7.3 第一阶段当前进展
This commit is contained in:
liuwei
2026-04-30 16:21:29 +08:00
parent 0d7fe5d6f0
commit 1db8681636
8 changed files with 496 additions and 7 deletions

View File

@@ -40,7 +40,7 @@ def api_list_schedules():
data = server.plugin_schedule_manager.list_schedules_with_runtime()
# 后端统一格式化时间字段,避免前端出现 Fri, 17 Apr 2026 ... 这类 RFC 时间串。
for row in data:
for key in ("next_run_at", "last_run_at", "created_at", "updated_at"):
for key in ("next_run_at", "last_run_at", "latest_success_at", "latest_failed_at", "created_at", "updated_at"):
if key in row:
row[key] = _normalize_datetime_text(row.get(key))
return jsonify({"success": True, "data": data})

View File

@@ -21,6 +21,40 @@ def _normalize_datetime_text(value):
return text
def _build_job_health_status(*, enabled: bool, running: bool, last_status: str, latest_success_at, latest_failure_summary: str) -> str:
"""根据任务启停、运行态和历史结果输出后台可读的健康状态。"""
# 状态设计尽量贴近运维判断顺序:
# 1. 停用态单独标记,避免和“从未执行”混淆;
# 2. 执行中的任务优先展示 running方便后台快速识别实时动作
# 3. 最近一次执行失败时直接标记 failed让异常任务在列表里一眼可见
# 4. 有成功历史且最近不是失败时视为 healthy否则落到 idle。
if not enabled:
return "disabled"
if running:
return "running"
if str(last_status or "").strip().lower() == "failed":
return "failed"
if latest_success_at or str(last_status or "").strip().lower() == "success":
return "healthy"
if str(latest_failure_summary or "").strip():
return "failed"
return "idle"
def _build_job_health_message(*, health_status: str, latest_success_at, latest_failure_summary: str) -> str:
"""为后台列表生成一句简短的任务健康提示。"""
if health_status == "disabled":
return "任务已停用"
if health_status == "running":
return "任务正在执行中"
if health_status == "failed":
return str(latest_failure_summary or "最近一次执行失败").strip()
if health_status == "healthy":
success_text = _normalize_datetime_text(latest_success_at)
return f"最近成功于 {success_text}" if success_text else "任务近期执行正常"
return "暂无执行记录"
@system_jobs_bp.route("/")
@login_required
def page_system_jobs():
@@ -34,11 +68,31 @@ def api_list_jobs():
db_rows = server.system_job_db.list_jobs()
runtime_rows = async_job.get_jobs_snapshot()
runtime_by_key = {row.get("job_key", ""): row for row in runtime_rows if row.get("job_key")}
job_keys = [str(row.get("job_key") or "").strip() for row in db_rows if str(row.get("job_key") or "").strip()]
latest_log_by_key = server.system_job_db.get_latest_logs_map(job_keys)
history_summary_by_key = server.system_job_db.get_job_history_summary_map(job_keys)
result = []
for row in db_rows:
job_key = row.get("job_key")
runtime = runtime_by_key.get(job_key, {})
latest_log = latest_log_by_key.get(job_key, {})
history_summary = history_summary_by_key.get(job_key, {})
last_status = runtime.get("last_status") or latest_log.get("status") or "never"
last_run_at = runtime.get("last_run_at") or latest_log.get("triggered_at")
last_error = runtime.get("last_error") or ""
if not last_error and str(last_status or "").strip().lower() == "failed":
last_error = (
str(latest_log.get("summary") or "").strip()
or str(history_summary.get("latest_failure_summary") or "").strip()
)
health_status = _build_job_health_status(
enabled=bool(row.get("enabled", 0)),
running=bool(runtime.get("running", False)),
last_status=str(last_status or ""),
latest_success_at=history_summary.get("latest_success_at"),
latest_failure_summary=str(history_summary.get("latest_failure_summary") or ""),
)
result.append(
{
"job_key": job_key,
@@ -51,14 +105,26 @@ def api_list_jobs():
"runtime_enabled": runtime.get("enabled"),
"running": runtime.get("running", False),
"trigger_text": runtime.get("trigger_text", ""),
"last_run_at": _normalize_datetime_text(runtime.get("last_run_at")),
"last_status": runtime.get("last_status"),
"last_error": runtime.get("last_error"),
"last_duration_ms": runtime.get("last_duration_ms"),
"last_run_at": _normalize_datetime_text(last_run_at),
"last_status": last_status,
"last_error": last_error,
"last_duration_ms": runtime.get("last_duration_ms") or latest_log.get("duration_ms"),
"next_run_at": _normalize_datetime_text(runtime.get("next_run_at")),
"run_count": runtime.get("run_count", 0),
"success_count": runtime.get("success_count", 0),
"fail_count": runtime.get("fail_count", 0),
"latest_success_at": _normalize_datetime_text(history_summary.get("latest_success_at")),
"latest_failed_at": _normalize_datetime_text(history_summary.get("latest_failed_at")),
"latest_failure_summary": str(history_summary.get("latest_failure_summary") or "").strip(),
"history_success_count": int(history_summary.get("history_success_count", 0) or 0),
"history_fail_count": int(history_summary.get("history_fail_count", 0) or 0),
"history_total_count": int(history_summary.get("history_total_count", 0) or 0),
"health_status": health_status,
"health_message": _build_job_health_message(
health_status=health_status,
latest_success_at=history_summary.get("latest_success_at"),
latest_failure_summary=str(history_summary.get("latest_failure_summary") or "").strip(),
),
}
)