Browse Source

修复了data/label/list 和 api/resource/list的问题

wangxq 1 tháng trước cách đây
mục cha
commit
62f59863a7

+ 13 - 7
app/api/data_metric/routes.py

@@ -244,13 +244,19 @@ def data_metric_list_graph():
             collect(DISTINCT {{from: toString(id(n)), to: toString(id(child)), text: '下级'}}) AS lines
         RETURN nodes  + nodes2 AS nodes, lines  AS lines
         """
-        data = connect_graph.run(query, **params)
-        res = {}
-        for item in data:
-            res = {
-                "nodes": [record for record in item['nodes'] if record['id']],
-                "lines": [record for record in item['lines'] if record['from'] and record['to']],
-            }
+        # 修复:使用正确的session方式执行查询
+        driver = connect_graph()
+        if not driver:
+            return json.dumps(failed({}, "无法连接到数据库"), ensure_ascii=False, cls=MyEncoder)
+            
+        with driver.session() as session:
+            result = session.run(query, **params)
+            res = {}
+            for item in result:
+                res = {
+                    "nodes": [record for record in item['nodes'] if record['id']],
+                    "lines": [record for record in item['lines'] if record['from'] and record['to']],
+                }
         return json.dumps(success(res, "success"), ensure_ascii=False, cls=MyEncoder)
     except Exception as e:
         return json.dumps(failed({}, str(e)), ensure_ascii=False, cls=MyEncoder)

+ 165 - 101
app/core/data_interface/interface.py

@@ -8,8 +8,11 @@
 - 动态标签识别等功能
 """
 
+import logging
 from app.core.graph.graph_operations import connect_graph
 
