Repository navigation
Expand file tree
/
Copy pathanalyze_graph_stats.py
More file actions
219 lines (180 loc) · 7.37 KB
/
Copy pathanalyze_graph_stats.py
File metadata and controls
219 lines (180 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python2
"""
Script to analyze LocAgent graph JSON and provide detailed statistics
"""
import json
import os
from collections import defaultdict
def analyze_graph_stats(json_file_path):
"""Analyze the graph JSON file and provide detailed statistics"""
print("=" * 60)
print("LocAgent Graph Analysis")
print("=" * 60)
# Check if file exists
if not os.path.exists(json_file_path):
print("❌ Error: File not found: {}".format(json_file_path))
return
# Load JSON
try:
with open(json_file_path, 'r') as f:
graph = json.load(f)
except Exception as e:
print("❌ Error loading JSON: {}".format(e))
return
nodes = graph.get('nodes', [])
edges = graph.get('edges', [])
print("\n=== NODE STATISTICS ===")
print("Total nodes: {}".format(len(nodes)))
# Node types
node_types = defaultdict(int)
nodes_with_code = 0
nodes_without_code = 0
nodes_with_node_path = 0
nodes_without_node_path = 0
nodes_with_start_line = 0
nodes_without_start_line = 0
nodes_with_end_line = 0
nodes_without_end_line = 0
nodes_with_logs = 0
nodes_with_class_id = 0
for node in nodes:
node_type = node.get('type', 'unknown')
node_types[node_type] += 1
# Check for code content
if 'code' in node and node['code']:
nodes_with_code += 1
else:
nodes_without_code += 1
# Check for node_path
if 'node_path' in node and node['node_path']:
nodes_with_node_path += 1
else:
nodes_without_node_path += 1
# Check for start_line
if 'start_line' in node and node['start_line']:
nodes_with_start_line += 1
else:
nodes_without_start_line += 1
# Check for end_line
if 'end_line' in node and node['end_line']:
nodes_with_end_line += 1
else:
nodes_without_end_line += 1
# Check for logs
if 'logs_statique' in node and node['logs_statique']:
nodes_with_logs += 1
# Check for class_id (methods)
if 'class_id' in node and node['class_id']:
nodes_with_class_id += 1
# Print node type breakdown
print("\nNode types:")
for ntype, count in sorted(node_types.items()):
print(" - {}: {}".format(ntype, count))
# Print enrichment statistics
print("\nNode enrichment:")
print(" - With code: {}".format(nodes_with_code))
print(" - Without code: {}".format(nodes_without_code))
print(" - Enrichment rate: {:.1f}%".format(
(nodes_with_code / float(len(nodes))) * 100 if nodes else 0
))
print("\nNode attributes:")
print(" - With node_path: {}".format(nodes_with_node_path))
print(" - Without node_path: {}".format(nodes_without_node_path))
print(" - With start_line: {}".format(nodes_with_start_line))
print(" - Without start_line: {}".format(nodes_without_start_line))
print(" - With end_line: {}".format(nodes_with_end_line))
print(" - Without end_line: {}".format(nodes_without_end_line))
print(" - With logs: {}".format(nodes_with_logs))
print(" - With class_id (methods): {}".format(nodes_with_class_id))
# Analyze nodes without code by type
print("\nNodes without code by type:")
nodes_without_code_by_type = defaultdict(int)
for node in nodes:
if not ('code' in node and node['code']):
node_type = node.get('type', 'unknown')
nodes_without_code_by_type[node_type] += 1
for ntype, count in sorted(nodes_without_code_by_type.items()):
print(" - {}: {}".format(ntype, count))
# Analyze nodes without node_path by type
print("\nNodes without node_path by type:")
nodes_without_path_by_type = defaultdict(int)
for node in nodes:
if not ('node_path' in node and node['node_path']):
node_type = node.get('type', 'unknown')
nodes_without_path_by_type[node_type] += 1
for ntype, count in sorted(nodes_without_path_by_type.items()):
print(" - {}: {}".format(ntype, count))
print("\n=== EDGE STATISTICS ===")
print("Total edges: {}".format(len(edges)))
# Edge types
edge_types = defaultdict(int)
for edge in edges:
edge_type = edge.get('type', 'unknown')
edge_types[edge_type] += 1
print("\nEdge types:")
for etype, count in sorted(edge_types.items()):
print(" - {}: {}".format(etype, count))
# Check for orphaned nodes (nodes without any edges)
print("\n=== CONNECTIVITY ANALYSIS ===")
nodes_with_edges = set()
for edge in edges:
nodes_with_edges.add(edge.get('source'))
nodes_with_edges.add(edge.get('target'))
orphaned_nodes = []
for node in nodes:
if node.get('id') not in nodes_with_edges:
orphaned_nodes.append(node)
print("Orphaned nodes (no edges): {}".format(len(orphaned_nodes)))
if orphaned_nodes:
orphaned_by_type = defaultdict(int)
for node in orphaned_nodes:
node_type = node.get('type', 'unknown')
orphaned_by_type[node_type] += 1
print("Orphaned nodes by type:")
for ntype, count in sorted(orphaned_by_type.items()):
print(" - {}: {}".format(ntype, count))
# File size
file_size_mb = os.path.getsize(json_file_path) / 1024.0 / 1024.0
print("\n=== FILE INFO ===")
print("File size: {:.2f} MB".format(file_size_mb))
print("File path: {}".format(json_file_path))
# Sample node analysis
print("\n=== SAMPLE NODE ANALYSIS ===")
sample_nodes = nodes[:5] # First 5 nodes
for i, node in enumerate(sample_nodes, 1):
print("\nSample node {}:".format(i))
print(" ID: {}".format(node.get('id', 'N/A')))
print(" Type: {}".format(node.get('type', 'N/A')))
print(" Node path: {}".format(node.get('node_path', 'N/A')))
print(" Start line: {}".format(node.get('start_line', 'N/A')))
print(" End line: {}".format(node.get('end_line', 'N/A')))
print(" Has code: {}".format('Yes' if ('code' in node and node['code']) else 'No'))
print(" Has logs: {}".format('Yes' if ('logs_statique' in node and node['logs_statique']) else 'No'))
print(" Has class_id: {}".format('Yes' if ('class_id' in node and node['class_id']) else 'No'))
print("\n" + "=" * 60)
print("Analysis complete!")
print("=" * 60)
def main():
"""Main function"""
# Default path
json_file = '/home/faycal/code/sipgw/data/locagent_graph.json'
# Check if file exists, if not try alternative paths
if not os.path.exists(json_file):
alternative_paths = [
'/home/faycal/code/sipgw/index_data/custom/graph_index/sipgw.json',
'./locagent_graph.json',
'./sipgw.json'
]
for alt_path in alternative_paths:
if os.path.exists(alt_path):
json_file = alt_path
break
else:
print("❌ Could not find graph JSON file. Tried:")
print(" - {}".format(json_file))
for alt_path in alternative_paths:
print(" - {}".format(alt_path))
return
analyze_graph_stats(json_file)
if __name__ == '__main__':
main()