deploy.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #coding=utf-8
  2. import pymysql
  3. import pandas as pd
  4. from sqlalchemy import create_engine
  5. from sqlalchemy.orm import sessionmaker
  6. import time, datetime
  7. import dateutil.relativedelta
  8. import os
  9. import traceback
  10. from LIB.BACKEND import Log
  11. if __name__ == "__main__":
  12. # 时间设置
  13. now_time = datetime.datetime.now()
  14. pre_time = now_time + dateutil.relativedelta.relativedelta(days=-1)
  15. end_time=datetime.datetime.strftime(now_time,"%Y-%m-%d 00:00:00")
  16. start_time=datetime.datetime.strftime(pre_time,"%Y-%m-%d 00:00:00")
  17. # 结果数据库配置
  18. host = '172.16.121.236'
  19. port = 3306
  20. user = 'root'
  21. password = 'FastFun1234,'
  22. database = 'fastfun'
  23. db_engine = create_engine(
  24. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  25. user, password, host, port, database
  26. ))
  27. DbSession = sessionmaker(bind=db_engine)
  28. # 日志配置
  29. now_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()).replace(":","_")
  30. log_path = 'log/' + now_str
  31. if not os.path.exists(log_path):
  32. os.makedirs(log_path)
  33. log = Log.Mylog(log_name='oss_to_iotp', log_level = 'info')
  34. log.set_file_hl(file_name='{}/info.log'.format(log_path), log_level='info', size=1024* 1024 * 100)
  35. log.set_file_hl(file_name='{}/error.log'.format(log_path), log_level='error', size=1024* 1024 * 100)
  36. logger = log.get_logger()
  37. logger.info("pid is + {}".format(os.getpid()))
  38. try:
  39. logger.info("pid-{} --{} START!".format(os.getpid(), str(start_time)))
  40. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  41. port=3306
  42. db='qixiang_oss'
  43. user='qx_read'
  44. password='Qx@123456'
  45. conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)
  46. cursor = conn.cursor()
  47. # cursor.execute("select * from (select distinct(t.sn) from (select * from app_device where status = 3)t inner join (select * from app_device_log where type=3 and out_type=1) t2 on t.sn=t2.sn)t3")
  48. cursor.execute("select distinct sn from app_device")
  49. res = cursor.fetchall()
  50. df_res = pd.DataFrame(res, columns=['sn'])
  51. df_res = df_res.reset_index(drop=True)
  52. all_sns = df_res['sn'].tolist()
  53. host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
  54. port=3306
  55. db='qixiang_manage'
  56. user='qx_query'
  57. password='@Qx_query'
  58. conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)
  59. cursor = conn.cursor()
  60. cursor.execute("select qrcode from py_battery where py_battery.status not in (0,1,2)")
  61. res = cursor.fetchall()
  62. df_filt = pd.DataFrame(res, columns=['sn'])
  63. df_filt = df_filt.reset_index(drop=True)
  64. filt_sns = df_filt['sn'].tolist()
  65. sns = list(set(all_sns)-set(filt_sns))
  66. df_res = pd.DataFrame({'sn':sns})
  67. df_res['factory'] = '骑享'
  68. # 金茂换电sn列表
  69. # 更新sn列表
  70. host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
  71. port=3306
  72. db='qixiang_oss'
  73. user='qx_algo_rw'
  74. password='qx@123456'
  75. db = 'qx_cas'
  76. conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)
  77. cursor = conn.cursor()
  78. cursor.execute("select sn, factory from sn_list")
  79. res = cursor.fetchall()
  80. df_sn2 = pd.DataFrame(res, columns=['sn', 'factory'])
  81. df_sn2['factory'] = "金茂换电"
  82. df_res = pd.concat([df_res, df_sn2], axis=0)
  83. df_res = df_res.reset_index(drop=True)
  84. df_res.to_sql("operation_sn",con=db_engine, if_exists="replace",index=False)
  85. logger.info("{}DONE!".format(str(start_time)))
  86. except Exception as e:
  87. logger.error(traceback.format_exc)
  88. logger.error(u" 任务运行错误", exc_info=True)
  89. finally:
  90. cursor.close()
  91. conn.close()
  92. db_engine.dispose()