|
| 1 | +import json |
| 2 | +import os |
| 3 | +from tkinter import Toplevel, Text, Scrollbar, messagebox |
| 4 | +from matplotlib import pyplot as plt |
| 5 | + |
| 6 | + |
| 7 | +# Path to the file for storing history |
| 8 | +HISTORY_FILE = "test_history.json" |
| 9 | + |
| 10 | + |
| 11 | +def save_test_results(download_speed, upload_speed, ping, file_path="test_history.json"): |
| 12 | + """Saves the test results to a specified JSON file.""" |
| 13 | + data = { |
| 14 | + "download_speed": download_speed, |
| 15 | + "upload_speed": upload_speed, |
| 16 | + "ping": ping |
| 17 | + } |
| 18 | + |
| 19 | + # If the file exists, load the current data |
| 20 | + if os.path.exists(file_path): |
| 21 | + with open(file_path, "r") as file: |
| 22 | + history = json.load(file) |
| 23 | + else: |
| 24 | + history = [] |
| 25 | + |
| 26 | + # Add new data |
| 27 | + history.append(data) |
| 28 | + |
| 29 | + # Save the updated file |
| 30 | + with open(file_path, "w") as file: |
| 31 | + json.dump(history, file, indent=4) |
| 32 | + |
| 33 | + |
| 34 | +def view_history(root, history_path): |
| 35 | + """Opens a new window with the test history.""" |
| 36 | + if not os.path.exists(history_path): |
| 37 | + messagebox.showinfo("History", "No history available.") |
| 38 | + return |
| 39 | + |
| 40 | + with open(history_path, "r") as file: |
| 41 | + history = json.load(file) |
| 42 | + |
| 43 | + # Create a new window to display the history |
| 44 | + history_window = Toplevel(root) |
| 45 | + history_window.title("Test History") |
| 46 | + history_window.geometry("400x400") |
| 47 | + |
| 48 | + # Add a text area with scroll |
| 49 | + text_area = Text(history_window, wrap="word") |
| 50 | + scrollbar = Scrollbar(history_window, command=text_area.yview) |
| 51 | + text_area.config(yscrollcommand=scrollbar.set) |
| 52 | + text_area.pack(side="left", fill="both", expand=True) |
| 53 | + scrollbar.pack(side="right", fill="y") |
| 54 | + |
| 55 | + # Populate the text area with data |
| 56 | + for idx, entry in enumerate(history, start=1): |
| 57 | + text_area.insert( |
| 58 | + "end", |
| 59 | + f"Test {idx}:\n" |
| 60 | + f" Download Speed: {entry['download_speed']} Mbps\n" |
| 61 | + f" Upload Speed: {entry['upload_speed']} Mbps\n" |
| 62 | + f" Ping: {entry['ping']} ms\n\n" |
| 63 | + ) |
| 64 | + |
| 65 | + text_area.config(state="disabled") # Make the text read-only |
| 66 | + |
| 67 | + |
| 68 | +def plot_history(root, history_path): |
| 69 | + """Plots the history of the tests.""" |
| 70 | + if not os.path.exists(history_path): |
| 71 | + messagebox.showinfo("History", "No history available.") |
| 72 | + return |
| 73 | + |
| 74 | + with open(history_path, "r") as file: |
| 75 | + history = json.load(file) |
| 76 | + |
| 77 | + # Get data for the plot |
| 78 | + download_speeds = [entry["download_speed"] for entry in history] |
| 79 | + upload_speeds = [entry["upload_speed"] for entry in history] |
| 80 | + pings = [entry["ping"] for entry in history] |
| 81 | + tests = range(1, len(history) + 1) |
| 82 | + |
| 83 | + # Plot the graph |
| 84 | + plt.figure(figsize=(10, 5)) |
| 85 | + plt.plot(tests, download_speeds, label="Download Speed (Mbps)", marker="o") |
| 86 | + plt.plot(tests, upload_speeds, label="Upload Speed (Mbps)", marker="o") |
| 87 | + plt.plot(tests, pings, label="Ping (ms)", marker="o") |
| 88 | + plt.xlabel("Test Number") |
| 89 | + plt.ylabel("Value") |
| 90 | + plt.title("Internet Speed Test History") |
| 91 | + plt.legend() |
| 92 | + plt.grid(True) |
| 93 | + plt.show() |
0 commit comments