caelum0x d56639a594
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
Menivor — open source release (MIT), free to use and fork
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.
2026-12-04 02:10:26 +03:00

74 lines
2.6 KiB
Python

"""Funnel read-out routes (MR1).
* ``GET /api/v1/admin/funnel/readout?since=YYYY-MM-DD`` - staff only (403
otherwise; 404 for non-staff under the production allowlist).
* ``POST /api/v1/funnel/attribution`` - the signed-in user's first-touch UTM
and referrer, sent once by the web after sign-up (first touch wins).
"""
from __future__ import annotations
from datetime import datetime, timezone
from typing import Any
from fastapi import Body, Query, Request
from pydantic import BaseModel, Field
from app.controllers import base
from app.controllers.v1.base import new_router
from app.controllers.v1.jobs import _require_db, _resolve_user, get_session
from app.domains.funnel_readout import service
from app.models.exception import HttpException
from app.utils import utils
router = new_router()
class AttributionRequest(BaseModel):
utm_source: str = Field("", max_length=120)
utm_medium: str = Field("", max_length=120)
utm_campaign: str = Field("", max_length=120)
referrer: str = Field("", max_length=300)
def _parse_since(raw: str, request_id: str) -> datetime:
if not raw:
return service.DEFAULT_SINCE
try:
return datetime.strptime(raw, "%Y-%m-%d").replace(tzinfo=timezone.utc)
except ValueError as exc:
raise HttpException(task_id=request_id, status_code=422, message="since must be YYYY-MM-DD") from exc
@router.get("/admin/funnel/readout", tags=["Admin"], summary="Funnel read-out per user (staff)")
def funnel_readout(request: Request, since: str = Query("", max_length=10)) -> Any:
request_id = base.get_task_id(request)
start = _parse_since(since, request_id)
_require_db(request_id)
with get_session() as session:
user = _resolve_user(request, session, request_id)
if not bool(getattr(user, "is_staff", False)):
raise HttpException(task_id=request_id, status_code=403, message="staff access required")
data = service.readout_payload(session, start)
return utils.get_response(200, data)
@router.post("/funnel/attribution", summary="Record the first-touch source (once)")
def funnel_attribution(request: Request, body: AttributionRequest = Body(...)) -> Any:
request_id = base.get_task_id(request)
_require_db(request_id)
with get_session() as session:
user = _resolve_user(request, session, request_id)
written = service.record_attribution(
session,
str(user.id),
utm_source=body.utm_source,
utm_medium=body.utm_medium,
utm_campaign=body.utm_campaign,
referrer=body.referrer,
)
return utils.get_response(200, {"recorded": written})
__all__ = ["router"]