-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify_phase1.py
More file actions
80 lines (67 loc) · 2.34 KB
/
Copy pathverify_phase1.py
File metadata and controls
80 lines (67 loc) · 2.34 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
import threading
import time
import os
from logsense_ai.src.ingestion.ingestor import LogIngestor
import subprocess
import sys
# Define paths
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
LOG_FILE = os.path.join(PROJECT_ROOT, "logsense_ai/data/raw/app.log")
def run_generator():
"""Runs the log generator in a subprocess for 10 seconds."""
print("Starting Log Generator...")
# Ensure directory exists
os.makedirs(os.path.dirname(LOG_FILE), exist_ok=True)
# Run the generator script
proc = subprocess.Popen([sys.executable, "logsense_ai/generate_logs.py"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
time.sleep(10)
proc.terminate()
print("Log Generator stopped.")
def verify_ingestion_static():
print("\nVerifying Static Ingestion...")
ingestor = LogIngestor()
logs = ingestor.load_file(LOG_FILE)
print(f"Loaded {len(logs)} logs from {LOG_FILE}")
if len(logs) > 0:
print("Sample Log:", logs[0])
else:
print("ALERT: No logs found!")
def verify_ingestion_stream():
print("\nVerifying Streaming Ingestion (monitoring for 5 seconds)...")
ingestor = LogIngestor()
# Create a temporary file for streaming test
stream_file = os.path.join(PROJECT_ROOT, "logsense_ai/data/raw/stream_test.log")
with open(stream_file, 'w') as f:
f.write("")
# Start a writer thread
def writer():
time.sleep(1)
with open(stream_file, 'a') as f:
f.write('{"message": "Stream Test 1"}\n')
f.flush()
time.sleep(1)
with open(stream_file, 'a') as f:
f.write('{"message": "Stream Test 2"}\n')
f.flush()
t = threading.Thread(target=writer)
t.start()
# Monitor
stream = ingestor.monitor_stream(stream_file, poll_interval=0.5)
count = 0
start_time = time.time()
for log in stream:
print(f"Stream received: {log}")
count += 1
if count >= 2:
break
if time.time() - start_time > 5:
print("Timeout waiting for stream logs")
break
t.join()
print(f"Stream verification completed. Received {count} logs.")
if __name__ == "__main__":
if os.path.exists(LOG_FILE):
os.remove(LOG_FILE)
run_generator()
verify_ingestion_static()
verify_ingestion_stream()