analytics_models.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from datetime import date, datetime
  2. from sqlalchemy import BigInteger, Date, Index, Numeric, String
  3. from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
  4. from app.infrastructure.mysql.types import UTCDateTime
  5. class AnalyticsBase(DeclarativeBase):
  6. pass
  7. class DimDateRecord(AnalyticsBase):
  8. __tablename__ = "dim_date"
  9. date_key: Mapped[int] = mapped_column(primary_key=True)
  10. full_date: Mapped[date] = mapped_column(Date, unique=True, nullable=False)
  11. year: Mapped[int] = mapped_column(nullable=False)
  12. month: Mapped[int] = mapped_column(nullable=False)
  13. day: Mapped[int] = mapped_column(nullable=False)
  14. class DimProductRecord(AnalyticsBase):
  15. __tablename__ = "dim_product"
  16. id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
  17. product_id: Mapped[str] = mapped_column(String(26), nullable=False)
  18. product_version_id: Mapped[str] = mapped_column(String(26), unique=True, nullable=False)
  19. product_code: Mapped[str] = mapped_column(String(32), nullable=False)
  20. product_name: Mapped[str] = mapped_column(String(128), nullable=False)
  21. version_no: Mapped[str] = mapped_column(String(32), nullable=False)
  22. class DimSalespersonRecord(AnalyticsBase):
  23. __tablename__ = "dim_salesperson"
  24. id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
  25. salesperson_id: Mapped[str] = mapped_column(String(26), unique=True, nullable=False)
  26. salesperson_code: Mapped[str] = mapped_column(String(32), nullable=False)
  27. display_name: Mapped[str] = mapped_column(String(64), nullable=False)
  28. organization_code: Mapped[str] = mapped_column(String(64), nullable=False)
  29. class FactOrderRecord(AnalyticsBase):
  30. __tablename__ = "fact_orders"
  31. __table_args__ = (Index("ix_fact_orders_date_product", "date_key", "product_key"),)
  32. order_id: Mapped[str] = mapped_column(String(26), primary_key=True)
  33. date_key: Mapped[int] = mapped_column(nullable=False)
  34. product_key: Mapped[int] = mapped_column(BigInteger, nullable=False)
  35. salesperson_key: Mapped[int | None] = mapped_column(BigInteger)
  36. status: Mapped[str] = mapped_column(String(32), nullable=False)
  37. premium_cents: Mapped[int] = mapped_column(BigInteger, nullable=False)
  38. created_at: Mapped[datetime] = mapped_column(UTCDateTime(), nullable=False)
  39. class FactPolicyRecord(AnalyticsBase):
  40. __tablename__ = "fact_policies"
  41. policy_id: Mapped[str] = mapped_column(String(26), primary_key=True)
  42. order_id: Mapped[str] = mapped_column(String(26), unique=True, nullable=False)
  43. date_key: Mapped[int] = mapped_column(nullable=False)
  44. product_key: Mapped[int] = mapped_column(BigInteger, nullable=False)
  45. status: Mapped[str] = mapped_column(String(32), nullable=False)
  46. premium_cents: Mapped[int] = mapped_column(BigInteger, nullable=False)
  47. class MetricDefinitionRecord(AnalyticsBase):
  48. __tablename__ = "metric_definitions"
  49. metric_code: Mapped[str] = mapped_column(String(64), primary_key=True)
  50. name: Mapped[str] = mapped_column(String(128), nullable=False)
  51. description: Mapped[str] = mapped_column(String(500), nullable=False)
  52. unit: Mapped[str] = mapped_column(String(16), nullable=False)
  53. expression: Mapped[str] = mapped_column(String(1000), nullable=False)
  54. warning_threshold: Mapped[float | None] = mapped_column(Numeric(18, 4))
  55. class AnalyticsSyncOffsetRecord(AnalyticsBase):
  56. __tablename__ = "analytics_sync_offsets"
  57. source_name: Mapped[str] = mapped_column(String(64), primary_key=True)
  58. last_event_id: Mapped[str | None] = mapped_column(String(26))
  59. last_occurred_at: Mapped[datetime | None] = mapped_column(UTCDateTime())
  60. updated_at: Mapped[datetime] = mapped_column(UTCDateTime(), nullable=False)