deploy.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. __author__ = 'lmstack'
  2. #coding=utf-8
  3. import os
  4. import datetime
  5. import pandas as pd
  6. from LIB.BACKEND import DBManager, Log
  7. from sqlalchemy import create_engine
  8. from sqlalchemy.orm import sessionmaker
  9. import time, datetime
  10. import dateutil.relativedelta
  11. import traceback
  12. from LIB.MIDDLE.OutlierDetection.VoltOutlier.V_1_0_0 import sta
  13. from urllib import parse
  14. import pymysql
  15. import pdb
  16. dbManager = DBManager.DBManager()
  17. if __name__ == "__main__":
  18. # 时间设置
  19. now_time = datetime.datetime.now()
  20. pre_time = now_time + dateutil.relativedelta.relativedelta(days=-1)# 最近一周
  21. end_time=datetime.datetime.strftime(now_time,"%Y-%m-%d 00:00:00")
  22. start_time=datetime.datetime.strftime(pre_time,"%Y-%m-%d 00:00:00")
  23. history_run_flag = False # 历史数据运行标志
  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, add_time from app_device")
  33. res = cursor.fetchall()
  34. df_sn = pd.DataFrame(res, columns=['sn', 'imei', 'add_time'])
  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. # 运行历史数据配置
  49. df_first_data_time = pd.read_sql("select * from bat_first_data_time", db_engine)
  50. # 日志配置
  51. now_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()).replace(":","_")
  52. log_path = 'log/' + now_str
  53. if not os.path.exists(log_path):
  54. os.makedirs(log_path)
  55. log = Log.Mylog(log_name='voltoutlier', log_level = 'info')
  56. log.set_file_hl(file_name='{}/info.log'.format(log_path), log_level='info', size=1024* 1024 * 100)
  57. log.set_file_hl(file_name='{}/error.log'.format(log_path), log_level='error', size=1024* 1024 * 100)
  58. logger = log.get_logger()
  59. logger.info("pid is {}".format(os.getpid()))
  60. # 算法参数
  61. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  62. port=3306
  63. db='qx_cas'
  64. user='qx_read'
  65. password='Qx@123456'
  66. tablename='cellStateEstimation_soh'
  67. for i in range(0, len(df_sn)):
  68. try:
  69. sn = df_sn.loc[i, 'sn']
  70. logger.info("pid-{} SN: {} START!".format(os.getpid(), sn))
  71. # 处理运行历史数据
  72. if (history_run_flag):
  73. this_sn = df_first_data_time[df_first_data_time['sn']==sn]
  74. if (len(this_sn) == 0):
  75. start_time = pd.to_datetime(str(df_sn.loc[df_sn[df_sn['sn']==sn].index, 'add_time'].values[0])).strftime("%Y-%m-%d 00:00:00")
  76. else:
  77. first_data_time = df_first_data_time.loc[df_first_data_time[df_first_data_time['sn']==sn].index, 'first_data_time'].values[0]
  78. if pd.isnull(first_data_time):
  79. start_time = "2018-01-01 00:00:00"
  80. else:
  81. start_time = pd.to_datetime(str(df_first_data_time.loc[df_first_data_time[df_first_data_time['sn']==sn].index, 'first_data_time'].values[0])).strftime("%Y-%m-%d 00:00:00")
  82. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  83. df_bms = df_data['bms']
  84. volt_column = [x for x in df_bms.columns if '单体电压' in x]
  85. columns = ['时间戳']
  86. columns.extend(volt_column)
  87. df_ori = df_bms[columns]
  88. df_ori.rename(columns = {'时间戳':'time'}, inplace=True)
  89. df = df_ori.drop_duplicates(subset=['time']).reset_index(drop=True) # 删除时间相同的数据
  90. df_result_1,time_list_1 = sta.cal_voltdiff_uniform(df,volt_column, window=50, step=10, window2=5, step2=3)
  91. df_result_2,time_list_2 = sta.cal_volt_uniform(df,volt_column, window=50, step=10)
  92. df_result_1['time'] = time_list_1
  93. df_result_2['time'] = time_list_2
  94. df_all_result = sta.instorage(sn, df_result_1, df_result_2)
  95. if not df_all_result.empty:
  96. df_all_result.to_sql("outlierdetection_volt",con=db_engine, if_exists="append",index=False)
  97. logger.info("pid-{} SN: {} DONE!".format(os.getpid(), sn))
  98. except:
  99. logger.error(traceback.format_exc)
  100. logger.error(u"{} :{},{} 任务运行错误\n".format(sn,start_time,end_time), exc_info=True)