video_discovery.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. rule_version: Mapped[str | None] = mapped_column(
  68. String(64), nullable=True, comment="本次候选门禁规则版本"
  69. )
  70. rule_config_json: Mapped[str | None] = mapped_column(
  71. Text, nullable=True, comment="运行时规则、当前时间和阈值快照 JSON"
  72. )
  73. create_time: Mapped[datetime] = mapped_column(
  74. nullable=False, server_default=func.now(), comment="创建时间"
  75. )
  76. update_time: Mapped[datetime] = mapped_column(
  77. nullable=False,
  78. server_default=func.now(),
  79. onupdate=func.now(),
  80. comment="更新时间",
  81. )
  82. class VideoDiscoverySearch(Base):
  83. """关键词探索图中的一次具体搜索页。"""
  84. __tablename__ = "video_discovery_search"
  85. __table_args__ = (
  86. Index("idx_video_discovery_search_run", "run_id", "id"),
  87. Index(
  88. "idx_video_discovery_search_parent",
  89. "run_id",
  90. "parent_search_id",
  91. ),
  92. Index("idx_video_discovery_search_keyword", "keyword"),
  93. )
  94. id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
  95. run_id: Mapped[str] = mapped_column(String(64), nullable=False, comment="发现运行 run_id")
  96. search_key: Mapped[str] = mapped_column(
  97. String(64), nullable=False, comment="搜索参数哈希,仅用于追踪,不作为幂等键"
  98. )
  99. keyword: Mapped[str] = mapped_column(
  100. String(256), nullable=False, comment="Agent 自主确定的搜索词"
  101. )
  102. query_reason: Mapped[str] = mapped_column(
  103. Text, nullable=False, comment="为何形成该搜索词、希望验证什么"
  104. )
  105. source_type: Mapped[str] = mapped_column(
  106. String(32),
  107. nullable=False,
  108. comment="demand / seed / point / tag / author / pagination / mixed",
  109. )
  110. source_value: Mapped[str | None] = mapped_column(
  111. Text, nullable=True, comment="来源点位、标签或父关键词"
  112. )
  113. parent_search_id: Mapped[int | None] = mapped_column(
  114. BigInteger, nullable=True, comment="由哪次搜索扩展而来"
  115. )
  116. provider: Mapped[str] = mapped_column(
  117. String(32),
  118. nullable=False,
  119. default="internal_keyword",
  120. comment="internal_keyword / tikhub / internal_blogger",
  121. )
  122. provider_state_json: Mapped[str | None] = mapped_column(
  123. Text, nullable=True, comment="供应方分页状态,如 search_id/backtrace"
  124. )
  125. content_type: Mapped[str] = mapped_column(
  126. String(16), nullable=False, default="视频", comment="搜索内容类型"
  127. )
  128. sort_type: Mapped[str] = mapped_column(
  129. String(32), nullable=False, default="综合排序", comment="搜索排序"
  130. )
  131. publish_time: Mapped[str] = mapped_column(
  132. String(32), nullable=False, default="不限", comment="发布时间筛选"
  133. )
  134. cursor: Mapped[str] = mapped_column(
  135. String(128), nullable=False, default="0", comment="本页游标"
  136. )
  137. page_no: Mapped[int] = mapped_column(
  138. Integer, nullable=False, default=1, comment="该关键词的页码"
  139. )
  140. results_count: Mapped[int] = mapped_column(
  141. Integer, nullable=False, default=0, comment="本页结果数"
  142. )
  143. new_candidate_count: Mapped[int] = mapped_column(
  144. Integer, nullable=False, default=0, comment="本页新增候选数"
  145. )
  146. has_more: Mapped[int] = mapped_column(
  147. Integer, nullable=False, default=0, comment="接口是否有下一页"
  148. )
  149. next_cursor: Mapped[str | None] = mapped_column(
  150. String(128), nullable=True, comment="下一页游标"
  151. )
  152. result_ids_json: Mapped[str | None] = mapped_column(
  153. Text, nullable=True, comment="本页 aweme_id 列表 JSON"
  154. )
  155. status: Mapped[str] = mapped_column(
  156. String(16), nullable=False, default="success", comment="success / failed"
  157. )
  158. error_message: Mapped[str | None] = mapped_column(
  159. Text, nullable=True, comment="搜索失败信息"
  160. )
  161. create_time: Mapped[datetime] = mapped_column(
  162. nullable=False, server_default=func.now(), comment="创建时间"
  163. )
  164. update_time: Mapped[datetime] = mapped_column(
  165. nullable=False,
  166. server_default=func.now(),
  167. onupdate=func.now(),
  168. comment="更新时间",
  169. )
  170. class VideoDiscoveryCandidate(Base):
  171. """本次运行发现的视频及其可审计评估快照。"""
  172. __tablename__ = "video_discovery_candidate"
  173. __table_args__ = (
  174. Index("idx_video_discovery_candidate_search", "search_id", "id"),
  175. Index("idx_video_discovery_candidate_bucket", "run_id", "decision_bucket"),
  176. Index("idx_video_discovery_candidate_author", "author_sec_uid"),
  177. )
  178. id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
  179. run_id: Mapped[str] = mapped_column(String(64), nullable=False, comment="发现运行 run_id")
  180. search_id: Mapped[int | None] = mapped_column(
  181. BigInteger,
  182. ForeignKey(
  183. "video_discovery_search.id",
  184. name="fk_video_discovery_candidate_search",
  185. ondelete="RESTRICT",
  186. ),
  187. nullable=True,
  188. comment="直接关联 video_discovery_search.id;历史数据允许为空",
  189. )
  190. aweme_id: Mapped[str] = mapped_column(String(64), nullable=False, comment="抖音视频 id")
  191. title: Mapped[str | None] = mapped_column(String(512), nullable=True, comment="视频标题")
  192. content_link: Mapped[str | None] = mapped_column(
  193. String(1024), nullable=True, comment="抖音页面链接"
  194. )
  195. author_name: Mapped[str | None] = mapped_column(
  196. String(256), nullable=True, comment="作者名"
  197. )
  198. author_sec_uid: Mapped[str | None] = mapped_column(
  199. String(256), nullable=True, comment="作者 sec_uid"
  200. )
  201. source_keywords_json: Mapped[str | None] = mapped_column(
  202. Text, nullable=True, comment="命中过该视频的搜索词 JSON"
  203. )
  204. source_search_ids_json: Mapped[str | None] = mapped_column(
  205. Text, nullable=True, comment="来源搜索轨迹 id JSON"
  206. )
  207. tags_json: Mapped[str | None] = mapped_column(
  208. Text, nullable=True, comment="视频标签/话题 JSON"
  209. )
  210. publish_at: Mapped[datetime | None] = mapped_column(
  211. nullable=True, comment="视频真实发布时间,按运行时区解释"
  212. )
  213. duration_seconds: Mapped[Decimal | None] = mapped_column(
  214. Numeric(10, 3), nullable=True, comment="视频时长(秒)"
  215. )
  216. play_count: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
  217. like_count: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
  218. comment_count: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
  219. collect_count: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
  220. share_count: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
  221. content_age_evidence_json: Mapped[str | None] = mapped_column(
  222. Text, nullable=True, comment="视频点赞用户年龄证据 JSON"
  223. )
  224. account_age_evidence_json: Mapped[str | None] = mapped_column(
  225. Text, nullable=True, comment="作者粉丝年龄证据 JSON"
  226. )
  227. age_normalization_json: Mapped[str | None] = mapped_column(
  228. Text, nullable=True, comment="双侧年龄画像标准化结果 JSON"
  229. )
  230. content_50_plus_ratio: Mapped[Decimal | None] = mapped_column(
  231. Numeric(8, 6), nullable=True, comment="视频点赞用户中 50+ 占比"
  232. )
  233. content_50_plus_tgi: Mapped[Decimal | None] = mapped_column(
  234. Numeric(10, 4), nullable=True, comment="视频点赞用户 50+ TGI"
  235. )
  236. content_portrait_status: Mapped[str | None] = mapped_column(
  237. String(16), nullable=True, comment="视频画像结论 pass / fail / missing"
  238. )
  239. account_50_plus_ratio: Mapped[Decimal | None] = mapped_column(
  240. Numeric(8, 6), nullable=True, comment="账号粉丝中 50+ 占比"
  241. )
  242. account_50_plus_tgi: Mapped[Decimal | None] = mapped_column(
  243. Numeric(10, 4), nullable=True, comment="账号粉丝 50+ TGI"
  244. )
  245. account_portrait_status: Mapped[str | None] = mapped_column(
  246. String(16), nullable=True, comment="账号画像结论 pass / fail / missing"
  247. )
  248. portrait_conflict: Mapped[int] = mapped_column(
  249. Integer, nullable=False, default=0, comment="视频与账号画像结论是否冲突"
  250. )
  251. temporal_type: Mapped[str | None] = mapped_column(
  252. String(24), nullable=True, comment="daypart / festival / event / seasonal / evergreen"
  253. )
  254. temporal_status: Mapped[str | None] = mapped_column(
  255. String(16), nullable=True, comment="时间有效性 pass / fail / unknown"
  256. )
  257. temporal_evidence_json: Mapped[str | None] = mapped_column(
  258. Text, nullable=True, comment="时间语义、有效窗口和判断理由 JSON"
  259. )
  260. gate_status: Mapped[str | None] = mapped_column(
  261. String(16), nullable=True, comment="硬门槛 pass / fail / pending"
  262. )
  263. gate_results_json: Mapped[str | None] = mapped_column(
  264. Text, nullable=True, comment="逐项硬门槛结果 JSON"
  265. )
  266. reject_reason_code: Mapped[str | None] = mapped_column(
  267. String(64), nullable=True, comment="主要淘汰原因码"
  268. )
  269. rule_version: Mapped[str | None] = mapped_column(
  270. String(64), nullable=True, comment="候选评估使用的规则版本"
  271. )
  272. relevance_score: Mapped[Decimal | None] = mapped_column(
  273. Numeric(8, 6), nullable=True, comment="R,范围 0~1"
  274. )
  275. elder_score: Mapped[Decimal | None] = mapped_column(
  276. Numeric(8, 6), nullable=True, comment="E,范围 0~1"
  277. )
  278. share_score: Mapped[Decimal | None] = mapped_column(
  279. Numeric(8, 6), nullable=True, comment="S,范围 0~1"
  280. )
  281. value_score: Mapped[Decimal | None] = mapped_column(
  282. Numeric(8, 2), nullable=True, comment="联合价值 V,范围 0~1"
  283. )
  284. decision_reason: Mapped[str | None] = mapped_column(Text, nullable=True)
  285. decision_bucket: Mapped[str] = mapped_column(
  286. String(24),
  287. nullable=False,
  288. default="pending_evaluation",
  289. comment="最终为 primary / rejected;pending_evaluation 仅为过程状态",
  290. )
  291. aigc_crawler_plan_id: Mapped[str | None] = mapped_column(
  292. String(64), nullable=True, comment="已创建的 AIGC 爬取计划 id"
  293. )
  294. aigc_produce_plan_id: Mapped[str | None] = mapped_column(
  295. String(64), nullable=True, comment="绑定的 AIGC 生成计划 id"
  296. )
  297. aigc_publish_plan_id: Mapped[str | None] = mapped_column(
  298. String(64), nullable=True, comment="关联的 AIGC 发布计划 id"
  299. )
  300. aigc_plan_label: Mapped[str | None] = mapped_column(
  301. String(64), nullable=True, comment="均匀分发时使用的计划标签"
  302. )
  303. create_time: Mapped[datetime] = mapped_column(
  304. nullable=False, server_default=func.now(), comment="创建时间"
  305. )
  306. update_time: Mapped[datetime] = mapped_column(
  307. nullable=False,
  308. server_default=func.now(),
  309. onupdate=func.now(),
  310. comment="更新时间",
  311. )