supply_workflow_monitor.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. else '未知' end AS status,
  118. case
  119. when swcpr.error_msg like '%Data too long%' then '数据超过字段长度限制'
  120. when swcpr.error_msg like '%Deadlock%' then '数据库死锁'
  121. when (swcpr.error_msg = '' or swcpr.error_msg is null) then ''
  122. else substring_index(swcpr.error_msg, ',', 1)
  123. end AS error_msg
  124. from supply_workflow_task swt
  125. left join supply_workflow_crawler_plan_record swcpr
  126. on swt.task_id = swcpr.task_id
  127. where swt.create_timestamp >= {ts}
  128. ) as t
  129. group by workflow_id, status, error_msg
  130. union all
  131. select '生成计划绑定' as step,
  132. workflow_id as workflow_id,
  133. status as status,
  134. error_msg as error_msg,
  135. count(distinct task_id) as task_cnt,
  136. count(1) as cnt
  137. from (
  138. select swt.workflow_id as workflow_id,
  139. swt.task_id as task_id,
  140. case
  141. when swppr.status = 0 then '初始化'
  142. when swppr.status = 1 then '运行中'
  143. when swppr.status = 2 then '成功'
  144. when swppr.status = 3 then '失败'
  145. else '未知' end AS status,
  146. case
  147. when swppr.error_msg like '%Data too long%' then '数据超过字段长度限制'
  148. when swppr.error_msg like '%Deadlock%' then '数据库死锁'
  149. when (swppr.error_msg = '' or swppr.error_msg is null) then ''
  150. else substring_index(swppr.error_msg, ',', 1)
  151. end AS error_msg
  152. from supply_workflow_task swt
  153. left join supply_workflow_produce_bind_record swppr
  154. on swt.task_id = swppr.task_id
  155. where swt.create_timestamp >= {ts}
  156. ) as t
  157. group by workflow_id, status, error_msg
  158. ) as t
  159. left join supply_workflow sw on t.workflow_id = sw.id
  160. where workflow_id = '{workflow_id}';
  161. '''
  162. stat = mysql_helper.execute_query(sql)
  163. df = pd.DataFrame(stat)
  164. feishu_inform_util.send_card_msg_to_feishu(
  165. webhook=fei_shu_webhook,
  166. card_json=build_table_json(df, f"【workflow-{workflow_name}】爬取和生成详情")
  167. )
  168. def main():
  169. today_midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
  170. timestamp_ms = int(today_midnight.timestamp() * 1000)
  171. workflows = mysql_helper.execute_query('select id, name from supply_workflow;')
  172. for workflow in workflows:
  173. task_exe_step_stat(timestamp_ms, workflow['id'], workflow['name'])
  174. workflow_status_stat(timestamp_ms, workflow['id'], workflow['name'])
  175. if __name__ == '__main__':
  176. main()