Added Test Mode
This commit is contained in:
59
data_storage.py
Normal file
59
data_storage.py
Normal file
@@ -0,0 +1,59 @@
|
||||
# Data Storage for the LLM-Powered Monitoring Agent
|
||||
|
||||
import json
|
||||
|
||||
DATA_FILE = "historical_data.json"
|
||||
|
||||
def store_data(data):
|
||||
"""Stores data in a JSON file."""
|
||||
try:
|
||||
with open(DATA_FILE, 'r+') as f:
|
||||
try:
|
||||
historical_data = json.load(f)
|
||||
except json.JSONDecodeError:
|
||||
historical_data = []
|
||||
historical_data.append(data)
|
||||
f.seek(0)
|
||||
json.dump(historical_data, f, indent=2)
|
||||
except FileNotFoundError:
|
||||
with open(DATA_FILE, 'w') as f:
|
||||
json.dump([data], f, indent=2)
|
||||
|
||||
def get_historical_data():
|
||||
"""Retrieves historical data from the JSON file."""
|
||||
try:
|
||||
with open(DATA_FILE, 'r') as f:
|
||||
return json.load(f)
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
return []
|
||||
|
||||
def calculate_baselines():
|
||||
"""Calculates baseline averages for network metrics."""
|
||||
historical_data = get_historical_data()
|
||||
if not historical_data:
|
||||
return None
|
||||
|
||||
# Calculate average network metrics
|
||||
total_packets_transmitted = 0
|
||||
total_packets_received = 0
|
||||
total_packet_loss_percent = 0
|
||||
total_round_trip_ms_avg = 0
|
||||
count = 0
|
||||
|
||||
for data in historical_data:
|
||||
if "network_metrics" in data and data["network_metrics"]:
|
||||
total_packets_transmitted += data["network_metrics"].get("packets_transmitted", 0) or 0
|
||||
total_packets_received += data["network_metrics"].get("packets_received", 0) or 0
|
||||
total_packet_loss_percent += data["network_metrics"].get("packet_loss_percent", 0) or 0
|
||||
total_round_trip_ms_avg += data["network_metrics"].get("round_trip_ms_avg", 0) or 0
|
||||
count += 1
|
||||
|
||||
if count == 0:
|
||||
return None
|
||||
|
||||
return {
|
||||
"avg_packets_transmitted": total_packets_transmitted / count,
|
||||
"avg_packets_received": total_packets_received / count,
|
||||
"avg_packet_loss_percent": total_packet_loss_percent / count,
|
||||
"avg_round_trip_ms_avg": total_round_trip_ms_avg / count,
|
||||
}
|
||||
Reference in New Issue
Block a user