main.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from MyUtils.MysqlUtils import MysqlUtils
  2. from MyUtils.DateUtils import get_day_1
  3. import datetime
  4. import dateutil.relativedelta
  5. import time
  6. import pytz
  7. import schedule
  8. REPLACE_EQUIP_SQL = "replace into sagacloud_review.custom_project_equipment_quarter_history SELECT * from sagacloud_review.custom_project_equipment_quarter where date = '%s'"
  9. REPLACE_SPACE_SQL = "replace into sagacloud_review.custom_space_quarter_history SELECT * from sagacloud_review.custom_space_quarter where date = '%s'"
  10. DELETE_EQUIP_SQL = "delete from sagacloud_review.custom_project_equipment_quarter where date = '%s'"
  11. DELETE_SPACE_SQL = "delete from sagacloud_review.custom_space_quarter where date = '%s'"
  12. REPLACE_TARGET_SQL = "replace into sagacloud_customization.custom_persist_target_history SELECT * from sagacloud_customization.custom_persist_target where date = '%s'"
  13. DELETE_TARGET_SQL = "delete from sagacloud_customization.custom_persist_target where date = '%s'"
  14. def datetime_now():
  15. # datetime_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
  16. #容器时间
  17. # tz = pytz.timezone('Asia/Shanghai') # 东八区
  18. datetime_now = datetime.datetime.fromtimestamp(int(time.time()),
  19. pytz.timezone('Asia/Shanghai')).strftime('%Y-%m-%d %H:%M:%S')
  20. return datetime_now
  21. mysql = {'database': 'sagacloud_review', 'host': 'rm-2zek656j2bn934176xo.mysql.rds.aliyuncs.com', 'passwd': 'H%k3!BHw1kQXIc70', 'port': 53306, 'user': 'root'}
  22. # print(start_date_month,end_date_month)
  23. # start_date_month = "20230301"
  24. # end_date_month = "20230401"
  25. def job():
  26. if datetime.date.today().day != 3:
  27. print("%s 等待4号执行程序"%datetime_now())
  28. else:
  29. # 连接mysql
  30. MysqlUtil = MysqlUtils(**mysql)
  31. start_date_month = (datetime.datetime.now() + dateutil.relativedelta.relativedelta(months=-2)).strftime(
  32. "%Y%m") + "01"
  33. end_date_month = (datetime.datetime.now() + dateutil.relativedelta.relativedelta(months=-1)).strftime(
  34. "%Y%m") + "01"
  35. days = get_day_1(start_date_month, end_date_month)
  36. for date in days:
  37. print("%s 开始导入%s的数据"%(datetime_now(),date))
  38. MysqlUtil.update_two(REPLACE_EQUIP_SQL%(date),DELETE_EQUIP_SQL%(date))
  39. MysqlUtil.update_two(REPLACE_SPACE_SQL%(date),DELETE_SPACE_SQL%(date))
  40. MysqlUtil.update_two(REPLACE_TARGET_SQL%(date),DELETE_TARGET_SQL%(date))
  41. # 关闭数据库
  42. MysqlUtil.close()
  43. schedule.every().day.at("20:30").do(job)
  44. while True:
  45. schedule.run_pending()
  46. time.sleep(30)