deploy_driving_range.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. import time, datetime
  10. import traceback
  11. from LIB.MIDDLE.DrivingRange import UpdtFct
  12. from urllib import parse
  13. import pymysql
  14. import dateutil.relativedelta
  15. from create_table2 import DrivingRangeResult
  16. dbManager = DBManager.DBManager()
  17. if __name__ == "__main__":
  18. # 时间设置
  19. now_time = datetime.datetime.now()
  20. pre_time = now_time + dateutil.relativedelta.relativedelta(minutes=-30)
  21. end_time=datetime.datetime.strftime(now_time,"%Y-%m-%d %H:%M:%S")
  22. start_time=datetime.datetime.strftime(pre_time,"%Y-%m-%d %H:%M:%S")
  23. print(pre_time)
  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/driving_range/{}/'.format(now_str[0:10]) + 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. try:
  66. #调度周期:每天运行一次。
  67. #更新所有sn,连读多日的factor,如果start_date和end_date相隔一天,代表更新start_date的factor。
  68. logger.info(u"{},{} 任务运行开始\n".format(start_time,end_time), exc_info=True)
  69. df_res = UpdtFct.updtVehElecRng(db_qx, db_qx, sn_newest_table_name='driving_range_sn_factor_newest', input_time=start_time)
  70. df_res = df_res.reset_index(drop=True)
  71. if not df_res.empty:
  72. df_res.columns = ['sn', 'time', 'soc', 'a0', 'a1', 'a2', 'a3', 'a4', 'vehelecrng']
  73. df_res.to_sql("driving_range_result",con=db_engine, if_exists="append",index=False)
  74. # session = DbSession()
  75. # for i in range(0, len(df_res)-1):
  76. # obj = session.query(DrivingRangeResult).filter_by(sn=df_res.loc[i,'sn']).first()
  77. # if not obj:
  78. # obj = DrivingRangeResult(df_res.loc[i,'sn'],df_res.loc[i,'time'],df_res.loc[i,'soc'],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'],
  79. # df_res.loc[i,'vehelecrng'])
  80. # else:
  81. # obj.time = df_res.loc[i,'time']
  82. # obj.soc = df_res.loc[i,'soc']
  83. # obj.a0 = df_res.loc[i,'a0']
  84. # obj.a1 = df_res.loc[i,'a1']
  85. # obj.a2 = df_res.loc[i,'a2']
  86. # obj.a3 = df_res.loc[i,'a3']
  87. # obj.a4 = df_res.loc[i,'a4']
  88. # obj.vehelecrng = df_res.loc[i,'vehelecrng']
  89. # session.add(obj)
  90. # session.commit()
  91. # session.close()
  92. logger.info(u"{},{} 续驶里程计算完成\n".format(start_time,end_time), exc_info=True)
  93. except:
  94. logger.error(traceback.format_exc)
  95. logger.error(u"{},{} 任务运行错误\n".format(start_time,end_time), exc_info=True)
  96. db_engine.dispose()
  97. db_qx.close()