graph.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from fastapi import APIRouter, Depends, HTTPException, status
  2. from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
  3. from pydantic import BaseModel
  4. from datetime import datetime, timedelta
  5. from typing import Optional
  6. from passlib.context import CryptContext
  7. from db.neo4j import get_neo4j_db
  8. # 路由
  9. router = APIRouter()
  10. def get_node_value(n):
  11. label = str(n.labels)
  12. if label == ":Disease":
  13. return 7000
  14. if label == ":Department":
  15. return 6000
  16. if label == ":Symptom":
  17. return 5500
  18. if label == ":Drug":
  19. return 5000
  20. return 4000
  21. @router.get("/api/graph/{node_id}")
  22. def get_graph_node_by_id(node_id: str):
  23. graph = get_neo4j_db()
  24. data = { "nodes":[], "edges":[]}
  25. graph = get_neo4j_db()
  26. result = graph.run(f"match path=(n)-[*1]->(p) where id(n)={node_id} or id(p)={node_id} return nodes(path) as nodes,relationships(path) as relations limit 50")
  27. ids = []
  28. for r in result:
  29. nodes = r["nodes"]
  30. relations = r["relations"]
  31. for n in nodes:
  32. if n.identity in ids:
  33. continue
  34. else:
  35. ids.append(n.identity)
  36. data["nodes"].append({"id": n.identity, "name": n["name"], "value":get_node_value(n)})
  37. for r in relations:
  38. start_pos = ids.index(r.start_node.identity)
  39. end_pos = ids.index(r.end_node.identity)
  40. if start_pos >=0 and end_pos >=0:
  41. data["edges"].append({"id": r.identity, "name": type(r).__name__, "start": start_pos, "end":end_pos})
  42. return { "status": 200, "data": data}
  43. @router.get("/api/node/{node_id}")
  44. def get_node_by_id(node_id: str):
  45. graph = get_neo4j_db()
  46. data = { "nodes":[]}
  47. graph = get_neo4j_db()
  48. result = graph.run(f"match (n) where id(n)={node_id} return n")
  49. for r in result:
  50. node = r["n"]
  51. itemData = {"id": node.identity,"type":str(node.labels), "name": node.get("name"), "value":5000, "props": []}
  52. for item in node.items():
  53. key, value = item
  54. if isinstance(value,list):
  55. value = ",".join(value)
  56. itemData["props"].append({ "name" : key, "value":value})
  57. data["nodes"].append(itemData)
  58. return { "status": 200, "data": data}
  59. @router.get("/api/edge/{node_id}")
  60. def get_edge_by_id(node_id: str):
  61. graph = get_neo4j_db()
  62. data = { "edges":[]}
  63. graph = get_neo4j_db()
  64. result = graph.run(f"match (n)-[r]->(p) where id(r)={node_id} return r")
  65. for r in result:
  66. edges = r["r"]
  67. itemData = {"id": edges.identity,"type":type(edges).__name__, "name": edges.get("name"), "value":5000, "props": []}
  68. for item in edges.items():
  69. key, value = item
  70. if isinstance(value,list):
  71. value = ",".join(value)
  72. itemData["props"].append({ "name" : key, "value":value})
  73. data["edges"].append(itemData)
  74. return { "status": 200, "data": data}
  75. @router.get("/api/graph-search/{name}")
  76. def search_node_by_name(name: str):
  77. graph = get_neo4j_db()
  78. data = { "nodes":[], "edges":[]}
  79. graph = get_neo4j_db()
  80. result = graph.run(f"match path=(n)-[*1]->(p) where n.name contains \"{name}\" return nodes(path) as nodes,relationships(path) as relations limit 50")
  81. ids = []
  82. for r in result:
  83. nodes = r["nodes"]
  84. relations = r["relations"]
  85. for n in nodes:
  86. if n.identity in ids:
  87. continue
  88. else:
  89. ids.append(n.identity)
  90. data["nodes"].append({"id": n.identity, "name": n["name"], "value":get_node_value(n)})
  91. for r in relations:
  92. start_pos = ids.index(r.start_node.identity)
  93. end_pos = ids.index(r.end_node.identity)
  94. if start_pos >=0 and end_pos >=0:
  95. data["edges"].append({"id": r.identity, "name": type(r).__name__, "start": start_pos, "end":end_pos})
  96. return { "status": 200, "data": data}
  97. graph_router = router