deploy.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #coding=utf-8
  2. # 计算里程
  3. from math import radians, cos, sin, asin, sqrt
  4. import pandas as pd
  5. import numpy as np
  6. from datetime import timedelta
  7. from sqlalchemy import create_engine
  8. from sqlalchemy.orm import sessionmaker
  9. from GpsRank import *
  10. from ProcessDfBms import *
  11. from ProcessDfGps import *
  12. from LIB.MIDDLE.odo.CalDist import *
  13. from LIB.BACKEND import DBManager, Log
  14. import pdb
  15. from urllib import parse
  16. import traceback
  17. import os
  18. import time, datetime
  19. import pymysql
  20. import dateutil.relativedelta
  21. if __name__ == "__main__":
  22. # 时间设置
  23. now_time = datetime.datetime.now()
  24. pre_time = now_time + dateutil.relativedelta.relativedelta(days=-1)
  25. end_time=datetime.datetime.strftime(now_time,"%Y-%m-%d 00:00:00")
  26. start_time=datetime.datetime.strftime(pre_time,"%Y-%m-%d 00:00:00")
  27. # end_time = "2021-08-13 00:00:00"
  28. # start_time = "2019-01-01 00:00:00"
  29. # start_hour='00:00:00'#每日查询最早时间
  30. # end_hour='23:59:00'#每日查询最晚时间
  31. # 更新sn列表
  32. host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
  33. port=3306
  34. db='qixiang_oss'
  35. user='qixiang_oss'
  36. password='Qixiang2021'
  37. conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)
  38. cursor = conn.cursor()
  39. cursor.execute("select sn, imei from app_device")
  40. res = cursor.fetchall()
  41. df_sn = pd.DataFrame(res, columns=['sn', 'imei'])
  42. df_sn = df_sn.sort_values(['sn'])
  43. df_sn = df_sn.reset_index(drop=True)
  44. conn.close();
  45. # 结果数据库配置
  46. host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  47. port = 3306
  48. user = 'qx_cas'
  49. password = parse.quote_plus('Qx@123456')
  50. database = 'qx_cas'
  51. db_engine = create_engine(
  52. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  53. user, password, host, port, database
  54. ))
  55. DbSession = sessionmaker(bind=db_engine)
  56. # 日志配置
  57. now_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()).replace(":","_")
  58. log_path = 'log/' + now_str
  59. if not os.path.exists(log_path):
  60. os.makedirs(log_path)
  61. log = Log.Mylog(log_name='odo', log_level = 'info')
  62. log.set_file_hl(file_name='{}/info.log'.format(log_path), log_level='info', size=1024* 1024 * 100)
  63. log.set_file_hl(file_name='{}/error.log'.format(log_path), log_level='error', size=1024* 1024 * 100)
  64. logger = log.get_logger()
  65. logger.info("pid is + {}".format(os.getpid()))
  66. # date_index=pd.date_range(start_time[:10], end_time[:10])
  67. # for date in date_index:
  68. # '''遍历日期'''
  69. input_starttime = start_time
  70. input_endtime = end_time
  71. drive_info_aday=pd.DataFrame()
  72. for SN in df_sn['sn'].tolist()[1200:]:
  73. '''遍历SN号'''
  74. SN=SN.strip('\t')
  75. SN=SN.strip('\n')
  76. try:
  77. logger.info("pid-{} {}---{} START!".format(os.getpid(), SN, str(input_starttime)))
  78. range=GetDistInfo(SN,input_starttime,input_endtime)
  79. range_df=pd.DataFrame([range])
  80. drive_info_aday=pd.concat([drive_info_aday,range_df],axis=0)
  81. logger.info("{}---{} DONE!".format(SN, str(input_starttime)))
  82. if not drive_info_aday.empty:
  83. drive_info_aday['time'] = input_starttime[0:10]
  84. drive_info_aday.columns = ['sn', 'odo', 'accum_soc', 'start_soc', 'end_soc', 'start_time','end_time', 'min_soc', 'time']
  85. drive_info_aday.to_sql("odo_result",con=db_engine, if_exists="append",index=False)
  86. except Exception as e:
  87. logger.error(traceback.format_exc)
  88. logger.error(u"{} :{},{} 任务运行错误".format(SN,input_starttime,input_endtime), exc_info=True)