main.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. start_date_month = (datetime.datetime.now() + dateutil.relativedelta.relativedelta(months=-2)).strftime("%Y%m")+"01"
  23. end_date_month = (datetime.datetime.now() + dateutil.relativedelta.relativedelta(months=-1)).strftime("%Y%m")+"01"
  24. # print(start_date_month,end_date_month)
  25. # start_date_month = "20230301"
  26. # end_date_month = "20230401"
  27. def job():
  28. if datetime.date.today().day != 3:
  29. print("%s 等待4号执行程序"%datetime_now())
  30. else:
  31. # 连接mysql
  32. MysqlUtil = MysqlUtils(**mysql)
  33. days = get_day_1(start_date_month, end_date_month)
  34. for date in days:
  35. print("%s 开始导入%s的数据"%(datetime_now(),date))
  36. MysqlUtil.update_two(REPLACE_EQUIP_SQL%(date),DELETE_EQUIP_SQL%(date))
  37. MysqlUtil.update_two(REPLACE_SPACE_SQL%(date),DELETE_SPACE_SQL%(date))
  38. MysqlUtil.update_two(REPLACE_TARGET_SQL%(date),DELETE_TARGET_SQL%(date))
  39. # 关闭数据库
  40. MysqlUtil.close()
  41. schedule.every().day.at("20:30").do(job)
  42. while True:
  43. schedule.run_pending()
  44. time.sleep(30)