Skip to content

Commit c7f692c

Browse files
committed
graph stats
1 parent 37e60f6 commit c7f692c

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

code_graph/graph.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,3 +654,22 @@ def find_paths(self, src: int, dest: int) -> List[Path]:
654654

655655
return paths
656656

657+
def stats(self) -> dict:
658+
"""
659+
Retrieve statistics about the graph, including the number of nodes and edges.
660+
661+
Returns:
662+
dict: A dictionary containing:
663+
- 'node_count' (int): The total number of nodes in the graph.
664+
- 'edge_count' (int): The total number of edges in the graph.
665+
"""
666+
667+
q = "MATCH (n) RETURN count(n)"
668+
node_count = self.g.query(q).result_set[0][0]
669+
670+
q = "MATCH ()-[e]->() RETURN count(e)"
671+
edge_count = self.g.query(q).result_set[0][0]
672+
673+
# Return the statistics
674+
return {'node_count': node_count, 'edge_count': edge_count}
675+

main.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,45 @@ def process_list_commits():
329329

330330
return jsonify(response), 200
331331

332+
333+
@app.route('/repo_stats', methods=['POST'])
334+
def repo_stats():
335+
"""
336+
Endpoint to retrieve statistics about a specific repository.
337+
338+
Expected JSON payload:
339+
{
340+
"repo": <repository name>
341+
}
342+
343+
Returns:
344+
JSON: A response containing the status and graph statistics (node and edge counts).
345+
- 'status': 'success' if successful, or an error message.
346+
- 'stats': A dictionary with the node and edge counts if the request is successful.
347+
"""
348+
349+
# Get JSON data from the request
350+
data = request.get_json()
351+
352+
# Validate the 'repo' parameter
353+
repo = data.get('repo')
354+
if repo is None:
355+
return jsonify({'status': f'Missing mandatory parameter "repo"'}), 400
356+
357+
# Initialize the graph with the provided repository name
358+
g = Graph(repo)
359+
360+
# Retrieve statistics from the graph
361+
stats = g.stats()
362+
363+
# Create a response
364+
response = {
365+
'status': 'success',
366+
'stats': stats
367+
}
368+
369+
return jsonify(response), 200
370+
332371
@app.route('/find_paths', methods=['POST'])
333372
def find_paths():
334373
"""

0 commit comments

Comments
 (0)