New database schema.

This commit is contained in:
kalzu rekku 2024-05-26 18:57:01 +03:00
parent 93e785c2ae
commit 4ffd7052a5

View File

@ -43,31 +43,60 @@ def parse_traceroute_output(output):
previous_ip = ip_address
return hops
def create_tables():
conn = sqlite3.connect('traceroute.db')
def create_tables(databasefile):
# Connect to the SQLite database
conn = sqlite3.connect(databasefile)
cursor = conn.cursor()
# Table to store unique links
cursor.execute('''
CREATE TABLE IF NOT EXISTS links (
id INTEGER PRIMARY KEY,
source_ip TEXT,
destination_ip TEXT,
UNIQUE(source_ip, destination_ip)
)
''')
# SQL statements to create the tables
create_links_table = """
CREATE TABLE IF NOT EXISTS Links (
id INTEGER PRIMARY KEY,
source_ip TEXT NOT NULL,
destination_ip TEXT NOT NULL,
UNIQUE(source_ip, destination_ip)
);
"""
# Table to store latency and timestamp for each link
cursor.execute('''
CREATE TABLE IF NOT EXISTS link_latency (
id INTEGER PRIMARY KEY,
link_id INTEGER,
latency REAL,
timestamp TEXT,
FOREIGN KEY (link_id) REFERENCES links (id)
)
''')
create_paths_table = """
CREATE TABLE IF NOT EXISTS Paths (
id INTEGER PRIMARY KEY,
start_ip TEXT NOT NULL,
end_ip TEXT NOT NULL,
hops_json TEXT NOT NULL,
UNIQUE(start_ip, end_ip, hops_json)
);
"""
create_latency_table = """
CREATE TABLE IF NOT EXISTS Latency (
id INTEGER PRIMARY KEY,
link_id INTEGER NOT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
latency_ms REAL NOT NULL,
FOREIGN KEY (link_id) REFERENCES Links(id)
);
"""
create_hopdetails_table = """
CREATE TABLE IF NOT EXISTS HopDetails (
id INTEGER PRIMARY KEY,
link_id INTEGER NOT NULL,
ttl INTEGER,
icmp_type INTEGER,
icmp_code INTEGER,
packet_loss REAL,
FOREIGN KEY (link_id) REFERENCES Links(id)
);
"""
# Execute the SQL statements
cursor.execute(create_links_table)
cursor.execute(create_paths_table)
cursor.execute(create_latency_table)
cursor.execute(create_hopdetails_table)
# Commit changes and close the connection
conn.commit()
conn.close()