|
@@ -11,10 +11,11 @@ import pandas as pd
|
|
from pytz import timezone
|
|
from pytz import timezone
|
|
from flask import Flask,request,jsonify
|
|
from flask import Flask,request,jsonify
|
|
import time, datetime, os, traceback, re
|
|
import time, datetime, os, traceback, re
|
|
-import logging, zipfile, tempfile, shutil, fnmatch
|
|
|
|
|
|
+import zipfile, tempfile, shutil, fnmatch
|
|
from common.database_dml import insert_data_into_mongo
|
|
from common.database_dml import insert_data_into_mongo
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
|
-
|
|
|
|
|
|
+from common.logs import Log
|
|
|
|
+logger = Log('data-processing').logger
|
|
|
|
|
|
app = Flask('data_nwp_ftp——service')
|
|
app = Flask('data_nwp_ftp——service')
|
|
|
|
|
|
@@ -27,10 +28,7 @@ def update_thread():
|
|
def start_jobs():
|
|
def start_jobs():
|
|
scheduler = BackgroundScheduler()
|
|
scheduler = BackgroundScheduler()
|
|
scheduler.configure({'timezone': timezone("Asia/Shanghai")})
|
|
scheduler.configure({'timezone': timezone("Asia/Shanghai")})
|
|
- scheduler.add_job(func=download_zip_files_from_ftp, args=['00'], trigger="cron", hour=0, minute=0)
|
|
|
|
- scheduler.add_job(func=download_zip_files_from_ftp, args=['06'], trigger="cron", hour=6, minute=0)
|
|
|
|
- scheduler.add_job(func=download_zip_files_from_ftp, args=['12'], trigger="cron", hour=12, minute=0)
|
|
|
|
- scheduler.add_job(func=download_zip_files_from_ftp, args=['18'], trigger="cron", hour=18, minute=0)
|
|
|
|
|
|
+ scheduler.add_job(func=download_zip_files_from_ftp, trigger="interval", seconds=300)
|
|
scheduler.start()
|
|
scheduler.start()
|
|
|
|
|
|
def match_date(date, filename):
|
|
def match_date(date, filename):
|
|
@@ -69,10 +67,20 @@ def delete_zip_files(date):
|
|
except OSError as e:
|
|
except OSError as e:
|
|
print(f"Error deleting file {csv_file_path}: {e.strerror}")
|
|
print(f"Error deleting file {csv_file_path}: {e.strerror}")
|
|
|
|
|
|
-def download_zip_files_from_ftp(hour):
|
|
|
|
- date = datetime.datetime.now().strftime("%Y%m%d")
|
|
|
|
- date_2 = (datetime.datetime.now() - timedelta(days=2)).strftime("%Y%m%d")
|
|
|
|
- host, moment = 'xxl', hour
|
|
|
|
|
|
+def download_zip_files_from_ftp(moment=None):
|
|
|
|
+ now = datetime.datetime.now(pytz.utc).astimezone(timezone("Asia/Shanghai"))
|
|
|
|
+ date = now.strftime("%Y%m%d")
|
|
|
|
+ date_2 = (now - timedelta(days=2)).strftime("%Y%m%d")
|
|
|
|
+ if moment is None:
|
|
|
|
+ if now.hour >= 18:
|
|
|
|
+ moment = '18'
|
|
|
|
+ elif now.hour >= 12:
|
|
|
|
+ moment = '12'
|
|
|
|
+ elif now.hour >= 6:
|
|
|
|
+ moment = '06'
|
|
|
|
+ else:
|
|
|
|
+ moment = '00'
|
|
|
|
+ host = 'xxl'
|
|
ftp_host, ftp_user, ftp_password, remote_dir, local_dir = ftp_params[host]['host'], ftp_params[host]['user'], ftp_params[host]['password'], ftp_params[host]['remote_dir'], ftp_params['xxl']['local_dir']
|
|
ftp_host, ftp_user, ftp_password, remote_dir, local_dir = ftp_params[host]['host'], ftp_params[host]['user'], ftp_params[host]['password'], ftp_params[host]['remote_dir'], ftp_params['xxl']['local_dir']
|
|
zip_extension = f'meteoforce_{date}{str(moment)}_*.zip'
|
|
zip_extension = f'meteoforce_{date}{str(moment)}_*.zip'
|
|
zip_file_path = []
|
|
zip_file_path = []
|
|
@@ -93,10 +101,10 @@ def download_zip_files_from_ftp(hour):
|
|
local_file_path = os.path.join(local_dir, file_name)
|
|
local_file_path = os.path.join(local_dir, file_name)
|
|
|
|
|
|
with open(local_file_path, 'wb') as local_file:
|
|
with open(local_file_path, 'wb') as local_file:
|
|
- logging.info(f"Downloading {remote_file_path} to {local_file_path}")
|
|
|
|
|
|
+ logger.info(f"Downloading {remote_file_path} to {local_file_path}")
|
|
ftp.retrbinary(f'RETR {remote_file_path}', local_file.write)
|
|
ftp.retrbinary(f'RETR {remote_file_path}', local_file.write)
|
|
|
|
|
|
- logging.info(f"Downloaded {file_name}")
|
|
|
|
|
|
+ logger.info(f"Downloaded {file_name}")
|
|
zip_file_path.append(local_file_path)
|
|
zip_file_path.append(local_file_path)
|
|
# 解压 ZIP 文件到临时目录
|
|
# 解压 ZIP 文件到临时目录
|
|
for zip_file_p in zip_file_path:
|
|
for zip_file_p in zip_file_path:
|
|
@@ -136,7 +144,7 @@ def select_file_to_mongo(args):
|
|
df = select_dx_from_nwp(weather_power, args)
|
|
df = select_dx_from_nwp(weather_power, args)
|
|
insert_data_into_mongo(df, args)
|
|
insert_data_into_mongo(df, args)
|
|
else:
|
|
else:
|
|
- logging.info(f"CSV 文件 {csv_file_power} 或 {csv_file_weather} 在目标目录 {farmId} 中未找到")
|
|
|
|
|
|
+ logger.info(f"CSV 文件 {csv_file_power} 或 {csv_file_weather} 在目标目录 {farmId} 中未找到")
|
|
else:
|
|
else:
|
|
if csv_weather_path:
|
|
if csv_weather_path:
|
|
weather = select_dx_from_nwp(weather, args)
|
|
weather = select_dx_from_nwp(weather, args)
|
|
@@ -144,7 +152,7 @@ def select_file_to_mongo(args):
|
|
df = select_dx_from_nwp(weather, args)
|
|
df = select_dx_from_nwp(weather, args)
|
|
insert_data_into_mongo(df, args)
|
|
insert_data_into_mongo(df, args)
|
|
else:
|
|
else:
|
|
- logging.info(f"CSV 文件 {csv_file_weather} 在目标目录 {farmId} 中未找到")
|
|
|
|
|
|
+ logger.info(f"CSV 文件 {csv_file_weather} 在目标目录 {farmId} 中未找到")
|
|
|
|
|
|
|
|
|
|
def select_dx_from_nwp(df, args):
|
|
def select_dx_from_nwp(df, args):
|
|
@@ -177,11 +185,12 @@ def get_nwp_from_ftp():
|
|
start_time = time.time()
|
|
start_time = time.time()
|
|
result = {}
|
|
result = {}
|
|
success = 0
|
|
success = 0
|
|
- print("Program starts execution!")
|
|
|
|
|
|
+ args = {}
|
|
|
|
+ # print("data_nwp_ftp starts execution!")
|
|
try:
|
|
try:
|
|
args = request.values.to_dict()
|
|
args = request.values.to_dict()
|
|
# 1. 获取参数:日期,数据源,时刻,D0-9,场站ID,存储的 mongo 和表
|
|
# 1. 获取参数:日期,数据源,时刻,D0-9,场站ID,存储的 mongo 和表
|
|
- print('args', args)
|
|
|
|
|
|
+ # print('args', args)
|
|
logger.info(args)
|
|
logger.info(args)
|
|
# 2. 连接FTP,从FTP服务器中获取指定参数的压缩文件(定时任务)
|
|
# 2. 连接FTP,从FTP服务器中获取指定参数的压缩文件(定时任务)
|
|
# 3. 解压压缩文件,将其存储到mongo中
|
|
# 3. 解压压缩文件,将其存储到mongo中
|
|
@@ -191,32 +200,32 @@ def get_nwp_from_ftp():
|
|
my_exception = traceback.format_exc()
|
|
my_exception = traceback.format_exc()
|
|
my_exception.replace("\n", "\t")
|
|
my_exception.replace("\n", "\t")
|
|
result['msg'] = my_exception
|
|
result['msg'] = my_exception
|
|
|
|
+ logger.info("生产,获取原始nwp出错:{}".format(my_exception))
|
|
end_time = time.time()
|
|
end_time = time.time()
|
|
result['success'] = success
|
|
result['success'] = success
|
|
result['args'] = args
|
|
result['args'] = args
|
|
result['start_time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start_time))
|
|
result['start_time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start_time))
|
|
result['end_time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(end_time))
|
|
result['end_time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(end_time))
|
|
- print("Program execution ends!")
|
|
|
|
|
|
+ # print("Program execution ends!")
|
|
return result
|
|
return result
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if __name__ == "__main__":
|
|
print("Program starts execution!")
|
|
print("Program starts execution!")
|
|
- logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
|
|
- logger = logging.getLogger("data_nwp_ftp")
|
|
|
|
from waitress import serve
|
|
from waitress import serve
|
|
update_thread() #定时任务开启
|
|
update_thread() #定时任务开启
|
|
- current_time = datetime.datetime.now(pytz.utc).astimezone(timezone("Asia/Shanghai"))
|
|
|
|
- current_hour = current_time.hour
|
|
|
|
- if current_hour >= 18:
|
|
|
|
- current_hour = '18'
|
|
|
|
- elif current_hour >= 12:
|
|
|
|
- current_hour = '12'
|
|
|
|
- elif current_hour >= 6:
|
|
|
|
- current_hour = '06'
|
|
|
|
- else:
|
|
|
|
- current_hour = '00'
|
|
|
|
- threading.Thread(target=download_zip_files_from_ftp, args=(current_hour,)).start()
|
|
|
|
|
|
+ # current_time = datetime.datetime.now(pytz.utc).astimezone(timezone("Asia/Shanghai"))
|
|
|
|
+ # current_hour = current_time.hour
|
|
|
|
+ # if current_hour >= 18:
|
|
|
|
+ # current_hour = '18'
|
|
|
|
+ # elif current_hour >= 12:
|
|
|
|
+ # current_hour = '12'
|
|
|
|
+ # elif current_hour >= 6:
|
|
|
|
+ # current_hour = '06'
|
|
|
|
+ # else:
|
|
|
|
+ # current_hour = '00'
|
|
|
|
+ current_hour = '06' # 默认首次运行下载06时刻的zip
|
|
|
|
+ threading.Thread(target=download_zip_files_from_ftp, kwargs={'moment': current_hour}).start()
|
|
serve(app, host="0.0.0.0", port=10102)
|
|
serve(app, host="0.0.0.0", port=10102)
|
|
print("server start!")
|
|
print("server start!")
|
|
|
|
|