supply_workflow_monitor.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. else '未知' end AS status,
  75. case
  76. when swtes.error_msg like '%Data too long%' then '数据超过字段长度限制'
  77. when swtes.error_msg like '%Deadlock%' then '数据库死锁'
  78. when (swtes.error_msg = '' or swtes.error_msg is null) then ''
  79. else substring_index(swtes.error_msg, ',', 1)
  80. end AS error_msg
  81. from supply_workflow_task swt
  82. left join supply_workflow_task_exe_step swtes on swt.task_id = swtes.task_id
  83. where swt.create_timestamp >= {ts}
  84. and swt.workflow_id = {workflow_id}
  85. ) as t
  86. group by step_name, status, error_msg;
  87. '''
  88. stat = mysql_helper.execute_query(sql)
  89. df = pd.DataFrame(stat)
  90. feishu_inform_util.send_card_msg_to_feishu(
  91. webhook=fei_shu_webhook,
  92. card_json=build_table_json(df, f"【workflow-{workflow_name}】各步骤执行情况")
  93. )
  94. def workflow_status_stat(ts: int, workflow_id: str, workflow_name: str):
  95. sql = f'''
  96. select t.step as '阶段',
  97. t.status as '状态',
  98. t.error_msg as '失败原因',
  99. t.task_cnt as '任务数',
  100. t.cnt as '记录数'
  101. from (
  102. select '创建爬取计划' as step,
  103. workflow_id as workflow_id,
  104. status as status,
  105. error_msg as error_msg,
  106. count(distinct task_id) as task_cnt,
  107. count(distinct crawler_plan_id) as cnt
  108. from (
  109. select swt.workflow_id as workflow_id,
  110. swt.task_id as task_id,
  111. swcpr.crawler_plan_id as crawler_plan_id,
  112. case
  113. when swcpr.status = 0 then '初始化'
  114. when swcpr.status = 1 then '运行中'
  115. when swcpr.status = 2 then '成功'
  116. when swcpr.status = 3 then '失败'
  117. when swt.task_status = 0 then '初始化'
  118. when swt.task_status = 1 then '运行中'
  119. when swt.task_status = 2 then '成功'
  120. when swt.task_status = 3 then '失败'
  121. else '未知' end AS status,
  122. case
  123. when swcpr.error_msg like '%Data too long%' then '数据超过字段长度限制'
  124. when swcpr.error_msg like '%Deadlock%' then '数据库死锁'
  125. when (swcpr.error_msg <> '' AND swcpr.error_msg is not null) then substring_index(swcpr.error_msg, ',', 1)
  126. when swt.error_msg like '%Data too long%' then '数据超过字段长度限制'
  127. when swt.error_msg like '%Deadlock%' then '数据库死锁'
  128. when (swt.error_msg <> '' AND swt.error_msg is not null) then substring_index(swt.error_msg, ',', 1)
  129. else ''
  130. end AS error_msg
  131. from supply_workflow_task swt
  132. left join supply_workflow_crawler_plan_record swcpr
  133. on swt.task_id = swcpr.task_id
  134. where swt.create_timestamp >= {ts}
  135. ) as t
  136. group by workflow_id, status, error_msg
  137. union all
  138. select '生成计划绑定' as step,
  139. workflow_id as workflow_id,
  140. status as status,
  141. error_msg as error_msg,
  142. count(distinct task_id) as task_cnt,
  143. count(1) as cnt
  144. from (
  145. select swt.workflow_id as workflow_id,
  146. swt.task_id as task_id,
  147. case
  148. when swppr.status = 0 then '初始化'
  149. when swppr.status = 1 then '运行中'
  150. when swppr.status = 2 then '成功'
  151. when swppr.status = 3 then '失败'
  152. when swt.task_status = 0 then '初始化'
  153. when swt.task_status = 1 then '运行中'
  154. when swt.task_status = 2 then '成功'
  155. when swt.task_status = 3 then '失败'
  156. else '未知' end AS status,
  157. case
  158. when swppr.error_msg like '%Data too long%' then '数据超过字段长度限制'
  159. when swppr.error_msg like '%Deadlock%' then '数据库死锁'
  160. when (swppr.error_msg <> '' and swppr.error_msg is not null) then substring_index(swppr.error_msg, ',', 1)
  161. when swt.error_msg like '%Data too long%' then '数据超过字段长度限制'
  162. when swt.error_msg like '%Deadlock%' then '数据库死锁'
  163. when (swt.error_msg <> '' AND swt.error_msg is not null) then substring_index(swt.error_msg, ',', 1)
  164. else ''
  165. end AS error_msg
  166. from supply_workflow_task swt
  167. left join supply_workflow_produce_bind_record swppr
  168. on swt.task_id = swppr.task_id
  169. where swt.create_timestamp >= {ts}
  170. ) as t
  171. group by workflow_id, status, error_msg
  172. ) as t
  173. left join supply_workflow sw on t.workflow_id = sw.id
  174. where workflow_id = '{workflow_id}';
  175. '''
  176. stat = mysql_helper.execute_query(sql)
  177. df = pd.DataFrame(stat)
  178. feishu_inform_util.send_card_msg_to_feishu(
  179. webhook=fei_shu_webhook,
  180. card_json=build_table_json(df, f"【workflow-{workflow_name}】爬取和生成详情")
  181. )
  182. def main():
  183. today_midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
  184. timestamp_ms = int(today_midnight.timestamp() * 1000)
  185. workflows = mysql_helper.execute_query('select id, name from supply_workflow;')
  186. for workflow in workflows:
  187. task_exe_step_stat(timestamp_ms, workflow['id'], workflow['name'])
  188. workflow_status_stat(timestamp_ms, workflow['id'], workflow['name'])
  189. if __name__ == '__main__':
  190. main()