Skip to content

Commit 243f7e5

Browse files
committed
feat: add test history functionality and update README
1 parent 4eb9236 commit 243f7e5

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ macOS_application_speedtest_for_python/
9494
├── .github/workflows/ # GitHub Actions configuration
9595
│ └── pylint.yml
9696
├── LICENSE # MIT License
97-
└── README.md # This documentation
97+
├── README.md # This documentation
98+
└── test_history.py # Test history file
9899
```
99100

100101
#### How It Works

main.spec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ a = Analysis(
44
['speedtest_app/alexs_speedtest.py'],
55
pathex=[],
66
binaries=[],
7+
datas=[('test_history.json', '.')],
78
hiddenimports=[
89
'speedtest_cli',
910
'speedtest_app.network_adapter_information',

speedtest_app/test_history.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,54 @@
66
from matplotlib import pyplot as plt
77
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
88

9-
# Получаем логгер
109
logger = logging.getLogger("SpeedTest")
1110

12-
# Path to the file for storing history
13-
HISTORY_FILE = "test_history.json"
1411

12+
def get_history_file_path():
13+
"""Returns the path to the history file in Downloads directory."""
14+
downloads_dir = os.path.join(os.path.expanduser("~"), "Downloads")
15+
return os.path.join(downloads_dir, "speedtest_history.json")
16+
17+
18+
def save_test_results(download_speed, upload_speed, ping, file_path=None):
19+
"""Saves the test results to the Downloads directory."""
20+
if file_path is None:
21+
file_path = get_history_file_path()
1522

16-
def save_test_results(download_speed, upload_speed, ping, file_path="test_history.json"):
17-
"""Saves the test results to a specified JSON file with timestamp."""
1823
data = {
1924
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
2025
"download_speed": download_speed,
2126
"upload_speed": upload_speed,
2227
"ping": ping
2328
}
2429

25-
# If the file exists, load the current data
26-
if os.path.exists(file_path):
27-
try:
28-
with open(file_path, "r", encoding="utf-8") as file:
29-
history = json.load(file)
30-
except json.JSONDecodeError:
31-
logger.error(f"Error decoding JSON from {file_path}. Creating new history.")
30+
try:
31+
# Ensure directory exists (although Downloads should always exist)
32+
downloads_dir = os.path.dirname(file_path)
33+
if not os.path.exists(downloads_dir):
34+
logger.warning(f"Downloads directory not found at {downloads_dir}")
35+
messagebox.showerror("Error", "Downloads directory not found")
36+
return
37+
38+
# Load existing history or create new
39+
if os.path.exists(file_path):
40+
try:
41+
with open(file_path, "r", encoding="utf-8") as file:
42+
history = json.load(file)
43+
except json.JSONDecodeError:
44+
logger.error(f"Error decoding JSON from {file_path}. Creating new history.")
45+
history = []
46+
else:
3247
history = []
33-
else:
34-
history = []
3548

36-
# Add new data
37-
history.append(data)
49+
# Add new data
50+
history.append(data)
3851

39-
# Save the updated file
40-
try:
52+
# Save the updated file
4153
with open(file_path, "w", encoding="utf-8") as file:
4254
json.dump(history, file, indent=4)
4355
logger.info(f"Test results saved to {file_path}")
56+
4457
except Exception as e:
4558
logger.error(f"Error saving test results: {e}", exc_info=True)
4659
messagebox.showerror("Error", f"Could not save test results: {e}")
@@ -246,4 +259,4 @@ def save_plot():
246259
messagebox.showerror("Save Error", f"Could not save plot: {e}")
247260

248261
save_button = Button(button_frame, text="Save Plot", command=save_plot)
249-
save_button.pack(side="left", padx=5)
262+
save_button.pack(side="left", padx=5)

test_history.json

Whitespace-only changes.

0 commit comments

Comments
 (0)