TorbatYar/backend/services/experience/app/models/analytics.py
Mortezakoohjani 203671a7bf feat(experience): ship Experience Platform phases 11.0-11.10
Add the experience service with sites through analytics/AI hooks, migrations through 0011, and phase docs/manifests marking the track complete.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-27 11:43:10 +03:30

110 lines
3.4 KiB
Python

"""Analytics aggregates — Phase 11.10.
Report definitions describe which local metric keys to compute; snapshots
store point-in-time metrics payloads from simple local SQL counts only.
"""
from __future__ import annotations
import uuid
from sqlalchemy import Index, String, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.types import JSON
from app.core.database import Base
from app.models.base import (
ActorAuditMixin,
OptimisticLockMixin,
SoftDeleteMixin,
TenantMixin,
TimestampMixin,
UUIDPrimaryKeyMixin,
)
from app.models.types import AnalyticsReportStatus, AnalyticsSnapshotStatus, GUID
class ExperienceAnalyticsReportDefinition(
Base,
UUIDPrimaryKeyMixin,
TenantMixin,
TimestampMixin,
SoftDeleteMixin,
ActorAuditMixin,
OptimisticLockMixin,
):
__tablename__ = "experience_analytics_report_definitions"
workspace_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
site_id: Mapped[uuid.UUID | None] = mapped_column(GUID(), nullable=True)
code: Mapped[str] = mapped_column(String(50), nullable=False)
name: Mapped[str] = mapped_column(String(255), nullable=False)
metric_keys: Mapped[list] = mapped_column(JSON, nullable=False)
filters: Mapped[dict | None] = mapped_column(JSON, nullable=True)
status: Mapped[AnalyticsReportStatus] = mapped_column(
default=AnalyticsReportStatus.DRAFT, nullable=False
)
metadata_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)
__table_args__ = (
UniqueConstraint(
"tenant_id",
"code",
name="uq_experience_analytics_report_definitions_tenant_code",
),
Index(
"ix_experience_analytics_report_definitions_workspace",
"tenant_id",
"workspace_id",
),
Index(
"ix_experience_analytics_report_definitions_status",
"tenant_id",
"status",
),
Index(
"ix_experience_analytics_report_definitions_tenant_deleted",
"tenant_id",
"is_deleted",
),
)
class ExperienceAnalyticsSnapshot(
Base,
UUIDPrimaryKeyMixin,
TenantMixin,
TimestampMixin,
SoftDeleteMixin,
ActorAuditMixin,
OptimisticLockMixin,
):
__tablename__ = "experience_analytics_snapshots"
report_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
workspace_id: Mapped[uuid.UUID | None] = mapped_column(GUID(), nullable=True)
site_id: Mapped[uuid.UUID | None] = mapped_column(GUID(), nullable=True)
period_start: Mapped[str | None] = mapped_column(String(32), nullable=True)
period_end: Mapped[str | None] = mapped_column(String(32), nullable=True)
metrics: Mapped[dict] = mapped_column(JSON, nullable=False)
status: Mapped[AnalyticsSnapshotStatus] = mapped_column(
default=AnalyticsSnapshotStatus.ACTIVE, nullable=False
)
__table_args__ = (
Index(
"ix_experience_analytics_snapshots_report",
"tenant_id",
"report_id",
),
Index(
"ix_experience_analytics_snapshots_workspace",
"tenant_id",
"workspace_id",
),
Index(
"ix_experience_analytics_snapshots_tenant_deleted",
"tenant_id",
"is_deleted",
),
)