123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- """
- Graph API module
- 提供图数据库操作的API接口
- """
- from flask import request, jsonify
- from app.api.graph import bp
- from app.models.result import success, failed
- from app.core.graph import (
- connect_graph,
- create_or_get_node,
- create_relationship,
- get_subgraph,
- execute_cypher_query
- )
- from app.core.graph.graph_operations import MyEncoder
- import logging
- import json
- logger = logging.getLogger("app")
- # 查询图数据
- @bp.route('/query', methods=['POST'])
- def query_graph():
- """
- 执行自定义Cypher查询
-
- Args (通过JSON请求体):
- cypher (str): Cypher查询语句
- params (dict, optional): 查询参数
-
- Returns:
- JSON: 包含查询结果的响应
- """
- try:
- # 获取查询语句
- cypher = request.json.get('cypher', '')
- params = request.json.get('params', {})
-
- if not cypher:
- return jsonify(failed("查询语句不能为空"))
-
- # 执行查询
- data = execute_cypher_query(cypher, params)
- return jsonify(success(data))
- except Exception as e:
- logger.error(f"图数据查询失败: {str(e)}")
- return jsonify(failed(str(e)))
- # 创建节点
- @bp.route('/node/create', methods=['POST'])
- def create_node():
- """
- 创建新节点
-
- Args (通过JSON请求体):
- labels (list): 节点标签列表
- properties (dict): 节点属性
-
- Returns:
- JSON: 包含创建的节点信息的响应
- """
- try:
- # 获取节点信息
- labels = request.json.get('labels', [])
- properties = request.json.get('properties', {})
-
- if not labels:
- return jsonify(failed("节点标签不能为空"))
-
- # 构建标签字符串
- label = ':'.join(labels)
-
- # 创建节点
- node_id = create_or_get_node(label, **properties)
-
- # 查询创建的节点
- cypher = f"MATCH (n) WHERE id(n) = {node_id} RETURN n"
- result = execute_cypher_query(cypher)
-
- if result and len(result) > 0:
- return jsonify(success(result[0]))
- else:
- return jsonify(failed("节点创建失败"))
- except Exception as e:
- logger.error(f"创建节点失败: {str(e)}")
- return jsonify(failed(str(e)))
- # 创建关系
- @bp.route('/relationship/create', methods=['POST'])
- def create_rel():
- """
- 创建节点间的关系
-
- Args (通过JSON请求体):
- startNodeId (int): 起始节点ID
- endNodeId (int): 结束节点ID
- type (str): 关系类型
- properties (dict, optional): 关系属性
-
- Returns:
- JSON: 包含创建的关系信息的响应
- """
- try:
- # 获取关系信息
- start_node_id = request.json.get('startNodeId')
- end_node_id = request.json.get('endNodeId')
- rel_type = request.json.get('type')
- properties = request.json.get('properties', {})
-
- if not all([start_node_id, end_node_id, rel_type]):
- return jsonify(failed("关系参数不完整"))
-
- # 创建关系
- rel_id = create_relationship(start_node_id, end_node_id, rel_type, **properties)
-
- if rel_id:
- # 查询创建的关系
- cypher = f"MATCH ()-[r]-() WHERE id(r) = {rel_id} RETURN r"
- result = execute_cypher_query(cypher)
-
- if result and len(result) > 0:
- return jsonify(success(result[0]))
-
- return jsonify(failed("关系创建失败"))
- except Exception as e:
- logger.error(f"创建关系失败: {str(e)}")
- return jsonify(failed(str(e)))
- # 获取图谱数据
- @bp.route('/subgraph', methods=['POST'])
- def get_graph_data():
- """
- 获取子图数据
-
- Args (通过JSON请求体):
- nodeIds (list): 节点ID列表
- relationshipTypes (list, optional): 关系类型列表
- maxDepth (int, optional): 最大深度,默认为1
-
- Returns:
- JSON: 包含节点和关系的子图数据
- """
- try:
- # 获取请求参数
- node_ids = request.json.get('nodeIds', [])
- rel_types = request.json.get('relationshipTypes', [])
- max_depth = request.json.get('maxDepth', 1)
-
- if not node_ids:
- return jsonify(failed("节点ID列表不能为空"))
-
- # 获取子图
- graph_data = get_subgraph(node_ids, rel_types, max_depth)
-
- return jsonify(success(graph_data))
- except Exception as e:
- logger.error(f"获取图谱数据失败: {str(e)}")
- return jsonify(failed(str(e)))
|