simple network logging app

This commit is contained in:
2025-05-04 22:38:47 +02:00
commit 25f016595f
9 changed files with 371 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
__pycache__
config.py
speedtest.db

38
app.py Normal file
View File

@@ -0,0 +1,38 @@
from flask import Flask, render_template
import sqlite3
import pandas as pd
from config import DB_PATH
from datetime import datetime
app = Flask(__name__)
def load_data():
# Connect to SQLite database
conn = sqlite3.connect(DB_PATH)
query = "SELECT * FROM speed_tests"
df = pd.read_sql(query, conn)
conn.close()
return df
@app.route('/')
def index():
df = load_data()
# Convert timestamps to human-readable format
df['datetime'] = pd.to_datetime(df['timestamp'], unit='s')
# Suppose your DataFrame is called `df`
df["timestamp"] = df["timestamp"].apply(lambda ts: datetime.fromtimestamp(ts).strftime("%Y-%m-%d %H:%M:%S"))
# Collect the data for charts
chart_data = {
"times": df['datetime'].dt.strftime('%Y-%m-%d %H:%M:%S').tolist(),
"down_90th": df['down_90th'].tolist(),
"up_90th": df['up_90th'].tolist()
}
return render_template('index.html', data=df.to_dict(orient='records'), chart_data=chart_data)
if __name__ == '__main__':
app.run(debug=True)

26
com.user.speedtest.plist Normal file
View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.user.speedtest</string>
<key>ProgramArguments</key>
<array>
<string>/Users/cato/Code/Cato447/speed-logger/run_speedtest.py</string>
</array>
<key>StartInterval</key>
<integer>30</integer>
<key>RunAtLoad</key>
<true/>
<key>StandardOutPath</key>
<string>/tmp/speedtest.out</string>
<key>StandardErrorPath</key>
<string>/tmp/speedtest.err</string>
</dict>
</plist>

2
config.py.example Normal file
View File

@@ -0,0 +1,2 @@
DB_PATH =
ROUTER_MAC =

89
db.py Normal file
View File

@@ -0,0 +1,89 @@
import sqlite3
from config import DB_PATH
def init_db(db_path=DB_PATH):
conn = sqlite3.connect(db_path)
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS speed_tests (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp REAL NOT NULL,
failed BOOLEAN NOT NULL,
isp TEXT,
ip TEXT,
location_code TEXT,
location_city TEXT,
location_region TEXT,
latency REAL,
jitter REAL,
down_100kB REAL,
down_1MB REAL,
down_10MB REAL,
down_25MB REAL,
down_90th REAL,
up_100kB REAL,
up_1MB REAL,
up_10MB REAL,
up_90th REAL
)
''')
conn.commit()
conn.close()
def insert_result(results: dict|None, db_path=DB_PATH):
conn = sqlite3.connect(db_path)
c = conn.cursor()
# If the test failed entirely, store it as a failure with timestamp now
if results is None or "tests" not in results:
from time import time
c.execute("INSERT INTO speed_tests (timestamp, failed) VALUES (?, ?)", (time(), True))
conn.commit()
conn.close()
return
tests = results.get("tests", {})
meta = results.get("meta", {})
# Get a consistent timestamp from any TestResult (or fallback to now)
from time import time as now
sample_test = next(iter(tests.values()), None)
timestamp = sample_test.time if sample_test else now()
print(tests)
print(meta)
def get(tests, key):
return tests[key].value if key in tests else None
c.execute('''
INSERT INTO speed_tests (
timestamp, failed, isp, ip, location_code, location_city, location_region,
latency, jitter,
down_100kB, down_1MB, down_10MB, down_25MB, down_90th,
up_100kB, up_1MB, up_10MB, up_90th
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
timestamp, False,
get(tests, "isp"),
get(meta, "ip"),
get(meta, "location_code"),
get(meta, "location_city"),
get(meta, "location_region"),
get(tests, "latency"),
get(tests, "jitter"),
get(tests, "100kB_down_mbps"),
get(tests, "1MB_down_mbps"),
get(tests, "10MB_down_mbps"),
get(tests, "25MB_down_mbps"),
get(tests, "90th_percentile_down_mbps"),
get(tests, "100kB_up_mbps"),
get(tests, "1MB_up_mbps"),
get(tests, "10MB_up_mbps"),
get(tests, "90th_percentile_up_mbps")
))
conn.commit()
conn.close()

24
run_speedtest.py Executable file
View File

