Browse Source

添加超时钉钉推送

yunwinLiu 6 tháng trước cách đây
mục cha
commit
7ea5fa8609
5 tập tin đã thay đổi với 41 bổ sung14 xóa
  1. 0 1
      .gitignore
  2. 29 4
      DingDing.py
  3. 6 2
      common/request_util.py
  4. 5 0
      config.yaml
  5. 1 7
      extract.yaml

+ 0 - 1
.gitignore

@@ -163,5 +163,4 @@ cython_debug/
 /pytest.ini/
 /README.md/
 /temps/
-
 /venv/

+ 29 - 4
DingDing.py

@@ -6,6 +6,8 @@ import datetime
 # 获取jenkins构建信息和本次报告地址
 import jenkins  # 安装pip install python-jenkins
 
+from common.yaml_util import read_config_yaml
+
 # 获取本机IP
 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 # s.connect(("8.8.8.8", 80))
@@ -34,6 +36,9 @@ else:
 '''
 
 
+# url = 'https://oapi.dingtalk.com/robot/send?access_token=f8566d2ed0feb8dc3bf2856df161241a8cf5a4c76af3d903abcabfdebebdff0e'  # webhook
+
+
 def DingTalkSend(info):
     d = {}
     # 获取项目绝对路径
@@ -42,7 +47,7 @@ def DingTalkSend(info):
     print(rf"{path}" + rf"{file}")
     # 打开prometheusData 获取需要发送的信息
     # f = open(rf"{path}" + rf"{file}", 'r', encoding='UTF-8')
-    f =open(os.getcwd()+file,'r', encoding='UTF-8')
+    f = open(os.getcwd() + file, 'r', encoding='UTF-8')
     for lines in f:
         for c in lines:
             launch_name = lines.strip('\n').split(' ')[0]
@@ -64,7 +69,6 @@ def DingTalkSend(info):
     print('不通过数量:{}'.format(status_failed))
 
     # 钉钉推送
-    url = 'https://oapi.dingtalk.com/robot/send?access_token=f8566d2ed0feb8dc3bf2856df161241a8cf5a4c76af3d903abcabfdebebdff0e'  # webhook
     text = (
                    f"<font color=\'#FFA500\'>[通知] </font>青春重庆-{info}报告"
                    "\n\n用例运行总数: " + retries_run +
@@ -75,10 +79,31 @@ def DingTalkSend(info):
                    "\n\n测试报告地址: \n" + report_url +
                    "\n\n播报时间: " + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) % (status_defects)
 
-    dd_robot = DingtalkChatbot(url)
+    dd_robot = DingtalkChatbot(read_config_yaml("dingding", "prod_webhook"))
     ret = dd_robot.send_markdown(title='青春重庆', text=text, is_at_all=False)
     print(ret)
 
 
+def dingtalk_send_warning(msg):
+    """
+    发送报警超时消息dingtalkchatbot
+    :param msg: 请求返回res
+    :return: none
+    """
+    try:
+        res_time = round(msg.elapsed.total_seconds() * 1000, 2)
+        text = f"<font color=\'#FFA500\'>[通知] </font>青春重庆-报警"\
+               f"\n\n>请求接口路径: {msg.url}% " \
+               f"\n\n>请求体: {str(msg.request.body).replace('%','')}% "\
+               f"\n\n>接口响应时间: {res_time}毫秒 " \
+               f"\n\n>播报时间: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')} "
+
+        dd_robot = DingtalkChatbot('')
+        dd_robot.send_markdown(title='青春重庆', text=text, is_at_all=False)
+    except Exception as e:
+        raise e
+
+
 if __name__ == '__main__':
-    DingTalkSend("生产APP巡检")
+    dingtalk_send_warning("测试")
+    # DingTalkSend("生产APP巡检")

+ 6 - 2
common/request_util.py

@@ -4,6 +4,7 @@ import traceback
 import jsonpath
 import requests
 
+from DingDing import dingtalk_send_warning
 from common.database_util import DatabaseUtil
 from common.log_util import error_log, logs, warring_log
 from common.yaml_util import write_yaml, read_yaml, read_config_yaml, write_yaml_projectCode
@@ -113,12 +114,15 @@ class RequestUtil:
                     #获取接口的请求时间
                     res_time = round(res.elapsed.total_seconds() * 1000, 2)
                     act_time = read_config_yaml("request", "request_out_time")
+                    print("act_time============",act_time)
+                    print("res_time============",res_time)
                     # 判断响应时间
                     if res_time >= act_time:
                         warring_log(
                             "接口实际请求时间{res_time}毫秒,请求时间大于{act_time}毫秒,请关注".format(res_time=res_time,
-                                                                                                      act_time=act_time))
-                    # TODO 发送通知
+                                                                                                 act_time=act_time))
+                        # 超时发送钉钉推送
+                        dingtalk_send_warning(res)
 
                     # print(res.json())
                     return_text = res.text

+ 5 - 0
config.yaml

@@ -19,3 +19,8 @@ request:
   request_out_time: 3000
 
 
+dingding:
+  prod_webhook: 'https://oapi.dingtalk.com/robot/send?access_token=f8566d2ed0feb8dc3bf2856df161241a8cf5a4c76af3d903abcabfdebebdff0e'
+  test_yunwin_webhook: 'https://oapi.dingtalk.com/robot/send?access_token=a9842cfd9a486b4516b1f228df14b3f9c45826a85c05edbdf7457ee2d2f14b78'
+
+

+ 1 - 7
extract.yaml

@@ -1,7 +1 @@
-token: PHONE_qdslrfpybxrnpbwewfxhwzuc
-thqcid: '1778720595806466048'
-hdzxid: '838'
-sstdid: '1776784240427298817'
-xspxid: '1741114772279947264'
-xxpxid: '833'
-qczcid: '1778690715361914880'
+token: PHONE_qqwysosfajmkpjeszetqbtls