deploy_sn_factor.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #coding=utf-8
  2. import os
  3. import datetime
  4. import pandas as pd
  5. from LIB.BACKEND import DBManager, Log
  6. from LIB.MIDDLE import SignalMonitor
  7. from sqlalchemy import create_engine
  8. from sqlalchemy.orm import sessionmaker
  9. from sqlalchemy.dialects.mysql import insert
  10. import time, datetime
  11. import traceback
  12. from LIB.MIDDLE.DrivingRange import UpdtFct
  13. from urllib import parse
  14. import pymysql
  15. import dateutil.relativedelta
  16. from create_table1 import DrivingRangeSnFactorNewest
  17. dbManager = DBManager.DBManager()
  18. if __name__ == "__main__":
  19. # 时间设置
  20. now_time = datetime.datetime.now()
  21. pre_time = now_time + dateutil.relativedelta.relativedelta(days=-50)
  22. end_time=datetime.datetime.strftime(now_time,"%Y-%m-%d 23:59:00")
  23. start_time=datetime.datetime.strftime(pre_time,"%Y-%m-%d 23:59:00")
  24. # # 更新sn列表
  25. # host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
  26. # port=3306
  27. # db='qixiang_oss'
  28. # user='qixiang_oss'
  29. # password='Qixiang2021'
  30. # conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)
  31. # cursor = conn.cursor()
  32. # cursor.execute("select sn, imei from app_device")
  33. # res = cursor.fetchall()
  34. # df_sn = pd.DataFrame(res, columns=['sn', 'imei'])
  35. # df_sn = df_sn.reset_index(drop=True)
  36. # conn.close();
  37. # 数据库配置
  38. host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  39. port = 3306
  40. user = 'qx_cas'
  41. password = parse.quote_plus('Qx@123456')
  42. database = 'qx_cas'
  43. db_engine = create_engine(
  44. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  45. user, password, host, port, database
  46. ))
  47. DbSession = sessionmaker(bind=db_engine)
  48. db_qx = pymysql.connect(
  49. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com',
  50. user='qx_read',
  51. password='Qx@123456',#Qx@123456
  52. database='qx_cas',
  53. charset='utf8'
  54. )
  55. # 日志配置
  56. now_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()).replace(":","_")
  57. log_path = 'log/sn_factor/' + now_str
  58. if not os.path.exists(log_path):
  59. os.makedirs(log_path)
  60. log = Log.Mylog(log_name='driving_range', log_level = 'info')
  61. log.set_file_hl(file_name='{}/info.log'.format(log_path), log_level='info', size=1024* 1024 * 100)
  62. log.set_file_hl(file_name='{}/error.log'.format(log_path), log_level='error', size=1024* 1024 * 100)
  63. logger = log.get_logger()
  64. logger.info("pid is {}".format(os.getpid()))
  65. # SNdata_6060 = pd.read_excel('骑享资产梳理-20210621.xlsx', sheet_name='6060')
  66. # SNnums_6060=SNdata_6060['SN号']
  67. # # SNnums_6060 = ['PK504B00100004019','PK504B00100004029', 'PK504B00100004080', 'PK504B00100004264', 'PK504B10100004331']
  68. # now_time=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  69. # now_time = "2021-08-01 00:00:00" # 定时任务开启时,需要删除改行!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  70. # now_time=datetime.datetime.strptime(now_time,'%Y-%m-%d %H:%M:%S')
  71. # start_time=now_time-datetime.timedelta(days=1000)
  72. # end_time=str(now_time)
  73. # start_time=str(start_time)
  74. try:
  75. #调度周期:每天运行一次。
  76. #更新所有sn,连读多日的factor,如果start_date和end_date相隔一天,代表更新start_date的factor。
  77. logger.info(u"{},{} 任务运行开始\n".format(start_time,end_time), exc_info=True)
  78. # UpdtFct.updtAllSnFct(start_time[0:10],end_time[0:10], db_engine, db_qx, db_qx, sn_table_name='driving_range_sn_factor')
  79. logger.info(u"{},{} sn 参数更新完成\n".format(start_time,end_time), exc_info=True)
  80. logger.info(u"{},{} sn 最新参数更新开始\n".format(start_time,end_time), exc_info=True)
  81. df_res = UpdtFct.updtNewestFctTb(now_time, db_qx, sn_table_name='driving_range_sn_factor')
  82. df_res = df_res.reset_index(drop=True)
  83. session = DbSession()
  84. for i in range(0, len(df_res)-1):
  85. obj = session.query(DrivingRangeSnFactorNewest).filter_by(sn=df_res.loc[i,'sn']).first()
  86. if not obj:
  87. obj = DrivingRangeSnFactorNewest(df_res.loc[i,'sn'],df_res.loc[i,'date'],df_res.loc[i,'a0'],df_res.loc[i,'a1'],df_res.loc[i,'a2'],df_res.loc[i,'a3'],df_res.loc[i,'a4'])
  88. else:
  89. obj.date = df_res.loc[i,'date']
  90. obj.a0 = df_res.loc[i,'a0']
  91. obj.a1 = df_res.loc[i,'a1']
  92. obj.a2 = df_res.loc[i,'a2']
  93. obj.a3 = df_res.loc[i,'a3']
  94. obj.a4 = df_res.loc[i,'a4']
  95. session.add(obj)
  96. session.commit()
  97. session.close()
  98. logger.info(u"{},{} sn 最新参数更新完成\n".format(start_time,end_time), exc_info=True)
  99. except:
  100. logger.error(traceback.format_exc)
  101. logger.error(u"{},{} 任务运行错误\n".format(start_time,end_time), exc_info=True)