supply_workflow_monitor.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. from datetime import datetime
  2. from typing import Dict, Any
  3. import pandas
  4. import pandas as pd
  5. from helper.MySQLHelper import MySQLHelper
  6. from util import feishu_inform_util
  7. fei_shu_webhook = "https://open.feishu.cn/open-apis/bot/v2/hook/c09712a8-22cd-4bfa-93a5-30ae7b1db11b"
  8. mysql_helper = MySQLHelper(
  9. host="rm-t4na9qj85v7790tf84o.mysql.singapore.rds.aliyuncs.com",
  10. username="readonly",
  11. password="HdkZ4TDmeK6SQ3BRtJBk",
  12. database="aigc-admin-prod"
  13. )
  14. column_width_map = {
  15. "状态": "80px",
  16. "任务数": "80px",
  17. "记录数": "80px",
  18. }
  19. def build_table_json(df: pandas.DataFrame, title: str) -> Dict[str, Any]:
  20. columns = []
  21. for column in df.columns:
  22. columns.append({
  23. "name": column,
  24. "display_name": column,
  25. "width": column_width_map.get(column, "auto"),
  26. "data_type": "text",
  27. "horizontal_align": "center",
  28. "vertical_align": "center",
  29. })
  30. rows = df.to_dict(orient="records")
  31. return {
  32. "schema": "2.0",
  33. "header": {
  34. "title": {
  35. "content": title
  36. },
  37. "template": "green",
  38. },
  39. "body": {
  40. "elements": [
  41. {
  42. "tag": "table",
  43. "element_id": "table_detail",
  44. "margin": "0px 0px 0px 0px",
  45. "page_size": 10,
  46. "header_style": {
  47. "text_align": "center",
  48. "text_size": "normal",
  49. "background_style": "none",
  50. "text_color": "grey",
  51. "bold": True,
  52. "lines": 1
  53. },
  54. "columns": columns,
  55. "rows": rows
  56. }
  57. ]
  58. }
  59. }
  60. def task_exe_step_stat(ts: int, workflow_id: str, workflow_name: str):
  61. sql = f'''
  62. select step_name AS '步骤名称',
  63. status as '执行状态',
  64. error_msg as '错误原因',
  65. count(1) AS '个数'
  66. from (
  67. select swt.task_id,
  68. step_name,
  69. case
  70. when status = 0 then '初始化'
  71. when status = 1 then '运行中'
  72. when status = 2 then '成功'
  73. when status = 3 then '失败'
  74. when swt.task_status = 0 then '初始化'
  75. when swt.task_status = 1 then '运行中'
  76. when swt.task_status = 2 then '成功'
  77. when swt.task_status = 3 then '失败'
  78. else '未知' end AS status,
  79. case
  80. when swtes.error_msg like '%Data too long%' then '数据超过字段长度限制'
  81. when swtes.error_msg like '%Deadlock%' then '数据库死锁'
  82. when (swtes.error_msg <> '' and swtes.error_msg is not null) then substring_index(swtes.error_msg, ',', 1)
  83. when swt.error_msg like '%Data too long%' then '数据超过字段长度限制'
  84. when swt.error_msg like '%Deadlock%' then '数据库死锁'
  85. when (swt.error_msg <> '' AND swt.error_msg is not null) then substring_index(swt.error_msg, ',', 1)
  86. else '' end AS error_msg
  87. from supply_workflow_task swt
  88. left join supply_workflow_task_exe_step swtes on swt.task_id = swtes.task_id
  89. where swt.create_timestamp >= {ts}
  90. and swt.workflow_id = {workflow_id}
  91. ) as t
  92. group by step_name, status, error_msg;
  93. '''
  94. stat = mysql_helper.execute_query(sql)
  95. df = pd.DataFrame(stat)
  96. feishu_inform_util.send_card_msg_to_feishu(
  97. webhook=fei_shu_webhook,
  98. card_json=build_table_json(df, f"【workflow-{workflow_name}】各步骤执行情况")
  99. )
  100. def workflow_status_stat(ts: int, workflow_id: str, workflow_name: str):
  101. sql = f'''
  102. select t.step as '阶段',
  103. t.status as '状态',
  104. t.error_msg as '失败原因',
  105. t.task_cnt as '任务数',
  106. t.cnt as '记录数'
  107. from (
  108. select '创建爬取计划' as step,
  109. workflow_id as workflow_id,
  110. status as status,
  111. error_msg as error_msg,
  112. count(distinct task_id) as task_cnt,
  113. count(distinct crawler_plan_id) as cnt
  114. from (
  115. select swt.workflow_id as workflow_id,
  116. swt.task_id as task_id,
  117. swcpr.crawler_plan_id as crawler_plan_id,
  118. case
  119. when swcpr.status = 0 then '初始化'
  120. when swcpr.status = 1 then '运行中'
  121. when swcpr.status = 2 then '成功'
  122. when swcpr.status = 3 then '失败'
  123. when swt.task_status = 0 then '初始化'
  124. when swt.task_status = 1 then '运行中'
  125. when swt.task_status = 2 then '成功'
  126. when swt.task_status = 3 then '失败'
  127. else '未知' end AS status,
  128. case
  129. when swcpr.error_msg like '%Data too long%' then '数据超过字段长度限制'
  130. when swcpr.error_msg like '%Deadlock%' then '数据库死锁'
  131. when (swcpr.error_msg <> '' AND swcpr.error_msg is not null) then substring_index(swcpr.error_msg, ',', 1)
  132. when swt.error_msg like '%Data too long%' then '数据超过字段长度限制'
  133. when swt.error_msg like '%Deadlock%' then '数据库死锁'
  134. when (swt.error_msg <> '' AND swt.error_msg is not null) then substring_index(swt.error_msg, ',', 1)
  135. else ''
  136. end AS error_msg
  137. from supply_workflow_task swt
  138. left join supply_workflow_crawler_plan_record swcpr
  139. on swt.task_id = swcpr.task_id
  140. where swt.create_timestamp >= {ts}
  141. ) as t
  142. group by workflow_id, status, error_msg
  143. union all
  144. select '生成计划绑定' as step,
  145. workflow_id as workflow_id,
  146. status as status,
  147. error_msg as error_msg,
  148. count(distinct task_id) as task_cnt,
  149. count(1) as cnt
  150. from (
  151. select swt.workflow_id as workflow_id,
  152. swt.task_id as task_id,
  153. case
  154. when swppr.status = 0 then '初始化'
  155. when swppr.status = 1 then '运行中'
  156. when swppr.status = 2 then '成功'
  157. when swppr.status = 3 then '失败'
  158. when swt.task_status = 0 then '初始化'
  159. when swt.task_status = 1 then '运行中'
  160. when swt.task_status = 2 then '成功'
  161. when swt.task_status = 3 then '失败'
  162. else '未知' end AS status,
  163. case
  164. when swppr.error_msg like '%Data too long%' then '数据超过字段长度限制'
  165. when swppr.error_msg like '%Deadlock%' then '数据库死锁'
  166. when (swppr.error_msg <> '' and swppr.error_msg is not null) then substring_index(swppr.error_msg, ',', 1)
  167. when swt.error_msg like '%Data too long%' then '数据超过字段长度限制'
  168. when swt.error_msg like '%Deadlock%' then '数据库死锁'
  169. when (swt.error_msg <> '' AND swt.error_msg is not null) then substring_index(swt.error_msg, ',', 1)
  170. else ''
  171. end AS error_msg
  172. from supply_workflow_task swt
  173. left join supply_workflow_produce_bind_record swppr
  174. on swt.task_id = swppr.task_id
  175. where swt.create_timestamp >= {ts}
  176. ) as t
  177. group by workflow_id, status, error_msg
  178. ) as t
  179. left join supply_workflow sw on t.workflow_id = sw.id
  180. where workflow_id = '{workflow_id}';
  181. '''
  182. stat = mysql_helper.execute_query(sql)
  183. df = pd.DataFrame(stat)
  184. feishu_inform_util.send_card_msg_to_feishu(
  185. webhook=fei_shu_webhook,
  186. card_json=build_table_json(df, f"【workflow-{workflow_name}】爬取和生成详情")
  187. )
  188. def main():
  189. today_midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
  190. timestamp_ms = int(today_midnight.timestamp() * 1000)
  191. workflows = mysql_helper.execute_query('select id, name from supply_workflow;')
  192. for workflow in workflows:
  193. task_exe_step_stat(timestamp_ms, workflow['id'], workflow['name'])
  194. workflow_status_stat(timestamp_ms, workflow['id'], workflow['name'])
  195. if __name__ == '__main__':
  196. main()