-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
76 lines (55 loc) · 2.39 KB
/
main.py
File metadata and controls
76 lines (55 loc) · 2.39 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
import os
import json
# Function to calculate the average of a list of numbers
def calculate_average(pings):
return sum(pings) / len(pings) if pings else float("inf")
# Function to process a single JSON file and return the average pings per server
def process_json_file(filepath):
with open(filepath, "r") as file:
data = json.load(file)
servers = data.get("servers", {})
averages = {}
for server_name, server_data in servers.items():
pings = server_data.get("pings", [])
avg_ping = calculate_average(pings)
averages[server_name] = avg_ping
return averages
# Function to aggregate data from all JSON files in a folder
def process_folder(folder_path):
all_averages = {}
# Loop through all .json files in the folder
for filename in os.listdir(folder_path):
if filename.endswith(".json"):
filepath = os.path.join(folder_path, filename)
print(f"Processing {filename}...")
averages = process_json_file(filepath)
# Aggregate the averages
for server_name, avg_ping in averages.items():
if server_name not in all_averages:
all_averages[server_name] = []
all_averages[server_name].append(avg_ping)
return all_averages
# Function to determine the best server based on the lowest average ping
def determine_best_server(folder_path):
all_averages = process_folder(folder_path)
overall_averages = {}
# Calculate the overall average ping for each server
for server_name, pings in all_averages.items():
overall_avg = calculate_average(pings)
overall_averages[server_name] = overall_avg
# Find the server with the lowest average ping
best_server = min(overall_averages, key=overall_averages.get)
print("\nOverall Average Pings Per Server:")
for server_name, avg in overall_averages.items():
print(f"{server_name}: {avg:.2f} ms")
print(
f"\nThe best server to use is: {best_server} with an average ping of {overall_averages[best_server]:.2f} ms"
)
# Main execution
if __name__ == "__main__":
# Define the folder containing the .json files
input_folder = input("Enter the path to the folder with the JSON files: ").strip()
if os.path.isdir(input_folder):
determine_best_server(input_folder)
else:
print("Invalid folder path. Please try again.")