Browse Source

四川大厦会议室和空间绑定核查

李莎 1 year ago
commit
3acf8fefae
6 changed files with 240 additions and 0 deletions
  1. 10 0
      Dockerfile
  2. 18 0
      MyUtils/Dingtalk.py
  3. 115 0
      MyUtils/MysqlUtils.py
  4. 11 0
      config.json
  5. 83 0
      main.py
  6. 3 0
      requirements.txt

+ 10 - 0
Dockerfile

@@ -0,0 +1,10 @@
+FROM python:3.7.15-slim
+
+
+WORKDIR ./check_meetingid
+ADD . .
+
+RUN pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
+RUN  pip install -r requirements.txt
+
+CMD ["python", "-u","./main.py"]

+ 18 - 0
MyUtils/Dingtalk.py

@@ -0,0 +1,18 @@
+from  dingtalkchatbot.chatbot import DingtalkChatbot
+
+def send_message_mobiles(message,dingtalk,mobiles):
+    webhook = dingtalk
+    xiaoding = DingtalkChatbot(webhook)
+    xiaoding.send_text(msg=message,at_mobiles=[mobiles],is_auto_at=False)
+    return
+
+def send_message(message,dingtalk):
+    webhook = dingtalk
+    xiaoding = DingtalkChatbot(webhook)
+    xiaoding.send_text(msg=message)
+    return
+def send_message_markdown(title,text,dingtalk):
+    webhook = dingtalk
+    xiaoding = DingtalkChatbot(webhook)
+    xiaoding.send_markdown(title=title,text=text)
+    return

+ 115 - 0
MyUtils/MysqlUtils.py

@@ -0,0 +1,115 @@
+import pymysql
+from dbutils.pooled_db import PooledDB
+
+
+class MysqlUtils():
+    # 连接池对象
+    __pool = None
+    def __init__(self,**config):
+        self.host = config["host"]
+        self.port = int(config["port"])
+        self.user = config["user"]
+        self.passwd = config["passwd"]
+        self.database = None
+        if "database" in config:
+            self.database = config["database"]
+        self.pool = self.get_conn()
+        # self.pool = None
+
+    def get_conn(self):
+        if MysqlUtils.__pool is None:
+            __pool = PooledDB(pymysql, mincached=1, maxcached=5, maxconnections=5,
+                              host=self.host, port=self.port, user=self.user, passwd=self.passwd,
+                              database=self.database,
+                               use_unicode=True, blocking=False, charset="utf8")
+            self.pool = __pool
+            return __pool
+
+
+    def query(self,sql):
+        con = None
+        cur = None
+        try:
+            con = self.pool.connection()
+            cur = con.cursor()
+            count = cur.execute(sql)
+            if count >= 0:
+                result = cur.fetchall()
+                result = [[j.decode() if isinstance(j, bytes) else j for j in i] for i in result]
+                # print(result)
+            else:
+                result = False
+            return result
+        except Exception as e:
+            print(e)
+        finally:
+            if cur:
+                cur.close()
+            if con:
+                con.close()
+
+
+    def update(self,sql):
+        con = None
+        cur = None
+        try:
+            con = self.pool.connection()
+            cur = con.cursor()
+            cur.execute(sql)
+            con.commit()
+        except Exception as e:
+            con.rollback()  # 事务回滚
+            print(e)
+        finally:
+            if cur:
+                cur.close()
+            if con:
+                con.close()
+
+    def update_mult(self,sql1,sql2,sql3):
+        con = None
+        cur = None
+        try:
+            con = self.pool.connection()
+            cur = con.cursor()
+            cur.execute(sql1)
+            cur.execute(sql2)
+            cur.execute(sql3)
+            con.commit()
+            result = True
+        except Exception as e:
+            con.rollback()  # 事务回滚
+            print(e)
+            result = False
+
+        finally:
+            if cur:
+                cur.close()
+            if con:
+                con.close()
+        return  result
+
+    def update_two(self,sql1,sql2):
+        con = None
+        cur = None
+        try:
+            con = self.pool.connection()
+            cur = con.cursor()
+            cur.execute(sql1)
+            cur.execute(sql2)
+            con.commit()
+        except Exception as e:
+            con.rollback()  # 事务回滚
+            print(e)
+        finally:
+            if cur:
+                cur.close()
+            if con:
+                con.close()
+
+
+
+
+    def close(self):
+        if MysqlUtils.__pool:
+            MysqlUtils.__pool.close()

