import pandas as pd from flask import Flask,request,jsonify import time import logging import traceback from functools import reduce from common.database_dml import get_df_list_from_mongo,insert_data_into_mongo app = Flask('data_join——service') @app.route('/hello', methods=['POST']) def hello(): return jsonify(message='Hello, World!') #1.AGC/AVC信号判断限电(有的场站准 有的不准) 1种方法 数据库数据有问题 暂时用不了 def data_merge(df_list, args): join_key,join_type,features = args['join_key'], args['join_type'], str_to_list(args['col_reserve']) result = reduce(lambda left, right: pd.merge(left, right, how=join_type, on=join_key), df_list) if len(features)==0: return result else: return result[features] @app.route('/data_join', methods=['POST']) def data_join(): # 获取程序开始时间 start_time = time.time() result = {} success = 0 print("Program starts execution!") try: args = request.values.to_dict() print('args',args) logger.info(args) df_list = get_df_list_from_mongo(args) res_df = data_merge(df_list,args) insert_data_into_mongo(res_df,args) success = 1 except Exception as e: my_exception = traceback.format_exc() my_exception.replace("\n","\t") result['msg'] = my_exception end_time = time.time() result['success'] = success result['args'] = args 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)) print("Program execution ends!") return result def str_to_list(arg): if arg == '': return [] else: return arg.split(',') if __name__=="__main__": print("Program starts execution!") logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') logger = logging.getLogger("data_join") from waitress import serve serve(app, host="0.0.0.0", port=10094) print("server start!")