Browse Source

金融街中海每小时空调开启率

李莎 1 year ago
commit
da13348d15
7 changed files with 346 additions and 0 deletions
  1. 9 0
      Dockerfile
  2. 49 0
      MyUtils/ConfigUtils.py
  3. 137 0
      MyUtils/DateUtils.py
  4. 31 0
      MyUtils/HttpRequestsUtil.py
  5. 72 0
      MyUtils/MysqlUtils.py
  6. 44 0
      main.py
  7. 4 0
      requirements.txt

+ 9 - 0
Dockerfile

@@ -0,0 +1,9 @@
+FROM python:3.7.15-slim
+
+
+WORKDIR ./aircondition_open_rate
+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"]

+ 49 - 0
MyUtils/ConfigUtils.py

@@ -0,0 +1,49 @@
+import xml.etree.ElementTree as ET
+
+
+#读取xml文件
+class ConfigUtils():
+    def __init__(self,file):
+        self.url = ""
+        self.tree = ET.parse(file)
+        self.root = self.tree.getroot()
+
+    def readTop(self, key, child):
+        datas = []
+        data = self.root.find(key)
+        for key in child:
+            datas.append(data.get(key))
+        return datas
+
+    def readTopDict(self, key, child):
+        datas = {}
+        data = self.root.find(key)
+        for key in child:
+            datas[key] = data.get(key)
+        return datas
+
+    def readConfig(self,parent,child):
+        datas=[]
+        for childLine in self.root.find(parent):
+            data=[]
+            for key in child:
+                data.append(childLine.get(key))
+            datas.append(data)
+        return datas
+
+    def readConfigDict(self,parent,child):
+        datas=[]
+        for childLine in self.root.find(parent):
+            data={}
+            for key in child:
+                data[key]=childLine.get(key)
+            datas.append(data)
+        return datas
+
+    def readConfigSingle(self,key):
+        data = self.root.find(key).text
+        return data
+
+
+if __name__ == '__main__':
+    config = ConfigUtils("logger.conf")

+ 137 - 0
MyUtils/DateUtils.py

@@ -0,0 +1,137 @@
+import datetime
+from dateutil.relativedelta import relativedelta
+from dateutil import rrule
+
+YYmmddHHMMSS = "%Y%m%d%H%M%S"
+YYmmdd = "%Y%m%d"
+YYmm = "%Y%m"
+# 根据开始月份结束月份获取所有月份
+def get_each_month(start_month, end_month):
+    if str(start_month).count('-') != 1 or str(end_month).count('-') != 1:
+        print("Parameter Error: Pls input a string such as '2019-01'")
+        return []
+    if int(str(start_month).split('-')[1]) > 12 or int(str(end_month).split('-')[1]) > 12:
+        print('Parameter Error: Pls input correct month range such as between 1 to 12')
+        return []
+    if int(str(start_month).split('-')[1]) == 0 or int(str(end_month).split('-')[1]) == 13:
+        print('Parameter Error: Pls input correct month range such as between 1 to 12')
+        return []
+    start = datetime.datetime.strptime(start_month, "%Y-%m")
+    end = datetime.datetime.strptime(end_month, "%Y-%m")
+    month_count = rrule.rrule(rrule.MONTHLY, dtstart=start, until=end).count()  # 计算总月份数
+    if end < start:
+        print("Parameter Error: Pls input right date range,start_month can't latter than end_month")
+        return []
+    else:
+        list_month = []
+        year = int(str(start)[:7].split('-')[0])  # 截取起始年份
+        for m in range(month_count):  # 利用range函数填充结果列表
+            month = int(str(start)[:7].split('-')[1])  # 截取起始月份,写在for循环里,作为每次迭代的累加基数
+            month = month + m
+            if month > 12:
+                if month % 12 > 0:
+                    month = month % 12  # 计算结果大于12,取余数
+                    if month == 1:
+                        year += 1  # 只需在1月份的时候对年份加1,注意year的初始化在for循环外
+                else:
+                    month = 12
+            if len(str(month)) == 1:
+                list_month.append(str(year) + '-0' + str(month))
+            else:
+                list_month.append(str(year) + '-' + str(month))
+        return list_month
+
+
+# 转换格式,去掉"-",%Y%m
+def get_eachmonth(start_month, end_month):
+    startmonth = start_month[0:4] + "-" + start_month[4:6]
+    endmonth = end_month[0:4] + "-" + end_month[4:6]
+    months = get_each_month(startmonth, endmonth)
+    list_month = [i[0:4] + i[5:7] for i in months]
+    return list_month
+
+
+def get_month(starttime, endtime):
+    months = []
+    starttime = datetime.datetime.strptime(starttime, YYmmddHHMMSS)
+    endtime = datetime.datetime.strptime(endtime, YYmmddHHMMSS)
+    while starttime <= endtime:
+        start = starttime.strftime("%Y%m")
+        if start not in months:
+            months.append(start)
+        starttime = starttime + datetime.timedelta(days=1)
+    return months
+
+def get_month_range(starttime, endtime):
+    months = []
+    starttime = datetime.datetime.strptime(starttime[0:8], YYmmdd)
+    endtime = datetime.datetime.strptime(endtime[0:8], YYmmdd)
+    while starttime <= endtime:
+        start = starttime.strftime(YYmmddHHMMSS)
+        endMonth = datetime.datetime.strptime(start[0:6], YYmm) + relativedelta(months=+1)
+
+        end = endMonth.strftime(YYmmddHHMMSS)
+        if endtime <= endMonth:
+            end = endtime.strftime(YYmmddHHMMSS)
+        starttime = endMonth
+        months.append([start, end])
+    return months
+
+
+
+def get_month1(starttime, endtime):
+    months = []
+    startyear = int(starttime[0:4])
+    startmonth = int(starttime[4:6])
+    endyear = int(endtime[0:4])
+    endmonth = int(endtime[4:6])
+    while startyear != endyear or startmonth != endmonth:
+        startyearstr = str(startyear)
+        startmonthstr = str(startmonth)
+        if startmonth < 10:
+            startmonthstr = "0" + str(startmonth)
+        months.append(startyearstr + startmonthstr)
+        startmonth += 1
+        if startmonth == 13:
+            startyear += 1
+            startmonth = 1
+    months.append(endtime[0:6])
+    return months
+
+#根据开始结束时间获取最大区间为1天的时间区间
+# def get_day(starttime,endtime):
+#     times = []
+#     starttime = datetime.datetime.strptime(starttime, YYmmddHHMMSS)
+#     endtime = datetime.datetime.strptime(endtime, YYmmddHHMMSS)
+#     while starttime < endtime:
+#         start = starttime.strftime(YYmmddHHMMSS)
+#         starttime_delta = starttime + datetime.timedelta(days=1)
+#         end = starttime_delta.strftime(YYmmddHHMMSS)
+#         if str(starttime)[0:7]<str(starttime_delta)[0:7] :
+#             end = starttime_delta.strftime("%Y%m") +"01000000"
+#             starttime = datetime.datetime.strptime(end, YYmmddHHMMSS)
+#             times.append([start, end])
+#             continue
+#         if starttime >= starttime_delta:
+#             end = starttime_delta.strftime(YYmmddHHMMSS)
+#         starttime = starttime + datetime.timedelta(days=1)
+#         times.append([start,end])
+#     return times
+#根据开始结束时间获取最大区间为1天的时间区间
+def get_day(starttime,endtime):
+    times = []
+    starttime = datetime.datetime.strptime(starttime, YYmmddHHMMSS)
+    endtime = datetime.datetime.strptime(endtime, YYmmddHHMMSS)
+    while starttime < endtime:
+        start = starttime.strftime(YYmmddHHMMSS)
+        stratDay = datetime.datetime.strptime(start[0:8],YYmmdd)
+        starttime_delta = stratDay + datetime.timedelta(days=1)
+        end = starttime_delta.strftime(YYmmddHHMMSS)
+        if endtime <= starttime_delta:
+            end = endtime.strftime(YYmmddHHMMSS)
+        starttime = stratDay + datetime.timedelta(days=1)
+        times.append([start,end])
+    return times
+
+if __name__ == '__main__':
+    print(get_month_range("20200106000000", "20200115000000"))

