models.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. """Completely isolated persistence models for ``find_agent_v2``."""
  2. from __future__ import annotations
  3. from datetime import datetime
  4. from decimal import Decimal
  5. from sqlalchemy import BigInteger, ForeignKey, Index, Integer, Numeric, String, Text, UniqueConstraint, func
  6. from sqlalchemy.dialects.mysql import LONGTEXT
  7. from sqlalchemy.orm import Mapped, mapped_column
  8. from supply_infra.db.base import Base
  9. _LONG_TEXT = Text().with_variant(LONGTEXT(), "mysql")
  10. class FindAgentV2Run(Base):
  11. __tablename__ = "find_agent_v2_run"
  12. __table_args__ = (
  13. UniqueConstraint("run_id", name="uk_find_agent_v2_run_id"),
  14. Index("idx_find_agent_v2_run_status", "status"),
  15. Index("idx_find_agent_v2_run_demand", "demand_grade_id"),
  16. )
  17. id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
  18. run_id: Mapped[str] = mapped_column(String(64), nullable=False)
  19. demand_grade_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
  20. demand_word: Mapped[str] = mapped_column(String(256), nullable=False)
  21. input_json: Mapped[str] = mapped_column(_LONG_TEXT, nullable=False)
  22. rule_config_json: Mapped[str] = mapped_column(Text, nullable=False)
  23. status: Mapped[str] = mapped_column(String(24), nullable=False, default="running")
  24. outcome_status: Mapped[str | None] = mapped_column(String(24), nullable=True)
  25. current_round: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
  26. search_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
  27. candidate_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
  28. valid_primary_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
  29. intent_summary: Mapped[str | None] = mapped_column(Text, nullable=True)
  30. stop_reason: Mapped[str | None] = mapped_column(Text, nullable=True)
  31. obagent_run_uid: Mapped[str | None] = mapped_column(String(64), nullable=True)
  32. create_time: Mapped[datetime] = mapped_column(nullable=False, server_default=func.now())
  33. update_time: Mapped[datetime] = mapped_column(nullable=False, server_default=func.now(), onupdate=func.now())
  34. class FindAgentV2Round(Base):
  35. __tablename__ = "find_agent_v2_round"
  36. __table_args__ = (
  37. UniqueConstraint("run_id", "round_index", name="uk_find_agent_v2_round"),
  38. Index("idx_find_agent_v2_round_run", "run_id", "round_index"),
  39. )
  40. id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
  41. run_id: Mapped[str] = mapped_column(String(64), nullable=False)
  42. round_index: Mapped[int] = mapped_column(Integer, nullable=False)
  43. phase: Mapped[str] = mapped_column(String(24), nullable=False, default="planning")
  44. status: Mapped[str] = mapped_column(String(16), nullable=False, default="open")
  45. plan_json: Mapped[str | None] = mapped_column(_LONG_TEXT, nullable=True)
  46. start_snapshot_json: Mapped[str | None] = mapped_column(Text, nullable=True)
  47. end_snapshot_json: Mapped[str | None] = mapped_column(Text, nullable=True)
  48. error_message: Mapped[str | None] = mapped_column(Text, nullable=True)
  49. create_time: Mapped[datetime] = mapped_column(nullable=False, server_default=func.now())
  50. update_time: Mapped[datetime] = mapped_column(nullable=False, server_default=func.now(), onupdate=func.now())
  51. class FindAgentV2Search(Base):
  52. __tablename__ = "find_agent_v2_search"
  53. __table_args__ = (Index("idx_find_agent_v2_search_run", "run_id", "id"),)
  54. id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
  55. run_id: Mapped[str] = mapped_column(String(64), nullable=False)
  56. round_index: Mapped[int] = mapped_column(Integer, nullable=False)
  57. keyword: Mapped[str] = mapped_column(String(256), nullable=False)
  58. query_reason: Mapped[str] = mapped_column(Text, nullable=False)
  59. source_type: Mapped[str] = mapped_column(String(32), nullable=False)
  60. provider: Mapped[str] = mapped_column(String(32), nullable=False)
  61. cursor: Mapped[str] = mapped_column(String(128), nullable=False, default="0")
  62. page_no: Mapped[int] = mapped_column(Integer, nullable=False, default=1)
  63. has_more: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
  64. next_cursor: Mapped[str | None] = mapped_column(String(128), nullable=True)
  65. provider_state_json: Mapped[str | None] = mapped_column(Text, nullable=True)
  66. result_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
  67. status: Mapped[str] = mapped_column(String(16), nullable=False)
  68. error_message: Mapped[str | None] = mapped_column(Text, nullable=True)
  69. raw_response_json: Mapped[str | None] = mapped_column(_LONG_TEXT, nullable=True)
  70. create_time: Mapped[datetime] = mapped_column(nullable=False, server_default=func.now())
  71. class FindAgentV2Candidate(Base):
  72. __tablename__ = "find_agent_v2_candidate"
  73. __table_args__ = (
  74. UniqueConstraint("run_id", "aweme_id", name="uk_find_agent_v2_candidate_aweme"),
  75. Index("idx_find_agent_v2_candidate_bucket", "run_id", "decision_bucket"),
  76. )
  77. id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
  78. run_id: Mapped[str] = mapped_column(String(64), nullable=False)
  79. first_search_id: Mapped[int | None] = mapped_column(
  80. BigInteger,
  81. ForeignKey("find_agent_v2_search.id", name="fk_find_agent_v2_candidate_search"),
  82. nullable=True,
  83. )
  84. aweme_id: Mapped[str] = mapped_column(String(64), nullable=False)
  85. title: Mapped[str | None] = mapped_column(String(512), nullable=True)
  86. content_link: Mapped[str | None] = mapped_column(String(1024), nullable=True)
  87. author_name: Mapped[str | None] = mapped_column(String(256), nullable=True)
  88. author_sec_uid: Mapped[str | None] = mapped_column(String(256), nullable=True)
  89. source_keywords_json: Mapped[str | None] = mapped_column(Text, nullable=True)
  90. tags_json: Mapped[str | None] = mapped_column(Text, nullable=True)
  91. publish_at: Mapped[datetime | None] = mapped_column(nullable=True)
  92. duration_seconds: Mapped[Decimal | None] = mapped_column(Numeric(10, 3), nullable=True)
  93. play_count: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
  94. like_count: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
  95. comment_count: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
  96. collect_count: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
  97. share_count: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
  98. detail_json: Mapped[str | None] = mapped_column(_LONG_TEXT, nullable=True)
  99. portrait_json: Mapped[str | None] = mapped_column(_LONG_TEXT, nullable=True)
  100. detail_status: Mapped[str] = mapped_column(String(16), nullable=False, default="pending")
  101. portrait_status: Mapped[str] = mapped_column(String(16), nullable=False, default="pending")
  102. content_50_plus_ratio: Mapped[Decimal | None] = mapped_column(Numeric(8, 6), nullable=True)
  103. account_50_plus_ratio: Mapped[Decimal | None] = mapped_column(Numeric(8, 6), nullable=True)
  104. relevance_score: Mapped[Decimal | None] = mapped_column(Numeric(8, 6), nullable=True)
  105. elder_score: Mapped[Decimal | None] = mapped_column(Numeric(8, 6), nullable=True)
  106. share_score: Mapped[Decimal | None] = mapped_column(Numeric(8, 6), nullable=True)
  107. value_score: Mapped[Decimal | None] = mapped_column(Numeric(8, 6), nullable=True)
  108. gate_status: Mapped[str | None] = mapped_column(String(16), nullable=True)
  109. gate_result_json: Mapped[str | None] = mapped_column(Text, nullable=True)
  110. decision_bucket: Mapped[str] = mapped_column(String(24), nullable=False, default="pending_evaluation")
  111. decision_reason: Mapped[str | None] = mapped_column(Text, nullable=True)
  112. reject_reason_code: Mapped[str | None] = mapped_column(String(64), nullable=True)
  113. create_time: Mapped[datetime] = mapped_column(nullable=False, server_default=func.now())
  114. update_time: Mapped[datetime] = mapped_column(nullable=False, server_default=func.now(), onupdate=func.now())
  115. class FindAgentV2Evidence(Base):
  116. __tablename__ = "find_agent_v2_evidence"
  117. __table_args__ = (Index("idx_find_agent_v2_evidence_subject", "run_id", "candidate_id", "id"),)
  118. id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
  119. run_id: Mapped[str] = mapped_column(String(64), nullable=False)
  120. candidate_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
  121. evidence_type: Mapped[str] = mapped_column(String(32), nullable=False)
  122. provider: Mapped[str] = mapped_column(String(32), nullable=False)
  123. status: Mapped[str] = mapped_column(String(16), nullable=False)
  124. raw_json: Mapped[str | None] = mapped_column(_LONG_TEXT, nullable=True)
  125. normalized_json: Mapped[str | None] = mapped_column(_LONG_TEXT, nullable=True)
  126. error_message: Mapped[str | None] = mapped_column(Text, nullable=True)
  127. create_time: Mapped[datetime] = mapped_column(nullable=False, server_default=func.now())