33import psutil
44import networkx as nx
55from datetime import datetime
6+ import hashlib
7+ from pathlib import Path
68
79
810class GexfNodeGenerator :
911 def __init__ (self ):
1012 self .save_dir = os .path .join (os .path .dirname (os .path .dirname (__file__ )), "gexf" )
1113 os .makedirs (self .save_dir , exist_ok = True )
12- self .gexf_path = os .path .join (self .save_dir , "generated_nodes.gexf" )
1314
1415 # DuckDB connection (copied from TopicService)
1516 db_path = os .path .join (
@@ -33,13 +34,30 @@ def __init__(self):
3334 f"Database not found at { db_path } . Please ensure the database file exists before running the application."
3435 )
3536
37+ def get_unique_filename (self , topics ):
38+ """Generate a unique filename based on the topics"""
39+ # Sort topics to ensure consistent hash for same topics in different order
40+ sorted_topics = sorted (topics )
41+ # Create a hash of the topics
42+ topics_str = "|" .join (sorted_topics )
43+ hash_object = hashlib .md5 (topics_str .encode ())
44+ hash_hex = hash_object .hexdigest ()[:12 ] # Use first 12 characters of hash
45+ # Include timestamp to ensure uniqueness even for same topics
46+ timestamp = datetime .now ().strftime ("%Y%m%d_%H%M%S" )
47+ return f"topics_{ hash_hex } _{ timestamp } .gexf"
48+
3649 def generate_gexf_nodes_for_topics (self , topics ):
3750 """
3851 Generate and store a GEXF file for all repos containing any of the given topics.
3952 Returns the path to the generated GEXF file.
4053 """
4154 if not topics :
4255 return None
56+
57+ # Generate unique filename for this search
58+ filename = self .get_unique_filename (topics )
59+ gexf_path = os .path .join (self .save_dir , filename )
60+
4361 topics_lower = [t .lower () for t in topics ]
4462 placeholders = "," .join (["?" ] * len (topics_lower ))
4563
@@ -150,5 +168,5 @@ def generate_gexf_nodes_for_topics(self, topics):
150168 # print(f"Years range: {min(years)} to {max(years)}")
151169 # print(f"Number of nodes with year=0: {years.count(0)}")
152170
153- nx .write_gexf (G , self . gexf_path )
154- return self . gexf_path # Return the file path
171+ nx .write_gexf (G , gexf_path )
172+ return gexf_path # Return the unique file path
0 commit comments