_mapper.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. from typing import Dict, List
  2. from app.core.database import DatabaseManager
  3. from ._const import DecodeArticleConst
  4. TABLE = "long_articles_decode_tasks_v2"
  5. class ArticlesDecodeTaskMapper(DecodeArticleConst):
  6. def __init__(self, pool: DatabaseManager):
  7. self.pool = pool
  8. async def insert_decode_task(
  9. self,
  10. channel_content_id: str,
  11. content_id: str,
  12. source: int,
  13. payload: str,
  14. remark: str = None,
  15. ) -> int:
  16. query = f"""
  17. INSERT IGNORE INTO {TABLE}
  18. (channel_content_id, config_id, content_id, source, payload, remark)
  19. VALUES (%s, %s, %s, %s, %s, %s)
  20. """
  21. return await self.pool.async_save(
  22. query=query,
  23. params=(
  24. channel_content_id,
  25. self.CONFIG_ID,
  26. content_id,
  27. source,
  28. payload,
  29. remark,
  30. ),
  31. )
  32. async def update_task_status_by_channel(
  33. self,
  34. channel_content_id: str,
  35. ori_status: int,
  36. new_status: int,
  37. remark: str = None,
  38. ) -> int:
  39. query = f"""
  40. UPDATE {TABLE}
  41. SET status = %s, remark = %s
  42. WHERE channel_content_id = %s AND status = %s AND config_id = %s
  43. """
  44. return await self.pool.async_save(
  45. query=query,
  46. params=(new_status, remark, channel_content_id, ori_status, self.CONFIG_ID),
  47. )
  48. async def set_decode_result(
  49. self,
  50. channel_content_id: str,
  51. result: str,
  52. remark: str = None,
  53. ) -> int:
  54. query = f"""
  55. UPDATE {TABLE}
  56. SET status = %s, result = %s, remark = %s
  57. WHERE channel_content_id = %s AND status IN (%s, %s) AND config_id = %s
  58. """
  59. return await self.pool.async_save(
  60. query=query,
  61. params=(
  62. self.TaskStatus.SUCCESS,
  63. result,
  64. remark,
  65. channel_content_id,
  66. self.TaskStatus.INIT,
  67. self.TaskStatus.PROCESSING,
  68. self.CONFIG_ID,
  69. ),
  70. )
  71. async def fetch_pending_tasks(
  72. self, source: int = None
  73. ) -> List[Dict]:
  74. if source is not None:
  75. query = f"""
  76. SELECT channel_content_id, content_id
  77. FROM {TABLE}
  78. WHERE status = %s AND source = %s AND config_id = %s
  79. LIMIT %s
  80. """
  81. params = (self.TaskStatus.INIT, source, self.CONFIG_ID, self.TASK_BATCH)
  82. else:
  83. query = f"""
  84. SELECT channel_content_id, content_id
  85. FROM {TABLE}
  86. WHERE status = %s AND config_id = %s
  87. LIMIT %s
  88. """
  89. params = (self.TaskStatus.INIT, self.CONFIG_ID, self.TASK_BATCH)
  90. return await self.pool.async_fetch(query=query, params=params)
  91. async def fetch_existing_channel_content_ids(
  92. self, channel_content_ids: List[str]
  93. ) -> set:
  94. """批量查询哪些 channel_content_id 已有任务记录"""
  95. if not channel_content_ids:
  96. return set()
  97. placeholders = ",".join(["%s"] * len(channel_content_ids))
  98. query = f"""
  99. SELECT channel_content_id FROM {TABLE}
  100. WHERE channel_content_id IN ({placeholders}) AND config_id = %s
  101. """
  102. rows = await self.pool.async_fetch(
  103. query=query,
  104. params=(*channel_content_ids, self.CONFIG_ID),
  105. )
  106. return {r["channel_content_id"] for r in rows}
  107. async def fetch_extract_tasks(self) -> List[Dict]:
  108. query = f"""
  109. SELECT id, result FROM {TABLE}
  110. WHERE extract_status = %s AND status = %s AND config_id = %s
  111. """
  112. return await self.pool.async_fetch(
  113. query=query,
  114. params=(self.ExtractStatus.INIT, self.TaskStatus.SUCCESS, self.CONFIG_ID),
  115. )
  116. async def update_extract_status(
  117. self, task_id: int, ori_status: int, new_status: int
  118. ) -> int:
  119. query = f"""
  120. UPDATE {TABLE}
  121. SET extract_status = %s
  122. WHERE extract_status = %s AND id = %s
  123. """
  124. return await self.pool.async_save(
  125. query=query, params=(new_status, ori_status, task_id)
  126. )
  127. async def record_extract_detail(
  128. self, decode_task_id: int, detail: Dict
  129. ) -> int:
  130. query = """
  131. INSERT INTO long_articles_decode_task_detail_v2
  132. (decode_task_id, inspiration, purpose, key_point, topic)
  133. VALUES (%s, %s, %s, %s, %s)
  134. """
  135. return await self.pool.async_save(
  136. query=query,
  137. params=(
  138. decode_task_id,
  139. detail.get("inspiration", ""),
  140. detail.get("purpose", ""),
  141. detail.get("key_point", ""),
  142. detail.get("topic", ""),
  143. ),
  144. )
  145. class AdPlatformArticlesDecodeTaskMapper(ArticlesDecodeTaskMapper):
  146. def __init__(self, pool: DatabaseManager):
  147. super().__init__(pool)
  148. async def update_article_decode_status(
  149. self, id_: int, ori_status: int, new_status: int
  150. ) -> int:
  151. query = """
  152. UPDATE ad_platform_accounts_daily_detail
  153. SET decode_status = %s
  154. WHERE id = %s AND decode_status = %s
  155. """
  156. return await self.pool.async_save(
  157. query=query, params=(new_status, id_, ori_status)
  158. )
  159. async def fetch_decode_articles(self) -> List[Dict]:
  160. query = """
  161. SELECT id, account_name, gh_id, article_title, article_cover,
  162. article_text, article_images, wx_sn
  163. FROM ad_platform_accounts_daily_detail
  164. WHERE fetch_status = %s AND decode_status = %s
  165. LIMIT %s
  166. """
  167. return await self.pool.async_fetch(
  168. query=query,
  169. params=(self.TaskStatus.SUCCESS, self.TaskStatus.INIT, self.TASK_BATCH),
  170. )
  171. class InnerArticlesDecodeTaskMapper(ArticlesDecodeTaskMapper):
  172. def __init__(self, pool: DatabaseManager):
  173. super().__init__(pool)
  174. async def fetch_inner_articles(self) -> List[Dict]:
  175. query = """
  176. SELECT title
  177. ,SUM(fans) AS total_fans
  178. ,SUM(view_count) AS total_view
  179. ,SUM(view_count) / SUM(fans) AS avg_read_rate
  180. ,SUM(first_level) AS total_first_level
  181. ,MAX(source_id) as source_id
  182. ,MAX(wx_sn) as wx_sn
  183. FROM datastat_sort_strategy
  184. WHERE date_str >= '20250101'
  185. GROUP BY title
  186. HAVING total_fans > 100000
  187. AND avg_read_rate > 0.002
  188. AND total_first_level > 0
  189. """
  190. return await self.pool.async_fetch(query=query)
  191. async def fetch_inner_articles_produce_detail(
  192. self, source_id
  193. ) -> List[Dict]:
  194. query = """
  195. SELECT produce_module_type, output
  196. FROM produce_plan_module_output
  197. WHERE plan_exe_id = %s
  198. AND produce_module_type in (1,2,3,4,18)
  199. """
  200. return await self.pool.async_fetch(
  201. query=query, db_name="aigc", params=(source_id,)
  202. )
  203. __all__ = [
  204. "ArticlesDecodeTaskMapper",
  205. "AdPlatformArticlesDecodeTaskMapper",
  206. "InnerArticlesDecodeTaskMapper",
  207. ]