video_discovery.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. from __future__ import annotations
  2. from datetime import datetime
  3. from decimal import Decimal
  4. from sqlalchemy import (
  5. BigInteger,
  6. ForeignKey,
  7. Index,
  8. Integer,
  9. Numeric,
  10. String,
  11. Text,
  12. UniqueConstraint,
  13. func,
  14. )
  15. from sqlalchemy.orm import Mapped, mapped_column
  16. from supply_infra.db.base import Base
  17. class VideoDiscoveryRun(Base):
  18. """一次需求找片运行,保存输入、意图解释和完成状态。"""
  19. __tablename__ = "video_discovery_run"
  20. __table_args__ = (
  21. UniqueConstraint("run_id", name="uk_video_discovery_run_id"),
  22. UniqueConstraint(
  23. "biz_dt",
  24. "demand_grade_id",
  25. name="uk_video_discovery_run_biz_grade",
  26. ),
  27. Index("idx_video_discovery_run_demand", "demand_word"),
  28. Index("idx_video_discovery_run_grade", "demand_grade_id"),
  29. Index("idx_video_discovery_run_biz_dt", "biz_dt"),
  30. Index("idx_video_discovery_run_status", "status"),
  31. )
  32. id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
  33. run_id: Mapped[str] = mapped_column(String(64), nullable=False, comment="Agent 运行标识")
  34. biz_dt: Mapped[str | None] = mapped_column(
  35. String(32), nullable=True, comment="业务日 YYYYMMDD"
  36. )
  37. demand_grade_id: Mapped[int | None] = mapped_column(
  38. BigInteger, nullable=True, comment="可选 demand_grade.id"
  39. )
  40. demand_word: Mapped[str] = mapped_column(
  41. String(256), nullable=False, comment="用户给定需求词"
  42. )
  43. seed_video_id: Mapped[str | None] = mapped_column(
  44. String(64), nullable=True, comment="参考视频 id"
  45. )
  46. seed_video_title: Mapped[str | None] = mapped_column(
  47. String(512), nullable=True, comment="参考视频标题"
  48. )
  49. relevant_points_json: Mapped[str] = mapped_column(
  50. Text, nullable=False, comment="与需求相关的视频点位 JSON"
  51. )
  52. intent_summary: Mapped[str | None] = mapped_column(
  53. Text, nullable=True, comment="Agent 对真实内容意图的解释"
  54. )
  55. status: Mapped[str] = mapped_column(
  56. String(24), nullable=False, default="running", comment="running / finished / failed"
  57. )
  58. search_count: Mapped[int] = mapped_column(
  59. Integer, nullable=False, default=0, comment="已保存搜索页数"
  60. )
  61. primary_count: Mapped[int] = mapped_column(
  62. Integer, nullable=False, default=0, comment="主推荐数"
  63. )
  64. stop_reason: Mapped[str | None] = mapped_column(
  65. Text, nullable=True, comment="停止搜索的证据或失败原因"
  66. )
  67. create_time: Mapped[datetime] = mapped_column(
  68. nullable=False, server_default=func.now(), comment="创建时间"
  69. )
  70. update_time: Mapped[datetime] = mapped_column(
  71. nullable=False,
  72. server_default=func.now(),
  73. onupdate=func.now(),
  74. comment="更新时间",
  75. )
  76. class VideoDiscoverySearch(Base):
  77. """关键词探索图中的一次具体搜索页。"""
  78. __tablename__ = "video_discovery_search"
  79. __table_args__ = (
  80. Index("idx_video_discovery_search_run", "run_id", "id"),
  81. Index(
  82. "idx_video_discovery_search_parent",
  83. "run_id",
  84. "parent_search_id",
  85. ),
  86. Index("idx_video_discovery_search_keyword", "keyword"),
  87. )
  88. id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
  89. run_id: Mapped[str] = mapped_column(String(64), nullable=False, comment="发现运行 run_id")
  90. search_key: Mapped[str] = mapped_column(
  91. String(64), nullable=False, comment="搜索参数哈希,仅用于追踪,不作为幂等键"
  92. )
  93. keyword: Mapped[str] = mapped_column(
  94. String(256), nullable=False, comment="Agent 自主确定的搜索词"
  95. )
  96. query_reason: Mapped[str] = mapped_column(
  97. Text, nullable=False, comment="为何形成该搜索词、希望验证什么"
  98. )
  99. source_type: Mapped[str] = mapped_column(
  100. String(32),
  101. nullable=False,
  102. comment="demand / seed / point / tag / author / pagination / mixed",
  103. )
  104. source_value: Mapped[str | None] = mapped_column(
  105. Text, nullable=True, comment="来源点位、标签或父关键词"
  106. )
  107. parent_search_id: Mapped[int | None] = mapped_column(
  108. BigInteger, nullable=True, comment="由哪次搜索扩展而来"
  109. )
  110. provider: Mapped[str] = mapped_column(
  111. String(32),
  112. nullable=False,
  113. default="internal_keyword",
  114. comment="internal_keyword / tikhub / internal_blogger",
  115. )
  116. provider_state_json: Mapped[str | None] = mapped_column(
  117. Text, nullable=True, comment="供应方分页状态,如 search_id/backtrace"
  118. )
  119. content_type: Mapped[str] = mapped_column(
  120. String(16), nullable=False, default="视频", comment="搜索内容类型"
  121. )
  122. sort_type: Mapped[str] = mapped_column(
  123. String(32), nullable=False, default="综合排序", comment="搜索排序"
  124. )
  125. publish_time: Mapped[str] = mapped_column(
  126. String(32), nullable=False, default="不限", comment="发布时间筛选"
  127. )
  128. cursor: Mapped[str] = mapped_column(
  129. String(128), nullable=False, default="0", comment="本页游标"
  130. )
  131. page_no: Mapped[int] = mapped_column(
  132. Integer, nullable=False, default=1, comment="该关键词的页码"
  133. )
  134. results_count: Mapped[int] = mapped_column(
  135. Integer, nullable=False, default=0, comment="本页结果数"
  136. )
  137. new_candidate_count: Mapped[int] = mapped_column(
  138. Integer, nullable=False, default=0, comment="本页新增候选数"
  139. )
  140. has_more: Mapped[int] = mapped_column(
  141. Integer, nullable=False, default=0, comment="接口是否有下一页"
  142. )
  143. next_cursor: Mapped[str | None] = mapped_column(
  144. String(128), nullable=True, comment="下一页游标"
  145. )
  146. result_ids_json: Mapped[str | None] = mapped_column(
  147. Text, nullable=True, comment="本页 aweme_id 列表 JSON"
  148. )
  149. status: Mapped[str] = mapped_column(
  150. String(16), nullable=False, default="success", comment="success / failed"
  151. )
  152. error_message: Mapped[str | None] = mapped_column(
  153. Text, nullable=True, comment="搜索失败信息"
  154. )
  155. create_time: Mapped[datetime] = mapped_column(
  156. nullable=False, server_default=func.now(), comment="创建时间"
  157. )
  158. update_time: Mapped[datetime] = mapped_column(
  159. nullable=False,
  160. server_default=func.now(),
  161. onupdate=func.now(),
  162. comment="更新时间",
  163. )
  164. class VideoDiscoveryCandidate(Base):
  165. """本次运行发现的视频及其可审计评估快照。"""
  166. __tablename__ = "video_discovery_candidate"
  167. __table_args__ = (
  168. Index("idx_video_discovery_candidate_search", "search_id", "id"),
  169. Index("idx_video_discovery_candidate_bucket", "run_id", "decision_bucket"),
  170. Index("idx_video_discovery_candidate_author", "author_sec_uid"),
  171. )
  172. id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
  173. run_id: Mapped[str] = mapped_column(String(64), nullable=False, comment="发现运行 run_id")
  174. search_id: Mapped[int | None] = mapped_column(
  175. BigInteger,
  176. ForeignKey(
  177. "video_discovery_search.id",
  178. name="fk_video_discovery_candidate_search",
  179. ondelete="RESTRICT",
  180. ),
  181. nullable=True,
  182. comment="直接关联 video_discovery_search.id;历史数据允许为空",
  183. )
  184. aweme_id: Mapped[str] = mapped_column(String(64), nullable=False, comment="抖音视频 id")
  185. title: Mapped[str | None] = mapped_column(String(512), nullable=True, comment="视频标题")
  186. content_link: Mapped[str | None] = mapped_column(
  187. String(1024), nullable=True, comment="抖音页面链接"
  188. )
  189. author_name: Mapped[str | None] = mapped_column(
  190. String(256), nullable=True, comment="作者名"
  191. )
  192. author_sec_uid: Mapped[str | None] = mapped_column(
  193. String(256), nullable=True, comment="作者 sec_uid"
  194. )
  195. source_keywords_json: Mapped[str | None] = mapped_column(
  196. Text, nullable=True, comment="命中过该视频的搜索词 JSON"
  197. )
  198. source_search_ids_json: Mapped[str | None] = mapped_column(
  199. Text, nullable=True, comment="来源搜索轨迹 id JSON"
  200. )
  201. tags_json: Mapped[str | None] = mapped_column(
  202. Text, nullable=True, comment="视频标签/话题 JSON"
  203. )
  204. play_count: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
  205. like_count: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
  206. comment_count: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
  207. collect_count: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
  208. share_count: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
  209. content_age_evidence_json: Mapped[str | None] = mapped_column(
  210. Text, nullable=True, comment="视频点赞用户年龄证据 JSON"
  211. )
  212. account_age_evidence_json: Mapped[str | None] = mapped_column(
  213. Text, nullable=True, comment="作者粉丝年龄证据 JSON"
  214. )
  215. age_normalization_json: Mapped[str | None] = mapped_column(
  216. Text, nullable=True, comment="双侧年龄画像标准化结果 JSON"
  217. )
  218. relevance_score: Mapped[Decimal | None] = mapped_column(
  219. Numeric(8, 6), nullable=True, comment="R,范围 0~1"
  220. )
  221. elder_score: Mapped[Decimal | None] = mapped_column(
  222. Numeric(8, 6), nullable=True, comment="E,范围 0~1"
  223. )
  224. share_score: Mapped[Decimal | None] = mapped_column(
  225. Numeric(8, 6), nullable=True, comment="S,范围 0~1"
  226. )
  227. value_score: Mapped[Decimal | None] = mapped_column(
  228. Numeric(8, 2), nullable=True, comment="联合价值 V,范围 0~1"
  229. )
  230. decision_reason: Mapped[str | None] = mapped_column(Text, nullable=True)
  231. decision_bucket: Mapped[str] = mapped_column(
  232. String(24),
  233. nullable=False,
  234. default="pending_evaluation",
  235. comment="最终为 primary / rejected;pending_evaluation 仅为过程状态",
  236. )
  237. aigc_crawler_plan_id: Mapped[str | None] = mapped_column(
  238. String(64), nullable=True, comment="已创建的 AIGC 爬取计划 id"
  239. )
  240. aigc_produce_plan_id: Mapped[str | None] = mapped_column(
  241. String(64), nullable=True, comment="绑定的 AIGC 生成计划 id"
  242. )
  243. aigc_publish_plan_id: Mapped[str | None] = mapped_column(
  244. String(64), nullable=True, comment="关联的 AIGC 发布计划 id"
  245. )
  246. aigc_plan_label: Mapped[str | None] = mapped_column(
  247. String(64), nullable=True, comment="均匀分发时使用的计划标签"
  248. )
  249. create_time: Mapped[datetime] = mapped_column(
  250. nullable=False, server_default=func.now(), comment="创建时间"
  251. )
  252. update_time: Mapped[datetime] = mapped_column(
  253. nullable=False,
  254. server_default=func.now(),
  255. onupdate=func.now(),
  256. comment="更新时间",
  257. )