4
4
from code_graph import *
5
5
from typing import Optional
6
6
from falkordb import FalkorDB
7
+ from dotenv import load_dotenv
7
8
from urllib .parse import urlparse
8
9
from flask import Flask , request , jsonify , abort
9
10
10
- # Configuration
11
- FALKORDB_HOST = 'localhost'
12
- FALKORDB_PORT = 6379
13
- FALKORDB_USERNAME = None
14
- FALKORDB_PASSWORD = None
11
+ # Load environment variables from .env file
12
+ load_dotenv ()
13
+
14
+ # Access environment variables
15
+ FALKORDB_HOST = os .getenv ('FALKORDB_HOST' )
16
+ FALKORDB_PORT = os .getenv ('FALKORDB_PORT' )
17
+ FALKORDB_USERNAME = os .getenv ('FALKORDB_USERNAME' )
18
+ FALKORDB_PASSWORD = os .getenv ('FALKORDB_PASSWORD' )
15
19
16
20
app = Flask (__name__ , static_folder = 'static' )
17
21
@@ -34,50 +38,77 @@ def extract_org_name_from_url(url: str) -> Optional[tuple[str, str]]:
34
38
35
39
@app .route ('/graph_entities' , methods = ['GET' ])
36
40
def graph_entities ():
41
+ """
42
+ Endpoint to fetch sub-graph entities from a given repository.
43
+ The repository is specified via the 'repo' query parameter.
44
+
45
+ Returns:
46
+ - 200: Successfully returns the sub-graph.
47
+ - 400: Missing or invalid 'repo' parameter.
48
+ - 500: Internal server error or database connection issue.
49
+ """
50
+
37
51
# Access the 'repo' parameter from the GET request
38
52
repo = request .args .get ('repo' )
39
53
40
- g = Graph (repo , host = FALKORDB_HOST , port = FALKORDB_PORT ,
41
- username = FALKORDB_USERNAME , password = FALKORDB_PASSWORD )
54
+ if not repo :
55
+ logging .error ("Missing 'repo' parameter in request." )
56
+ return jsonify ({"error" : "Missing 'repo' parameter" }), 400
57
+
58
+ try :
59
+ # Initialize the graph with the provided repo and credentials
60
+ g = Graph (repo , host = FALKORDB_HOST , port = FALKORDB_PORT ,
61
+ username = FALKORDB_USERNAME , password = FALKORDB_PASSWORD )
62
+
63
+ # Retrieve a sub-graph of up to 100 entities
64
+ sub_graph = g .get_sub_graph (100 )
42
65
43
- sub_graph = g .get_sub_graph (100 )
66
+ logging .info (f"Successfully retrieved sub-graph for repo: { repo } " )
67
+ return jsonify (sub_graph ), 200
44
68
45
- return jsonify (sub_graph ), 200
69
+ except Exception as e :
70
+ logging .error (f"Error retrieving sub-graph for repo '{ repo } ': { e } " )
71
+ return jsonify ({"error" : "Internal server error" }), 500
46
72
47
73
@app .route ('/get_neighbors' , methods = ['GET' ])
48
74
def get_neighbors ():
49
- # Access the 'node_id' parameter from the GET request
50
- node_id = int (request .args .get ('node_id' ))
51
- graph_id = request .args .get ('graph_id' )
75
+ """
76
+ Endpoint to get neighbors of a specific node in the graph.
77
+ Expects 'repo' and 'node_id' as query parameters.
78
+ """
79
+ repo = request .args .get ('repo' )
80
+ node_id = request .args .get ('node_id' )
52
81
53
- # Connect to FalkorDB
54
- db = FalkorDB ( host = FALKORDB_HOST , port = FALKORDB_PORT ,
55
- username = FALKORDB_USERNAME , password = FALKORDB_PASSWORD )
82
+ if not repo :
83
+ logging . error ( "Repository name is missing in the request." )
84
+ return jsonify ({ "error" : "Repository name is required." }), 400
56
85
57
- # Select graph
58
- g = db .select_graph (graph_id )
86
+ if not node_id :
87
+ logging .error ("Node ID is missing in the request." )
88
+ return jsonify ({"error" : "Node ID is required." }), 400
59
89
60
- query = """MATCH (n)
61
- WHERE ID(n) = $node_id
62
- MATCH (n)-[e]-(neighbor)
63
- RETURN neighbor, e"""
90
+ try :
91
+ # Validate and convert node_id to integer
92
+ node_id = int (node_id )
93
+ except ValueError :
94
+ logging .error (f"Invalid node ID: { node_id } . It must be an integer." )
95
+ return jsonify ({"error" : "Invalid node ID. It must be an integer." }), 400
64
96
65
- data = []
66
- res = g .query (query , {'node_id' : node_id }).result_set
67
- for row in res :
68
- neighbor = row [0 ]
69
- e = row [1 ]
97
+ try :
98
+ # Initialize the graph with the provided repo and credentials
99
+ g = Graph (repo , host = FALKORDB_HOST , port = FALKORDB_PORT ,
100
+ username = FALKORDB_USERNAME , password = FALKORDB_PASSWORD )
70
101
71
- data .append ({'data' : {'id' : neighbor .id ,
72
- 'label' : neighbor .labels [0 ]} })
73
- data .append ({'data' : {'source' : node_id , 'target' : neighbor .id , 'relation' : e .relation } })
102
+ # Get neighbors of the given node
103
+ neighbors = g .get_neighbors (node_id )
74
104
75
- # [
76
- # { data: { id: 'e' } },
77
- # { data: { source: 'a', target: 'b' } }
78
- # ]
105
+ logging .info (f"Successfully retrieved neighbors for node ID { node_id } in repo '{ repo } '." )
106
+ return jsonify (neighbors ), 200
107
+
108
+ except Exception as e :
109
+ logging .error (f"Error retrieving node neighbors for repo '{ repo } ': { e } " )
110
+ return jsonify ({"error" : "Internal server error" }), 500
79
111
80
- return jsonify (data ), 200
81
112
82
113
@app .route ('/process_repo' , methods = ['POST' ])
83
114
def process_repo ():
0 commit comments