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

+43
View File
@@ -38,6 +38,49 @@ def run_internal_job(name: str):
}
],
})
if key in {'faq-aggregate-hot', 'faq_hot_aggregate', 'faq-hot-aggregate'}:
from app.services.hot_faq_aggregator import HotFaqAggregator
schedule_task_id = data.get('schedule_task_id')
agent_id = data.get('agent_id')
if schedule_task_id:
task = db.session.get(ScheduleTask, int(schedule_task_id))
if task and task.dify_agent_id:
agent_id = task.dify_agent_id
result = HotFaqAggregator.aggregate_hot_faq_candidates(
agent_id=int(agent_id) if agent_id else None,
dry_run=False,
)
if not result.get('enabled'):
message = str(result.get('message') or 'FAQ_HOT_ENABLED is false')
return jsonify({
'summary': {'total_count': 0, 'success_count': 0, 'failed_count': 0, 'skipped_count': 1},
'details': [],
'errors': [message],
'step_logs': [{'step': 'faq_hot_aggregate', 'status': 'skipped', 'detail': {'message': message}}],
})
promoted = int(result.get('promoted_count') or 0)
skipped = int(result.get('skipped_count') or 0)
total = max(promoted + skipped, 1)
return jsonify({
'summary': {
'total_count': total,
'success_count': promoted,
'failed_count': 0,
'skipped_count': skipped,
},
'details': (result.get('candidates') or [])[:20],
'errors': [],
'step_logs': [{
'step': 'faq_hot_aggregate',
'status': 'success',
'detail': {
'scanned_logs': result.get('scanned_logs'),
'promoted_count': promoted,
'skipped_count': skipped,
},
}],
})
run_no = str(data.get('run_no') or '').strip()
schedule_task_id = data.get('schedule_task_id')
task = None
+5 -2
View File
@@ -334,11 +334,14 @@ def post_schedule():
task.kb_sync_enabled = bool(data.get('kb_sync_enabled', False))
raw_kb_group_id = data.get('kb_group_id')
task.kb_group_id = int(raw_kb_group_id) if raw_kb_group_id not in (None, '', 0, '0') else None
task.platform_api_path = f'/{task.id}'
if data_source_type == 'faq_hot_aggregate':
task.platform_api_path = 'faq-aggregate-hot'
else:
task.platform_api_path = f'/{task.id}'
db.session.commit()
db.session.refresh(task)
config_data = data.get('config')
if config_data:
if config_data and data_source_type != 'faq_hot_aggregate':
upsert_schedule_task_config(task.id, config_data)
if not task.dify_agent_id:
cfg_model = ScheduleTaskConfig.query.filter_by(schedule_task_id=task.id).first()
@@ -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: