nex_animation/backend/app/services/status.py

613 lines
36 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.

"""状态跟踪与流程进度:把数据库里的镜头/任务真实状态收敛成统一视图。
同时负责把「业务流程图(图2)」的 12 个节点映射成可展示的流程进度,
并在每次状态变化时回写 project.status / project.current_stage。
"""
from __future__ import annotations
from datetime import datetime, timedelta, timezone
from sqlalchemy import func, select
from sqlalchemy.orm import Session, selectinload
from app.constants import ProjectStatus, ShotStatus, TaskKind, TaskStatus
from app.models import Asset, ExportRecord, Project, Shot, Task, Timeline, now_utc
from app.services import costing, storage
# ---------------------------------------------------------------- 业务流程(图2)节点
PIPELINE_STAGES: list[dict] = [
{"key": "create_project", "label": "创建视频项目", "group": "准备"},
{"key": "import_storyboard", "label": "导入分镜图片与脚本", "group": "准备"},
{"key": "bind_assets", "label": "绑定人物/场景与声音资产", "group": "准备"},
{"key": "plan_tasks", "label": "生成分镜任务清单", "group": "编排"},
{"key": "preflight", "label": "检查素材与参数", "group": "编排"},
{"key": "dispatch", "label": "任务队列调度", "group": "生成"},
{"key": "generate", "label": "调用视频模型", "group": "生成"},
{"key": "save_output", "label": "下载并保存视频", "group": "生成"},
{"key": "quality_check", "label": "质量检查", "group": "审核"},
{"key": "review", "label": "人工审核", "group": "审核"},
{"key": "compose", "label": "时间线合成 + 配音/字幕/混音", "group": "成片"},
{"key": "export", "label": "导出完整视频", "group": "成片"},
]
STAGE_LABELS = {s["key"]: s["label"] for s in PIPELINE_STAGES}
def as_utc(value: datetime | None) -> datetime | None:
"""SQLite 读出来的是 naive datetime,这里统一成 UTC aware,便于比较。"""
if value is None:
return None
return value.replace(tzinfo=timezone.utc) if value.tzinfo is None else value.astimezone(timezone.utc)
def liveness_seconds() -> float:
"""「执行体大概还活着」的心跳窗口,与调度守护进程同一口径(daemon.LIVENESS_SECONDS)。
心跳断几分钟不足以判定任务死了(误杀会把正在生成的镜头再跑一遍、成本翻倍),
但已经足够说明界面上必须给个解释,而不是继续显示「进行中」。
延迟导入避开 status ↔ daemon 的环。
"""
try:
from app.services.daemon import LIVENESS_SECONDS
return float(LIVENESS_SECONDS)
except Exception: # noqa: BLE001 - 取不到就用保守默认
return 300.0
def heartbeat_age_seconds(task: Task) -> int | None:
"""距离上一次心跳过了多久(秒)。从未心跳过返回 None。"""
beat = as_utc(task.heartbeat_at)
if beat is None:
return None
return max(0, int((now_utc() - beat).total_seconds()))
def elapsed_ms(start: datetime | None, end: datetime | None = None) -> int:
s = as_utc(start)
if not s:
return 0
e = as_utc(end) or now_utc()
return max(0, int((e - s).total_seconds() * 1000))
# ---------------------------------------------------------------- 序列化
def task_dict(task: Task) -> dict:
# 「进行中」必须能被解释。以前只要执行体没写心跳,界面上就只剩一个转圈的图标:
# 任务其实早就随进程重启没了,却要等满 stale_running_seconds(本机 1800s)才自愈。
# 这里把心跳年龄直接吐给前端,卡片可以立刻写明「心跳已停止 X 分钟,平台会自动续跑」。
age = heartbeat_age_seconds(task)
threshold = liveness_seconds()
stale = bool(task.status == TaskStatus.RUNNING and (age is None or age > threshold))
return {
"id": task.id, "project_id": task.project_id, "shot_id": task.shot_id,
"timeline_id": task.timeline_id, "kind": task.kind, "name": task.name, "stage": task.stage,
# 非项目归属(平台资产母版):卡片按它亮「正在生成」,活动面板按它跳回资产
"owner_type": task.owner_type or "", "owner_id": task.owner_id,
# 「此刻在做什么」优先取执行体自己说的,退回深处 progress.report 写的那句
"message": _task_message(task),
"status": task.status, "priority": task.priority, "progress": round(task.progress or 0, 4),
"provider": task.provider, "model": task.model, "mode": task.mode,
"execution_backend": task.execution_backend, "celery_task_id": task.celery_task_id,
"worker_id": task.worker_id, "idempotency_key": task.idempotency_key,
"attempts": task.attempts, "max_attempts": task.max_attempts,
"next_retry_at": task.next_retry_at, "error_code": task.error_code,
"error_message": task.error_message, "error_history": task.error_history or [],
"output_path": task.output_path, "output_url": storage.url_for(task.output_path),
"result": task.result_json or {}, "checkpoint": task.checkpoint_json or {},
"payload": task.payload_json or {},
"cost": round(task.cost or 0, 4), "cost_currency": task.cost_currency,
"queued_at": task.queued_at, "started_at": task.started_at, "heartbeat_at": task.heartbeat_at,
"finished_at": task.finished_at, "created_at": task.created_at, "updated_at": task.updated_at,
"heartbeat_age_seconds": age, "stale": stale,
"stale_after_seconds": int(threshold),
"elapsed_ms": elapsed_ms(task.started_at, task.finished_at),
"queue_wait_ms": elapsed_ms(task.queued_at, task.started_at),
}
def _task_message(task: Task) -> str:
"""任务此刻在做什么的一句话:优先 ``task.message``(执行体与算力锁写的),
退回 ``checkpoint_json.last_note``(项目侧 handler 一直在往这里写阶段说明),
两边都有时取更新的那一条——两侧写入者不同,谁最后落地谁就是真相。
"""
note = str((task.checkpoint_json or {}).get("last_note") or "")
mine = str(task.message or "")
if not note:
return mine
if not mine:
return note
at = (task.checkpoint_json or {}).get("at") or ""
heartbeat = task.heartbeat_at.isoformat() if task.heartbeat_at else ""
return note if str(at) >= str(heartbeat or "") else mine
#: 活动面板默认回吐多少条历史:够翻完一个下午的生成,又不至于让 3 秒一次的轮询变重
ACTIVITY_LIMIT = 40
#: 已落定的任务在活动面板里保留多久(与 ACTIVITY_LIMIT 一起构成「刚刚完成」那一段)
ACTIVITY_WINDOW = timedelta(hours=6)
def activity(db: Session, *, limit: int = ACTIVITY_LIMIT, owner_type: str = "",
active_only: bool = False) -> dict:
"""顶栏「平台动态」要的那一份:进行中的全部 + 最近落定的若干条。
数据源就是统一任务表,所以项目侧的出片与平台侧的生成天然同屏——
用户问的是「平台现在在忙什么」,不关心这条活算不算某个项目的。
``limit`` 只截历史:进行中的任务一条都不能少,否则用户在面板里
看不到自己刚点的那次,比没有面板更糟(他会以为排队排丢了)。
"""
stmt = select(Task).order_by(Task.id.desc())
if owner_type:
stmt = stmt.where(Task.owner_type == owner_type)
if active_only:
rows = db.scalars(stmt.where(Task.status.in_(TaskStatus.ACTIVE))).all()
else:
active = db.scalars(stmt.where(Task.status.in_(TaskStatus.ACTIVE))).all()
settled = db.scalars(
stmt.where(Task.status.notin_(TaskStatus.ACTIVE),
Task.updated_at >= now_utc() - ACTIVITY_WINDOW)
.limit(min(200, max(1, limit)))
).all()
rows = list(active) + list(settled)
items = [task_dict(t) for t in rows]
counts: dict[str, int] = {}
for row in rows:
counts[row.status] = counts.get(row.status, 0) + 1
return {"tasks": items,
"active": sum(counts.get(s, 0) for s in TaskStatus.ACTIVE),
"counts": counts, "window_hours": ACTIVITY_WINDOW.total_seconds() / 3600}
def shot_dict(db: Session, shot: Shot, *, assets: bool = True) -> dict:
from app.services import preview as preview_service
from app.services import prompt as prompt_service
payload = {
"id": shot.id, "project_id": shot.project_id, "order_index": shot.order_index, "code": shot.code,
"title": shot.title, "synopsis": shot.synopsis, "description": shot.description,
"prompt": shot.prompt, "negative_prompt": shot.negative_prompt, "style_note": shot.style_note,
"shot_size": shot.shot_size, "camera_angle": shot.camera_angle, "camera_movement": shot.camera_movement,
"lens_note": shot.lens_note, "duration": shot.duration,
"transition_out": shot.transition_out, "transition_duration_ms": shot.transition_duration_ms,
"dialogue": shot.dialogue,
"lines": shot.all_lines(), "ambience": shot.ambience or "",
"sound_effects": shot.all_sound_effects(),
"speaker_character_id": shot.speaker_character_id,
"speaker_name": shot.speaker.name if shot.speaker else "",
"voice_asset_id": shot.voice_asset_id, "voice_name": shot.voice.name if shot.voice else "",
"subtitle_text": shot.subtitle_text, "subtitle_enabled": shot.subtitle_enabled,
"voiceover_path": shot.voiceover_path, "voiceover_url": storage.url_for(shot.voiceover_path),
"voiceover_duration_ms": shot.voiceover_duration_ms,
"provider": shot.provider, "model": shot.model, "mode": shot.mode, "seed": shot.seed,
# 留空 = 跟随项目默认模型,这里一并给出「实际会派发给谁」,避免界面上看着像 mock
"effective_provider": shot.provider or (shot.project.default_provider if shot.project else ""),
"effective_model": shot.model or (shot.project.default_model if shot.project else ""),
"params": shot.params_json or {},
"status": shot.status, "stage": shot.stage, "stage_label": STAGE_LABELS.get(shot.stage, shot.stage),
"error_code": shot.error_code, "error_message": shot.error_message, "attempts": shot.attempts,
"current_task_id": shot.current_task_id,
"output_path": shot.output_path, "output_url": storage.url_for(shot.output_path),
"output_exists": storage.exists(shot.output_path),
"output_duration": shot.output_duration, "provider_job_id": shot.provider_job_id,
"generated_at": shot.generated_at,
"review_state": shot.review_state, "review_notes": shot.review_notes,
"qc_score": shot.qc_score, "qc": shot.qc_json or {},
"characters": [{"id": c.id, "name": c.name, "role": c.role} for c in (shot.characters or [])],
"scenes": [{"id": s.id, "name": s.name} for s in (shot.scenes or [])],
"prompt_revisions": [{"id": r.id, "prompt": r.prompt, "reason": r.reason, "actor": r.actor,
"created_at": r.created_at} for r in (shot.prompt_revisions or [])[:10]],
"reviews": [{"id": r.id, "kind": r.kind, "verdict": r.verdict, "score": r.score, "notes": r.notes,
"reviewer": r.reviewer, "checks": r.checks_json or {}, "created_at": r.created_at}
for r in (shot.reviews or [])[:10]],
"assets": [],
}
# 审片预览:分镜/审核页默认听到的是平台配音,不是模型自带音轨
try:
payload.update(preview_service.preview_state(db, shot))
except Exception: # noqa: BLE001 - 预览只是增强,不能拖垮镜头负载
payload.update({"preview_url": "", "preview_path": "", "preview_ready": False,
"native_audio": False, "voiceover_ready": storage.exists(shot.voiceover_path),
"voice_source": "dub"})
if assets:
rows = db.query(Asset).filter(Asset.owner_type == "shot", Asset.owner_id == shot.id) \
.order_by(Asset.id).all()
payload["assets"] = [storage.asset_dict(a) for a in rows]
payload["assembled_prompt_preview"] = prompt_service.compose_prompt(shot.project, shot, db=db)
return payload
def preload_assets(db: Session, project_ids: list[int]) -> dict[tuple[str, int], list]:
"""一次查询取回这些项目下所有人物 / 场景 / 声线素材(含版本),按 owner 分组。
project_dict 原来对每个 owner 单独查素材、再逐条懒加载 versions:
项目一多,`GET /api/projects` 就是几十上百条 SQL。这里压成两条。
"""
if not project_ids:
return {}
rows = db.execute(
select(Asset).options(selectinload(Asset.versions))
.where(Asset.project_id.in_(project_ids)).order_by(Asset.id)
).scalars().all()
born = _owner_born(db, project_ids)
grouped: dict[tuple[str, int], list] = {}
for asset in rows:
key = (str(asset.owner_type), int(asset.owner_id))
started = born.get(key)
# 主键复用带回来的脏素材(比宿主还老)不进素材库
born_at = storage.asset_born(asset)
if started and born_at and born_at < started:
continue
grouped.setdefault(key, []).append(asset)
return grouped
def _owner_born(db: Session, project_ids: list[int]) -> dict[tuple[str, int], object]:
"""宿主(人物/场景/声线/镜头)的创建时间,用于识别主键复用继承下来的脏素材。"""
from app.models import Character, Scene, Shot, VoiceAsset
out: dict[tuple[str, int], object] = {}
for owner_type, model in (("human", Character), ("scene", Scene),
("voice", VoiceAsset), ("shot", Shot)):
for row in db.execute(select(model.id, model.created_at)
.where(model.project_id.in_(project_ids))).all():
out[(owner_type, int(row[0]))] = storage.as_utc(row[1])
return out
def project_dict(db: Session, project: Project, *, stats: bool = True,
asset_map: dict[tuple[str, int], list] | None = None) -> dict:
def assets_of(owner_type: str, owner_id: int) -> list:
if asset_map is None:
return [storage.asset_dict(a) for a in _assets_of(db, owner_type, owner_id)]
return [storage.asset_dict(a) for a in asset_map.get((owner_type, int(owner_id)), [])]
payload = {
"id": project.id, "name": project.name, "description": project.description, "genre": project.genre,
"target_duration": project.target_duration, "aspect_ratio": project.aspect_ratio,
"resolution": project.resolution, "fps": project.fps, "style_guide": project.style_guide,
"global_negative_prompt": project.global_negative_prompt, "default_provider": project.default_provider,
"default_model": project.default_model, "default_transition": project.default_transition,
"settings": project.settings_json or {}, "status": project.status,
"current_stage": project.current_stage, "stage_label": STAGE_LABELS.get(project.current_stage, ""),
"created_at": project.created_at, "updated_at": project.updated_at,
"created_by": project.created_by or "", "created_by_id": project.created_by_id,
"updated_by": project.updated_by or "", "updated_by_id": project.updated_by_id,
"characters": [{"id": c.id, "name": c.name, "role": c.role, "code": c.code,
"voice_profile": c.voice_profile, "default_voice_id": c.default_voice_id,
"default_voice_name": c.default_voice.name if c.default_voice else "",
"persona_prompt": c.persona_prompt, "appearance": c.appearance,
"personality": c.personality, "tags": c.tags or [], "is_active": c.is_active,
"platform_source": _platform_source(c),
"assets": assets_of("human", c.id)}
for c in project.characters],
"scenes": [{"id": s.id, "name": s.name, "code": s.code, "description": s.description,
"prompt": s.prompt, "lighting": s.lighting, "time_of_day": s.time_of_day,
"palette": s.palette, "tags": s.tags or [],
"platform_source": _platform_source(s),
"assets": assets_of("scene", s.id)}
for s in project.scenes],
"voices": [{"id": v.id, "name": v.name, "provider": v.provider,
"provider_voice_id": v.provider_voice_id, "language": v.language, "gender": v.gender,
"description": v.description, "tuning": v.tuning or {}, "version": v.version,
"is_default": v.is_default,
"platform_source": _platform_source(v),
"assets": assets_of("voice", v.id)}
for v in project.voices],
}
if stats:
payload["stats"] = stats_of(db, project)
payload["workflow"] = workflow_of(db, project)
return payload
def _assets_of(db: Session, owner_type: str, owner_id: int) -> list:
from app.models import Asset
rows = db.query(Asset).filter(Asset.owner_type == owner_type, Asset.owner_id == owner_id) \
.order_by(Asset.id).all()
return [row for row in rows if storage.belongs_to_owner(db, owner_type, int(owner_id), row)]
def _platform_source(owner) -> dict | None:
source_id = getattr(owner, "platform_source_id", None)
if not source_id:
return None
return {
"type": getattr(owner, "platform_source_type", "") or "",
"id": source_id,
"version": getattr(owner, "platform_source_version", "") or "",
"snapshot": getattr(owner, "platform_snapshot_json", None) or {},
}
# ---------------------------------------------------------------- 统计
def stats_of(db: Session, project: Project) -> dict:
shots = list(project.shots or [])
shot_status: dict[str, int] = {}
for s in shots:
shot_status[s.status] = shot_status.get(s.status, 0) + 1
rows = db.execute(select(Task.status, func.count(Task.id)).where(Task.project_id == project.id)
.group_by(Task.status)).all()
task_status = {s: int(c) for s, c in rows}
tasks_total = sum(task_status.values())
gen_seconds = sum(float(s.output_duration or 0) for s in shots if s.output_path)
planned = sum(float(s.duration or 0) for s in shots)
timeline = current_timeline(db, project)
exports = db.scalars(select(ExportRecord).where(ExportRecord.project_id == project.id)
.order_by(ExportRecord.id.desc())).all()
current_export_id = None
if timeline is not None:
current_export_id = next(
(e.id for e in exports
if e.timeline_id == timeline.id and e.status == "succeeded" and e.path),
None,
)
cost = costing.summarize(db, project.id)
approved = shot_status.get(ShotStatus.APPROVED, 0)
return {
"shots": len(shots), "shot_status": shot_status,
"shots_ready": sum(1 for s in shots if s.status == ShotStatus.READY),
"shots_generated": sum(1 for s in shots if s.status in
(ShotStatus.GENERATED, ShotStatus.QC_PASSED, ShotStatus.APPROVED)),
"shots_approved": approved,
"shots_rejected": shot_status.get(ShotStatus.REJECTED, 0),
"shots_failed": shot_status.get(ShotStatus.FAILED, 0),
"progress_ratio": round(approved / len(shots), 4) if shots else 0.0,
"planned_duration": round(planned, 2), "generated_duration": round(gen_seconds, 2),
"characters": len(project.characters or []), "scenes": len(project.scenes or []),
"voices": len(project.voices or []),
"assets": int(db.execute(select(func.count(Asset.id))
.where(Asset.project_id == project.id)).scalar() or 0),
"tasks": tasks_total, "task_status": task_status,
"tasks_active": sum(task_status.get(s, 0) for s in TaskStatus.ACTIVE),
"timeline": ({"id": timeline.id, "name": timeline.name, "segments": len(timeline.segments or []),
"total_duration_ms": timeline.total_duration_ms,
"subtitles": len(timeline.subtitles or []),
"audio_clips": len(timeline.audio_clips or [])} if timeline else None),
"exports": [{"id": e.id, "name": e.name, "preset": e.preset, "status": e.status,
"url": storage.url_for(e.path), "path": e.path, "size_bytes": e.size_bytes,
"duration_ms": e.duration_ms, "width": e.width, "height": e.height,
"fps": e.fps, "created_at": e.created_at, "error_message": e.error_message}
for e in exports[:20]],
# 历史导出保留在 exports 里供追溯;latest_export 只代表「当前时间线」的成片,
# 否则换主题、作废旧时间线后,旧片会继续被界面误标为当前交付。
"latest_export": next(({"id": e.id, "url": storage.url_for(e.path), "path": e.path, "preset": e.preset,
"duration_ms": e.duration_ms, "size_bytes": e.size_bytes}
for e in exports if e.id == current_export_id), None),
"cost": cost,
}
def current_timeline(db: Session, project: Project) -> Timeline | None:
return db.scalars(select(Timeline).where(Timeline.project_id == project.id, Timeline.is_current.is_(True))
.order_by(Timeline.id.desc())).first()
# ---------------------------------------------------------------- 流程进度(图2)
def workflow_of(db: Session, project: Project) -> list[dict]:
shots = list(project.shots or [])
total = len(shots)
tasks = list(db.scalars(select(Task).where(Task.project_id == project.id)).all())
gen_tasks = [t for t in tasks if t.kind == TaskKind.GENERATE_SHOT]
shot_status = {s.id: s.status for s in shots}
shot_ids = {s.id for s in shots}
ready = sum(1 for s in shots if s.status in (ShotStatus.READY, *ShotStatus.ACTIVE,
ShotStatus.GENERATED, ShotStatus.QC_PASSED,
ShotStatus.APPROVED))
generated = sum(1 for s in shots if s.output_path and storage.exists(s.output_path))
qc_done = sum(1 for s in shots if s.qc_json)
reviewed = sum(1 for s in shots if s.status in (ShotStatus.APPROVED, ShotStatus.REJECTED))
approved = sum(1 for s in shots if s.status == ShotStatus.APPROVED)
rejected = sum(1 for s in shots if s.status == ShotStatus.REJECTED)
assets_bound = sum(1 for s in shots if (s.characters or s.scenes or s.voice_asset_id
or s.speaker_character_id))
timeline = current_timeline(db, project)
export = (db.scalars(select(ExportRecord).where(
ExportRecord.project_id == project.id,
ExportRecord.timeline_id == timeline.id,
).order_by(ExportRecord.id.desc())).first() if timeline else None)
succeeded = [t for t in tasks if t.status == TaskStatus.SUCCEEDED]
active = [t for t in tasks if t.status in TaskStatus.ACTIVE]
failed = [t for t in tasks if t.status == TaskStatus.FAILED]
retried = sum(1 for t in tasks if (t.attempts or 0) > 1)
def node(key: str, state: str, detail: str, *, done: int | None = None, of: int | None = None) -> dict:
# done 永远不应超过 total(驳回重做会让同一镜头有多条任务,曾出现 5/4)
if done is not None and of is not None:
done = min(done, of)
return {"key": key, "label": STAGE_LABELS.get(key, key), "state": state, "detail": detail,
"done": done, "total": of}
title = project.name if project.name.startswith("《") else f"《{project.name}》" # 避免《《…》》
nodes: list[dict] = [node("create_project", "done", f"{title}已创建")]
nodes.append(node("import_storyboard", "done" if total else "pending",
f"{total} 个分镜" if total else "尚未导入分镜与脚本", done=total, of=max(total, 1)))
nodes.append(node("bind_assets", "done" if total and assets_bound == total else
("active" if assets_bound else "pending"),
f"{assets_bound}/{total} 个镜头已绑定人物、场景或声音资产" if total else "等待分镜",
done=assets_bound, of=max(total, 1)))
# 与 dispatch 节点一致,按「镜头」而不是「任务」计数,
# 否则一个镜头有重试任务时会出现 5/4 这种分母错误。
planned = len({t.shot_id for t in gen_tasks if t.shot_id} & shot_ids)
nodes.append(node("plan_tasks", "done" if total and planned >= total else
("active" if planned or gen_tasks else "pending"),
(f"{planned}/{total} 个镜头已排入任务清单(共 {len(gen_tasks)} 条生成任务)"
if gen_tasks else "尚未生成任务清单"),
done=planned, of=max(total, 1)))
nodes.append(node("preflight", "done" if total and ready == total else ("active" if total else "pending"),
f"{ready}/{total} 个镜头通过素材与参数检查" if total else "等待分镜",
done=ready, of=max(total, 1)))
# 注意:一个镜头驳回重做后会有多条生成任务,这里按「镜头」而非「任务」计数,
# 否则驳回过一次的项目永远停在 active。
dispatched_ids = {t.shot_id for t in gen_tasks
if t.shot_id and t.status in (*TaskStatus.ACTIVE, TaskStatus.SUCCEEDED,
TaskStatus.SKIPPED)}
dispatched = len(dispatched_ids & shot_ids)
nodes.append(node("dispatch", "done" if total and dispatched == total else
("active" if dispatched else "pending"),
f"{dispatched}/{total} 个镜头的任务已进入队列并被调度({len(gen_tasks)} 条任务)",
done=dispatched, of=max(total, 1)))
running = sum(1 for t in active)
gen_failed = sum(1 for t in failed if t.kind == TaskKind.GENERATE_SHOT)
failed_shots = sum(1 for s in shots if s.status == ShotStatus.FAILED)
# generated 是按「产物文件是否存在」计数的,重新生成失败时旧产物仍留在磁盘上,
# 于是会出现「生成成功 4/4」与「1 个生成任务失败」并存、失败被完全掩盖的情况。
# 因此把已落败的镜头从「可用产物」里扣除:只要还有镜头停在 failed,
# 节点8/9 就必须转红(图2 的两条回路要在界面上看得见)。
stale = sum(1 for s in shots if s.status == ShotStatus.FAILED
and s.output_path and storage.exists(s.output_path))
usable = max(0, generated - stale)
gen_broken = bool(gen_failed) and (failed_shots > 0 or usable < total)
gen_state = ("failed" if gen_broken else
("done" if total and usable == total else ("active" if dispatched else "pending")))
nodes.append(node("generate", gen_state,
f"生成成功 {usable}/{total}" + (f",{running} 个任务执行中" if running else "")
+ (f",{gen_failed} 个生成任务失败" if gen_failed else ""),
done=usable, of=max(total, 1)))
nodes.append(node("save_output", "failed" if gen_broken else
("done" if total and usable == total else ("active" if usable else "pending")),
f"{usable} 个镜头产物已下载保存" + (f",{len(failed)} 个任务失败" if failed else ""),
done=usable, of=max(usable, 1)))
nodes.append(node("quality_check", "done" if total and qc_done >= generated and generated else
("active" if generated else "pending"),
f"{qc_done}/{generated} 个镜头完成自动质检" if generated else "等待生成产物",
done=qc_done, of=max(generated, 1)))
nodes.append(node("review", "done" if total and approved == total else
("active" if reviewed or rejected else "pending"),
f"{approved}/{total} 个镜头审核合格" + (f",{rejected} 个需返工" if rejected else ""),
done=approved, of=max(total, 1)))
if rejected:
nodes.append(node("revise", "active", f"{rejected} 个镜头被驳回,需调整提示词或重新生成"))
segs = len(timeline.segments or []) if timeline else 0
nodes.append(node("compose", "done" if timeline and segs and segs == approved and approved else
("active" if timeline else "pending"),
(f"时间线「{timeline.name}」已排布 {segs} 个片段"
f"、{len(timeline.subtitles or [])} 条字幕、{len(timeline.audio_clips or [])} 条音轨")
if timeline else "尚未建立时间线", done=segs, of=max(approved, 1)))
delivered = bool(export and export.status == "succeeded" and export.path)
nodes.append(node("export", "done" if delivered else ("active" if export else "pending"),
(f"当前成片:{export.preset},{(export.duration_ms or 0) / 1000:.1f}s")
if delivered else ("当前时间线正在导出/导出失败" if export else "尚未导出当前时间线成片")))
return nodes
# ---------------------------------------------------------------- 状态回写
def refresh(db: Session, project: Project, *, commit: bool = False) -> dict:
"""按镜头与任务的真实状态回写项目状态与当前流程节点。"""
shots = list(project.shots or [])
total = len(shots)
stats = stats_of(db, project)
tasks = list(db.scalars(select(Task).where(Task.project_id == project.id)).all())
active = [t for t in tasks if t.status in TaskStatus.ACTIVE]
approved = sum(1 for s in shots if s.status == ShotStatus.APPROVED)
rejected = sum(1 for s in shots if s.status == ShotStatus.REJECTED)
generated = sum(1 for s in shots if s.output_path and storage.exists(s.output_path))
failed = sum(1 for s in shots if s.status == ShotStatus.FAILED)
timeline = current_timeline(db, project)
export = (db.scalars(select(ExportRecord).where(
ExportRecord.project_id == project.id,
ExportRecord.timeline_id == timeline.id,
).order_by(ExportRecord.id.desc())).first() if timeline else None)
if project.status == ProjectStatus.ARCHIVED:
stage = project.current_stage
elif total == 0:
project.status = ProjectStatus.DRAFT
stage = "create_project"
elif approved == total:
if export and export.status == "succeeded" and export.path:
project.status = ProjectStatus.DELIVERED
stage = "export"
elif timeline:
project.status = ProjectStatus.COMPOSING
stage = "compose"
else:
project.status = ProjectStatus.COMPOSING
stage = "compose"
elif active:
project.status = ProjectStatus.GENERATING
stage = "generate"
elif rejected:
project.status = ProjectStatus.REVIEWING
stage = "revise"
elif generated and (stats["shot_status"].get(ShotStatus.GENERATED, 0)
or stats["shot_status"].get(ShotStatus.QC_PASSED, 0)
or stats["shot_status"].get(ShotStatus.APPROVED, 0)):
project.status = ProjectStatus.REVIEWING
stage = "review" if stats["shot_status"].get(ShotStatus.QC_PASSED, 0) or approved else "quality_check"
elif failed and generated == 0:
project.status = ProjectStatus.GENERATING
stage = "dispatch"
elif any(s.status in ShotStatus.ACTIVE for s in shots):
project.status = ProjectStatus.GENERATING
stage = "dispatch"
elif any(s.status == ShotStatus.READY for s in shots) and tasks:
project.status = ProjectStatus.GENERATING
stage = "dispatch"
elif any(s.status == ShotStatus.READY for s in shots):
project.status = ProjectStatus.STORYBOARD
stage = "plan_tasks"
else:
bound = sum(1 for s in shots if (s.characters or s.scenes or s.voice_asset_id
or s.speaker_character_id))
project.status = ProjectStatus.BINDING if bound else ProjectStatus.STORYBOARD
stage = "bind_assets" if bound else "import_storyboard"
project.current_stage = stage
db.flush()
if commit:
db.commit()
return {"status": project.status, "current_stage": stage, "stats": stats,
"workflow": workflow_of(db, project)}
# ---------------------------------------------------------------- 队列视图
def task_counts(db: Session, *, project_id: int | None = None) -> dict:
stmt = select(Task.status, func.count(Task.id)).group_by(Task.status)
if project_id is not None:
stmt = stmt.where(Task.project_id == project_id)
return {s: int(c) for s, c in db.execute(stmt).all()}
def timeline_dict(timeline: Timeline) -> dict:
# 关系集合带 order_by,但同一 Session 内改过 order_index/start_ms 后不会自动重排,
# 因此这里显式排序,保证接口返回的片段顺序与时间线真实顺序一致。
segments = sorted(timeline.segments or [], key=lambda s: (int(s.order_index or 0), int(s.id or 0)))
cues = sorted(timeline.subtitles or [], key=lambda c: (int(c.start_ms or 0), int(c.id or 0)))
clips = sorted(timeline.audio_clips or [], key=lambda a: (int(a.start_ms or 0), int(a.id or 0)))
# 每个片段的台词到底用谁的声音,前端要在片段条上直接标出来,
# 否则用户只能靠导出以后听声音反推「定妆照和音色到底生效了没有」。
native_shots = {int(a.shot_id) for a in clips if getattr(a, "native_audio", False) and a.shot_id}
from app.services import timeline as timeline_service
conf = timeline.settings_json or {}
# 老时间线的 settings 里压根没有 voice_source 这个键,直接回传原始字典会让前端的
# 「台词语音来源」显示成空白,用户以为没保存 —— 一律回传真正生效的值。
settings_out = {**conf, "voice_source": timeline_service.voice_source_mode(conf)}
return {
"id": timeline.id, "project_id": timeline.project_id, "name": timeline.name, "version": timeline.version,
"is_current": timeline.is_current, "fps": timeline.fps, "width": timeline.width,
"height": timeline.height, "sample_rate": timeline.sample_rate,
"total_duration_ms": timeline.total_duration_ms, "settings": settings_out,
"voice_source": settings_out["voice_source"],
"native_voice_shots": sorted(native_shots),
"created_at": timeline.created_at, "updated_at": timeline.updated_at,
"segments": [{"id": s.id, "shot_id": s.shot_id, "order_index": s.order_index, "label": s.label,
"source_path": s.source_path, "source_url": storage.url_for(s.source_path),
"start_ms": s.start_ms, "duration_ms": s.duration_ms,
"source_in_ms": s.source_in_ms, "source_out_ms": s.source_out_ms,
"transition_out": s.transition_out, "transition_duration_ms": s.transition_duration_ms,
"volume": s.volume, "muted": s.muted,
"voice_source": "native" if int(s.shot_id or 0) in native_shots else "dub"}
for s in segments],
"subtitles": [{"id": c.id, "shot_id": c.shot_id, "start_ms": c.start_ms, "end_ms": c.end_ms,
"text": c.text, "speaker": c.speaker, "emphasis": c.emphasis}
for c in cues],
"audio_clips": [{"id": a.id, "shot_id": a.shot_id, "kind": a.kind, "label": a.label,
"source_path": a.source_path, "source_url": storage.url_for(a.source_path),
"start_ms": a.start_ms, "duration_ms": a.duration_ms, "gain_db": a.gain_db,
"loop": a.loop, "text": a.text, "ducking": a.ducking,
"voice_asset_id": a.voice_asset_id,
"native_audio": bool(getattr(a, "native_audio", False))} for a in clips],
}