Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Paid, hosted product at menivor.com, now retired as a company project. Fresh squashed history of the cleaned working tree (secret-scanned, no .env, no customer data, no outreach logs). Built on MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
"""TemplateInstance repository — record instantiations + genre analytics.
|
|
|
|
Records each template→reel instantiation and answers the analytics the growth
|
|
team needs: which genres creators actually run (``popular_templates``) and a
|
|
creator's own history (``list_for_user``). Session-injected; the pure templates
|
|
service stays pure — the router calls ``record`` after a successful instantiate.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from sqlalchemy import func
|
|
|
|
from app.models.db.templates import TemplateInstance
|
|
|
|
|
|
class TemplateInstanceRepository:
|
|
"""CRUD + analytics for ``template_instances``."""
|
|
|
|
def __init__(self, session: Any) -> None:
|
|
self._s = session
|
|
|
|
def record(
|
|
self, *, user_id: str, template_slug: str, template_version: str,
|
|
inputs: dict, duration_s: float, reel_job_id: str = "",
|
|
) -> TemplateInstance:
|
|
row = TemplateInstance(
|
|
user_id=user_id, template_slug=template_slug, template_version=template_version,
|
|
inputs=dict(inputs or {}), duration_s=int(round(duration_s)), reel_job_id=reel_job_id,
|
|
)
|
|
self._s.add(row)
|
|
self._s.flush()
|
|
return row
|
|
|
|
def get(self, instance_id: str) -> TemplateInstance | None:
|
|
return self._s.get(TemplateInstance, instance_id)
|
|
|
|
def list_for_user(self, user_id: str, *, limit: int = 50) -> list[TemplateInstance]:
|
|
return (
|
|
self._s.query(TemplateInstance)
|
|
.filter(TemplateInstance.user_id == user_id)
|
|
.order_by(TemplateInstance.created_at.desc())
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
|
|
def popular_templates(self, *, limit: int = 10) -> list[dict]:
|
|
"""Most-instantiated templates (slug, count) — genre conversion analytics."""
|
|
rows = (
|
|
self._s.query(
|
|
TemplateInstance.template_slug,
|
|
func.count(TemplateInstance.id).label("count"),
|
|
)
|
|
.group_by(TemplateInstance.template_slug)
|
|
.order_by(func.count(TemplateInstance.id).desc())
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
return [{"template_slug": slug, "count": int(count)} for slug, count in rows]
|