+ 31 - 0
MyUtils/HttpRequestsUtil.py

@@ -0,0 +1,31 @@
+# coding=utf-8
+import requests
+import json
+
+
+
+class HttpRequestsUtil(object):
+    #post方法获取数据  application/json
+    @staticmethod
+    def post(url, postData,headers):  #
+        data = json.dumps(postData)
+        response = requests.request("POST",url, data=data.encode('utf-8'),
+                              headers=headers)
+        res = response.json()
+        return res
+
+    @staticmethod
+    def get(url,getData):  #
+
+        response = requests.request("GET",url+getData)
+        res = response.json()
+        # print(res)
+        return res
+
+
+
+if __name__ == '__main__':
+    httputil = HttpRequestsUtil()
+    # httputil.postText()
+    # HttpUtil.postText()
+    # httputil.get("http://web.sagacloud.cn:8008/sgdaping/duoduo-service/setup-service/environment/queryConditionerStatus?projectId=","Pj1101010007")

+ 72 - 0
MyUtils/MysqlUtils.py

@@ -0,0 +1,72 @@
+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=False, 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 close(self):
+        if MysqlUtils.__pool:
+            MysqlUtils.__pool.close()
+

+ 44 - 0
main.py

@@ -0,0 +1,44 @@
+from MyUtils.HttpRequestsUtil import HttpRequestsUtil
+import schedule
+import datetime,time
+import pytz
+from MyUtils.MysqlUtils import MysqlUtils
+
+INSERT_SQL = "insert into aircondition_open_rate(project_id,create_time,`value`) values "
+
+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
+
+url = "http://web.sagacloud.cn:8008/sgdaping/duoduo-service/setup-service/environment/queryConditionerStatus?projectId="
+projectId = "Pj1101020002"
+
+mysql = {'database': 'sagacloud_customization', 'host': 'rm-2zek656j2bn934176xo.mysql.rds.aliyuncs.com', 'passwd': 'H%k3!BHw1kQXIc70', 'port': 53306, 'user': 'root'}
+
+
+def job():
+
+    httputil = HttpRequestsUtil()
+    data = httputil.get(url,projectId)["data"]["openRate"]
+    # #连接hbase
+    MysqlUtil = MysqlUtils(**mysql)
+
+    sql ="('%s','%s','%s')"%(projectId,datetime_now(),data)
+    insert_sql = INSERT_SQL+sql
+    MysqlUtil.update(insert_sql)
+    MysqlUtil.close()
+    print(datetime_now(),data)
+
+
+# 每小时的第0分钟执行job函数
+schedule.every().hour.at(":00").do(job)
+
+
+
+while True:
+    schedule.run_pending()
+    time.sleep(61)

+ 4 - 0
requirements.txt

@@ -0,0 +1,4 @@
+requests==2.27.1
+pymysql==1.0.2
+dbutils==2.0.2
+pytz==2021.1