+# 配置logger
+logger = logging.getLogger(__name__)
 
 # 数据标准列表展示
 def standard_list(skip_count, page_size, en_name_filter=None,
@@ -54,31 +57,40 @@ def standard_list(skip_count, page_size, en_name_filter=None,
     cql = f"""
     MATCH (n:data_standard)
     WHERE {where_str}
-    RETURN properties(n) as properties,n.time as time,id(n) as nodeid,size((n)--()) as relationship_count
+    RETURN properties(n) as properties,n.time as time,id(n) as nodeid,
+           COUNT{{()-[]->(n)}} + COUNT{{(n)-[]->()}} as relationship_count
     ORDER BY time desc
     SKIP $skip_count
     LIMIT $page_size
     """
     params['skip_count'] = skip_count
     params['page_size'] = page_size
-    result = connect_graph.run(cql, **params)
-    for record in result:
-        properties = {
-            key: value for key, value in record['properties'].items()
-            if key not in ['input', 'code', 'output']
-        }
-        properties.setdefault("describe", None)
-
-        new_attr = {
-            'id': record['nodeid'],
-            'number': record['relationship_count']
-        }
-        properties.update(new_attr)
-        data.append(properties)
-
-    # 获取总量
-    total_query = f"MATCH (n:data_standard) WHERE {where_str} RETURN COUNT(n) AS total"
-    total_result = connect_graph.run(total_query, **params).evaluate()
+    
+    # 修复:使用正确的session方式执行查询
+    driver = connect_graph()
+    if not driver:
+        return [], 0
+        
+    with driver.session() as session:
+        result = session.run(cql, **params)
+        for record in result:
+            properties = {
+                key: value for key, value in record['properties'].items()
+                if key not in ['input', 'code', 'output']
+            }
+            properties.setdefault("describe", None)
+
+            new_attr = {
+                'id': record['nodeid'],
+                'number': record['relationship_count']
+            }
+            properties.update(new_attr)
+            data.append(properties)
+
+        # 获取总量
+        total_query = f"MATCH (n:data_standard) WHERE {where_str} RETURN COUNT(n) AS total"
+        total_result = session.run(total_query, **params).single()["total"]
+        
     return data, total_result
 
 
@@ -111,15 +123,22 @@ def standard_kinship_graph(nodeid):
         apoc.coll.toSet(nodes) as nodes
     RETURN nodes,lines,rootId
     """
-    data = connect_graph.run(cql, nodeId=nodeid)
-    res = {}
-    for item in data:
-        res = {
-            "nodes": [record for record in item['nodes'] if record['id']],
-            "lines": [record for record in item['lines'] if record['from'] and record['to']],
-            "rootId": item['rootId']
-        }
-    return res
+    
+    # 修复:使用正确的session方式执行查询
+    driver = connect_graph()
+    if not driver:
+        return {}
+        
+    with driver.session() as session:
+        result = session.run(cql, nodeId=nodeid)
+        res = {}
+        for item in result:
+            res = {
+                "nodes": [record for record in item['nodes'] if record['id']],
+                "lines": [record for record in item['lines'] if record['from'] and record['to']],
+                "rootId": item['rootId']
+            }
+        return res
 
 
 # 数据标准图谱展示(影响关系)下游
@@ -151,15 +170,22 @@ def standard_impact_graph(nodeid):
             apoc.coll.toSet(nodes) as nodes
         RETURN nodes,lines,rootId
         """
-    data = connect_graph.run(cql, nodeId=nodeid)
-    res = {}
-    for item in data:
-        res = {
-            "nodes": [record for record in item['nodes'] if record['id']],
-            "lines": [record for record in item['lines'] if record['from'] and record['to']],
-            "rootId": item['rootId']
-        }
-    return res
+    
+    # 修复:使用正确的session方式执行查询
+    driver = connect_graph()
+    if not driver:
+        return {}
+        
+    with driver.session() as session:
+        result = session.run(cql, nodeId=nodeid)
+        res = {}
+        for item in result:
+            res = {
+                "nodes": [record for record in item['nodes'] if record['id']],
+                "lines": [record for record in item['lines'] if record['from'] and record['to']],
+                "rootId": item['rootId']
+            }
+        return res
 
 
 # 数据标准图谱展示(所有关系)
@@ -197,15 +223,21 @@ def standard_all_graph(nodeid):
         apoc.coll.toSet(nodes) as nodes
     RETURN nodes,lines,rootId
     """
-    data = connect_graph.run(cql, nodeId=nodeid)
-    res = {}
-    for item in data:
-        res = {
-            "nodes": [record for record in item['nodes'] if record['id']],
-            "lines": [record for record in item['lines'] if record['from'] and record['to']],
-            "rootId": item['rootId']
-        }
-    return res
+    # 修复:使用正确的session方式执行查询
+    driver = connect_graph()
+    if not driver:
+        return {}
+        
+    with driver.session() as session:
+        result = session.run(cql, nodeId=nodeid)
+        res = {}
+        for item in result:
+            res = {
+                "nodes": [record for record in item['nodes'] if record['id']],
+                "lines": [record for record in item['lines'] if record['from'] and record['to']],
+                "rootId": item['rootId']
+            }
+        return res
 
 
 # 数据标签列表展示
@@ -252,30 +284,39 @@ def label_list(skip_count, page_size, en_name_filter=None,
     MATCH (n:data_label)
     WHERE {where_str}
     RETURN properties(n) as properties,n.time as time,id(n) as nodeid,
-           size((n)--()) as relationship_count
+           COUNT{{()-[]->(n)}} + COUNT{{(n)-[]->()}} as relationship_count
     ORDER BY time desc
     SKIP $skip_count
     LIMIT $page_size
     """
     params['skip_count'] = skip_count
     params['page_size'] = page_size
-    result = connect_graph.run(cql, **params)
-    for record in result:
-        properties = record['properties']
-        new_attr = {
-            'id': record['nodeid'],
-            'number': record['relationship_count']
-        }
-        if "describe" not in properties:
-            properties["describe"] = None
-        if "scope" not in properties:
-            properties["scope"] = None
-        properties.update(new_attr)
-        data.append(properties)
-
-    # 获取总量
-    total_query = f"MATCH (n:data_label) WHERE {where_str} RETURN COUNT(n) AS total"
-    total_result = connect_graph.run(total_query, **params).evaluate()
+    
+    # 修复:使用正确的session方式执行查询
+    driver = connect_graph()
+    if not driver:
+        logger.error("无法连接到数据库")
+        return [], 0
+        
+    with driver.session() as session:
+        result = session.run(cql, **params)
+        for record in result:
+            properties = record['properties']
+            new_attr = {
+                'id': record['nodeid'],
+                'number': record['relationship_count']
+            }
+            if "describe" not in properties:
+                properties["describe"] = None
+            if "scope" not in properties:
+                properties["scope"] = None
+            properties.update(new_attr)
+            data.append(properties)
+
+        # 获取总量
+        total_query = f"MATCH (n:data_label) WHERE {where_str} RETURN COUNT(n) AS total"
+        total_result = session.run(total_query, **params).single()["total"]
+        
     return data, total_result
 
 
@@ -303,16 +344,21 @@ def id_label_graph(id):
                  toString(id(n)) AS res
     RETURN lines, nodes, res
     """
-    data = connect_graph.run(query, nodeId=id)
-
-    res = {}
-    for item in data:
-        res = {
-            "nodes": [record for record in item['nodes'] if record['id']],
-            "lines": [record for record in item['lines'] if record['from'] and record['to']],
-            "rootId": item['res'],
-        }
-    return res
+    # 修复:使用正确的session方式执行查询
+    driver = connect_graph()
+    if not driver:
+        return {}
+        
+    with driver.session() as session:
+        result = session.run(query, nodeId=id)
+        res = {}
+        for item in result:
+            res = {
+                "nodes": [record for record in item['nodes'] if record['id']],
+                "lines": [record for record in item['lines'] if record['from'] and record['to']],
+                "rootId": item['res'],
+            }
+        return res
 
 
 # 数据标签图谱展示(血缘关系)父节点/(所有关系)
@@ -353,15 +399,21 @@ def label_kinship_graph(nodeid):
         apoc.coll.toSet(nodes) as nodes
     RETURN nodes,lines,rootId
     """
-    data = connect_graph.run(cql, nodeId=nodeid)
-    res = {}
-    for item in data:
-        res = {
-            "nodes": [record for record in item['nodes'] if record['id']],
-            "lines": [record for record in item['lines'] if record['from'] and record['to']],
-            "rootId": item['rootId']
-        }
-    return res
+    # 修复:使用正确的session方式执行查询
+    driver = connect_graph()
+    if not driver:
+        return {}
+        
+    with driver.session() as session:
+        result = session.run(cql, nodeId=nodeid)
+        res = {}
+        for item in result:
+            res = {
+                "nodes": [record for record in item['nodes'] if record['id']],
+                "lines": [record for record in item['lines'] if record['from'] and record['to']],
+                "rootId": item['rootId']
+            }
+        return res
 
 
 # 数据标签图谱展示(影响关系)下游
@@ -382,15 +434,21 @@ def label_impact_graph(nodeid):
         RETURN {id:toString(id(n)),text:(n.name),type:"label"} AS nodes,
                toString(id(n)) as rootId
         """
-    data = connect_graph.run(cql, nodeId=nodeid)
-    res = {}
-    for item in data:
-        res = {
-            "nodes": item['nodes'],
-            "rootId": item['rootId'],
-            "lines": []
-        }
-    return res
+    # 修复:使用正确的session方式执行查询
+    driver = connect_graph()
+    if not driver:
+        return {}
+        
+    with driver.session() as session:
+        result = session.run(cql, nodeId=nodeid)
+        res = {}
+        for item in result:
+            res = {
+                "nodes": item['nodes'],
+                "rootId": item['rootId'],
+                "lines": []
+            }
+        return res
 
 
 # 数据标签按照提交内容查询相似分组,并且返回
@@ -412,12 +470,18 @@ def dynamic_label_list(name_filter=None):
     RETURN DISTINCT n.group as name,id(n) as nodeid
     """
 
-    result = connect_graph.run(cql).data()
-    data = []
-    for record in result:
-        data.append({
-            "name": record['name'],
-            "id": record['nodeid']
-        })
-
-    return data 
+    # 修复:使用正确的session方式执行查询
+    driver = connect_graph()
+    if not driver:
+        return []
+        
+    with driver.session() as session:
+        result = session.run(cql)
+        data = []
+        for record in result:
+            data.append({
+                "name": record['name'],
+                "id": record['nodeid']
+            })
+        
+        return data 

+ 46 - 43
app/core/data_metric/metric_interface.py

@@ -23,16 +23,16 @@ logging.basicConfig(filename='metric_interface.log', level=logging.INFO)
 def metric_list(skip_count, page_size, en_name_filter=None,
                 name_filter=None, category_filter=None, time_filter=None, tag_filter=None):
     """
-    获取数据指标列表,支持多种过滤条件
+    获取数据指标列表
     
     Args:
-        skip_count: 分页跳过的记录数
-        page_size: 每页记录数
-        en_name_filter: 英文名称过滤
-        name_filter: 名称过滤
-        category_filter: 类过滤
-        time_filter: 时间过滤
-        tag_filter: 标签过滤
+        skip_count: 跳过的记录数
+        page_size: 每页记录数
+        en_name_filter: 英文名称过滤条件
+        name_filter: 名称过滤条件
+        category_filter: 类过滤条件
+        time_filter: 时间过滤条件
+        tag_filter: 标签过滤条件
         
     Returns:
         tuple: (数据列表, 总记录数)
@@ -51,30 +51,24 @@ def metric_list(skip_count, page_size, en_name_filter=None,
     if category_filter:
         where_clause.append("n.category CONTAINS $category_filter")
         params['category_filter'] = category_filter
-    if tag_filter:
-        where_clause.append("n.tag = $nodeId")
-        params['nodeId'] = tag_filter
     if time_filter:
         where_clause.append("n.time CONTAINS $time_filter")
         params['time_filter'] = time_filter
-    else:
-        where_clause.append("TRUE")
+    # 添加tag标签查询逻辑
+    if tag_filter:
+        where_clause.append("id(la) = $tag_filter")
+        params['tag_filter'] = tag_filter
 
     where_str = " AND ".join(where_clause)
+    if where_str == "":
+        where_str = "TRUE"
 
     # 构建完整的查询语句
     cql = f"""
-    MATCH (n:data_metric)
+    MATCH (n:data_metric)-[:label]->(la:data_label)
     WHERE {where_str}
-    OPTIONAL MATCH (la:label) where id(la) = n.tag 
-    OPTIONAL MATCH (n:data_metric)-[:origin]-(m:data_model)
-    OPTIONAL MATCH (n:data_metric)-[:origin]-(mr:data_metric)
-    //数据标签
-    WITH 
-    CASE 
-        WHEN m IS NOT NULL THEN collect(m.name)
-        WHEN mr IS NOT NULL THEN collect(mr.name)
-        ELSE []
+    OPTIONAL MATCH (n)-[:origin]->(m:data_model)
+    WITH n, la, CASE WHEN m IS NULL THEN null ELSE {{id: id(m), name: m.name}}
     END AS data_model,properties(n) as properties,
            n.time as time,id(n) as nodeid,{{id:id(la),name:la.name}} as tag
     return properties,time,nodeid,data_model,tag
@@ -84,26 +78,35 @@ def metric_list(skip_count, page_size, en_name_filter=None,
     """
     params['skip_count'] = skip_count
     params['page_size'] = page_size
-    result = connect_graph.run(cql, **params)
-    for record in result:
-        properties = record['properties']
-        properties['data_model'] = record['data_model']
-        properties['tag'] = record['tag']
-        new_attr = {
-            'id': record['nodeid']
-        }
-        if "id_list" in properties:
-            properties['id_list'] = json.loads(properties['id_list'])
-        if "describe" not in properties:
-            properties["describe"] = None
-
-        properties.update(new_attr)
-        data.append(properties)
-
-    # 获取总量
-    total_query = f"MATCH (n:data_metric) " \
-                  f"WHERE {where_str} RETURN COUNT(n) AS total"
-    total_result = connect_graph.run(total_query, **params).evaluate()
+    
+    # 修复:使用正确的session方式执行查询
+    driver = connect_graph()
+    if not driver:
+        logger.error("无法连接到数据库")
+        return [], 0
+        
+    with driver.session() as session:
+        result = session.run(cql, **params)
+        for record in result:
+            properties = record['properties']
+            properties['data_model'] = record['data_model']
+            properties['tag'] = record['tag']
+            new_attr = {
+                'id': record['nodeid']
+            }
+            if "id_list" in properties:
+                properties['id_list'] = json.loads(properties['id_list'])
+            if "describe" not in properties:
+                properties["describe"] = None
+
+            properties.update(new_attr)
+            data.append(properties)
+
+        # 获取总量
+        total_query = f"MATCH (n:data_metric) " \
+                      f"WHERE {where_str} RETURN COUNT(n) AS total"
+        total_result = session.run(total_query, **params).single()["total"]
+    
     return data, total_result
 
 

+ 48 - 43
app/core/data_model/model.py

@@ -385,8 +385,14 @@ def model_list(skip_count, page_size, en_name_filter=None, name_filter=None,
     """
     # 构建查询条件
     params = {}
+    match_clause = "MATCH (n:data_model)"
     where_clause = []
     
+    if tag:
+        match_clause = "MATCH (n:data_model)-[:label]->(t:data_label)"
+        where_clause.append("id(t) = $tag")
+        params['tag'] = int(tag)
+    
     if name_filter:
         where_clause.append("n.name =~ $name_filter")
         params['name_filter'] = f"(?i).*{name_filter}.*"
@@ -395,22 +401,13 @@ def model_list(skip_count, page_size, en_name_filter=None, name_filter=None,
         where_clause.append("n.en_name =~ $en_name_filter")
         params['en_name_filter'] = f"(?i).*{en_name_filter}.*"
     
-    if level:
-        where_clause.append("n.level = $level")
-        params['level'] = level
-    
     if category:
         where_clause.append("n.category = $category")
         params['category'] = category
     
-    # 添加tag标签查询逻辑
-    if tag:
-        match_clause = "MATCH (n:data_model)"
-        if tag:
-            match_clause += "\nMATCH (n)-[:label]->(t:data_label) WHERE id(t) = $tag_id"
-            params['tag_id'] = tag
-    else:
-        match_clause = "MATCH (n:data_model)"
+    if level:
+        where_clause.append("n.level = $level")
+        params['level'] = level
     
     # 转换为字符串形式
     where_str = " AND ".join(where_clause)
@@ -424,37 +421,45 @@ def model_list(skip_count, page_size, en_name_filter=None, name_filter=None,
     RETURN count(n) as count
     """
     
-    count = connect_graph.run(count_query, **params).evaluate()
-    
-    # 获取分页数据
-    params['skip'] = skip_count
-    params['limit'] = page_size
-    
-    data_query = f"""
-    {match_clause}
-    {where_str}
-    OPTIONAL MATCH (n)-[:label]->(t:data_label)
-    WITH n, t
-    OPTIONAL MATCH (n)-[:component]->(m:meta_node)
-    RETURN 
-        id(n) as id, 
-        n.name as name, 
-        n.en_name as en_name, 
-        n.category as category, 
-        n.description as description, 
-        n.time as time,
-        n.level as level,
-        t.name as tag_name,
-        id(t) as tag_id,
-        count(m) as component_count
-    ORDER BY n.time DESC
-    SKIP $skip
-    LIMIT $limit
-    """
-    
-    result = connect_graph.run(data_query, **params).data()
-    
-    return result, count
+    # 使用正确的session方式执行查询
+    driver = connect_graph()
+    if not driver:
+        return [], 0
+        
+    with driver.session() as session:
+        count_result = session.run(count_query, **params)
+        count = count_result.single()["count"]
+        
+        # 获取分页数据
+        params['skip'] = skip_count
+        params['limit'] = page_size
+        
+        data_query = f"""
+        {match_clause}
+        {where_str}
+        OPTIONAL MATCH (n)-[:label]->(t:data_label)
+        WITH n, t
+        OPTIONAL MATCH (n)-[:component]->(m:meta_node)
+        RETURN 
+            id(n) as id, 
+            n.name as name, 
+            n.en_name as en_name, 
+            n.category as category, 
+            n.description as description, 
+            n.time as time,
+            n.level as level,
+            t.name as tag_name,
+            id(t) as tag_id,
+            count(m) as component_count
+        ORDER BY n.time DESC
+        SKIP $skip
+        LIMIT $limit
+        """
+        
+        result = session.run(data_query, **params)
+        data = result.data()
+        
+    return data, count
 
 
 # 有血缘关系的数据资源列表

+ 11 - 2
app/services/package_function.py

@@ -73,8 +73,17 @@ def soure_organization_name(workplace):
     query = f"match (n:workplace)<-[r:workin]-(subordinate_person:worker)" \
             f"WHERE n.organization_no = '{workplace}' " \
             f"return subordinate_person.code as code"
-    result = connect_graph.run(query).data()
-    return result
+    
+    # 修复:使用正确的session方式执行查询
+    driver = connect_graph()
+    if not driver:
+        return []
+        
+    with driver.session() as session:
+        result = session.run(query)
+        data = result.data()
+        
+    return data
 
 
 # 输入人员编码列表,得到员工与工作单位的关系,并且在此函数内完成员工,亲属,以及人-工作单位关系的创建

+ 2 - 1
requirements.txt

@@ -14,4 +14,5 @@ SQLAlchemy>=2.0.0
 PyYAML>=6.0
 python-dateutil>=2.8.0
 psutil>=6.0.0
-flask_sqlalchemy>=3.1.1
+flask_sqlalchemy>=3.1.1
+openpyxl>=3.1.5