import pandas as pd from flask import Flask,request,jsonify import time import logging import traceback from common.database_dml import get_data_from_mongo,insert_data_into_mongo app = Flask('post_processing——service') @app.route('/hello', methods=['POST']) def hello(): return jsonify(message='Hello, World!') def predict_result_adjustment(df, args): col_time = args['col_time'] col_adjust = args['col_adjust'] ratio_dict = eval(args['ratio']) df['hour'] = df[col_time].apply(lambda x:x[11:13]) rates = ratio_dict.keys() for rate in rates: hours = ratio_dict[rate] df.loc[df['hour'].isin(hours), col_adjust] *= float(rate) return df.drop('hour', axis=1) @app.route('/pre_post_processing', 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_pre = get_data_from_mongo(args) res_df = predict_result_adjustment(df_pre, 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 if __name__=="__main__": print("Program starts execution!") logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') logger = logging.getLogger("post_processing") from waitress import serve serve(app, host="0.0.0.0", port=10107) print("server start!")