feat: track generation jobs
This commit is contained in:
@@ -83,11 +83,88 @@ class Story(Base):
|
||||
child_profile: Mapped["ChildProfile | None"] = relationship("ChildProfile")
|
||||
story_universe: Mapped["StoryUniverse | None"] = relationship("StoryUniverse")
|
||||
|
||||
@property
|
||||
def retryable_assets(self) -> list[str]:
|
||||
"""Assets that can be completed or retried from the current persisted state."""
|
||||
|
||||
assets: list[str] = []
|
||||
|
||||
image_is_busy_or_ready = self.image_status in {"ready", "generating"}
|
||||
if not image_is_busy_or_ready:
|
||||
if self.mode == "storybook":
|
||||
pages = self.pages or []
|
||||
has_missing_page_image = any(
|
||||
isinstance(page, dict)
|
||||
and page.get("image_prompt")
|
||||
and not page.get("image_url")
|
||||
for page in pages
|
||||
)
|
||||
if (self.cover_prompt and not self.image_url) or has_missing_page_image:
|
||||
assets.append("image")
|
||||
elif self.cover_prompt:
|
||||
assets.append("image")
|
||||
|
||||
audio_is_busy_or_ready = self.audio_status in {"ready", "generating"}
|
||||
if self.story_text and not audio_is_busy_or_ready:
|
||||
assets.append("audio")
|
||||
|
||||
return assets
|
||||
|
||||
|
||||
def _uuid() -> str:
|
||||
return str(uuid4())
|
||||
|
||||
|
||||
class GenerationJob(Base):
|
||||
"""User-visible generation attempt that can be inspected after the request returns."""
|
||||
|
||||
__tablename__ = "generation_jobs"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=_uuid)
|
||||
user_id: Mapped[str] = mapped_column(
|
||||
String(255), ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True
|
||||
)
|
||||
story_id: Mapped[int | None] = mapped_column(
|
||||
Integer, ForeignKey("stories.id", ondelete="SET NULL"), nullable=True, index=True
|
||||
)
|
||||
output_mode: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
input_type: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
status: Mapped[str] = mapped_column(String(32), nullable=False, default="running", index=True)
|
||||
current_step: Mapped[str] = mapped_column(
|
||||
String(64), nullable=False, default="request_accepted"
|
||||
)
|
||||
request_payload: Mapped[dict] = mapped_column(JSON, default=dict)
|
||||
result_snapshot: Mapped[dict] = mapped_column(JSON, default=dict)
|
||||
error_message: Mapped[str | None] = mapped_column(Text)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), server_default=func.now(), index=True
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
|
||||
)
|
||||
|
||||
|
||||
class GenerationJobEvent(Base):
|
||||
"""Append-only event emitted by a generation job."""
|
||||
|
||||
__tablename__ = "generation_job_events"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||
job_id: Mapped[str] = mapped_column(
|
||||
String(36), ForeignKey("generation_jobs.id", ondelete="CASCADE"), nullable=False, index=True
|
||||
)
|
||||
story_id: Mapped[int | None] = mapped_column(
|
||||
Integer, ForeignKey("stories.id", ondelete="SET NULL"), nullable=True, index=True
|
||||
)
|
||||
event_type: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
status: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
message: Mapped[str | None] = mapped_column(Text)
|
||||
event_metadata: Mapped[dict] = mapped_column(JSON, default=dict)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), server_default=func.now(), index=True
|
||||
)
|
||||
|
||||
|
||||
class ChildProfile(Base):
|
||||
"""Child profile entity."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user