""" Telemetry events (4A). Store canonical product events for funnel KPIs: - Deal funnel: listing_view → inquiry_created → message_sent → listing_marked_sold - Yield funnel: yield_connected → yield_click → yield_conversion → payout_paid """ from __future__ import annotations from datetime import datetime from typing import Optional from sqlalchemy import Boolean, DateTime, ForeignKey, Index, Integer, String, Text from sqlalchemy.orm import Mapped, mapped_column from app.database import Base class TelemetryEvent(Base): __tablename__ = "telemetry_events" id: Mapped[int] = mapped_column(primary_key=True, index=True) # Who user_id: Mapped[Optional[int]] = mapped_column(ForeignKey("users.id"), nullable=True, index=True) # What event_name: Mapped[str] = mapped_column(String(60), nullable=False, index=True) # Entity links (optional) listing_id: Mapped[Optional[int]] = mapped_column(Integer, nullable=True, index=True) inquiry_id: Mapped[Optional[int]] = mapped_column(Integer, nullable=True, index=True) yield_domain_id: Mapped[Optional[int]] = mapped_column(Integer, nullable=True, index=True) click_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True, index=True) domain: Mapped[Optional[str]] = mapped_column(String(255), nullable=True, index=True) # Context source: Mapped[Optional[str]] = mapped_column(String(30), nullable=True) # "public" | "terminal" | "webhook" | "scheduler" | "admin" ip_hash: Mapped[Optional[str]] = mapped_column(String(64), nullable=True) user_agent: Mapped[Optional[str]] = mapped_column(String(500), nullable=True) referrer: Mapped[Optional[str]] = mapped_column(String(500), nullable=True) metadata_json: Mapped[Optional[str]] = mapped_column(Text, nullable=True) # JSON string # Flags is_authenticated: Mapped[Optional[bool]] = mapped_column(Boolean, nullable=True) created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, index=True) __table_args__ = ( Index("ix_telemetry_event_name_created", "event_name", "created_at"), Index("ix_telemetry_user_created", "user_id", "created_at"), Index("ix_telemetry_listing_created", "listing_id", "created_at"), Index("ix_telemetry_yield_created", "yield_domain_id", "created_at"), )