فهرست منبع

After modifying the code, the project started successfully.

wangxq 2 ماه پیش
والد
کامیت
adfffd5b76

+ 4 - 0
app/__init__.py

@@ -2,12 +2,16 @@ from flask import Flask
 from flask_cors import CORS
 import logging
 from app.config.config import Config
+from app.services.package_function import db
 
 def create_app(config_class=Config):
     """Create and configure the Flask application"""
     app = Flask(__name__)
     app.config.from_object(config_class)
     
+    # Initialize SQLAlchemy
+    db.init_app(app)
+    
     # Enable CORS
     CORS(
         app,

+ 13 - 13
app/api/data_interface/routes.py

@@ -1,5 +1,5 @@
 from flask import request
-from app import app
+from app.api.data_interface import bp
 from app.models.result import success, failed
 from app.core.graph.graph_operations import connect_graph, MyEncoder, create_or_get_node
 from app.core.data_interface import interface
@@ -9,7 +9,7 @@ import json
 
 
 # 数据标准新增 data_standard
-@app.route('/data/standard/add', methods=['POST'])
+@bp.route('/data/standard/add', methods=['POST'])
 def data_standard_add():
     try:
         # 传入请求参数
@@ -31,7 +31,7 @@ def data_standard_add():
 
 
 # 数据标准详情 data_standard
-@app.route('/data/standard/detail', methods=['POST'])
+@bp.route('/data/standard/detail', methods=['POST'])
 def data_standard_detail():
     try:
         # 传入请求参数
@@ -58,7 +58,7 @@ def data_standard_detail():
 
 
 # 数据标准代码 data_standard
-@app.route('/data/standard/code', methods=['POST'])
+@bp.route('/data/standard/code', methods=['POST'])
 def data_standard_code():
     try:
         # 传入请求参数
@@ -81,7 +81,7 @@ def data_standard_code():
 
 
 # 数据标准更新 data_standard 未加到接口文档
-@app.route('/data/standard/update', methods=['POST'])
+@bp.route('/data/standard/update', methods=['POST'])
 def data_standard_update():
     try:
         # 传入请求参数
@@ -102,7 +102,7 @@ def data_standard_update():
 
 
 # 数据标准列表展示
-@app.route('/data/standard/list', methods=['POST'])
+@bp.route('/data/standard/list', methods=['POST'])
 def data_standard_list():
     try:
         # 传入请求参数
@@ -129,7 +129,7 @@ def data_standard_list():
 
 
 # 数据标准的图谱(血缘关系Kinship+影响关系Impact+所有关系all)
-@app.route('/data/standard/graph/all', methods=['POST'])
+@bp.route('/data/standard/graph/all', methods=['POST'])
 def data_standard_graph_all():
     try:
         # 传入请求参数
@@ -149,7 +149,7 @@ def data_standard_graph_all():
 
 
 # 数据标签新增 data_label
-@app.route('/data/label/add', methods=['POST'])
+@bp.route('/data/label/add', methods=['POST'])
 def data_label_add():
     try:
         # 传入请求参数
@@ -169,7 +169,7 @@ def data_label_add():
 
 
 # 数据标签详情 data_label
-@app.route('/data/label/detail', methods=['POST'])
+@bp.route('/data/label/detail', methods=['POST'])
 def data_label_detail():
     try:
         # 传入请求参数
@@ -191,7 +191,7 @@ def data_label_detail():
 
 
 # 数据标签列表展示(分类,名称,时间检索)
-@app.route('/data/label/list', methods=['POST'])
+@bp.route('/data/label/list', methods=['POST'])
 def data_label_list():
     try:
         # 传入请求参数
@@ -218,7 +218,7 @@ def data_label_list():
 
 
 # 24.11.19 数据标签动态识别分组
-@app.route('/data/label/dynamic/identify', methods=['POST'])
+@bp.route('/data/label/dynamic/identify', methods=['POST'])
 def data_label_dynamic_identify():
     try:
         # 传入请求参数
@@ -235,7 +235,7 @@ def data_label_dynamic_identify():
 
 
 # 数据标签的图谱(血缘关系Kinship+影响关系Impact+所有关系all)
-@app.route('/data/label/graph/all', methods=['POST'])
+@bp.route('/data/label/graph/all', methods=['POST'])
 def data_label_graph():
     try:
         # 传入请求参数
@@ -255,7 +255,7 @@ def data_label_graph():
 
 
 # 删除标签、标准、指标间的关系
-@app.route('/metric/label/standard/delete', methods=['POST'])
+@bp.route('/metric/label/standard/delete', methods=['POST'])
 def metric_label_standard_delete():
     try:
         # 传入请求参数

+ 13 - 13
app/api/data_model/routes.py

@@ -1,12 +1,12 @@
 from flask import request
-from app import app
+from app.api.data_model import bp
 from app.models.result import success, failed
-from app.routes.graph_routes import MyEncoder
+from app.api.graph.routes import MyEncoder
 from app.core.data_model import model as model_functions
 import json
 
 # 2024.09.11 数据模型血缘关系(传入数据资源id)
-@app.route('/model/data/relation', methods=['POST'])
+@bp.route('/model/data/relation', methods=['POST'])
 def data_model_relation():
     try:
         # 传入请求参数
@@ -21,7 +21,7 @@ def data_model_relation():
 
 
 # 传入一个数据资源的id,返回多个有血缘关系的数据资源
-@app.route('/model/relatives/relation', methods=['POST'])
+@bp.route('/model/relatives/relation', methods=['POST'])
 def data_relatives_relation():
     try:
         # 传入请求参数
@@ -48,7 +48,7 @@ def data_relatives_relation():
 
 
 # DDL数据模型保存
-@app.route('/data/model/save', methods=['POST'])
+@bp.route('/data/model/save', methods=['POST'])
 def data_model_save():
     # 传入请求参数
     receiver = request.get_json()
@@ -72,7 +72,7 @@ def data_model_save():
 
 
 # 新建数据模型请求接口(从数据资源中选取)
-@app.route('/model/data/search', methods=['POST'])
+@bp.route('/model/data/search', methods=['POST'])
 def data_model_search():
     # 传入请求参数
     receiver = request.get_json()
@@ -97,7 +97,7 @@ def data_model_search():
 
 
 # 新建数据模型请求接口(从数据模型中选取)
-@app.route('/model/data/model/add', methods=['POST'])
+@bp.route('/model/data/model/add', methods=['POST'])
 def data_model_model_add():
     # 传入请求参数
     receiver = request.get_json()
@@ -121,7 +121,7 @@ def data_model_model_add():
 
 
 # 数据模型-详情接口
-@app.route('/data/model/detail', methods=['POST'])
+@bp.route('/data/model/detail', methods=['POST'])
 def data_model_detail():
     try:
         # 传入请求参数
@@ -138,7 +138,7 @@ def data_model_detail():
 
 
 # 删除数据模型
-@app.route('/data/model/delete', methods=['POST'])
+@bp.route('/data/model/delete', methods=['POST'])
 def data_model_delete():
     try:
         # 传入请求参数
@@ -163,7 +163,7 @@ def data_model_delete():
 
 
 # 列表 数据模型查询
-@app.route('/data/model/list', methods=['POST'])
+@bp.route('/data/model/list', methods=['POST'])
 def data_model_list():
     try:
         # 传入请求参数
@@ -191,7 +191,7 @@ def data_model_list():
 
 
 # 数据模型的图谱(血缘关系Kinship+影响关系Impact+所有关系all)
-@app.route('/data/model/graph/all', methods=['POST'])
+@bp.route('/data/model/graph/all', methods=['POST'])
 def data_model_graph_all():
     try:
         # 传入请求参数
@@ -212,7 +212,7 @@ def data_model_graph_all():
 
 
 # 数据模型的列表图谱
-@app.route('/data/model/list/graph', methods=['POST'])
+@bp.route('/data/model/list/graph', methods=['POST'])
 def data_model_list_graph():
     try:
         # 传入请求参数
@@ -270,7 +270,7 @@ def data_model_list_graph():
 
 
 # 更新数据模型
-@app.route('/data/model/update', methods=['POST'])
+@bp.route('/data/model/update', methods=['POST'])
 def data_model_update():
     try:
         # 传入请求参数

+ 6 - 5
app/api/production_line/__init__.py

@@ -3,9 +3,10 @@ Production Line API module
 包含生产线相关的API接口
 """
 
-from app.api.production_line.routes import production_line_list, production_line_graph
+from flask import Blueprint
 
-__all__ = [
-    'production_line_list',
-    'production_line_graph',
-] 
+bp = Blueprint('production_line', __name__)
+
+from app.api.production_line import routes
+
+__all__ = ['bp'] 

+ 5 - 5
app/api/production_line/routes.py

@@ -1,13 +1,13 @@
 from flask import request
-from app import app
-from app.model.result import success, failed
-from app.routes.graph_routes import MyEncoder, connect_graph
+from app.api.production_line import bp
+from app.models.result import success, failed
+from app.api.graph.routes import MyEncoder, connect_graph
 from app.core.production_line import production_draw_graph
 import json
 
 
 # 生产线列表
-@app.route('/production/line/list', methods=['POST'])
+@bp.route('/production/line/list', methods=['POST'])
 def production_line_list():
     """
     获取生产线列表,支持分页和名称过滤
@@ -65,7 +65,7 @@ def production_line_list():
 
 
 # 根据生产线列表,传入id,绘制图谱
-@app.route('/production/line/graph', methods=['POST'])
+@bp.route('/production/line/graph', methods=['POST'])
 def production_line_graph():
     """
     根据生产线ID绘制关系图谱

+ 3 - 2
app/core/llm/__init__.py

@@ -4,9 +4,10 @@ LLM服务模块
 """
 
 from app.core.llm.llm_service import llm_client
-from app.core.llm.code_generation import code_generate_standard
+from app.core.llm.code_generation import code_generate_standard, code_generate_metric
 
 __all__ = [
     'llm_client',
-    'code_generate_standard'
+    'code_generate_standard',
+    'code_generate_metric'
 ] 

+ 28 - 0
app/core/llm/code_generation.py

@@ -36,4 +36,32 @@ def code_generate_standard(describe, relation):
         return result if result else "代码生成失败,请重试"
     except Exception as e:
         logger.error(f"代码生成失败: {str(e)}")
+        return f"代码生成错误: {str(e)}"
+
+def code_generate_metric(content, relation):
+    """
+    生成数据指标相关的代码
+    
+    Args:
+        content: 指标规则描述
+        relation: 映射关系字典
+        
+    Returns:
+        str: 生成的代码
+    """
+    try:
+        prompt = f"""
+        请根据以下指标规则和映射关系生成一个Python函数:
+        
+        指标规则: {content}
+        
+        映射关系: {relation}
+        
+        请提供标准实现的Python代码。
+        """
+        
+        result = llm_client(prompt)
+        return result if result else "代码生成失败,请重试"
+    except Exception as e:
+        logger.error(f"指标代码生成失败: {str(e)}")
         return f"代码生成错误: {str(e)}" 

+ 15 - 16
app/services/package_function.py

@@ -1,31 +1,30 @@
 # 封装mysql执行函数、创建节点函数
-from app.core.graph.graph_operations import mysql_configs
+from flask_sqlalchemy import SQLAlchemy
 from app.core.graph.graph_operations import connect_graph
 from py2neo import Node, RelationshipMatch
 
+db = SQLAlchemy()
 
 def execute_sql(cur, sql, params):
-    cur.execute(sql, params)
-    return cur.fetchall()
+    result = db.session.execute(sql, params)
+    return result.fetchall()
 
 
 def sql_commit(sql):
-    link = mysql_configs.POOL.connection()
-    cur = link.cursor()
-    cur.execute(sql)
-    link.commit()
-    cur.close()  # 关闭游标
-    link.close()  # 关闭连接
+    try:
+        db.session.execute(sql)
+        db.session.commit()
+    except Exception as e:
+        db.session.rollback()
+        raise e
 
 
 def sql_execute_result(sql):
-    link = mysql_configs.POOL.connection()
-    cur = link.cursor()
-    cur.execute(sql)
-    result = cur.fetchall()
-    cur.close()  # 关闭游标
-    link.close()  # 关闭连接
-    return result
+    try:
+        result = db.session.execute(sql)
+        return result.fetchall()
+    except Exception as e:
+        raise e
 
 
 # 创建或获取节点

+ 5 - 5
application.py

@@ -1,13 +1,13 @@
 from app import create_app
-from app.config.config import Config  # 新增配置导入
+from app.config.config import Config, DevelopmentConfig  # 新增配置导入
 
-app = create_app()
+app = create_app(DevelopmentConfig)
 
 if __name__ == '__main__':
     # 修改后带端口配置的启动命令
     app.run(
         host='0.0.0.0',
-        port=Config.PORT,  # 从配置类获取端口
-        debug=Config.DEBUG,
-        use_reloader=False if Config.PLATFORM == 'linux' else True
+        port=DevelopmentConfig.PORT,  # 从配置类获取端口
+        debug=DevelopmentConfig.DEBUG,
+        use_reloader=False if DevelopmentConfig.PLATFORM == 'linux' else True
     )