• 效率工具:企业微信机器人完成脚本工作-异常监控(sql篇)


    效率工具:定时完成脚本工作(sql篇)

    背景:elon在公司需要人工查看某些数据是否异常,并发送到指定企业微信群中。

    处理步骤:

    1. 工具:企业微信中有机器人
    • 可以指定webhook地址嵌入到脚本中,定时执行脚本即可向这个地址发送信息。
    1. 用python脚本完成对数据库的读取,并发送指定文本信息到webhook地址。

    一、导入库文件,连接数据库

    import mysql.connector
    import pandas as pd 
    import numpy as np
    from datetime import datetime
    import matplotlib.pyplot as plt
    
    import sqlalchemy
    from sqlalchemy import create_engine
    from sqlalchemy import text
    import urllib.parse
    
    import time
    import requests
    import json
    def execute_mysql_query(query):
        password = urllib.parse.quote('密码')
        engine = create_engine('mysql+mysqlconnector://用户名:' + password + '@主机地址:端口号/数据库')
        with engine.connect() as connection:
            df = pd.read_sql(query, connection)
        return df
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    二、读取sql文件,获取监控指标

    def read_file(file_path):
        sql = open(file_path, 'r', encoding = 'utf8')
        sqltxt = sql.readlines()
        # 此时 sqltxt 为 list 类型
        # 读取之后关闭文件
        sql.close()
        # list 转 str
        sql = "".join(sqltxt)
        return sql
    ##1.返点监控###########################################################################################################################
    #2. 执行地址下的sql文件
    返点监控_是否为空_sql = read_file("D:\Marshal1\datagrip_data\关键数据指标核对\返点监控_是否为空.sql")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    三、对指标进行异常判断

    df_返点监控_是否为空 = execute_mysql_query(返点监控_是否为空_sql)
    if df_返点监控_是否为空['计入应收时间'].notnull().any():
        已结算计划计入应收时间是否为空='是'
    else :
        已结算计划计入应收时间是否为空='否'
    
    • 1
    • 2
    • 3
    • 4
    • 5

    四、将异常信息传到企业微信机器人。

     #记录启动时间
     current_time=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    'https://developer.work.weixin.qq.com/document/path/91770#%E6%96%87%E6%9C%AC%E7%B1%BB%E5%9E%8B 微信文档'
    proxies = {}
    # 1、填入webhook的key值
    webhook =' https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=d13fbffb-7680-4f4c-aab5-1ea8e99f1f7b'
    # 2、选择一种样式,发消息
    def qywx_message():
        header = { "Content-Type": "application/json" }
        message = {
            "msgtype": "markdown",
            "markdown": {
               "content": 
               f"#{current_time}\n\n"
               f"# 监控中心\n\n"
               f"## \n"
               f"## 返点监控:\n"
               f"> 已结算计划计入应收时间是否为空:{已结算计划计入应收时间是否为空}\n"
               f"> 计划多个结算时间:{计划多个结算时间}\n"
               f"## 组织架构监控:\n"
               f"> bd中是否存在同组同名:{bd中是否存在同组同名}\n"
               f"> 媒介组织架构:{媒介组织架构}\n"
               f"> 媒介主管:{媒介主管是否有变动}\n"
               f"> BD组织架构:{BD组织架构}\n"
               f"> BD主管: {BD主管是否有变动}\n"
               f"## 海外联盟标签监控:\n"
               f"> DM:{DM系统业务线}\n"
               f"> KN:{KN系统业务线}\n"
               f"> LKB:{LKB系统业务线}\n"
            }}
        message_json = json.dumps(message)
        send_message = requests.post(url=webhook,data=message_json,headers=header,proxies=proxies)
        print(send_message.text)
    qywx_message()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    五、效果图

    在这里插入图片描述

  • 相关阅读:
    HTML网页设计制作——响应式网页影视动漫资讯bootstrap网页(9页)
    Service Mesh之Istio部署bookinfo
    高危安全漏洞风险提示:Oracle7月修复Weblogic Server严重RCE漏洞
    chattr设置文件只读
    19异常的学习笔记
    mybatispuls 批处理 rewriteBatchedStatements=true
    c++builder6.0 数据库查询函数select * into 功能的实现
    MyBatis
    2023最新SSM计算机毕业设计选题大全(附源码+LW)之java线上学习系统8e88w
    IDEA、DataGrip、Navicat连接openGauss数据库
  • 原文地址:https://blog.csdn.net/weixin_52001949/article/details/137915839