pre_data_ftp.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # @FileName :pre_data_ftp.py
  4. # @Time :2024/12/26 14:18
  5. # @Author :David
  6. # @Company: shenyang JY
  7. from ftplib import FTP
  8. import pandas as pd
  9. from flask import Flask, request, jsonify
  10. import time, datetime, os, traceback, pytz
  11. from pytz import timezone
  12. import zipfile, tempfile, shutil
  13. from common.database_dml import get_data_from_mongo
  14. from common.logs import Log
  15. logger = Log('data-processing').logger
  16. app = Flask('pre_data_ftp——service')
  17. ftp_params = {
  18. 'local_dir' : './data_processing/cache/data',
  19. 'host' : '192.168.12.20',
  20. 'port': 32121,
  21. 'zip_mode': 'w',
  22. 'liudawei' : {
  23. 'user' : 'liudawei',
  24. 'password' : 'liudawei@123',
  25. 'modeler' : 'koi'
  26. },
  27. 'anweiguo':{
  28. 'user' : 'anweiguo',
  29. 'password' : 'anweiguo@123',
  30. 'modeler' : 'seer'
  31. }
  32. }
  33. def get_moment(schedule_dt=False):
  34. if schedule_dt:
  35. now = datetime.datetime.strptime(str(schedule_dt), '%Y-%m-%d %H:%M:%S')
  36. else:
  37. now = datetime.datetime.now(pytz.utc).astimezone(timezone("Asia/Shanghai"))
  38. if now.hour >= 18:
  39. moment = '18'
  40. elif now.hour >= 12:
  41. moment = '12'
  42. elif now.hour >= 6:
  43. moment = '06'
  44. else:
  45. moment = '00'
  46. return moment
  47. def zip_temp_file(df, args):
  48. def zip_folder(folder_path, zip_filePath):
  49. zip_file = zipfile.ZipFile(zip_filePath, ftp_params['zip_mode'], zipfile.ZIP_DEFLATED)
  50. for root, dirs, files in os.walk(folder_path):
  51. for file in files:
  52. file_path = os.path.join(root, file)
  53. zip_file.write(file_path, os.path.relpath(file_path, folder_path))
  54. zip_file.close()
  55. temp_dir, tem_dir_zip = tempfile.mkdtemp(dir=ftp_params['local_dir']), tempfile.mkdtemp(dir=ftp_params['local_dir'])
  56. current_time = datetime.datetime.now(pytz.utc).astimezone(timezone("Asia/Shanghai"))
  57. dt = current_time.strftime('%Y%m%d')
  58. moment = get_moment() if args.get('dt') is None else get_moment(args.get('dt'))
  59. modeler, model, version, farmId = ftp_params[args['user']]['modeler'], args['model'], args['version'], args['farmId']
  60. csv_file = 'jy_{}.{}.{}_{}_{}{}_dq.csv'.format(modeler, model, version, farmId, dt, moment)
  61. csv_path = os.path.join(temp_dir, farmId, csv_file)
  62. os.makedirs(os.path.dirname(csv_path), exist_ok=True)
  63. df.to_csv(csv_path, index=False)
  64. zip_file = 'jy_{}.{}.{}_{}{}_dq.zip'.format(modeler, model, version, dt, moment)
  65. zip_path = os.path.join(tem_dir_zip, zip_file)
  66. zip_folder(temp_dir, zip_path)
  67. shutil.rmtree(temp_dir)
  68. return zip_path, zip_file
  69. def upload_ftp(zip_path, zip_file, args):
  70. ftp_host, ftp_port, ftp_user, ftp_pass = ftp_params['host'], ftp_params['port'], args['user'], ftp_params[args['user']]['password']
  71. # 创建 FTP 连接
  72. ftp = FTP()
  73. # 使用被动模式
  74. ftp.set_pasv(True)
  75. # 连接到 FTP 服务器并指定端口
  76. ftp.connect(ftp_host, ftp_port) # 使用自定义端口号
  77. # 登录到 FTP 服务器
  78. ftp.login(ftp_user, ftp_pass)
  79. # 上传文件
  80. with open(zip_path, 'rb') as f:
  81. ftp.storbinary('STOR /' + ftp_params[args['user']]['modeler'] + '/'+zip_file, f)
  82. # 退出 FTP 连接
  83. ftp.quit()
  84. shutil.rmtree(os.path.dirname(zip_path))
  85. # os.remove(zip_path)
  86. logger.info("File uploaded successfully")
  87. @app.route('/pre_data_ftp', methods=['POST'])
  88. def get_nwp_from_ftp():
  89. # 获取程序开始时间
  90. start_time = time.time()
  91. result = {}
  92. success = 0
  93. args = {}
  94. try:
  95. args = request.values.to_dict()
  96. # 1. 获取 mongo 中的预测结果
  97. logger.info(args)
  98. df = get_data_from_mongo(args)
  99. df['date_time'] = pd.to_datetime(df['date_time'])
  100. df = df.sort_values(by='date_time')
  101. # 2. 将预测结果保存成csv临时文件,命名压缩
  102. zip_path, zip_file = zip_temp_file(df, args)
  103. # 3. 上传到指定的FTP服务器中
  104. upload_ftp(zip_path, zip_file, args)
  105. success = 1
  106. except Exception as e:
  107. my_exception = traceback.format_exc()
  108. my_exception.replace("\n", "\t")
  109. result['msg'] = my_exception
  110. logger.info("预测文件下发ftp出错:{}".format(my_exception))
  111. end_time = time.time()
  112. result['success'] = success
  113. result['args'] = args
  114. result['start_time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start_time))
  115. result['end_time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(end_time))
  116. return result
  117. if __name__ == "__main__":
  118. print("Program starts execution!")
  119. from waitress import serve
  120. serve(app, host="0.0.0.0", port=10101)
  121. print("server start!")
  122. # args = {"user": 'anweiguo', 'model': 'Zone', 'version': 1.0, 'hour': '06',
  123. # 'farmId': 'J00645', 'mongodb_database': 'db2', 'mongodb_read_table': 'j00645_ori_res', 'day_begin':'D1',
  124. # 'day_end': 'D1'}
  125. # df = get_data_from_mongo(args)
  126. # df.rename(columns={'dateTime': 'date_time'}, inplace=True)
  127. # df['date_time'] = pd.to_datetime(df['date_time'])
  128. # zip_path, zip_file = zip_temp_file(df, args)
  129. # upload_ftp(zip_path, zip_file, args)