from MyUtils.MysqlUtils import MysqlUtils from MyUtils.DateUtils import get_day_1 import datetime import dateutil.relativedelta import time import pytz import schedule REPLACE_EQUIP_SQL = "replace into sagacloud_review.custom_project_equipment_quarter_history SELECT * from sagacloud_review.custom_project_equipment_quarter where date = '%s'" REPLACE_SPACE_SQL = "replace into sagacloud_review.custom_space_quarter_history SELECT * from sagacloud_review.custom_space_quarter where date = '%s'" DELETE_EQUIP_SQL = "delete from sagacloud_review.custom_project_equipment_quarter where date = '%s'" DELETE_SPACE_SQL = "delete from sagacloud_review.custom_space_quarter where date = '%s'" REPLACE_TARGET_SQL = "replace into sagacloud_customization.custom_persist_target_history SELECT * from sagacloud_customization.custom_persist_target where date = '%s'" DELETE_TARGET_SQL = "delete from sagacloud_customization.custom_persist_target where date = '%s'" 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 mysql = {'database': 'sagacloud_review', 'host': '10.100.28.84', 'passwd': 'gWK5o9WmCBF5LiW', 'port': 9934, 'user': 'root'} # print(start_date_month,end_date_month) # start_date_month = "20220903" # end_date_month = "20230201" def job(): if datetime.date.today().day != 3: print("%s 等待4号执行程序"%datetime_now()) else: ## 连接mysql MysqlUtil = MysqlUtils(**mysql) start_date_month = (datetime.datetime.now() + dateutil.relativedelta.relativedelta(months=-2)).strftime( "%Y%m") + "01" end_date_month = (datetime.datetime.now() + dateutil.relativedelta.relativedelta(months=-1)).strftime( "%Y%m") + "01" days = get_day_1(start_date_month, end_date_month) for date in days: print("%s 开始导入%s的数据"%(datetime_now(),date)) MysqlUtil.update_two(REPLACE_EQUIP_SQL%(date),DELETE_EQUIP_SQL%(date)) MysqlUtil.update_two(REPLACE_SPACE_SQL%(date),DELETE_SPACE_SQL%(date)) MysqlUtil.update_two(REPLACE_TARGET_SQL%(date),DELETE_TARGET_SQL%(date)) # 关闭数据库 MysqlUtil.close() schedule.every().day.at("20:30").do(job) while True: schedule.run_pending() time.sleep(30)