@@ -0,0 +1,24 @@
#! /Users/cato/Code/Cato447/speed-logger/.venv/bin/python3
from cfspeedtest import CloudflareSpeedtest
from db import init_db, insert_result
from getmac import get_mac_address
from config import ROUTER_MAC
def run_test_and_save():
if get_mac_address(ip="192.168.0.1") != ROUTER_MAC:
print(get_mac_address(ip="192.168.0.1"), ROUTER_MAC)
print("Not connected to home network")
return
try:
tester = CloudflareSpeedtest()
results = tester.run_all(megabits=True) # returns SuiteResults
except Exception:
results = None # Trigger a failed test record
init_db()
insert_result(results)
print("==== Running Speedtest ====")
run_test_and_save()
print("==== Speedtest ended ====")

27
static/css/style.css Normal file
View File

@@ -0,0 +1,27 @@
body {
font-family: Arial, sans-serif;
padding: 20px;
background-color: #f4f4f4;
}
header h1 {
text-align: center;
color: #333;
}
table {
width: 100%;
margin-top: 20px;
border-collapse: collapse;
}
th, td {
padding: 10px;
text-align: center;
border: 1px solid #ddd;
}
th {
background-color: #f2f2f2;
}

141
templates/index.html Normal file
View File

@@ -0,0 +1,141 @@
{% extends 'layout.html' %}
{% block content %}
<h2>Graph: Download vs. Upload Speeds</h2>
<canvas id="speedChart"></canvas>
<h2>Test Results</h2>
<table border="1">
<thead>
<tr>
<th>Timestamp</th>
<th>ISP</th>
<th>Latency</th>
<th>Jitter</th>
<th>Download (90th Percentile)</th>
<th>Upload (90th Percentile)</th>
</tr>
</thead>
<tbody>
{% for row in data %}
<tr>
<td>{{ row.timestamp }}</td>
<td>{{ row.isp }}</td>
<td>{{ row.latency }}</td>
<td>{{ row.jitter }}</td>
<td>{{ row.down_90th }}</td>
<td>{{ row.up_90th }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<script>
var chartData = {{ chart_data | tojson }};
const acceptableDownloadRange = {
min: 600, // Replace with your desired range
max: 1000
};
const acceptableUploadRange = {
min: 15,
max: 50
};
const times = chartData.times;
const downloadMinBand = times.map(() => acceptableDownloadRange.min);
const downloadMaxBand = times.map(() => acceptableDownloadRange.max);
const uploadMinBand = times.map(() => acceptableUploadRange.min);
const uploadMaxBand = times.map(() => acceptableUploadRange.max);
var ctx = document.getElementById('speedChart').getContext('2d');
var speedChart = new Chart(ctx, {
type: 'line',
data: {
labels: times,
datasets: [
{
label: 'Download Speed',
data: chartData.down_90th,
borderColor: 'rgba(75, 192, 192, 1)',
fill: false
},
{
label: 'Upload Speed',
data: chartData.up_90th,
borderColor: 'rgba(153, 102, 255, 1)',
fill: false
},
// Acceptable download range band
{
label: 'Acceptable Download Range',
data: downloadMinBand,
borderColor: 'rgba(0, 200, 0, 0.1)',
backgroundColor: 'rgba(0, 200, 0, 0.1)',
fill: '+1',
pointRadius: 0,
borderWidth: 0
},
{
data: downloadMaxBand,
borderColor: 'rgba(0, 200, 0, 0.1)',
backgroundColor: 'rgba(0, 200, 0, 0.1)',
fill: false,
pointRadius: 0,
borderWidth: 0
},
// Acceptable upload range band
{
label: 'Acceptable Upload Range',
data: uploadMinBand,
borderColor: 'rgba(255, 165, 0, 0.1)',
backgroundColor: 'rgba(255, 165, 0, 0.1)',
fill: '+1',
pointRadius: 0,
borderWidth: 0
},
{
data: uploadMaxBand,
borderColor: 'rgba(255, 165, 0, 0.1)',
backgroundColor: 'rgba(255, 165, 0, 0.1)',
fill: false,
pointRadius: 0,
borderWidth: 0
}
]
},
options: {
responsive: true,
plugins: {
legend: {
labels: {
filter: function(item, chart) {
// Hide max band labels
return item.text !== undefined;
}
}
}
},
scales: {
x: {
type: 'category',
title: {
display: true,
text: 'Time'
}
},
y: {
title: {
display: true,
text: 'Speed (Mbps)'
}
}
}
}
});
</script>
{% endblock %}

21
templates/layout.html Normal file
View File

@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Speed Test Results</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<header>
<h1>Network Speed Test Results</h1>
</header>
<main>
{% block content %}
{% endblock %}
</main>
</body>
</html>