From 4715b0e95f8b7f4f8a612edc70b42db1f9ff8909 Mon Sep 17 00:00:00 2001 From: P3ngSaM <61768364+P3ngSaM@users.noreply.github.com> Date: Wed, 29 Jun 2022 17:24:33 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E6=8E=A5=E5=8F=A3=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Modules/Functions/FinIndex/FinIndexImpl.py | 3 +- Modules/Interface/InterfaceImpl.py | 183 +++++++++++++++++++++ Modules/Interface/InterfaceObj.py | 36 ++++ Modules/Interface/InterfaceRoutes.py | 82 +++++++++ Modules/Interface/InterfaceSetRoutes.py | 21 +++ Modules/Interface/__init__.py | 0 app.py | 4 + requirements.txt | 2 + 8 files changed, 330 insertions(+), 1 deletion(-) create mode 100644 Modules/Interface/InterfaceImpl.py create mode 100644 Modules/Interface/InterfaceObj.py create mode 100644 Modules/Interface/InterfaceRoutes.py create mode 100644 Modules/Interface/InterfaceSetRoutes.py create mode 100644 Modules/Interface/__init__.py diff --git a/Modules/Functions/FinIndex/FinIndexImpl.py b/Modules/Functions/FinIndex/FinIndexImpl.py index b4fedc2..4e759b3 100644 --- a/Modules/Functions/FinIndex/FinIndexImpl.py +++ b/Modules/Functions/FinIndex/FinIndexImpl.py @@ -121,6 +121,7 @@ class FinanceIndexImpl(object): @staticmethod def lock_calculate_func(**kwargs): + """锁定函数""" fid = kwargs['fid'] func = FinIndexObj() func.status = 'lock' @@ -141,7 +142,7 @@ class FinanceIndexImpl(object): '模型数据', '计算函数', {'函数ID': fid}, - ['函数名称', '说明', '函数说明', '方法', '参数', '标签', '状态'] + ['函数名称', '说明', '方法', '参数', '标签', '状态'] ) return record diff --git a/Modules/Interface/InterfaceImpl.py b/Modules/Interface/InterfaceImpl.py new file mode 100644 index 0000000..93f126c --- /dev/null +++ b/Modules/Interface/InterfaceImpl.py @@ -0,0 +1,183 @@ +import json +import time + +from DBHelper.MongoHelperInstance import DB_TEST +from Modules.Interface.InterfaceObj import InterfaceObj +from Utils.CommonUtil import CommonUtils +import requests + + +class InterfaceImpl(object): + + @staticmethod + def make_new_cid(): + new_cid = CommonUtils.random_code(8) + case = DB_TEST.find_single_column( + "模型数据", + "计算接口", + {"接口ID": new_cid}, + "接口ID" + ) is not None + while case: + new_cid = CommonUtils.random_code(8) + return new_cid + + def new_calculate_api(self, **kwargs): + """新建指标计算接口""" + data = kwargs['data'] + api = InterfaceObj() + api.api_id = self.make_new_cid() + api.api_name = data['api_name'] + api.describe = data['describe'] + api.address = data['address'] + api.params = list() + for item in data['params']: + param = api.Param() + param.name = item['name'] + param.mark = item['mark'] + api.tags = data['tags'] + api.edit_date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + api.status = 'normal' + + DB_TEST.insert_single_data( + '模型数据', + '计算接口', + api.fields_toggle() + ) + + return '新建成功' + + @staticmethod + def check_calculate_api(**kwargs): + """校验计算函数""" + address = kwargs['data']['address'] + params = kwargs['data']['params'] + + param_dict = dict() + for param in params: + param_dict[param['name']] = param['input'] + + res = requests.get(url=address, params=param_dict) + if res.status_code == 200: + return json.loads(res.text) + else: + return '校验失败' + + @staticmethod + def search_calculate_api(**kwargs): + """查询指标接口""" + + def make_search_body(param): + body = dict() + search_keys = list(param.keys()) + if 'name' in search_keys: + body['接口名称'] = {"$regex": param['name']} + + if 'tags' in search_keys: + body['标签'] = {"$in": param['tags']} + + return body + + def make_sort_body(param): + if param == 'asc': + body = {"编辑时间": 1} + else: + body = {"编辑时间": -1} + + return body + + data = kwargs['data'] + sort = data['sort'] + page_size = int(data['page_size']) + page_no = int(data['page_no']) + search_body = make_search_body(data) + sort_body = make_sort_body(sort) + page_size = 10 if page_size > 10 else page_size + + total = DB_TEST.find_all_data_with_count( + '模型数据', + '计算接口', + search_body + ) + + records = DB_TEST.find_data_by_page_with_sort( + '模型数据', + '计算接口', + search_body, + ['接口ID', '接口名称', '说明', '标签', '编辑时间'], + sort_body, + page_size, + page_no + ) + + result = { + "total": total, + "records": records + } + + return result + + @staticmethod + def lock_calculate_api(**kwargs): + """锁定接口""" + aid = kwargs['aid'] + api = InterfaceObj() + api.status = 'lock' + + DB_TEST.update_single_data( + '模型数据', + '计算接口', + {'接口ID': aid}, + api.fields_toggle() + ) + return '锁定成功' + + @staticmethod + def view_calculate_api(**kwargs): + aid = kwargs['aid'] + record = DB_TEST.find_single_data( + '模型数据', + '计算接口', + {'接口ID': aid}, + ['接口名称', '说明', '地址', '参数', '标签', '状态'] + ) + + return record + + @staticmethod + def edit_calculate_api(**kwargs): + data = kwargs['data'] + aid = data['api_id'] + + status = DB_TEST.find_single_column( + '模型数据', + '计算接口', + {'接口ID': aid}, + '状态' + ) + if status == 'lock': + return '函数已锁定' + + api = InterfaceObj() + api.api_name = data['api_name'] + api.describe = data['describe'] + api.address = data['address'] + api.params = list() + for params in data['params']: + param = api.Param() + param.name = params['name'] + param.mark = params['mark'] + api.params.append(param) + api.tags = data['tags'] + api.edit_date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + + update_data = api.fields_toggle() + + DB_TEST.update_single_data( + '模型数据', + '计算接口', + {'接口ID': aid}, + update_data + ) + return '修改成功' + diff --git a/Modules/Interface/InterfaceObj.py b/Modules/Interface/InterfaceObj.py new file mode 100644 index 0000000..4b35ec9 --- /dev/null +++ b/Modules/Interface/InterfaceObj.py @@ -0,0 +1,36 @@ +from Utils.ObjUtil import SpecObject +from Utils.ValidateUtil import ValidateAttr, Validate + + +class InterfaceObj(SpecObject): + """计算接口""" + + class Param(SpecObject): + """参数""" + name = ValidateAttr(field="name", type=str) + mark = ValidateAttr(field="mark", type=str) + + fields_map = { + "name": "参数名", + "mark": "备注" + } + + api_id = ValidateAttr(field="api_id", type=str) + api_name = ValidateAttr(field="api_name", type=str) + describe = ValidateAttr(field="describe", type=str) + address = ValidateAttr(field="address", type=str) + params = ValidateAttr(field="params", instance_list=Param) + tags = ValidateAttr(field="tags", type=list) + edit_date = ValidateAttr(field="edit_date", func=Validate.date_format) + status = ValidateAttr(field="status", type=str, in_list=['normal', 'lock']) + + fields_map = { + "api_id": "接口ID", + "api_name": "接口名称", + "describe": "说明", + "address": "地址", + "params": "参数", + "tags": "标签", + "edit_date": "编辑时间", + "status": "状态" + } diff --git a/Modules/Interface/InterfaceRoutes.py b/Modules/Interface/InterfaceRoutes.py new file mode 100644 index 0000000..7f65272 --- /dev/null +++ b/Modules/Interface/InterfaceRoutes.py @@ -0,0 +1,82 @@ +from flask import Blueprint, request + +from Modules.Interface.InterfaceImpl import InterfaceImpl +from Utils.ErrorUtil import APIReturnError +from Utils.RouteUtil import RouteParamsCheck + +interface_route = Blueprint('interface', __name__) + + +@interface_route.route('/new_calculate_api', methods=['POST']) +def new_calculate_api_route(): + """新建指标计算接口""" + try: + RouteParamsCheck(req=request.json, params=[ + "api_name", "describe", "address", "params", "tags" + ]).required() + data = request.json + impl = InterfaceImpl() + result = impl.new_calculate_api(data=data) + return {"info": "新增指标计算接口", "result": result}, 200 + except APIReturnError as e: + return {"info": e.__str__()}, e.status_code + + +@interface_route.route('/check_calculate_api', methods=['POST']) +def check_calculate_api_route(): + """校验指标计算接口""" + try: + RouteParamsCheck(req=request.json, params=["address", "params"]).required() + data = request.json + impl = InterfaceImpl() + result = impl.check_calculate_api(data=data) + return {"info": "校验指标计算接口", "result": result}, 200 + except APIReturnError as e: + return {"info": e.__str__()}, e.status_code + + +@interface_route.route('/search_calculate_api', methods=['POST']) +def search_indicators_api_route(): + """查询指标计算接口""" + try: + RouteParamsCheck(req=request.json, params=["sort", "page_no", "page_size"]).required() + data = request.json + impl = InterfaceImpl() + result = impl.search_calculate_api(data=data) + return {"info": "查询指标计算接口", "result": result}, 200 + except APIReturnError as e: + return {"info": e.__str__()}, e.status_code + + +@interface_route.route('/lock_calculate_api', methods=['GET']) +def lock_calculate_api_route(): + """锁定指标计算接口""" + try: + RouteParamsCheck(request.args, ["aid"]).required() + aid = request.args["aid"] + impl = InterfaceImpl() + result = impl.lock_calculate_api(aid=aid) + return {"info": "锁定指标计算接口", "result": result}, 200 + except APIReturnError as e: + return {"info": e.__str__()}, e.status_code + + +@interface_route.route('/operate_calculate_api', methods=['GET', 'POST']) +def operate_calculate_api_route(): + """查看/编辑接口""" + try: + if request.method == 'GET': + RouteParamsCheck(request.args, ["aid"]).required() + aid = request.args["aid"] + impl = InterfaceImpl() + result = impl.view_calculate_api(aid=aid) + return {"info": "查看指标计算接口", "result": result}, 200 + if request.method == 'POST': + RouteParamsCheck(request.json, ["api_id", "api_name", "describe", "address", "params", "tags"]).required() + data = request.json + impl = InterfaceImpl() + result = impl.edit_calculate_api(data=data) + return {"info": "编辑指标计算接口", "result": result}, 200 + + except APIReturnError as e: + return {"info": e.__str__()}, e.status_code diff --git a/Modules/Interface/InterfaceSetRoutes.py b/Modules/Interface/InterfaceSetRoutes.py new file mode 100644 index 0000000..3bdeca0 --- /dev/null +++ b/Modules/Interface/InterfaceSetRoutes.py @@ -0,0 +1,21 @@ +from flask import Blueprint, request + +from Utils.ErrorUtil import APIReturnError +from Utils.RouteUtil import RouteParamsCheck + +apiset_route = Blueprint('apiset', __name__) + + +@apiset_route.route('/interface_test', methods=['GET']) +def interface_test_route(): + """测试调用接口""" + try: + RouteParamsCheck(req=request.args, params=["param1", "param2", "param3"]).required() + args = request.args + param1 = float(args['param1']) + param2 = float(args['param2']) + param3 = float(args['param3']) + answer = (param1 + param2) / param3 + return str(answer) + except APIReturnError as e: + return {"info": e.__str__()}, e.status_code diff --git a/Modules/Interface/__init__.py b/Modules/Interface/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app.py b/app.py index cd2440e..d7b9a0e 100644 --- a/app.py +++ b/app.py @@ -3,6 +3,8 @@ from flask_cors import * from Modules.Functions.FinIndex.FinIndexRoutes import finance_route from Modules.Indicators.Tags.TagsRoutes import tags_route +from Modules.Interface.InterfaceRoutes import interface_route +from Modules.Interface.InterfaceSetRoutes import apiset_route app = Flask(__name__) CORS(app, supports_credentials=True) @@ -11,6 +13,8 @@ app.config['JSON_SORT_KEYS'] = False app.register_blueprint(finance_route, url_prefix='/rating/functions/finance') app.register_blueprint(tags_route, url_prefix='/rating/indicator/tags') +app.register_blueprint(interface_route, url_prefix='/rating/interface') +app.register_blueprint(apiset_route, url_prefix='/rating/interface/apiset') @app.route('/admin/version') diff --git a/requirements.txt b/requirements.txt index 35304b2..7dfac23 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,5 @@ flask_cors gevent pymongo~=3.11.0 Werkzeug~=2.1.2 +PyYAML~=6.0 +requests~=2.28.0 \ No newline at end of file