feat(admin): 排程管理触发热点问题离线聚合

This commit is contained in:
sophie committed 2026-06-22 18:53:53 +08:00
1 parent cdd227d550
commit 82ad4323f8
11 files changed
+378 -69

No files matched your search

@@ -273,6 +273,9 @@ def list_registered_api_paths() -> list[dict[str, str]]:
def resolve_schedule_executor(task: ScheduleTask) -> ScheduleExecutor:
dst = getattr(task, "data_source_type", None) or "database"
if dst == "faq_hot_aggregate":
from app.services.schedule.faq_hot_aggregate_job import run_faq_hot_aggregate_schedule
return run_faq_hot_aggregate_schedule
if dst in ("database", "external_api"):
return _run_generic_check_job
return _run_common_executor
@@ -0,0 +1,70 @@
"""
backend/app/services/schedule/faq_hot_aggregate_job.py
用途:排程管理触发的 FAQ 热点离线聚合执行器。
依赖:HotFaqAggregator、ScheduleTask、ScheduleRun。
逻辑:data_source_type=faq_hot_aggregate 时由 resolve_schedule_executor 调用。
"""
from __future__ import annotations
from typing import Any
from app.models import ScheduleRun, ScheduleTask
from app.services.hot_faq_aggregator import HotFaqAggregator
ScheduleExecuteResult = tuple[int, int, int, int, list[dict[str, Any]], list[str], list[dict[str, Any]]]
def run_faq_hot_aggregate_schedule(task: ScheduleTask, run: ScheduleRun) -> ScheduleExecuteResult:
"""
执行 FAQ 热点离线聚合(等同 flask faq-aggregate-hot,非 dry-run)。
核心逻辑:委托 HotFaqAggregator;可选 dify_agent_id 限定 Agent。
@param task - 排程任务;dify_agent_id 可选作 agent_id 过滤
@param run - 当前执行批次(未写入 run 字段,由 scheduler_service 汇总)
@returns ScheduleExecuteResult 元组
"""
agent_id = int(task.dify_agent_id) if task.dify_agent_id else None
result = HotFaqAggregator.aggregate_hot_faq_candidates(agent_id=agent_id, dry_run=False)
step_logs: list[dict[str, Any]] = [
{
'step': 'faq_hot_aggregate',
'status': 'started',
'detail': {'schedule_task_id': task.id, 'agent_id': agent_id},
}
]
if not result.get('enabled'):
message = str(result.get('message') or 'FAQ_HOT_ENABLED is false')
step_logs.append({'step': 'faq_hot_aggregate', 'status': 'skipped', 'detail': {'message': message}})
return 0, 0, 0, 1, [], [message], step_logs
promoted = int(result.get('promoted_count') or 0)
skipped = int(result.get('skipped_count') or 0)
scanned = int(result.get('scanned_logs') or 0)
total = max(promoted + skipped, 1)
step_logs.append({
'step': 'faq_hot_aggregate',
'status': 'success',
'detail': {
'scanned_logs': scanned,
'promoted_count': promoted,
'skipped_count': skipped,
'group_count': result.get('group_count'),
'agent_id': agent_id,
},
})
details = [
{
'canonical_query': item.get('canonical_query'),
'ask_count_7d': item.get('ask_count_7d'),
'user_count_7d': item.get('user_count_7d'),
'answer_dominance': item.get('answer_dominance'),
}
for item in (result.get('candidates') or [])[:20]
if isinstance(item, dict)
]
return total, promoted, 0, skipped, details, [], step_logs
@@ -226,7 +226,10 @@ def update_task(task: ScheduleTask, *, payload: dict[str, Any], actor_id: int |
task.kb_group_id = int(raw_group_id) if raw_group_id not in (None, "", 0, "0") else None
if not normalize_platform_api_path(task.platform_api_path):
raise ValueError("platform_api_path is required for platform_app")
dst = str(getattr(task, "data_source_type", None) or "database")
if dst != "faq_hot_aggregate":
raise ValueError("platform_api_path is required for platform_app")
task.platform_api_path = "faq-aggregate-hot"
task.schedule_type = "platform_app"
if task.enabled and task.cron_expr: