def callback(ch, method, properties, body): #print("[X] Received %r" % (body.decode('utf-8'),)) message = body.decode('utf-8') #message = body.decode() jsonobj = json.loads(message) task_model_name = jsonobj["mn"] task_service_name = jsonobj["sn"] task_service_file = jsonobj["sf"] indicatorId = "" if ("indicator_id" in jsonobj): indicatorId = jsonobj["indicator_id"] print("准备执行service层%r的%r方法" % (task_service_file, task_service_name)) uuid = uuid_util.gen_uuid() task_model_name = getattr(TaskModel, task_model_name) service_class_name = importlib.import_module('fof.service.' + task_service_file) off_line_service = getattr(service_class_name, task_service_name) if indicatorId == "": model = OfflineTaskModel(task_model_name, off_line_service, "", uuid) else: model = OfflineTaskModel(task_model_name, off_line_service, "", uuid, indicatorId, "1") logic_processor.doLogic(model, ) print("消费完成") ch.basic_ack(delivery_tag=method.delivery_tag)
def callback(ch, method, properties, body): # print("[X] Received %r" % (body.decode('utf-8'),)) message = body.decode('utf-8') jsonobj = json.loads(message) task_model_name = jsonobj["mn"] task_service_name = jsonobj["sn"] task_service_file = jsonobj["sf"] indicatorId = "" if ("indicator_id" in jsonobj): indicatorId = jsonobj["indicator_id"] print("准备执行service层%r" % task_service_name) uuid = uuid_util.gen_uuid() task_model_name = getattr(TaskModel, task_model_name) service_class_name = __import__(task_service_file) off_line_service = getattr(service_class_name, task_service_name) if indicatorId == "": model = OfflineTaskModel(task_model_name, off_line_service, "", uuid) else: model = OfflineTaskModel(task_model_name, off_line_service, "", uuid, indicatorId, "1") #启动长时间的任务线程执行 # worker = threading.Thread(target=thread_func, args=(ch, method, body,model)) logic_processor.doLogic(model) # time1 = time.time() # flag = True # while flag: # time2 = time.time() # span = time2 - time1 # if span > 28800: # flag = False # print("退出任务") ch.basic_ack(delivery_tag=method.delivery_tag)
def other_indicator(request): """ 离线计算其他指标 :param request: : indicatorId: 指标Id :return: """ req = request.data try: bun = Bunch(req) indicatorId = bun.indicatorId except: raise Error("indicatorId 不能为空") uuid = uuid_util.gen_uuid() model = OfflineTaskModel(TaskModel.qitazhibiao, offline_value_service.other_indicator, request, uuid, extVal=indicatorId) ThreadTool.pool.submit(logic_processor.doLogic, (model,)) view = OffLineView(uuid) return JsonResponse(convert_to_dict(view))
def indicator_score_rank(request, format=None): """ 指标打分 :param request: indicatorId : 指标id {"indicatorId": "1"} :return: """ req = request.data logger.info("req:", req) try: m = Bunch(req) indicatorId = m.indicatorId except Exception as ae: raise Error("参数验证异常", ae) uuid = uuid_util.gen_uuid() model = OfflineTaskModel(TaskModel.zhibiaodafen, offline_score_service.indicator_score_rank, request, uuid, indicatorId, "1") ThreadTool.pool.submit(logic_processor.doLogic, (model, )) view = OffLineView(uuid) return JsonResponse(convert_to_dict(view))
def maximum_drawdown(request): """ 离线计算最大回撤 :param request: :return: """ uuid = uuid_util.gen_uuid() model = OfflineTaskModel(TaskModel.zuidahuiche, offline_value_service.maximum_drawdown, request, uuid) ThreadTool.pool.submit(logic_processor.doLogic, (model,)) view = OffLineView(uuid) return JsonResponse(convert_to_dict(view))
def volatility(request): """ 离线计算年化波动率 :param request: :return: """ uuid = uuid_util.gen_uuid() model = OfflineTaskModel(TaskModel.nianhuabodonglv, offline_value_service.volatility, request, uuid) ThreadTool.pool.submit(logic_processor.doLogic, (model,)) view = OffLineView(uuid) return JsonResponse(convert_to_dict(view))
def risk_single(request): """ 风险因子业绩归因模块 fof_single_attr_riskmodel :param request: :return: """ uuid = uuid_util.gen_uuid() model = OfflineTaskModel(TaskModel.stockRiskSingle, offline_index_service.risk_single, request, uuid) ThreadTool.pool.submit(logic_processor.doLogic, (model, )) view = OffLineView(uuid) return JsonResponse(convert_to_dict(view))
def stockexpousre(request): """ 导入因子库 :param request: :return: """ uuid = uuid_util.gen_uuid() model = OfflineTaskModel(TaskModel.stockexpousre, offline_index_service.stockexpousre, request, uuid) ThreadTool.pool.submit(logic_processor.doLogic, (model, )) view = OffLineView(uuid) return JsonResponse(convert_to_dict(view))
def intervalYield(request): """ 离线计算区间收益率 :return: """ uuid = uuid_util.gen_uuid() model = OfflineTaskModel(TaskModel.qujianshouyilv, offline_value_service.interval_yield, request, uuid) ThreadTool.pool.submit(logic_processor.doLogic, model) view = OffLineView(uuid) return JsonResponse(convert_to_dict(view))
def calmar_ratio(request): """ 离线计算卡玛比率 :param request: :return: """ uuid = uuid_util.gen_uuid() model = OfflineTaskModel(TaskModel.kamabilv, offline_value_service.calmar_ratio, request, uuid) ThreadTool.pool.submit(logic_processor.doLogic, (model,)) view = OffLineView(uuid) return JsonResponse(convert_to_dict(view))
def net_value(request): """ 基金净值风格划分 fof_fundnav_style 考虑到服务器的承载能力,该程序前期可每周更新,后续服务器运载能力加大,可改为每日更新 :param request: :return: """ uuid = uuid_util.gen_uuid() model = OfflineTaskModel(TaskModel.jinglifengge_profit_style, manager_service.net_value, request, uuid) ThreadTool.pool.submit(logic_processor.doLogic, (model, )) view = OffLineView(uuid) return JsonResponse(convert_to_dict(view))
def return_his(request): """ 基金经理股票筛选能力 table: fof_fund_main_stock_return_his :param request: :return: """ uuid = uuid_util.gen_uuid() model = OfflineTaskModel(TaskModel.jingliShaixuanNengli_return_his, manager_service.return_his, request, uuid) ThreadTool.pool.submit(logic_processor.doLogic, (model, )) view = OffLineView(uuid) return JsonResponse(convert_to_dict(view))
def compute_manager_product(request, format=None): """ 计算基金经理管理的产品信息 :param request: :return: """ uuid = uuid_util.gen_uuid() model = OfflineTaskModel(TaskModel.jinglichanpin, manager_service.compute_manager_product, request, uuid) ThreadTool.pool.submit(logic_processor.doLogic, (model, )) view = OffLineView(uuid) return JsonResponse(convert_to_dict(view))
def holding_style_all(request): """ 风格分析 - 持仓风格 fof_fund_stock_style :param request: :return: """ uuid = uuid_util.gen_uuid() model = OfflineTaskModel(TaskModel.jinglifengge_holding_stype_all, manager_service.holding_style_all, request, uuid) ThreadTool.pool.submit(logic_processor.doLogic, (model, )) view = OffLineView(uuid) return JsonResponse(convert_to_dict(view))
def equ_timing(request): """ 基金经理股票择时能力评价模型 请于每季结束后的第一个月的15日开始运行本程序(即基金季报发布),按日更新,运行至该月末 如1季度结束后,于4月15日~4月30日每日更新该数据 :param request: :return: """ uuid = uuid_util.gen_uuid() model = OfflineTaskModel(TaskModel.jingliNengli, manager_service.equ_timing, request, uuid) ThreadTool.pool.submit(logic_processor.doLogic, (model, )) view = OffLineView(uuid) return JsonResponse(convert_to_dict(view))
def holding_style_main(request): """ 风格分析-持仓风格 fof_fund_tentop_stock_style 重仓股数风格暴露数据,请于每季结束后的第一个月的15日开始运行本程序,按日更新,运行至该月末 全部持仓数据,请于每年的8月21日~8月31日,以及3月21日~3月31日运行 :param request: :return: """ uuid = uuid_util.gen_uuid() model = OfflineTaskModel(TaskModel.jinglifengge_holding_stype_main, manager_service.holding_style_main, request, uuid) ThreadTool.pool.submit(logic_processor.doLogic, (model, )) view = OffLineView(uuid) return JsonResponse(convert_to_dict(view))
def industry_config_score(request): """ 离线计算基金经理行业配置能力 :keyword 表 fof_fund_industry_score 请于每季结束后的第一个月的15日开始运行本程序(即基金季报发布),按日更新,运行至该月末 如1季度结束后,于4月15日~4月30日每日更新该数据 :param request: :return: """ uuid = uuid_util.gen_uuid() model = OfflineTaskModel(TaskModel.jingliPeizhiNengli_score, manager_service.industry_config_score, request, uuid) ThreadTool.pool.submit(logic_processor.doLogic, (model, )) view = OffLineView(uuid) return JsonResponse(convert_to_dict(view))
def hand_turn_over(request): """ 能力分析-持股集中度、换手率 fof_fund_stock_porfolio 该程序于每半年进行一次更新 请于每年的3月20日~3月31日以及8月20日~8月31日更新 由于程序运行量不大,若更新时间配置麻烦,可设定为每日更新 :param request: :return: """ uuid = uuid_util.gen_uuid() model = OfflineTaskModel(TaskModel.jinglifengge_hand_change_rate, manager_service.hand_turn_over, request, uuid) ThreadTool.pool.submit(logic_processor.doLogic, (model, )) view = OffLineView(uuid) return JsonResponse(convert_to_dict(view))
print(model) class EnchanceThread(Thread): def __init__(self, group: None = ..., target: Optional[Callable[..., Any]] = ..., name: Optional[str] = ..., args: Iterable = ..., kwargs: Mapping[str, Any] = ..., *, daemon: Optional[bool] = ...) -> None: super().__init__(group, target, name, args, kwargs, daemon=daemon) if __name__ == '__main__': # pool = ThreadPoolExecutor(max_workers=100, thread_name_prefix='async_thread_pool') ThreadTool() # # request = 123123 # uuid = 3 # pool.submit(runnable, (request,), uuid=uuid) # task = r3(runnable, request, uuid=uuid) model = OfflineTaskModel("区间收益率", r5, None, None) # r3(r5, model) # pool.submit(r5, (model,)) ThreadTool.add_task(r5, model)
def doLogic(model: OfflineTaskModel): #model = model[0] model = model start_time = time.time() taskType = model.taskType uuid = model.uuid taskModel = model.taskModel func = model.func if model.extVal is not None: res = mysqlops.fetch_one( MysqlConf.DB.fof, "select indicator_name from fof_index where indicator_code ='" + model.extVal + "'") if res is None: raise Error("指标id不存在:{}".format(model.extVal)) taskName = res['indicator_name'].decode("utf-8") model.otherIndexName = taskName else: taskName = taskModel.value[0] if OPEN_PREVENT_DUPLICATION == 'False': # 如果开启了任务防重就做判断拦截 sql = "select task_status " \ "from fof_task " \ "where DATE(UPDATE_TIME) = CURRENT_DATE() " \ "AND task_name='{}' " \ "AND task_type='{}' " \ "AND task_status in (0,1)" \ .format(taskName, taskType) res = mysqlops.fetch_one(MysqlConf.DB.fof, sql) if res: log.warning("taskName:{},taskType:{},今天的任务处理状态为{},不需要再做处理".format( taskName, taskType, res)) return sql = "INSERT INTO fof_task VALUES ( %s, %s, %s, %s,%s ,%s,%s)" tp = (uuid, taskName, taskType, TaskStatus.accept.value[0], "", datetime.now(), datetime.now()) mysqlops.insert_one(MysqlConf.DB.fof, sql, tp) log.info("taskType:%s,taskName:%s,开始 查询流水号:%s" % (taskType, taskName, uuid)) try: func(model) except Exception as e: log.exception("未知异常:\n") sql = "update fof_task set task_status=%s,error_msg = %s,update_time = %s where task_id = %s" tp = (TaskStatus.fail.value[0], str(e), datetime.now(), uuid) mysqlops.insert_one(MysqlConf.DB.fof, sql, tp) hours, minutes, seconds = compute_time(start_time) log.error("taskType:%s,taskName:%s,出错 %s,查询流水号:%s,耗时: %s" % (taskType, taskName, e, uuid, "{:>02d}:{:>02d}:{:>02d}".format(hours, minutes, seconds))) return sql = "update fof_task set task_status=%s,update_time = %s where task_id = %s" tp = (TaskStatus.ok.value[0], datetime.now(), uuid) mysqlops.insert_one(MysqlConf.DB.fof, sql, tp) hours, minutes, seconds = compute_time(start_time) log.info("taskType:%s,taskName:%s,成功,查询流水号:%s,耗时: %s" % (taskType, taskName, uuid, "{:>02d}:{:>02d}:{:>02d}".format( hours, minutes, seconds)))