66from matplotlib import pyplot as plt
77from matplotlib .backends .backend_tkagg import FigureCanvasTkAgg , NavigationToolbar2Tk
88
9- # Получаем логгер
109logger = 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 )
0 commit comments