UnisKB/apps/system_manage/ai_hub_progress.py

43 lines
2.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""Expose real phases/counts only. A single-file v1 job has no page percentage."""
from django.utils import timezone
LABELS = {'parse': '文档解析中', 'organize': '智能整理中', 'render': '生成思维导图'}
def parser_progress(task):
state = task.get('status')
phase = {'queued': '等待解析服务处理', 'running': '正在解析文档', 'completed': '获取解析结果'}.get(state, '正在解析文档')
raw = task.get('progress') or {}
def count(key):
value = raw.get(key, 0) if isinstance(raw, dict) else 0
return value if isinstance(value, int) and not isinstance(value, bool) and value >= 0 else 0
completed, total = count('completed'), count('total')
percent = min(100, completed * 100 // total) if total > 1 and completed else None
if total > 1:
phase += f'({min(completed, total)}/{total} 个文件)'
return {'stage': phase, 'percent': percent, 'completed_files': completed, 'total_files': total}
def task_progress(task):
elapsed = max(0, int(((task.finished_at or timezone.now()) - (task.started_at or task.created_at)).total_seconds()))
section_detail = next((step.get('progress', {}) for step in reversed(task.steps or [])
if 'partial_markdown' in (step.get('progress') or {})), {})
section_fields = {key: section_detail[key] for key in
('partial_markdown', 'completed_sections', 'total_sections') if key in section_detail}
if task.status == 'SUCCESS':
return {'stage': '完成', 'percent': 100, 'elapsed_seconds': elapsed, **section_fields}
if task.status == 'PENDING':
return {'stage': '等待任务处理', 'percent': None, 'elapsed_seconds': elapsed}
if task.status == 'FAILED':
return {'stage': '处理失败', 'percent': None, 'elapsed_seconds': elapsed, **section_fields}
current = next((step for step in task.steps if step.get('status') == 'RUNNING'), {})
detail = current.get('progress') or {}
percent = detail.get('percent')
total_steps = max(1, len(task.snapshot.get('nodes', [])))
complete_steps = sum(step.get('status') == 'SUCCESS' for step in task.steps)
if percent is not None:
percent = min(99, int((complete_steps * 100 + percent) / total_steps))
return {'stage': detail.get('stage') or LABELS.get(current.get('type'), '处理中'),
'percent': percent, 'elapsed_seconds': elapsed,
**section_fields, **({'plan': detail['plan']} if isinstance(detail.get('plan'), dict) else {})}