supply_workflow_monitor.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from datetime import datetime
  2. from typing import Dict, List, Any
  3. import pandas as pd
  4. from helper.MySQLHelper import MySQLHelper
  5. from util import feishu_inform_util
  6. fei_shu_webhook = "https://open.feishu.cn/open-apis/bot/v2/hook/c09712a8-22cd-4bfa-93a5-30ae7b1db11b"
  7. mysql_helper = MySQLHelper(
  8. host="rm-t4na9qj85v7790tf84o.mysql.singapore.rds.aliyuncs.com",
  9. username="readonly",
  10. password="HdkZ4TDmeK6SQ3BRtJBk",
  11. database="aigc-admin-prod"
  12. )
  13. def build_card_join(content: str):
  14. return {
  15. "schema": "2.0",
  16. "header": {
  17. "title": {
  18. "content": "任务执行步骤统计"
  19. },
  20. "template": "green",
  21. },
  22. "body": {
  23. "elements": [
  24. {
  25. "tag": "markdown",
  26. "element_id": "detail",
  27. "margin": "0px 0px 0px 0px",
  28. "content": content,
  29. "text_size": "normal",
  30. "text_align": "left"
  31. }
  32. ]
  33. }
  34. }
  35. def df_to_markdown_table(df: pd.DataFrame) -> str:
  36. """将DataFrame转换为标准Markdown表格字符串"""
  37. headers = list(df.columns)
  38. str_rows = df.astype(str).values
  39. col_widths = []
  40. for i, h in enumerate(headers):
  41. max_w = len(str(h))
  42. for row in str_rows:
  43. max_w = max(max_w, len(str(row[i])))
  44. col_widths.append(max_w)
  45. def _row(values):
  46. cells = [str(v).ljust(col_widths[i]) for i, v in enumerate(values)]
  47. return '| ' + ' | '.join(cells) + ' |'
  48. sep = '| ' + ' | '.join('-' * w for w in col_widths) + ' |'
  49. lines = [_row(headers), sep]
  50. for row in str_rows:
  51. lines.append(_row(row))
  52. return '\n'.join(lines)
  53. def task_exe_step_stat(ts: int) -> List[Dict[str, Any]]:
  54. sql = f'''
  55. select step_name AS "步骤名称",
  56. status as '执行状态',
  57. error_msg as '错误原因',
  58. count(1) AS '个数'
  59. from (
  60. select step_name,
  61. case
  62. when status = 0 then '初始化'
  63. when status = 1 then '运行中'
  64. when status = 2 then '成功'
  65. when status = 3 then '失败'
  66. else '未知' end AS status,
  67. case
  68. when error_msg like '%Data too long%' then '数据超过字段长度限制'
  69. when error_msg like '%Deadlock%' then '数据库死锁'
  70. when (error_msg = '' or error_msg is null) then ''
  71. else substring_index(error_msg, ',', 1)
  72. end AS error_msg
  73. from supply_workflow_task_exe_step
  74. where create_timestamp >= {ts}
  75. ) as t
  76. group by step_name, status, error_msg
  77. '''
  78. return mysql_helper.execute_query(sql)
  79. def main():
  80. today_midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
  81. timestamp_ms = int(today_midnight.timestamp() * 1000)
  82. stat = task_exe_step_stat(timestamp_ms)
  83. df = pd.DataFrame(stat)
  84. print("当日任务步骤执行统计")
  85. msg = df_to_markdown_table(df)
  86. feishu_inform_util.send_card_msg_to_feishu(
  87. webhook=fei_shu_webhook,
  88. card_json=build_card_join(msg)
  89. )
  90. if __name__ == '__main__':
  91. main()