fix: auto-fix code issues (cron)
- 修复重复导入/字段 - 修复异常处理 - 修复PEP8格式问题 - 添加类型注解 - 修复缺失的urllib.parse导入
This commit is contained in:
@@ -26,6 +26,7 @@ except ImportError:
|
||||
NEO4J_AVAILABLE = False
|
||||
logger.warning("Neo4j driver not installed. Neo4j features will be disabled.")
|
||||
|
||||
|
||||
@dataclass
|
||||
class GraphEntity:
|
||||
"""图数据库中的实体节点"""
|
||||
@@ -44,6 +45,7 @@ class GraphEntity:
|
||||
if self.properties is None:
|
||||
self.properties = {}
|
||||
|
||||
|
||||
@dataclass
|
||||
class GraphRelation:
|
||||
"""图数据库中的关系边"""
|
||||
@@ -59,6 +61,7 @@ class GraphRelation:
|
||||
if self.properties is None:
|
||||
self.properties = {}
|
||||
|
||||
|
||||
@dataclass
|
||||
class PathResult:
|
||||
"""路径查询结果"""
|
||||
@@ -68,6 +71,7 @@ class PathResult:
|
||||
length: int
|
||||
total_weight: float = 0.0
|
||||
|
||||
|
||||
@dataclass
|
||||
class CommunityResult:
|
||||
"""社区发现结果"""
|
||||
@@ -77,6 +81,7 @@ class CommunityResult:
|
||||
size: int
|
||||
density: float = 0.0
|
||||
|
||||
|
||||
@dataclass
|
||||
class CentralityResult:
|
||||
"""中心性分析结果"""
|
||||
@@ -86,6 +91,7 @@ class CentralityResult:
|
||||
score: float
|
||||
rank: int = 0
|
||||
|
||||
|
||||
class Neo4jManager:
|
||||
"""Neo4j 图数据库管理器"""
|
||||
|
||||
@@ -172,7 +178,9 @@ class Neo4jManager:
|
||||
|
||||
# ==================== 数据同步 ====================
|
||||
|
||||
def sync_project(self, project_id: str, project_name: str, project_description: str = "") -> None:
|
||||
def sync_project(
|
||||
self, project_id: str, project_name: str, project_description: str = ""
|
||||
) -> None:
|
||||
"""同步项目节点到 Neo4j"""
|
||||
if not self._driver:
|
||||
return
|
||||
@@ -343,7 +351,9 @@ class Neo4jManager:
|
||||
|
||||
# ==================== 复杂图查询 ====================
|
||||
|
||||
def find_shortest_path(self, source_id: str, target_id: str, max_depth: int = 10) -> PathResult | None:
|
||||
def find_shortest_path(
|
||||
self, source_id: str, target_id: str, max_depth: int = 10
|
||||
) -> PathResult | None:
|
||||
"""
|
||||
查找两个实体之间的最短路径
|
||||
|
||||
@@ -378,7 +388,10 @@ class Neo4jManager:
|
||||
path = record["path"]
|
||||
|
||||
# 提取节点和关系
|
||||
nodes = [{"id": node["id"], "name": node["name"], "type": node["type"]} for node in path.nodes]
|
||||
nodes = [
|
||||
{"id": node["id"], "name": node["name"], "type": node["type"]}
|
||||
for node in path.nodes
|
||||
]
|
||||
|
||||
relationships = [
|
||||
{
|
||||
@@ -390,9 +403,13 @@ class Neo4jManager:
|
||||
for rel in path.relationships
|
||||
]
|
||||
|
||||
return PathResult(nodes=nodes, relationships=relationships, length=len(path.relationships))
|
||||
return PathResult(
|
||||
nodes=nodes, relationships=relationships, length=len(path.relationships)
|
||||
)
|
||||
|
||||
def find_all_paths(self, source_id: str, target_id: str, max_depth: int = 5, limit: int = 10) -> list[PathResult]:
|
||||
def find_all_paths(
|
||||
self, source_id: str, target_id: str, max_depth: int = 5, limit: int = 10
|
||||
) -> list[PathResult]:
|
||||
"""
|
||||
查找两个实体之间的所有路径
|
||||
|
||||
@@ -426,7 +443,10 @@ class Neo4jManager:
|
||||
for record in result:
|
||||
path = record["path"]
|
||||
|
||||
nodes = [{"id": node["id"], "name": node["name"], "type": node["type"]} for node in path.nodes]
|
||||
nodes = [
|
||||
{"id": node["id"], "name": node["name"], "type": node["type"]}
|
||||
for node in path.nodes
|
||||
]
|
||||
|
||||
relationships = [
|
||||
{
|
||||
@@ -438,11 +458,17 @@ class Neo4jManager:
|
||||
for rel in path.relationships
|
||||
]
|
||||
|
||||
paths.append(PathResult(nodes=nodes, relationships=relationships, length=len(path.relationships)))
|
||||
paths.append(
|
||||
PathResult(
|
||||
nodes=nodes, relationships=relationships, length=len(path.relationships)
|
||||
)
|
||||
)
|
||||
|
||||
return paths
|
||||
|
||||
def find_neighbors(self, entity_id: str, relation_type: str = None, limit: int = 50) -> list[dict]:
|
||||
def find_neighbors(
|
||||
self, entity_id: str, relation_type: str = None, limit: int = 50
|
||||
) -> list[dict]:
|
||||
"""
|
||||
查找实体的邻居节点
|
||||
|
||||
@@ -520,7 +546,11 @@ class Neo4jManager:
|
||||
)
|
||||
|
||||
return [
|
||||
{"id": record["common"]["id"], "name": record["common"]["name"], "type": record["common"]["type"]}
|
||||
{
|
||||
"id": record["common"]["id"],
|
||||
"name": record["common"]["name"],
|
||||
"type": record["common"]["type"],
|
||||
}
|
||||
for record in result
|
||||
]
|
||||
|
||||
@@ -720,13 +750,19 @@ class Neo4jManager:
|
||||
actual_edges = sum(n["connections"] for n in nodes) / 2
|
||||
density = actual_edges / max_edges if max_edges > 0 else 0
|
||||
|
||||
results.append(CommunityResult(community_id=comm_id, nodes=nodes, size=size, density=min(density, 1.0)))
|
||||
results.append(
|
||||
CommunityResult(
|
||||
community_id=comm_id, nodes=nodes, size=size, density=min(density, 1.0)
|
||||
)
|
||||
)
|
||||
|
||||
# 按大小排序
|
||||
results.sort(key=lambda x: x.size, reverse=True)
|
||||
return results
|
||||
|
||||
def find_central_entities(self, project_id: str, metric: str = "degree") -> list[CentralityResult]:
|
||||
def find_central_entities(
|
||||
self, project_id: str, metric: str = "degree"
|
||||
) -> list[CentralityResult]:
|
||||
"""
|
||||
查找中心实体
|
||||
|
||||
@@ -860,7 +896,9 @@ class Neo4jManager:
|
||||
"type_distribution": types,
|
||||
"average_degree": round(avg_degree, 2) if avg_degree else 0,
|
||||
"relation_type_distribution": relation_types,
|
||||
"density": round(relation_count / (entity_count * (entity_count - 1)), 4) if entity_count > 1 else 0,
|
||||
"density": round(relation_count / (entity_count * (entity_count - 1)), 4)
|
||||
if entity_count > 1
|
||||
else 0,
|
||||
}
|
||||
|
||||
def get_subgraph(self, entity_ids: list[str], depth: int = 1) -> dict:
|
||||
@@ -930,9 +968,11 @@ class Neo4jManager:
|
||||
|
||||
return {"nodes": nodes, "relationships": relationships}
|
||||
|
||||
|
||||
# 全局单例
|
||||
_neo4j_manager = None
|
||||
|
||||
|
||||
def get_neo4j_manager() -> Neo4jManager:
|
||||
"""获取 Neo4j 管理器单例"""
|
||||
global _neo4j_manager
|
||||
@@ -940,6 +980,7 @@ def get_neo4j_manager() -> Neo4jManager:
|
||||
_neo4j_manager = Neo4jManager()
|
||||
return _neo4j_manager
|
||||
|
||||
|
||||
def close_neo4j_manager() -> None:
|
||||
"""关闭 Neo4j 连接"""
|
||||
global _neo4j_manager
|
||||
@@ -947,8 +988,11 @@ def close_neo4j_manager() -> None:
|
||||
_neo4j_manager.close()
|
||||
_neo4j_manager = None
|
||||
|
||||
|
||||
# 便捷函数
|
||||
def sync_project_to_neo4j(project_id: str, project_name: str, entities: list[dict], relations: list[dict]) -> None:
|
||||
def sync_project_to_neo4j(
|
||||
project_id: str, project_name: str, entities: list[dict], relations: list[dict]
|
||||
) -> None:
|
||||
"""
|
||||
同步整个项目到 Neo4j
|
||||
|
||||
@@ -995,7 +1039,10 @@ def sync_project_to_neo4j(project_id: str, project_name: str, entities: list[dic
|
||||
]
|
||||
manager.sync_relations_batch(graph_relations)
|
||||
|
||||
logger.info(f"Synced project {project_id} to Neo4j: {len(entities)} entities, {len(relations)} relations")
|
||||
logger.info(
|
||||
f"Synced project {project_id} to Neo4j: {len(entities)} entities, {len(relations)} relations"
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 测试代码
|
||||
@@ -1016,7 +1063,11 @@ if __name__ == "__main__":
|
||||
|
||||
# 测试实体
|
||||
test_entity = GraphEntity(
|
||||
id="test-entity-1", project_id="test-project", name="Test Entity", type="Person", definition="A test entity"
|
||||
id="test-entity-1",
|
||||
project_id="test-project",
|
||||
name="Test Entity",
|
||||
type="Person",
|
||||
definition="A test entity",
|
||||
)
|
||||
manager.sync_entity(test_entity)
|
||||
print("✅ Entity synced")
|
||||
|
||||
Reference in New Issue
Block a user