Repository navigation
Expand file tree
/
Copy pathanalysis.py
More file actions
133 lines (110 loc) · 5.07 KB
/
Copy pathanalysis.py
File metadata and controls
133 lines (110 loc) · 5.07 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import os
import re
import matplotlib.pyplot as plt
import logging
# Set up logging
logging.basicConfig(level=logging.INFO)
USE_GRAPHIC = True
try:
import matplotlib.pyplot as plt
except ImportError as e:
if USE_GRAPHIC:
raise e
# List of colors for bars
BAR_COLORS = ['#1f78ed', '#FF7F0E', '#2CA02C'] # We'll use these 3 colors for each subplot
def extract_data(content, filename):
"""Extract data from the content of a speedtest file."""
try:
ssid = re.search(r'speedtest-(.+)\.txt', filename).group(1)
provider = re.search(r'Testing from (.*?) \(', content).group(1)
best_server = re.search(r'Hosted by (.*?) \(', content).group(1)
distance_km = re.search(r'\[(.*?) km\]', content).group(1)
ping_ms = re.search(r'\[(.*?) km\]: ([\d\.]+) ms', content).group(2)
download_speed_ms = re.search(r'Download: ([\d\.]+) Mbit/s', content).group(1)
upload_speed_ms = re.search(r'Upload: ([\d\.]+) Mbit/s', content).group(1)
except AttributeError as e:
logging.error(f"Error parsing {filename}: {e}")
return None # Skip the file if any data is missing
return {
'ssid': ssid,
'provider': provider,
'best_server': best_server,
'distance_km': distance_km,
'ping_ms': ping_ms,
'download_speed_ms': download_speed_ms,
'upload_speed_ms': upload_speed_ms
}
def read_speedtest_files(directory):
"""Reads all speedtest files in a directory."""
speedtest_files = [
filename for filename in os.listdir(directory)
if filename.startswith('speedtest-') and filename.endswith('.txt')
]
return speedtest_files
# Collect speedtest files
speedtest_files = read_speedtest_files(".")
# Extract data from files
data = []
for filename in speedtest_files:
with open(filename, 'r') as file:
content = file.read()
result = extract_data(content, filename)
if result: # Only append valid data
data.append(result)
# Sort data by download speed in descending order, ensuring no None or empty values
data.sort(key=lambda d: float(d['download_speed_ms']) if d.get('download_speed_ms') else 0, reverse=True)
# Display sorted results
for d in data:
print(f"SSID: {d['ssid']}")
print('==================')
print(f"Provider: {d['provider']}")
print(f"Best Server: {d['best_server']}")
print(f"Distance: {d['distance_km']} km")
print(f"Ping: {d['ping_ms']} ms")
print(f"Download Speed: {d['download_speed_ms']} Mbps")
print(f"Upload Speed: {d['upload_speed_ms']} Mbps")
print()
if USE_GRAPHIC:
# Prepare the data for plotting
ssids = [d['ssid'] for d in data]
download_speeds = [float(d['download_speed_ms']) if d['download_speed_ms'] else 0 for d in data]
upload_speeds = [float(d['upload_speed_ms']) if d['upload_speed_ms'] else 0 for d in data]
pings = [float(d['ping_ms']) if d['ping_ms'] else 0 for d in data]
# Create a single figure with 2 rows and 2 columns of subplots
fig, axes = plt.subplots(2, 2, figsize=(8, 8))
def plot_subplot(ax, y_data, title, ylabel, colors):
"""Plot individual subplots with each bar getting a unique color."""
bars = ax.bar(ssids, y_data) # Create bars first
# Assign colors to each bar
for idx, bar in enumerate(bars):
bar.set_color(colors[idx % len(colors)]) # Use modulo to cycle through colors
ax.set_xlabel('SSID')
ax.set_ylabel(ylabel)
ax.set_title(title)
ax.set_xticks(range(len(ssids))) # Set tick positions for each bar
ax.set_xticklabels(ssids, rotation=25, ha='right')
# Overlay the values on top of each bar
for bar in bars:
height = bar.get_height() # Get the height of each bar (value)
ax.text(
bar.get_x() + bar.get_width() / 2, # X position: middle of the bar
height, # Y position: at the height of the bar
f'{height:.2f}', # Value to display
ha='center', # Horizontal alignment
va='bottom', # Vertical alignment
fontsize=10, # Font size
color='black' # Text color
)
# Add extra space above the tallest bar to prevent clipping of the text
max_height = max(y_data) # Get the maximum height of the bars
ax.set_ylim(0, max_height * 1.1) # Set the upper limit to 10% higher than the tallest bar
# Plot each metric in a separate subplot with different colors
plot_subplot(axes[0, 0], download_speeds, 'Download Speed (Mbps)', 'Download Speed (Mbps)', BAR_COLORS) # Top-left
plot_subplot(axes[0, 1], upload_speeds, 'Upload Speed (Mbps)', 'Upload Speed (Mbps)', BAR_COLORS) # Top-right
# Only one plot for the second row (bottom plot), merging two columns
plot_subplot(axes[1, 0], pings, 'Ping (ms)', 'Ping (ms)', BAR_COLORS) # Bottom-left
fig.delaxes(axes[1, 1]) # Remove the empty plot on the bottom-right
# Adjust layout to avoid overlap and save the figure
plt.tight_layout()
# Display the figure with all subplots
plt.show()