Skip to content

Commit 2e439d7

Browse files
authored
fix: rename 'bytes' -> 'bytes_transferred' in log_generator.py to match train_model.py
1 parent ed4a4e3 commit 2e439d7

1 file changed

Lines changed: 25 additions & 23 deletions

File tree

backend/log_generator.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,34 @@
44
import os
55
from datetime import datetime
66

7-
# Define output file
8-
import os
97
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
108
LOG_FILE = os.path.join(BASE_DIR, "../data/generated_logs.csv")
119

1210
# Ensure data directory exists
1311
os.makedirs(os.path.dirname(LOG_FILE), exist_ok=True)
1412

15-
# Define headers if file doesn't exist
16-
headers = ["timestamp", "source_ip", "destination_ip", "bytes", "protocol", "event_type", "details"]
13+
# Column names MUST match what train_model.py and ci_generate_data.py expect
14+
headers = ["timestamp", "source_ip", "destination_ip", "bytes_transferred", "protocol", "event_type", "details"]
1715

1816
# Check if file exists to write headers
1917
file_exists = os.path.exists(LOG_FILE)
2018

19+
2120
def generate_ip():
2221
return f"{random.randint(10, 192)}.{random.randint(0, 255)}.{random.randint(0, 255)}.{random.randint(1, 255)}"
2322

23+
2424
def generate_log():
2525
protocols = ["TCP", "UDP", "ICMP", "HTTP", "HTTPS"]
2626
events = ["Normal", "Failed Login", "Port Scan", "Malware Detected", "File Access"]
27-
27+
2828
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
2929
src_ip = generate_ip()
3030
dst_ip = generate_ip()
3131
bytes_transferred = random.randint(100, 50000)
3232
protocol = random.choice(protocols)
3333
event_type = random.choices(events, weights=[0.7, 0.1, 0.1, 0.05, 0.05], k=1)[0]
34-
34+
3535
details = "Routine traffic"
3636
if event_type == "Failed Login":
3737
details = f"Failed attempt from {src_ip}"
@@ -44,20 +44,22 @@ def generate_log():
4444

4545
return [timestamp, src_ip, dst_ip, bytes_transferred, protocol, event_type, details]
4646

47-
print(f"Starting log generation to {LOG_FILE}...")
48-
print("Press Ctrl+C to stop.")
49-
50-
with open(LOG_FILE, "a", newline="") as f:
51-
writer = csv.writer(f)
52-
if not file_exists:
53-
writer.writerow(headers)
54-
55-
try:
56-
while True:
57-
log_entry = generate_log()
58-
writer.writerow(log_entry)
59-
print(f"Logged: {log_entry}")
60-
f.flush() # Ensure data is written immediately
61-
time.sleep(random.uniform(0.5, 2.0))
62-
except KeyboardInterrupt:
63-
print("\nLog generation stopped.")
47+
48+
if __name__ == "__main__":
49+
print(f"Starting log generation to {LOG_FILE}...")
50+
print("Press Ctrl+C to stop.")
51+
52+
with open(LOG_FILE, "a", newline="") as f:
53+
writer = csv.writer(f)
54+
if not file_exists:
55+
writer.writerow(headers)
56+
57+
try:
58+
while True:
59+
log_entry = generate_log()
60+
writer.writerow(log_entry)
61+
print(f"Logged: {log_entry}")
62+
f.flush() # Ensure data is written immediately
63+
time.sleep(random.uniform(0.5, 2.0))
64+
except KeyboardInterrupt:
65+
print("\nLog generation stopped.")

0 commit comments

Comments
 (0)