+ 11 - 0
config.json

@@ -0,0 +1,11 @@
+{
+  "mysql": {
+    "database": "sagacloud_setup",
+    "host": "rm-2zek656j2bn934176xo.mysql.rds.aliyuncs.com",
+    "passwd": "H%k3!BHw1kQXIc70",
+    "port": 53306,
+    "user": "root"
+  },
+  "dingding": "https://oapi.dingtalk.com/robot/send?access_token=9d7d888bb7c4d88a425595ae3eff87446b9e7b379dd99e074a2ecf1011928e19"
+
+}

+ 83 - 0
main.py

@@ -0,0 +1,83 @@
+import json
+from MyUtils.Dingtalk import send_message_markdown
+import datetime
+import time
+import pymysql
+import pytz,os
+
+SELECT_SQL = "SELECT sms.space_id, sms.meeting_id, m2. NAME FROM space_meet_set sms " \
+             "LEFT JOIN meeting m2 ON sms.meeting_id = m2.id WHERE meeting_id IN" \
+             " ( SELECT id FROM meeting m WHERE classification = %s AND m.building_id = '%s' AND m. STATUS = 1 );"
+
+def datetime_now():
+    # datetime_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
+    #容器时间
+    # tz = pytz.timezone('Asia/Shanghai')  # 东八区
+    datetime_now = datetime.datetime.fromtimestamp(int(time.time()),
+                                    pytz.timezone('Asia/Shanghai')).strftime('%Y-%m-%d %H:%M:%S')
+    return datetime_now
+
+
+building_id = "d89a6d4c73094f9f82eafb5d071e0934"
+
+classifications = {"1":"公共会议室","0":"租区会议室"}
+
+# # 读取配置文件信息
+# with open("config.json", "r") as f:
+#     data = json.load(f)
+#     mysql = data["mysql"]
+#     database = mysql["database"]
+#     dingding = data["dingding"]
+
+mysql = {
+    "host": os.getenv("host"),
+    "port": int(os.getenv("port")),
+    "user": os.getenv("user"),
+    "passwd": os.getenv("passwd"),
+    "database": os.getenv("database")
+}
+dingding = os.getenv("dingding")
+
+print("%s 等待开始检查..." % datetime_now())
+while True:
+
+    time_now = datetime.datetime.fromtimestamp(int(time.time()),
+                                           pytz.timezone('Asia/Shanghai')).strftime('%H:%M:%S')
+    # time_now = time.strftime("%H:%M:%S", time.localtime())  # 刷新
+    am_set_time = "11:00:00"
+    pm_set_time = "16:00:00"
+    if time_now == am_set_time or time_now == pm_set_time:  # 此处设置每天定时的时间
+        #钉钉推送消息
+        messages = []
+
+        # 连接mysql
+        conn = pymysql.connect(**mysql)
+        mysql_cur = conn.cursor()
+
+        for classification in classifications:
+            name = classifications[classification]
+            # 获取值
+            mysql_cur.execute(SELECT_SQL%(classification,building_id))
+            datas = mysql_cur.fetchall()
+            if datas:
+                null_sum = [i for i in datas if i[0] == None]
+                print("%s,四川大厦, %s: 总共%s个, 有%s个未绑定空间"%(datetime_now(),name,len(datas),len(null_sum)))
+                message = (" - **类型:** %s \n" + "- **总数量:** %s \n"+ " - **未绑定空间数量:**  <font color=#FF0000>%s</font> \n") % (name,len(datas),len(null_sum))
+                messages.append(message)
+                messages.append("___")
+            else:
+                print("%s,四川大厦, 没有查询到%s数据"%(datetime_now(),classification))
+
+        # 关闭数据库
+        mysql_cur.close()
+        conn.close()
+            #
+        if messages:
+            print("%s 发送钉钉报警" % datetime_now())
+            messages.append("<font color=#0080FF>详情查看MySQL数据库sagacloud_setup里的space_meet_set表</font>")
+            messages_info = "\n".join(messages)
+            # 如果有报警,发送钉钉
+            title = "## **【四川大厦】会议室未绑定空间报警** "
+            messages_info = title + "\n" + "___" + "\n" + messages_info
+            send_message_markdown(title, messages_info, dingding)
+        time.sleep(2)

+ 3 - 0
requirements.txt

@@ -0,0 +1,3 @@
+pymysql==1.0.2
+pytz==2021.1
+dingtalkchatbot==1.5.2